0

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?

teynon
  • 7,540
  • 10
  • 63
  • 106

1 Answers1

0

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
teynon
  • 7,540
  • 10
  • 63
  • 106
  • I just can't understand why it's the 9-bit. Why not 12-bit? As I know, the max possible output code number is 4095 which is 12-bit. – Jiacheng Wang Jul 03 '23 at 07:30
  • Like this article(https://giflib.sourceforge.net/whatsinagif/lzw_image_data.html) said: `As you can see we dynamically built many new codes for our code table as we compressed the data. For large files this can turn into a large number of codes. It turns out that the GIF format specifies a maximum code of #4095 (this happens to be the largest 12-bit number).` It's possbile the output code have 12 bits. – Jiacheng Wang Jul 03 '23 at 07:35
  • @JiachengWang See the answer on my other question (https://stackoverflow.com/questions/14203731/parsing-gif-raster-data-lzw). The byte code switches to 4-bit groups once the highest 3-bit group is seen. – teynon Jul 03 '23 at 13:41