title: LZW encoding and the GIF file format
body: <p>I'm trying to understand how to create a <code>.gif</code> file in C++. So far I think I understand everything except how the <code>LZW</code> encoding works. This is the file that I have generated with labels:</p>
  
  <pre><code>47 49 46 38 39 61 -header
  0A 00 01 00 91 00 -logical screen descriptor
  00 00 FF 00 FF 00 -color table [green,red,yellow,black]
  00 FF FF 00 00 00
  00 21 F9 04 00 00 -graphics control extension
  00 00 00 2C 00 00 -image descriptor
  00 00 0A 00 01 00 -(10 pixels wide x 1 pixel tall)
  00 02 04 8A 05 00 -encoded image
  3B                -terminator
  </code></pre>
  
  <p>And here it is again without labels for copy/paste purposes: 47 49 46 38 39 61 05 00 04 00 91 00 00 00 FF 00 FF 00 00 FF FF 00 00 00 00 21 F9 04 00 00 00 00 00 2C 00 00 00 00 0A 00 01 00 00 02 04 8A 05 00 3B</p>
  
  <p>I am having a lot of trouble understanding how <code>02 04 8A 05</code> translates to the image <code>yryryggyry</code>. I know the <code>02</code> is the minimum code size, and the <code>04</code> is the length of the image block, and I think I have identified the clear and <code>EOI</code> codes, but I don't understand the code inbetween.</p>
  
  <pre><code>8A       05
  10001010 00000101
  100|01010 00000|101
   ^      ????     ^
   clear code      EOI code
  </code></pre>
  
  <p>So far I have gotten the most information from the <code>.gif</code> specification:
  <a href="http://www.w3.org/Graphics/GIF/spec-gif89a.txt" rel="nofollow">http://www.w3.org/Graphics/GIF/spec-gif89a.txt</a></p>
  
  <p>And this website was also helpful:
  <a href="http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp" rel="nofollow">http://www.matthewflickinger.com/lab/whatsinagif/lzw_image_data.asp</a></p>
  
  <p>Thanks</p>
  
  <p>EDIT*</p>
  
  <p>I watched the Youtube video linked in the comments and encoded the image manually for the color stream "yryryggyry":</p>
  
  <pre><code>Color table-012=gry
  
  2   1   2   1   2   0   0   2   1   2
  010 001 010 001 010 000 000 010 001 010
  
  current next output dict
  010     001  010    21 6
  001     010  001    12 7
  010     001  -      -
  001     010  110    121 8
  010     000  010    212 9
  000     000  000    00  10
  000     010  1010   002 11
  010     001  -      -
  001     010  110    -
  010     -    010    -
  
  outputs-100 010 001 110 010 000 1010 110 010 101
  
  01010101 4th 55
  10101000 3rd A8
  00101100 2nd 2C
  01010100 1st 54
  
  Code-54 2C A8 55
  </code></pre>
  
  <p>I must have made a mistake because this code produces the image "yr" instead of "yryryggyry"</p>
  
  <p>I am going to try to redo the work to see if I get a different answer</p>