Decode data bytes of GIF87a raster data stream

2019-01-20 15:15发布

问题:

I'm trying to decode the data bytes from a GIF87a raster data stream. I'm unsure of how to read the LZW variable length codes (and how LSB...least significant byte first fits in this). The raster data stream starts as follows (in hex):

06 6b 40 86 70 48 2c 1a 8f 44 4b 44 22 89 58 8e 10 c7 e1 80
  • 06 ->code size of 6
  • 6b ->block byte count of 107
  • 40 ->clear code (2^6) which is 64 in decimal or 40 in hex
  • 86 -> start of actual data

GIF87a spec: http://www.w3.org/Graphics/GIF/spec-gif87.txt

The raster stream should have indexes that point to the global map (or to a parent in the LZW tree)...but I'm not sure how to read it.

Could someone convert the first few bytes (starting at 86) as an example?

回答1:

read this 3MF Project GIF and all subsections there is everything you need with stepping examples and the rest is in the spec file.

now the image data

Stream data starts with 1 Byte block size and then goes the bit stream. At the end goes another block size. It stops when you draw entire frame then set pointer after the last readed block.

if you found block size 0 then it means the end of frame data. If there is terminator 0x3b after then the end of file is reached.

The local color bits tells you how many bits you have per code in the stream at start.

I read LSB of actual processed BYTE, then shift it right then shift the code right and add this bit as MSB of it. After you reach the needed bit count of index process it by LZW decompressing and also add new code to dictionary.

if dictionary cross 2^bits boundary increase the code bitsize and continue. Do not forget to handle the clear and end special codes ...

So you have: 06 6b 40 86 70 48 2c 1a

  • 06h is initial bit size - 1 so the real bit size is 7 !!!
  • 6bh is block size in bytes
  • 40h is clear code (meaning 64 colors present in color[] table, and first free index is 66)
  • 86 70 48 2c 1a [hex]= |1 0000110|01 110000|010 01000|0010 1100|00011 010| [bin]

so the codes are:

  • |0000110|110000 1|01000 01|1100 010|010 0010| [bin]
  • |0000110|1100001 |0100001 |11000010|0100010 | [bin]
  • 06,61,21,c2,22 [hex]

the 61h suggest error in data is this really start of image data (or I made a mistake somewhere)? The code can be only one bigger then max index in dictionary. The dictionary is increased by each code except the first so while processing 61h the dictionary is only 42h in size. Try the example in the linked page they works ...