Search code examples
gifdecodinggraphiclzw

GIF Understanding Image Decoding - Lempel-Ziv-Welch


I'm trying to build some code I can use to convert a GIF file to another file format (which I already know how to create. [I am trying to streamline conversion from GIF to GRF - a printer graphic file format.])

I am working off of information from Wikipedia (http://en.wikipedia.org/wiki/Graphics_Interchange_Format#Image_coding).

There is a section that describes converting from bytes to 9-bit codes. The examples they show are:

9-bit   binary   Bytes
(hex)            (hex)

        00000000  00
100
        0101000|1  51
028
        111111|00  FC
0FF
        00011|011  1B
103
        0010|1000  28
102
        011|10000  70
103
        10|100000  A0
106
        1|1000001  C1
107
         10000011  83
         00000001  01
101
        0000000|1  01

I am able to generate the bytes given on the right side from the file. (I created a file exactly the way they described in the article (3x5 with black pixels in 0,0 and 1,1 in MSPaint.)

What I am not understanding is how they are converting these bytes to the 9-bit hex codes.

How does 00 become 100? What does the bar (|) in the binary mean?


Solution

  • Just realized what is happening...

             00000000  00        Add the next digit to the beginning - 100000000 = 100
    100
             0101000|1  51        Next Digit - 000101000 = 028
    028
             111111|00  FC        -011111111 = 0FF
    0FF
             00011|011  1B        -100000011 = 103
    103
             0010|1000  28        -100000010 = 102
    102
             011|10000  70        -100000011 = 103
    103
             10|100000  A0        -100000110 = 106
    106
             1|1000001  C1        -100000111 = 107
    107
             10000011  83
             00000001  01         -100000001 = 101
    101
             0000000|1  01