home *** CD-ROM | disk | FTP | other *** search
/ Esprit de Apple Corps / EDAC-2.iso / Graphics / ASCII.Art / Art1 / HOW.GIF.TXT < prev    next >
Text File  |  1995-11-27  |  6KB  |  125 lines

  1. LZW compression used to encode/decode a GIF file by Bob Montgomery [73357,3140]
  2.  
  3. ENCODER
  4. Consider the following input data stream in a 4 color (A, B, C, D)  system.
  5. We will build a table of codes which represent strings of colors. Each time
  6. we  find a new string, we will give it the next code, and break it  into  a
  7. prefix string and a suffix color. The symbols \ or --- represent the prefix
  8. string, and / represents the suffix color. The first 4 entries in the table
  9. are  the  4 colors with codes 0 thru 3, each of which represents  a  single
  10. color.  The next 2 codes (4 and 5) are the clear code and the end of  image
  11. code.  The first available code to represent a string of colors is 6.  Each
  12. time  we  find  a new code, we will send the prefix for that  code  to  the
  13. output data stream.
  14.  
  15. A B A B A B A B B B A B A B A  A  C  D A C D A D  C A B A A A B A B .....
  16. \/\/---/-----/\/---/-------/\/ \/ \ /\/---/---/\ /\/-----/---/---/
  17. 6 7   8     9 10 11      12 13 14 15 16  17 18 19 20   21  22  23     Code
  18.     6    8      10    8                14  16        8    13  7       Prefix
  19.  
  20. The  encoder table is built from input data stream. Always start  with  the
  21. suffix  of  last  code,  and  keep getting colors  until  you  have  a  new
  22. combination.
  23.  
  24. Color     Code      Prefix    Suffix    String    Output
  25.  A        0                             -
  26.  B        1                             -
  27.  C        2                             -
  28.  D        3                             -
  29. Clear     4                             -
  30. End       5                             -
  31.  A \                A         A                   First color is a special
  32. case.
  33.  B /  \   6         A         B         AB        A
  34.  A |  /   7         B         A         BA        B
  35.  B |
  36.  A /  |   8         6         A         ABA       6
  37.  B    |
  38.  A    |
  39.  B \  /   9         8         B         ABAB      8
  40.  B /  |   10        B         B         BB        B
  41.  B    |
  42.  A |  /   11        10        A         BBA       10
  43.  B |
  44.  A |
  45.  B |
  46.  A /  \   12        9         A         ABABA     9
  47.  A \  /   13        A         A         AA        A
  48.  C /  \   14        A         C         AC        A
  49.  D \  /   15        C         D         CD        C
  50.  A /  |   16        D         A         DA        D
  51.  C    |
  52.  D |  /   17        14        D         ACD       14
  53.  A |
  54.  D /  \   18        16        D         DAD       16
  55.  C \  /   19        D         C         DC        D
  56.  A /  |   20        C         A         CA        C
  57.  B    |
  58.  A    |
  59.  A |  /   21        8         A         ABAA      8
  60.  A |
  61.  B /  |   22        13        B         AAB       13
  62.  A    |
  63.  B    /   23        7         B         BAB       7
  64.  
  65. The  resultant  output stream is: A B 6 8 B 10 9 A A C D 14 16 D C 8 ....
  66. The  GIF encoder starts with a code length of 2+1=3 bits for 4  colors,  so
  67. when  the code reaches 8 we will have to increase the code size to 4  bits.
  68. Similarly,  when the code gets to 16 we will have to increse the code  size
  69. to 5 bits, etc. If the code gets to 13 bits, we send a clear code and start
  70. over.   See GIFENCOD.GIF for a flow diagram of the encoding  process.  This
  71. uses a tree method to search if a new string is already in the table, which
  72. is much simpler, faster, and easier to understand than hashing.
  73.  
  74. ===========================================================================
  75.  
  76. DECODER
  77.  
  78. We will now see if we can regenerate the original data stream and duplicate
  79. the  table looking only at the output data stream generated by the  encoder
  80. on the previous page. The output data stream is:
  81.  
  82.           A B 6 8 B 10 9 A A C D 14 16 D C 8 ....
  83.  
  84. The  docoding process is harder to see, but easier to implement,  than  the
  85. encoding process. The data is taken in pairs, and a new code is assigned to
  86. each  pair. The prefix is the left side of the pair, and the suffix is  the
  87. color  that  the right side of the pair decomposes to from the  table.  The
  88. decomposition  is done by outputing the suffix of the code, and  using  the
  89. prefix  as the new code. The process repeats until the prefix is  a  single
  90. color, and it is output too. The output of the decomposition is pushed onto
  91. a  stack, and then poped off the stack to the display, which  restores  the
  92. original  order that the colors were seen by the encoder. We will  go  thru
  93. the  first  few entries in detail, which will hopefully  make  the  process
  94. clearer.
  95.      The  first pair is (A B), so the prefix of code 6 is A and the  suffix
  96. is B, and 6 represents the string AB. The color A is sent to the display.
  97.      The 2nd pair is (B 6), so the prefix of code 7 is B and the suffix  is
  98. the  the last color in the decomposition of code 6. Code 6 decomposes  into
  99. BA, so code 7 = BA, and has a suffix A. The color B is sent to the display.
  100.      The 3rd pair is (6 8) and the next code is 8. How can we decompose  8.
  101. We  know that the prefix of code 8 is 6, but we don't know the suffix.  The
  102. answer  is that we use the suffix of the prefix code; A in this case  since
  103. the suffix of 6 is A. Thus, code 8 = ABA and has a suffix A. We decompose 6
  104. to get BA, which becomes AB when we pop it off the stack to the display.
  105.      The 4th pair is (8 B), so code 9 has a prefix of 8 and a suffix of  B,
  106. and  code  9  = ABAB. We output ABA to the stack, and pop  it  off  to  the
  107. display as ABA.
  108.      The 5th pair is (B 10) and the next code is 10. The prefix of code  10
  109. is  B  and the suffix is B (since the prefix is B). Code 10 =  BB,  and  we
  110. output the prefix B to the display.
  111.      The  6th  pair is (10 9) and the next code is 11. Thus the  prefix  of
  112. code  11 is 10 and the suffix is the last color in the decomposition of  9,
  113. which is A.  Thus code 11 = BBA, And we output BB to the display.
  114.      So  far, we have output the correct colors stream A B AB ABA B  BB  to
  115. the display, and have duplicated the codes 6 thru 11 in the encoder  table.
  116. This  process  is  repeated for the whole data stream  to  reconstruct  the
  117. original  color stream and build a table identical to the one built by  the
  118. encoder.  We start the table with codes 0-5 representing the 4 colors,  the
  119. clear  code, and the end code. When we get to code 8, we must  increse  the
  120. code  size  to  5 bits, etc.  See GIFDECOD.GIF for a flow  diagram  of  the
  121. decoding process.
  122.  
  123. I Hope this helps take some of the mystery out of LZW compression, which is
  124. really quite easy once you 'see' it.      Bob Montgomery