i try to get pixel data out of GIF89a images.
Parsing the file works ok, but now i struggle with the LZW encoded image data.
I have a byte stream of image data, out of which i need to extract n bits at a time. n can between 2 and 12 inclusive, that means that a code (sequence of bits) can spread over 1 - 2 bytes. n can increase when i read a certain value.
I thought of extracting the codes like this:
Code: Select all
unsigned int first_code_size = static_cast<int>(LZW_minimum_code_size) + 1;
const unsigned int increaseCodeSizeWhen = (1 << LZW_minimum_code_size) - 1;
//A special Clear code is defined which resets all compression/decompression
//parameters and tables to a start-up state.
const unsigned int clear_code = (1 << LZW_minimum_code_size);
//An End of Information code is defined that explicitly indicates the end of
//the image data stream.
const unsigned int end_code = clear_code + 1;
const unsigned char* data = code_stream.data();
unsigned char current_byte = 0;
unsigned short current_code = 0; //12 bit max
unsigned int last_code = 0;
//this in a loop until end_code is read
current_code = ?;
Should look something like:
Code: Select all
while (current_code == end_code)
{
current_code = ((data[index+1] - 0) & 0x0f) + ( ((data[index] - 0) & 0xfc) << 2 );
}
My question is all about splitting bytes into chunks of bits, the LZW stuff would be next.
Further reading about LZW compression in GIF:
http://www.matthewflickinger.com/lab/wh ... e_data.asp
http://gingko.homeip.net/docs/file_formats/lzwgif.html