Does anyone know the precise implementation of the Lempel-Ziv-Welch algorithm (LZW) as it is used in the GIF file format? (This isn't a java question but it seems like something java programmers might know. If there's a more appropriate place to post this please let me know.)<br><br>I am trying to write a specific encoder for gifs. Here is the problem. I have read a bunch of material which explains the gif file format and have come to understand how it apparently works in general. However, the stumbling block is that the last part of the file which contains the actual compressed pixel data. This is said to be compressed using a modified LZW strategy. By creating a series of simple gifs in photoshop then looking at the byte values manually, I can see how the various blocks are set up and how they vary depending of the image size, color set, etc. However, the last block is beyond me to figure out. <br><br>There are several issues here:<br>1. The compression of pixel data is of variable bit size which is specified in the first byte of the header of the image data block.<br>2. There are 2 special codes which are added to the actual data stream (a "clear code" to change the compression code size and an "end of information" code).<br>3. the data bits are sequentially organized so that the lowest order bit for each compression code is pointed towards the lower order of the bytes within which it is contained.<br>4. How is the LZW-initial root table organized? How do you know what order to put the root values in in relation to the global color palette? And are the root values the byte values or the indices of each color in the global palette?<br><br>As an example, here is a gif-image which is a 4 pixel square of green pixels (2x2). The color value of this pixel is (80,184,75) which you can see in the file data below:<br><br><br>byte no. byte value character description<br>------------------------------------------------------------------<br>1: 71 G <font color=red>start header</font><br>2: 73 I<br>3: 70 F<br>4: 56 8<br>5: 57 9<br>6: 97 a<br>7: 4 . <font color=red>logical screen width (2 bytes) </font><br>8: 0<br>9: 4 . <font color=red>logical screen height (2 bytes)</font><br>10: 0<br>11: 128 € <font color=red> global palette bits</font><br>12: 255 ÿ <font color=red> background color</font><br>13: 0 <font color=red> aspect ratio</font><br>14: 80 P <font color=red> green pixel (R value)</font> <font color=red>start global palette</font><br>15: 184 ¸ <font color=red> green pixel (G value)</font> <br>16: 75 K <font color=red> green pixel (B value)</font> <br>17: 0 <font color=red>[empty default pixel]</font><br>18: 0 <font color=red>[empty default pixel]</font><br>19: 0 <font color=red>[empty default pixel]</font><br>20: 44 , <font color=red>block type</font><br>21: 0 <font color=red>x position within logical screen (2 bytes)</font><br>22: 0 <br>23: 0 <font color=red>y position within logical screen (2 bytes)</font><br>24: 0 <br>25: 4 . <font color=red>image height (2 bytes)</font><br>26: 0<br>27: 4 . <font color=red>image width (2 bytes)</font><br>28: 0<br>29: 0 <font color=red>local palette bits (zero in this case)</font><br>30: 2 . <font color=red>compression code size</font><br>31: 4 . <font color=red>block size</font><br>32: 132 „ <font color=red>IMAGE DATA</font><br>33: 143 <font color=red>IMAGE DATA</font><br>34: 9 <font color=red>IMAGE DATA</font><br>35: 5 . <font color=red>IMAGE DATA</font><br>36: 0 <font color=red>new block specified as 0 (close data stream)</font><br>37: 59 ; <font color=red>file close</font><br><br><br>The real issue is the 4 IMAGE DATA pixels. These are:<br><br>132 143 9 5<br><br>which in binary are:<br><br>10000100 10001111 00001001 00000101<br><br>However, since the compression is not spaced in bytes the actual encoded data is contained within some break up of this stream. This break-up furthermore is not in the same direction as the order of bits but rather staggered with the lowest bit of each compression code-word starting toward the right and continuing in the same fashion throughout.<br><br>Although I understand the LZW process for a series of characters I don't know where the code is organized within this bits show above. How do I read this bit stream so that I can see where the root table and encoded data are? Where are the two special codes in this data?<br><br>Any comments or corrections gratefully appreciated!<br> <p>--Will Duty<br><a href=mailto:wduty@radicalfringe.com>wduty@radicalfringe.com</a><br><a href= > </a><br>