home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / PCGPEV10.ZIP / GIF.TXT < prev    next >
Text File  |  1994-05-10  |  49KB  |  1,322 lines

  1.  
  2.  
  3.                    LZW and GIF explained----Steve Blackstock
  4.  
  5.  
  6.       I hope this little document will help enlighten those of you out there
  7. who want to know more about the Lempel-Ziv Welch compression algorithm, and,
  8. specifically, the implementation that GIF uses.
  9.      Before we start, here's a little terminology, for the purposes of this
  10. document:
  11.  
  12.       "character": a fundamental data element. In normal text files, this is
  13. just a single byte. In raster images, which is what we're interested in, it's
  14. an index that specifies the color of a given pixel. I'll refer to an arbitray
  15. character as "K".
  16.       "charstream": a stream of characters, as in a data file.
  17.       "string": a number of continuous characters, anywhere from one to very
  18. many characters in length. I can specify an arbitrary string as "[...]K".
  19.       "prefix": almost the same as a string, but with the implication that a
  20. prefix immediately precedes a character, and a prefix can have a length of
  21. zero. So, a prefix and a character make up a string. I will refer to an
  22. arbitrary prefix as "[...]".
  23.       "root": a single-character string. For most purposes, this is a
  24. character, but we may occasionally make a distinction. It is [...]K, where
  25. [...] is empty.
  26.       "code": a number, specified by a known number of bits, which maps to a
  27. string.
  28.       "codestream": the output stream of codes, as in the "raster data"
  29.       "entry": a code and its string.
  30.       "string table": a list of entries; usually, but not necessarily, unique.
  31.       That should be enough of that.
  32.  
  33.      LZW is a way of compressing data that takes advantage of repetition of
  34. strings in the data. Since raster data usually contains a lot of this
  35. repetition, LZW is a good way of compressing and decompressing it.
  36.      For the moment, lets consider normal LZW encoding and decoding. GIF's
  37. variation on the concept is just an extension from there.
  38.      LZW manipulates three objects in both compression and decompression: the
  39. charstream, the codestream, and the string table. In compression, the
  40. charstream is the input and the codestream is the output. In decompression,
  41. the codestream is the input and the charstream is the output. The string table
  42. is a product of both compression and decompression, but is never passed from
  43. one to the other.
  44.      The first thing we do in LZW compression is initialize our string table.
  45. To do this, we need to choose a code size (how many bits) and know how many
  46. values our characters can possibly take. Let's say our code size is 12 bits,
  47. meaning we can store 0->FFF, or 4096 entries in our string table. Lets also
  48. say that we have 32 possible different characters. (This corresponds to, say,
  49. a picture in which there are 32 different colors possible for each pixel.) To
  50. initialize the table, we set code#0 to character#0, code #1 to character#1,
  51. and so on, until code#31 to character#31. Actually, we are specifying that
  52. each code from 0 to 31 maps to a root. There will be no more entries in the
  53. table that have this property.
  54.      Now we start compressing data. Let's first define something called the
  55. "current prefix". It's just a prefix that we'll store things in and compare
  56. things to now and then. I will refer to it as "[.c.]". Initially, the current
  57. prefix has nothing in it. Let's also define a "current string", which will be
  58. the current prefix plus the next character in the charstream. I will refer to
  59. the current string as "[.c.]K", where K is some character. OK, look at the
  60. first character in the charstream. Call it P. Make [.c.]P the current string.
  61. (At this point, of course, it's just the root P.) Now search through the
  62. string table to see if [.c.]P appears in it. Of course, it does now, because
  63. our string table is initialized to have all roots. So we don't do anything.
  64. Now make [.c.]P the current prefix. Look at the next character in the
  65. charstream. Call it Q. Add it to the current prefix to form [.c.]Q, the
  66. current string. Now search through the string table to see if [.c.]Q appears
  67. in it. In this case, of course, it doesn't. Aha! Now we get to do something.
  68. Add [.c.]Q (which is PQ in this case) to the string table for code#32, and
  69. output the code for [.c.] to the codestream. Now start over again with the
  70. current prefix being just the root P. Keep adding characters to [.c.] to form
  71. [.c.]K, until you can't find [.c.]K in the string table. Then output the code
  72. for [.c.] and add [.c.]K to the string table. In pseudo-code, the algorithm
  73. goes something like this:
  74.  
  75.      [1] Initialize string table;
  76.      [2] [.c.] <- empty;
  77.      [3] K <- next character in charstream;
  78.      [4] Is [.c.]K in string table?
  79.       (yes: [.c.] <- [.c.]K;
  80.             go to [3];
  81.       )
  82.       (no: add [.c.]K to the string table;
  83.            output the code for [.c.] to the codestream;
  84.            [.c.] <- K;
  85.            go to [3];
  86.       )
  87.  
  88.        It's as simple as that! Of course, when you get to step [3] and there
  89. aren't any more characters left, you just output the code for [.c.] and throw
  90. the table away. You're done.
  91.       Wanna do an example? Let's pretend we have a four-character alphabet:
  92. A,B,C,D. The charstream looks like ABACABA. Let's compress it. First, we
  93. initialize our string table to: #0=A, #1=B, #2=C, #3=D. The first character is
  94. A, which is in the string table, so [.c.] becomes A. Next we get AB, which is
  95. not in the table, so we output code #0 (for [.c.]),
  96.      and add AB to the string table as code #4. [.c.] becomes B. Next we get
  97. [.c.]A = BA, which is not in the string table, so output code #1, and add BA
  98. to the string table as code #5. [.c.] becomes A. Next we get AC, which is not
  99. in the string table. Output code #0, and add AC to the string table as code
  100. #6. Now [.c.] becomes C. Next we get [.c.]A = CA, which is not in the table.
  101. Output #2 for C, and add CA to table as code#7. Now [.c.] becomes A. Next we
  102. get AB, which IS in the string table, so [.c.] gets AB, and we look at ABA,
  103. which is not in the string table, so output the code for AB, which is #4, and
  104. add ABA to the string table as code #8. [.c.] becomes A. We can't get any more
  105. characters, so we just output #0 for the code for A, and we're done. So, the
  106. codestream is #0#1#0#2#4#0.
  107.       A few words (four) should be said here about efficiency: use a hashing
  108. strategy. The search through the string table can be computationally
  109. intensive, and some hashing is well worth the effort. Also, note that
  110. "straight LZW" compression runs the risk of overflowing the string table -
  111. getting to a code which can't be represented in the number of bits you've set
  112. aside for codes. There are several ways of dealing with this problem, and GIF
  113. implements a very clever one, but we'll get to that.
  114.       An important thing to notice is that, at any point during the
  115. compression, if [...]K is in the string table, [...] is there also. This fact
  116. suggests an efficient method for storing strings in the table. Rather than
  117. store the entire string of K's in the table, realize that any string can be
  118. expressed as a prefix plus a character: [...]K. If we're about to store [...]K
  119. in the table, we know that [...] is already there, so we can just store the
  120. code for [...] plus the final character K.
  121.       Ok, that takes care of compression. Decompression is perhaps more
  122. difficult conceptually, but it is really easier to program.
  123.       Here's how it goes: We again have to start with an initialized string
  124. table. This table comes from what knowledge we have about the charstream that
  125. we will eventually get, like what possible values the characters can take. In
  126. GIF files, this information is in the header as the number of possible pixel
  127. values. The beauty of LZW, though, is that this is all we need to know. We
  128. will build the rest of the string table as we decompress the codestream. The
  129. compression is done in such a way that we will never encounter a code in the
  130. codestream that we can't translate into a string.
  131.       We need to define something called a "current code", which I will refer
  132. to as "<code>", and an "old-code", which I will refer to as "<old>". To start
  133. things off, look at the first code. This is now <code>. This code will be in
  134. the intialized string table as the code for a root. Output the root to the
  135. charstream. Make this code the old-code <old>. *Now look at the next code, and
  136. make it <code>. It is possible that this code will not be in the string table,
  137. but let's assume for now that it is. Output the string corresponding to <code>
  138. to the codestream. Now find the first character in the string you just
  139. translated. Call this K. Add this to the prefix [...] generated by <old> to
  140. form a new string [...]K. Add this string [...]K to the string table, and set
  141. the old-code <old> to the current code <code>. Repeat from where I typed the
  142. asterisk, and you're all set. Read this paragraph again if you just skimmed
  143. it!!!  Now let's consider the possibility that <code> is not in the string
  144. table. Think back to compression, and try to understand what happens when you
  145. have a string like P[...]P[...]PQ appear in the charstream. Suppose P[...] is
  146. already in the string table, but P[...]P is not. The compressor will parse out
  147. P[...], and find that P[...]P is not in the string table. It will output the
  148. code for P[...], and add P[...]P to the string table. Then it will get up to
  149. P[...]P for the next string, and find that P[...]P is in the table, as
  150.      the code just added. So it will output the code for P[...]P if it finds
  151. that P[...]PQ is not in the table. The decompressor is always "one step
  152. behind" the compressor. When the decompressor sees the code for P[...]P, it
  153. will not have added that code to it's string table yet because it needed the
  154. beginning character of P[...]P to add to the string for the last code, P[...],
  155. to form the code for P[...]P. However, when a decompressor finds a code that
  156. it doesn't know yet, it will always be the very next one to be added to the
  157. string table. So it can guess at what the string for the code should be, and,
  158. in fact, it will always be correct. If I am a decompressor, and I see
  159. code#124, and yet my string table has entries only up to code#123, I can
  160. figure out what code#124 must be, add it to my string table, and output the
  161. string. If code#123 generated the string, which I will refer to here as a
  162. prefix, [...], then code#124, in this special case, will be [...] plus the
  163. first character of [...]. So just add the first character of [...] to the end
  164. of itself. Not too bad.  As an example (and a very common one) of this special
  165. case, let's assume we have a raster image in which the first three pixels have
  166. the same color value. That is, my charstream looks like: QQQ.... For the sake
  167. of argument, let's say we have 32 colors, and Q is the color#12. The
  168. compressor will generate the code sequence 12,32,.... (if you don't know why,
  169. take a minute to understand it.) Remember that #32 is not in the initial
  170. table, which goes from #0 to #31. The decompressor will see #12 and translate
  171. it just fine as color Q. Then it will see #32 and not yet know what that
  172. means. But if it thinks about it long enough, it can figure out that QQ should
  173. be entry#32 in the table and QQ should be the next string output.  So the
  174. decompression pseudo-code goes something like:
  175.  
  176.       [1] Initialize string table;
  177.      [2] get first code: <code>;
  178.      [3] output the string for <code> to the charstream;
  179.      [4] <old> = <code>;
  180.      [5] <code> <- next code in codestream;
  181.      [6] does <code> exist in the string table?
  182.       (yes: output the string for <code> to the charstream;
  183.             [...] <- translation for <old>;
  184.             K <- first character of translation for <code>;
  185.             add [...]K to the string table;        <old> <- <code>;  )
  186.       (no: [...] <- translation for <old>;
  187.            K <- first character of [...];
  188.            output [...]K to charstream and add it to string table;
  189.            <old> <- <code>
  190.       )
  191.      [7] go to [5];
  192.  
  193.       Again, when you get to step [5] and there are no more codes, you're
  194. finished.  Outputting of strings, and finding of initial characters in strings
  195. are efficiency problems all to themselves, but I'm not going to suggest ways
  196. to do them here. Half the fun of programming is figuring these things out!
  197.       ---
  198.       Now for the GIF variations on the theme. In part of the header of a GIF
  199. file, there is a field, in the Raster Data stream, called "code size". This is
  200. a very misleading name for the field, but we have to live with it. What it is
  201. really is the "root size". The actual size, in bits, of the compression codes
  202. actually changes during compression/decompression, and I will refer to that
  203. size here as the "compression size". The initial table is just the codes for
  204. all the roots, as usual, but two special codes are added on top of those.
  205. Suppose you have a "code size", which is usually the number of bits per pixel
  206. in the image, of N. If the number of bits/pixel is one, then N must be 2: the
  207. roots take up slots #0 and #1 in the initial table, and the two special codes
  208. will take up slots #4 and #5. In any other case, N is the number of bits per
  209. pixel, and the roots take up slots #0 through #(2**N-1), and the special codes
  210. are (2**N) and (2**N + 1). The initial compression size will be N+1 bits per
  211. code. If you're encoding, you output the codes (N+1) bits at a time to start
  212. with, and if you're decoding, you grab (N+1) bits from the codestream at a
  213. time.  As for the special codes: <CC> or the clear code, is (2**N), and <EOI>,
  214. or end-of-information, is (2**N + 1). <CC> tells the compressor to re-
  215. initialize the string table, and to reset the compression size to (N+1). <EOI>
  216. means there's no more in the codestream.  If you're encoding or decoding, you
  217. should start adding things to the string table at <CC> + 2. If you're
  218. encoding, you should output <CC> as the very first code, and then whenever
  219. after that you reach code #4095 (hex FFF), because GIF does not allow
  220. compression sizes to be greater than 12 bits. If you're decoding, you should
  221. reinitialize your string table when you observe <CC>.  The variable
  222. compression sizes are really no big deal. If you're encoding, you start with a
  223. compression size of (N+1) bits, and, whenever you output the code
  224. (2**(compression size)-1), you bump the compression size up one bit. So the
  225. next code you output will be one bit longer. Remember that the largest
  226. compression size is 12 bits, corresponding to a code of 4095. If you get that
  227. far, you must output <CC> as the next code, and start over.  If you're
  228. decoding, you must increase your compression size AS SOON AS YOU write entry
  229. #(2**(compression size) - 1) to the string table. The next code you READ will
  230. be one bit longer. Don't make the mistake of waiting until you need to add the
  231. code (2**compression size) to the table. You'll have already missed a bit from
  232. the last code.  The packaging of codes into a bitsream for the raster data is
  233. also a potential stumbling block for the novice encoder or decoder. The lowest
  234. order bit in the code should coincide with the lowest available bit in the
  235. first available byte in the codestream. For example, if you're starting with
  236. 5-bit compression codes, and your first three codes are, say, <abcde>,
  237. <fghij>, <klmno>, where e, j, and o are bit#0, then your codestream will start
  238. off like:
  239.  
  240.        byte#0: hijabcde
  241.        byte#1: .klmnofg
  242.  
  243.       So the differences between straight LZW and GIF LZW are: two additional
  244. special codes and variable compression sizes. If you understand LZW, and you
  245. understand those variations, you understand it all!
  246.       Just as sort of a P.S., you may have noticed that a compressor has a
  247. little bit of flexibility at compression time. I specified a "greedy" approach
  248. to the compression, grabbing as many characters as possible before outputting
  249. codes. This is, in fact, the standard LZW way of doing things, and it will
  250. yield the best compression ratio. But there's no rule saying you can't stop
  251. anywhere along the line and just output the code for the current prefix,
  252. whether it's already in the table or not, and add that string plus the next
  253. character to the string table. There are various reasons for wanting to do
  254. this, especially if the strings get extremely long and make hashing difficult.
  255. If you need to, do it.
  256.       Hope this helps out.----steve blackstock
  257.  
  258. ---------------------------------------------------------------------------
  259. Article 5729 of comp.graphics:
  260. Path: polya!shelby!labrea!agate!ucbvax!tut.cis.ohio-state.edu!rutgers!cmcl2!phri!cooper!john
  261. >From: john@cooper.cooper.EDU (John Barkaus)
  262. Newsgroups: comp.graphics
  263. Subject: GIF file format responses 4/5
  264. Keywords: GIF LZW
  265. Message-ID: <1489@cooper.cooper.EDU>
  266. Date: 21 Apr 89 20:56:35 GMT
  267. Organization: The Cooper Union (NY, NY)
  268. Lines: 1050
  269.  
  270.  
  271. >From: cmcl2!neuron1.Jpl.Nasa.Gov!harry (Harry Langenbacher)
  272.  
  273.                                 G I F (tm)
  274.  
  275.                      Graphics Interchange Format (tm)
  276.  
  277.                       A standard defining a mechanism
  278.  
  279.                      for the storage and transmission
  280.  
  281.                    of raster-based graphics information
  282.  
  283.                                June 15, 1987
  284.  
  285.                      (c) CompuServe Incorporated, 1987
  286.  
  287.                             All rights reserved
  288.  
  289.             While this document is copyrighted, the information
  290.  
  291.           contained within is made available for use in computer
  292.  
  293.           software without royalties, or licensing restrictions.
  294.  
  295.           GIF and 'Graphics Interchange Format' are trademarks of
  296.  
  297.                          CompuServe, Incorporated.
  298.  
  299.                            an H&R Block Company
  300.  
  301.                         5000 Arlington Centre Blvd.
  302.  
  303.                            Columbus, Ohio 43220
  304.  
  305.                               (614) 457-8600
  306.  
  307.                                                                      Page 2
  308.  
  309.               Graphics Interchange Format (GIF) Specification
  310.  
  311.                              Table of Contents
  312.  
  313.         INTRODUCTION . . . . . . . . . . . . . . . . . page 3
  314.  
  315.         GENERAL FILE FORMAT  . . . . . . . . . . . . . page 3
  316.  
  317.         GIF SIGNATURE  . . . . . . . . . . . . . . . . page 4
  318.  
  319.         SCREEN DESCRIPTOR  . . . . . . . . . . . . . . page 4
  320.  
  321.         GLOBAL COLOR MAP . . . . . . . . . . . . . . . page 5
  322.  
  323.         IMAGE DESCRIPTOR . . . . . . . . . . . . . . . page 6
  324.  
  325.         LOCAL COLOR MAP  . . . . . . . . . . . . . . . page 7
  326.  
  327.         RASTER DATA  . . . . . . . . . . . . . . . . . page 7
  328.  
  329.         GIF TERMINATOR . . . . . . . . . . . . . . . . page 8
  330.  
  331.         GIF EXTENSION BLOCKS . . . . . . . . . . . . . page 8
  332.  
  333.         APPENDIX A - GLOSSARY  . . . . . . . . . . . . page 9
  334.  
  335.         APPENDIX B - INTERACTIVE SEQUENCES . . . . . . page 10
  336.  
  337.         APPENDIX C - IMAGE PACKAGING & COMPRESSION . . page 12
  338.  
  339.         APPENDIX D - MULTIPLE IMAGE PROCESSING . . . . page 15
  340.  
  341. Graphics Interchange Format (GIF)                                    Page 3
  342.  
  343. Specification
  344.  
  345. INTRODUCTION
  346.  
  347.         'GIF' (tm) is CompuServe's standard for defining generalized  color
  348.  
  349.    raster   images.    This   'Graphics  Interchange  Format'  (tm)  allows
  350.  
  351.    high-quality, high-resolution graphics to be displayed on a  variety  of
  352.  
  353.    graphics  hardware  and is intended as an exchange and display mechanism
  354.  
  355.    for graphics images.  The image format described  in  this  document  is
  356.  
  357.    designed  to  support  current  and  future image technology and will in
  358.  
  359.    addition serve as a basis for future CompuServe graphics products.
  360.  
  361.         The main focus  of  this  document  is  to  provide  the  technical
  362.  
  363.    information  necessary  for  a  programmer to implement GIF encoders and
  364.  
  365.    decoders.  As such, some assumptions are made as to terminology relavent
  366.  
  367.    to graphics and programming in general.
  368.  
  369.         The first section of this document describes the  GIF  data  format
  370.  
  371.    and its components and applies to all GIF decoders, either as standalone
  372.  
  373.    programs or as part of  a  communications  package.   Appendix  B  is  a
  374.  
  375.    section  relavent to decoders that are part of a communications software
  376.  
  377.    package and describes the protocol requirements for entering and exiting
  378.  
  379.    GIF mode, and responding to host interrogations.  A glossary in Appendix
  380.  
  381.    A defines some of the terminology used in  this  document.   Appendix  C
  382.  
  383.    gives  a  detailed  explanation  of  how  the  graphics  image itself is
  384.  
  385.    packaged as a series of data bytes.
  386.  
  387.                 Graphics Interchange Format Data Definition
  388.  
  389.  GENERAL FILE FORMAT
  390.  
  391.         +-----------------------+
  392.  
  393.         | +-------------------+ |
  394.  
  395.         | |   GIF Signature   | |
  396.  
  397.         | +-------------------+ |
  398.  
  399.         | +-------------------+ |
  400.  
  401.         | | Screen Descriptor | |
  402.  
  403.         | +-------------------+ |
  404.  
  405.         | +-------------------+ |
  406.  
  407.         | | Global Color Map  | |
  408.  
  409.         | +-------------------+ |
  410.  
  411.         . . .               . . .
  412.  
  413.         | +-------------------+ |    ---+
  414.  
  415.         | |  Image Descriptor | |       |
  416.  
  417.         | +-------------------+ |       |
  418.  
  419.         | +-------------------+ |       |
  420.  
  421.         | |  Local Color Map  | |       |-   Repeated 1 to n times
  422.  
  423.         | +-------------------+ |       |
  424.  
  425.         | +-------------------+ |       |
  426.  
  427.         | |    Raster Data    | |       |
  428.  
  429.         | +-------------------+ |    ---+
  430.  
  431.         . . .               . . .
  432.  
  433.         |-    GIF Terminator   -|
  434.  
  435.         +-----------------------+
  436.  
  437. Graphics Interchange Format (GIF)                                    Page 4
  438.  
  439. Specification
  440.  
  441.  GIF SIGNATURE
  442.  
  443.         The following GIF Signature identifies  the  data  following  as  a
  444.  
  445.    valid GIF image stream.  It consists of the following six characters:
  446.  
  447.              G I F 8 7 a
  448.  
  449.         The last three characters '87a' may be viewed as a  version  number
  450.  
  451.    for  this  particular  GIF  definition  and will be used in general as a
  452.  
  453.    reference  in  documents  regarding  GIF  that   address   any   version
  454.  
  455.    dependencies.
  456.  
  457.  SCREEN DESCRIPTOR
  458.  
  459.         The Screen Descriptor describes the overall parameters for all  GIF
  460.  
  461.    images  following.  It defines the overall dimensions of the image space
  462.  
  463.    or logical screen required, the existance of color mapping  information,
  464.  
  465.    background  screen color, and color depth information.  This information
  466.  
  467.    is stored in a series of 8-bit bytes as described below.
  468.  
  469.               bits
  470.  
  471.          7 6 5 4 3 2 1 0  Byte #
  472.  
  473.         +---------------+
  474.  
  475.         |               |  1
  476.  
  477.         +-Screen Width -+      Raster width in pixels (LSB first)
  478.  
  479.         |               |  2
  480.  
  481.         +---------------+
  482.  
  483.         |               |  3
  484.  
  485.         +-Screen Height-+      Raster height in pixels (LSB first)
  486.  
  487.         |               |  4
  488.  
  489.         +-+-----+-+-----+      M = 1, Global color map follows Descriptor
  490.  
  491.         |M|  cr |0|pixel|  5   cr+1 = # bits of color resolution
  492.  
  493.         +-+-----+-+-----+      pixel+1 = # bits/pixel in image
  494.  
  495.         |   background  |  6   background=Color index of screen background
  496.  
  497.         +---------------+          (color is defined from the Global color
  498.  
  499.         |0 0 0 0 0 0 0 0|  7        map or default map if none specified)
  500.  
  501.         +---------------+
  502.  
  503.         The logical screen width and height can both  be  larger  than  the
  504.  
  505.    physical  display.   How  images  larger  than  the physical display are
  506.  
  507.    handled is implementation dependent and can take advantage  of  hardware
  508.  
  509.    characteristics  (e.g.   Macintosh scrolling windows).  Otherwise images
  510.  
  511.    can be clipped to the edges of the display.
  512.  
  513.         The value of 'pixel' also defines  the  maximum  number  of  colors
  514.  
  515.    within  an  image.   The  range  of  values  for 'pixel' is 0 to 7 which
  516.  
  517.    represents 1 to 8 bits.  This translates to a range of 2 (B & W) to  256
  518.  
  519.    colors.   Bit  3 of word 5 is reserved for future definition and must be
  520.  
  521.    zero.
  522.  
  523. Graphics Interchange Format (GIF)                                    Page 5
  524.  
  525. Specification
  526.  
  527.  GLOBAL COLOR MAP
  528.  
  529.         The Global Color Map is optional but recommended for  images  where
  530.  
  531.    accurate color rendition is desired.  The existence of this color map is
  532.  
  533.    indicated in the 'M' field of byte 5 of the Screen Descriptor.  A  color
  534.  
  535.    map  can  also  be associated with each image in a GIF file as described
  536.  
  537.    later.  However this  global  map  will  normally  be  used  because  of
  538.  
  539.    hardware  restrictions  in equipment available today.  In the individual
  540.  
  541.    Image Descriptors the 'M' flag will normally be  zero.   If  the  Global
  542.  
  543.    Color  Map  is  present,  it's definition immediately follows the Screen
  544.  
  545.    Descriptor.   The  number  of  color  map  entries  following  a  Screen
  546.  
  547.    Descriptor  is equal to 2**(# bits per pixel), where each entry consists
  548.  
  549.    of three byte values representing the relative intensities of red, green
  550.  
  551.    and blue respectively.  The structure of the Color Map block is:
  552.  
  553.               bits
  554.  
  555.          7 6 5 4 3 2 1 0  Byte #
  556.  
  557.         +---------------+
  558.  
  559.         | red intensity |  1    Red value for color index 0
  560.  
  561.         +---------------+
  562.  
  563.         |green intensity|  2    Green value for color index 0
  564.  
  565.         +---------------+
  566.  
  567.         | blue intensity|  3    Blue value for color index 0
  568.  
  569.         +---------------+
  570.  
  571.         | red intensity |  4    Red value for color index 1
  572.  
  573.         +---------------+
  574.  
  575.         |green intensity|  5    Green value for color index 1
  576.  
  577.         +---------------+
  578.  
  579.         | blue intensity|  6    Blue value for color index 1
  580.  
  581.         +---------------+
  582.  
  583.         :               :       (Continues for remaining colors)
  584.  
  585.         Each image pixel value received will be displayed according to  its
  586.  
  587.    closest match with an available color of the display based on this color
  588.  
  589.    map.  The color components represent a fractional intensity  value  from
  590.  
  591.    none  (0)  to  full (255).  White would be represented as (255,255,255),
  592.  
  593.    black as (0,0,0) and medium yellow as (180,180,0).  For display, if  the
  594.  
  595.    device  supports fewer than 8 bits per color component, the higher order
  596.  
  597.    bits of each component are used.  In the creation of  a  GIF  color  map
  598.  
  599.    entry  with  hardware  supporting  fewer  than 8 bits per component, the
  600.  
  601.    component values for the hardware  should  be  converted  to  the  8-bit
  602.  
  603.    format with the following calculation:
  604.  
  605.         <map_value> = <component_value>*255/(2**<nbits> -1)
  606.  
  607.         This assures accurate translation of colors for all  displays.   In
  608.  
  609.    the  cases  of  creating  GIF images from hardware without color palette
  610.  
  611.    capability, a fixed palette should be created  based  on  the  available
  612.  
  613.    display  colors for that hardware.  If no Global Color Map is indicated,
  614.  
  615.    a default color map is generated internally  which  maps  each  possible
  616.  
  617.    incoming  color  index to the same hardware color index modulo <n> where
  618.  
  619.    <n> is the number of available hardware colors.
  620.  
  621. Graphics Interchange Format (GIF)                                    Page 6
  622.  
  623. Specification
  624.  
  625.  IMAGE DESCRIPTOR
  626.  
  627.         The Image Descriptor defines the actual placement  and  extents  of
  628.  
  629.    the  following  image within the space defined in the Screen Descriptor.
  630.  
  631.    Also defined are flags to indicate the presence of a local color  lookup
  632.  
  633.    map, and to define the pixel display sequence.  Each Image Descriptor is
  634.  
  635.    introduced by an image separator  character.   The  role  of  the  Image
  636.  
  637.    Separator  is simply to provide a synchronization character to introduce
  638.  
  639.    an Image Descriptor.  This is desirable if a GIF file happens to contain
  640.  
  641.    more  than  one  image.   This  character  is defined as 0x2C hex or ','
  642.  
  643.    (comma).  When this character is encountered between images,  the  Image
  644.  
  645.    Descriptor will follow immediately.
  646.  
  647.         Any characters encountered between the end of a previous image  and
  648.  
  649.    the image separator character are to be ignored.  This allows future GIF
  650.  
  651.    enhancements to be present in newer image formats and yet ignored safely
  652.  
  653.    by older software decoders.
  654.  
  655.               bits
  656.  
  657.          7 6 5 4 3 2 1 0  Byte #
  658.  
  659.         +---------------+
  660.  
  661.         |0 0 1 0 1 1 0 0|  1    ',' - Image separator character
  662.  
  663.         +---------------+
  664.  
  665.         |               |  2    Start of image in pixels from the
  666.  
  667.         +-  Image Left -+       left side of the screen (LSB first)
  668.  
  669.         |               |  3
  670.  
  671.         +---------------+
  672.  
  673.         |               |  4
  674.  
  675.         +-  Image Top  -+       Start of image in pixels from the
  676.  
  677.         |               |  5    top of the screen (LSB first)
  678.  
  679.         +---------------+
  680.  
  681.         |               |  6
  682.  
  683.         +- Image Width -+       Width of the image in pixels (LSB first)
  684.  
  685.         |               |  7
  686.  
  687.         +---------------+
  688.  
  689.         |               |  8
  690.  
  691.         +- Image Height-+       Height of the image in pixels (LSB first)
  692.  
  693.         |               |  9
  694.  
  695.         +-+-+-+-+-+-----+       M=0 - Use global color map, ignore 'pixel'
  696.  
  697.         |M|I|0|0|0|pixel| 10    M=1 - Local color map follows, use 'pixel'
  698.  
  699.         +-+-+-+-+-+-----+       I=0 - Image formatted in Sequential order
  700.  
  701.                                 I=1 - Image formatted in Interlaced order
  702.  
  703.                                 pixel+1 - # bits per pixel for this image
  704.  
  705.         The specifications for the image position and size must be confined
  706.  
  707.    to  the  dimensions defined by the Screen Descriptor.  On the other hand
  708.  
  709.    it is not necessary that the image fill the entire screen defined.
  710.  
  711.  LOCAL COLOR MAP
  712.  
  713. Graphics Interchange Format (GIF)                                    Page 7
  714.  
  715. Specification
  716.  
  717.         A Local Color Map is optional and defined here for future use.   If
  718.  
  719.    the  'M' bit of byte 10 of the Image Descriptor is set, then a color map
  720.  
  721.    follows the Image Descriptor that applies only to the  following  image.
  722.  
  723.    At the end of the image, the color map will revert to that defined after
  724.  
  725.    the Screen Descriptor.  Note that the 'pixel' field of byte  10  of  the
  726.  
  727.    Image  Descriptor  is used only if a Local Color Map is indicated.  This
  728.  
  729.    defines the parameters not only for the image pixel size, but determines
  730.  
  731.    the  number  of color map entries that follow.  The bits per pixel value
  732.  
  733.    will also revert to the value specified in the  Screen  Descriptor  when
  734.  
  735.    processing of the image is complete.
  736.  
  737.  RASTER DATA
  738.  
  739.         The format of the actual image is defined as the  series  of  pixel
  740.  
  741.    color  index  values that make up the image.  The pixels are stored left
  742.  
  743.    to right sequentially for an image row.  By default each  image  row  is
  744.  
  745.    written  sequentially, top to bottom.  In the case that the Interlace or
  746.  
  747.    'I' bit is set in byte 10 of the Image Descriptor then the row order  of
  748.  
  749.    the  image  display  follows  a  four-pass process in which the image is
  750.  
  751.    filled in by widely spaced rows.  The first pass writes every  8th  row,
  752.  
  753.    starting  with  the top row of the image window.  The second pass writes
  754.  
  755.    every 8th row starting at the fifth row from the top.   The  third  pass
  756.  
  757.    writes every 4th row starting at the third row from the top.  The fourth
  758.  
  759.    pass completes the image, writing  every  other  row,  starting  at  the
  760.  
  761.    second row from the top.  A graphic description of this process follows:
  762.  
  763.    Image
  764.  
  765.    Row  Pass 1  Pass 2  Pass 3  Pass 4          Result
  766.  
  767.    ---------------------------------------------------
  768.  
  769.      0  **1a**                                  **1a**
  770.  
  771.      1                          **4a**          **4a**
  772.  
  773.      2                  **3a**                  **3a**
  774.  
  775.      3                          **4b**          **4b**
  776.  
  777.      4          **2a**                          **2a**
  778.  
  779.      5                          **4c**          **4c**
  780.  
  781.      6                  **3b**                  **3b**
  782.  
  783.      7                          **4d**          **4d**
  784.  
  785.      8  **1b**                                  **1b**
  786.  
  787.      9                          **4e**          **4e**
  788.  
  789.     10                  **3c**                  **3c**
  790.  
  791.     11                          **4f**          **4f**
  792.  
  793.     12          **2b**                          **2b**
  794.  
  795.    . . .
  796.  
  797.         The image pixel values are processed as a series of  color  indices
  798.  
  799.    which  map  into the existing color map.  The resulting color value from
  800.  
  801.    the map is what is actually displayed.  This series  of  pixel  indices,
  802.  
  803.    the  number  of  which  is equal to image-width*image-height pixels, are
  804.  
  805.    passed to the GIF image data stream one value per pixel, compressed  and
  806.  
  807.    packaged  according  to  a  version  of the LZW compression algorithm as
  808.  
  809.    defined in Appendix C.
  810.  
  811. Graphics Interchange Format (GIF)                                    Page 8
  812.  
  813. Specification
  814.  
  815.  GIF TERMINATOR
  816.  
  817.         In order to provide a synchronization for the termination of a  GIF
  818.  
  819.    image  file,  a  GIF  decoder  will process the end of GIF mode when the
  820.  
  821.    character 0x3B hex or ';' is found after an image  has  been  processed.
  822.  
  823.    By  convention  the  decoding software will pause and wait for an action
  824.  
  825.    indicating that the user is ready to continue.  This may be  a  carriage
  826.  
  827.    return  entered  at  the  keyboard  or  a  mouse click.  For interactive
  828.  
  829.    applications this user action must  be  passed  on  to  the  host  as  a
  830.  
  831.    carriage  return  character  so  that the host application can continue.
  832.  
  833.    The decoding software will then typically leave graphics mode and resume
  834.  
  835.    any previous process.
  836.  
  837.  GIF EXTENSION BLOCKS
  838.  
  839.         To provide for orderly extension of the GIF definition, a mechanism
  840.  
  841.    for  defining  the  packaging  of extensions within a GIF data stream is
  842.  
  843.    necessary.  Specific GIF extensions are to be defined and documented  by
  844.  
  845.    CompuServe in order to provide a controlled enhancement path.
  846.  
  847.         GIF Extension Blocks are packaged in a manner similar to that  used
  848.  
  849.    by the raster data though not compressed.  The basic structure is:
  850.  
  851.          7 6 5 4 3 2 1 0  Byte #
  852.  
  853.         +---------------+
  854.  
  855.         |0 0 1 0 0 0 0 1|  1       '!' - GIF Extension Block Introducer
  856.  
  857.         +---------------+
  858.  
  859.         | function code |  2       Extension function code (0 to 255)
  860.  
  861.         +---------------+    ---+
  862.  
  863.         |  byte count   |       |
  864.  
  865.         +---------------+       |
  866.  
  867.         :               :       +-- Repeated as many times as necessary
  868.  
  869.         |func data bytes|       |
  870.  
  871.         :               :       |
  872.  
  873.         +---------------+    ---+
  874.  
  875.         . . .       . . .
  876.  
  877.         +---------------+
  878.  
  879.         |0 0 0 0 0 0 0 0|       zero byte count (terminates block)
  880.  
  881.         +---------------+
  882.  
  883.         A GIF Extension Block may immediately preceed any Image  Descriptor
  884.  
  885.    or occur before the GIF Terminator.
  886.  
  887.         All GIF decoders must be able to recognize  the  existence  of  GIF
  888.  
  889.    Extension  Blocks  and  read past them if unable to process the function
  890.  
  891.    code.  This ensures that older decoders will be able to process extended
  892.  
  893.    GIF   image   files   in  the  future,  though  without  the  additional
  894.  
  895.    functionality.
  896.  
  897. Graphics Interchange Format (GIF)                                    Page 9
  898.  
  899. Appendix A - Glossary
  900.  
  901.                                  GLOSSARY
  902.  
  903. Pixel - The smallest picture element of a  graphics  image.   This  usually
  904.  
  905.    corresponds  to  a single dot on a graphics screen.  Image resolution is
  906.  
  907.    typically given in units of  pixels.   For  example  a  fairly  standard
  908.  
  909.    graphics  screen  format  is  one 320 pixels across and 200 pixels high.
  910.  
  911.    Each pixel can  appear  as  one  of  several  colors  depending  on  the
  912.  
  913.    capabilities of the graphics hardware.
  914.  
  915. Raster - A horizontal row of pixels representing one line of an  image.   A
  916.  
  917.    typical method of working with images since most hardware is oriented to
  918.  
  919.    work most efficiently in this manner.
  920.  
  921. LSB - Least Significant Byte.  Refers to a convention for two byte  numeric
  922.  
  923.    values in which the less significant byte of the value preceeds the more
  924.  
  925.    significant byte.  This convention is typical on many microcomputers.
  926.  
  927. Color Map - The list of definitions of each color  used  in  a  GIF  image.
  928.  
  929.    These  desired  colors are converted to available colors through a table
  930.  
  931.    which is derived by assigning an incoming color index (from  the  image)
  932.  
  933.    to  an  output  color  index  (of  the  hardware).   While the color map
  934.  
  935.    definitons are specified in a GIF image, the output  pixel  colors  will
  936.  
  937.    vary  based  on  the  hardware used and its ability to match the defined
  938.  
  939.    color.
  940.  
  941. Interlace - The method of displaying a GIF image in which  multiple  passes
  942.  
  943.    are  made,  outputting  raster  lines  spaced  apart to provide a way of
  944.  
  945.    visualizing the general content of an entire image  before  all  of  the
  946.  
  947.    data has been processed.
  948.  
  949. B Protocol - A CompuServe-developed error-correcting file transfer protocol
  950.  
  951.    available  in  the  public  domain  and implemented in CompuServe VIDTEX
  952.  
  953.    products.  This error checking mechanism will be used  in  transfers  of
  954.  
  955.    GIF images for interactive applications.
  956.  
  957. LZW - A sophisticated data compression algorithm  based  on  work  done  by
  958.  
  959.    Lempel-Ziv  &  Welch  which  has  the feature of very efficient one-pass
  960.  
  961.    encoding and decoding.  This allows the image  to  be  decompressed  and
  962.  
  963.    displayed  at  the  same  time.   The  original  article from which this
  964.  
  965.    technique was adapted is:
  966.  
  967.           Terry  A.   Welch,  "A  Technique  for  High   Performance   Data
  968.  
  969.           Compression", IEEE Computer, vol 17 no 6 (June 1984)
  970.  
  971.         This basic algorithm is also used in the  public  domain  ARC  file
  972.  
  973.    compression  utilities.   The  CompuServe  adaptation  of LZW for GIF is
  974.  
  975.    described in Appendix C.
  976.  
  977. Graphics Interchange Format (GIF)                                   Page 10
  978.  
  979. Appendix B - Interactive Sequences
  980.  
  981.            GIF Sequence Exchanges for an Interactive Environment
  982.  
  983.         The following sequences are defined for use  in  mediating  control
  984.  
  985.    between a GIF sender and GIF receiver over an interactive communications
  986.  
  987.    line.  These  sequences  do  not  apply  to  applications  that  involve
  988.  
  989.    downloading  of  static  GIF  files and are not considered part of a GIF
  990.  
  991.    file.
  992.  
  993.  GIF CAPABILITIES ENQUIRY
  994.  
  995.         The GCE sequence is issued from a host and requests an  interactive
  996.  
  997.    GIF  decoder  to  return  a  response  message that defines the graphics
  998.  
  999.    parameters for the decoder.  This involves returning  information  about
  1000.  
  1001.    available screen sizes, number of bits/color supported and the amount of
  1002.  
  1003.    color detail supported.  The escape sequence for the GCE is defined as:
  1004.  
  1005.         ESC [ > 0 g     (g is lower case, spaces inserted for clarity)
  1006.  
  1007.                          (0x1B 0x5B 0x3E 0x30 0x67)
  1008.  
  1009.  GIF CAPABILITIES RESPONSE
  1010.  
  1011.         The GIF Capabilities Response message is returned by an interactive
  1012.  
  1013.    GIF  decoder  and  defines  the  decoder's  display capabilities for all
  1014.  
  1015.    graphics modes that are supported by the software.  Note that  this  can
  1016.  
  1017.    also include graphics printers as well as a monitor screen.  The general
  1018.  
  1019.    format of this message is:
  1020.  
  1021.      #version;protocol{;dev, width, height, color-bits, color-res}... <CR>
  1022.  
  1023.    '#'          - GCR identifier character (Number Sign)
  1024.  
  1025.    version      - GIF format version number;  initially '87a'
  1026.  
  1027.    protocol='0' - No end-to-end protocol supported by decoder
  1028.  
  1029.                   Transfer as direct 8-bit data stream.
  1030.  
  1031.    protocol='1' - Can use an error correction protocol to transfer GIF data
  1032.  
  1033.                interactively from the host directly to the display.
  1034.  
  1035.    dev = '0'    - Screen parameter set follows
  1036.  
  1037.    dev = '1'    - Printer parameter set follows
  1038.  
  1039.    width- Maximum supported display width in pixels
  1040.  
  1041.    height       - Maximum supported display height in pixels
  1042.  
  1043.    color-bits   - Number of  bits  per  pixel  supported.   The  number  of
  1044.  
  1045.                supported colors is therefore 2**color-bits.
  1046.  
  1047.    color-res    - Number of bits  per  color  component  supported  in  the
  1048.  
  1049.                hardware  color  palette.   If  color-res  is  '0'  then  no
  1050.  
  1051.                hardware palette table is available.
  1052.  
  1053.         Note that all values in the  GCR  are  returned  as  ASCII  decimal
  1054.  
  1055.    numbers and the message is terminated by a Carriage Return character.
  1056.  
  1057. Graphics Interchange Format (GIF)                                   Page 11
  1058.  
  1059. Appendix B - Interactive Sequences
  1060.  
  1061.         The  following   GCR   message   describes   three   standard   EGA
  1062.  
  1063.    configurations  with  no  printer;  the GIF data stream can be processed
  1064.  
  1065.    within an error correcting protocol:
  1066.  
  1067.         #87a;1 ;0,320,200,4,0 ;0,640,200,2,2 ;0,640,350,4,2<CR>
  1068.  
  1069.  ENTER GIF GRAPHICS MODE
  1070.  
  1071.         Two sequences are currently defined to invoke  an  interactive  GIF
  1072.  
  1073.    decoder into action.  The only difference between them is that different
  1074.  
  1075.    output media are selected.  These sequences are:
  1076.  
  1077.      ESC [ > 1 g   Display GIF image on screen
  1078.  
  1079.                    (0x1B 0x5B 0x3E 0x31 0x67)
  1080.  
  1081.      ESC [ > 2 g   Display image directly to an attached graphics  printer.
  1082.  
  1083.                    The  image  may optionally be displayed on the screen as
  1084.  
  1085.                    well.
  1086.  
  1087.                    (0x1B 0x5B 0x3E 0x32 0x67)
  1088.  
  1089.         Note that the 'g' character terminating each sequence is  in  lower
  1090.  
  1091.    case.
  1092.  
  1093.  INTERACTIVE ENVIRONMENT
  1094.  
  1095.         The assumed environment for the transmission of GIF image data from
  1096.  
  1097.    an  interactive  application  is  a  full 8-bit data stream from host to
  1098.  
  1099.    micro.  All 256 character codes must be transferrable.  The establishing
  1100.  
  1101.    of  an 8-bit data path for communications will normally be taken care of
  1102.  
  1103.    by the host application programs.  It is however  up  to  the  receiving
  1104.  
  1105.    communications programs supporting GIF to be able to receive and pass on
  1106.  
  1107.    all 256 8-bit codes to the GIF decoder software.
  1108.  
  1109. Graphics Interchange Format (GIF)                                   Page 12
  1110.  
  1111. Appendix C - Image Packaging & Compression
  1112.  
  1113.         The Raster Data stream that represents the actual output image  can
  1114.  
  1115.    be represented as:
  1116.  
  1117.          7 6 5 4 3 2 1 0
  1118.  
  1119.         +---------------+
  1120.  
  1121.         |   code size   |
  1122.  
  1123.         +---------------+     ---+
  1124.  
  1125.         |blok byte count|        |
  1126.  
  1127.         +---------------+        |
  1128.  
  1129.         :               :        +-- Repeated as many times as necessary
  1130.  
  1131.         |  data bytes   |        |
  1132.  
  1133.         :               :        |
  1134.  
  1135.         +---------------+     ---+
  1136.  
  1137.         . . .       . . .
  1138.  
  1139.         +---------------+
  1140.  
  1141.         |0 0 0 0 0 0 0 0|       zero byte count (terminates data stream)
  1142.  
  1143.         +---------------+
  1144.  
  1145.         The conversion of the image from a series  of  pixel  values  to  a
  1146.  
  1147.    transmitted or stored character stream involves several steps.  In brief
  1148.  
  1149.    these steps are:
  1150.  
  1151.    1.  Establish the Code Size -  Define  the  number  of  bits  needed  to
  1152.  
  1153.        represent the actual data.
  1154.  
  1155.    2.  Compress the Data - Compress the series of image pixels to a  series
  1156.  
  1157.        of compression codes.
  1158.  
  1159.    3.  Build a Series of Bytes - Take the  set  of  compression  codes  and
  1160.  
  1161.        convert to a string of 8-bit bytes.
  1162.  
  1163.    4.  Package the Bytes - Package sets of bytes into blocks  preceeded  by
  1164.  
  1165.        character counts and output.
  1166.  
  1167. ESTABLISH CODE SIZE
  1168.  
  1169.         The first byte of the GIF Raster Data stream is a value  indicating
  1170.  
  1171.    the minimum number of bits required to represent the set of actual pixel
  1172.  
  1173.    values.  Normally this will be the same as the  number  of  color  bits.
  1174.  
  1175.    Because  of  some  algorithmic constraints however, black & white images
  1176.  
  1177.    which have one color bit must be indicated as having a code size  of  2.
  1178.  
  1179.    This  code size value also implies that the compression codes must start
  1180.  
  1181.    out one bit longer.
  1182.  
  1183. COMPRESSION
  1184.  
  1185.         The LZW algorithm converts a series of data values into a series of
  1186.  
  1187.    codes  which may be raw values or a code designating a series of values.
  1188.  
  1189.    Using text characters as an analogy,  the  output  code  consists  of  a
  1190.  
  1191.    character or a code representing a string of characters.
  1192.  
  1193. Graphics Interchange Format (GIF)                                   Page 13
  1194.  
  1195. Appendix C - Image Packaging & Compression
  1196.  
  1197.         The LZW algorithm used in  GIF  matches  algorithmically  with  the
  1198.  
  1199.    standard LZW algorithm with the following differences:
  1200.  
  1201.    1.  A   special   Clear   code   is    defined    which    resets    all
  1202.  
  1203.        compression/decompression parameters and tables to a start-up state.
  1204.  
  1205.        The value of this code is 2**<code size>.  For example if  the  code
  1206.  
  1207.        size  indicated  was 4 (image was 4 bits/pixel) the Clear code value
  1208.  
  1209.        would be 16 (10000 binary).  The Clear code can appear at any  point
  1210.  
  1211.        in the image data stream and therefore requires the LZW algorithm to
  1212.  
  1213.        process succeeding codes as if  a  new  data  stream  was  starting.
  1214.  
  1215.        Encoders  should output a Clear code as the first code of each image
  1216.  
  1217.        data stream.
  1218.  
  1219.    2.  An End of Information code is defined that explicitly indicates  the
  1220.  
  1221.        end  of  the image data stream.  LZW processing terminates when this
  1222.  
  1223.        code is encountered.  It must be the last code output by the encoder
  1224.  
  1225.        for an image.  The value of this code is <Clear code>+1.
  1226.  
  1227.    3.  The first available compression code value is <Clear code>+2.
  1228.  
  1229.    4.  The output codes are of variable length, starting  at  <code size>+1
  1230.  
  1231.        bits  per code, up to 12 bits per code.  This defines a maximum code
  1232.  
  1233.        value of 4095 (hex FFF).  Whenever the LZW code value  would  exceed
  1234.  
  1235.        the  current  code length, the code length is increased by one.  The
  1236.  
  1237.        packing/unpacking of these codes must then be altered to reflect the
  1238.  
  1239.        new code length.
  1240.  
  1241. BUILD 8-BIT BYTES
  1242.  
  1243.         Because the LZW compression  used  for  GIF  creates  a  series  of
  1244.  
  1245.    variable  length  codes, of between 3 and 12 bits each, these codes must
  1246.  
  1247.    be reformed into a series of 8-bit bytes that  will  be  the  characters
  1248.  
  1249.    actually stored or transmitted.  This provides additional compression of
  1250.  
  1251.    the image.  The codes are formed into a stream of bits as if  they  were
  1252.  
  1253.    packed  right to left and then picked off 8 bits at a time to be output.
  1254.  
  1255.    Assuming a character array of 8 bits per character and using 5 bit codes
  1256.  
  1257.    to be packed, an example layout would be similar to:
  1258.  
  1259.          byte n       byte 5   byte 4   byte 3   byte 2   byte 1
  1260.  
  1261.         +-.....-----+--------+--------+--------+--------+--------+
  1262.  
  1263.         | and so on |hhhhhggg|ggfffffe|eeeedddd|dcccccbb|bbbaaaaa|
  1264.  
  1265.         +-.....-----+--------+--------+--------+--------+--------+
  1266.  
  1267.         Note that the physical  packing  arrangement  will  change  as  the
  1268.  
  1269.    number  of  bits per compression code change but the concept remains the
  1270.  
  1271.    same.
  1272.  
  1273. PACKAGE THE BYTES
  1274.  
  1275.         Once the bytes have been created, they are grouped into blocks  for
  1276.  
  1277.    output by preceeding each block of 0 to 255 bytes with a character count
  1278.  
  1279.    byte.  A block with a zero byte count terminates the Raster Data  stream
  1280.  
  1281.    for  a  given  image.  These blocks are what are actually output for the
  1282.  
  1283. Graphics Interchange Format (GIF)                                   Page 14
  1284.  
  1285. Appendix C - Image Packaging & Compression
  1286.  
  1287.    GIF image.  This block format has the side effect of allowing a decoding
  1288.  
  1289.    program  the  ability to read past the actual image data if necessary by
  1290.  
  1291.    reading block counts and then skipping over the data.
  1292.  
  1293. Graphics Interchange Format (GIF)                                   Page 15
  1294.  
  1295. Appendix D - Multiple Image Processing
  1296.  
  1297.         Since a  GIF  data  stream  can  contain  multiple  images,  it  is
  1298.  
  1299.    necessary  to  describe  processing and display of such a file.  Because
  1300.  
  1301.    the image descriptor allows  for  placement  of  the  image  within  the
  1302.  
  1303.    logical  screen,  it is possible to define a sequence of images that may
  1304.  
  1305.    each be a partial screen, but in total  fill  the  entire  screen.   The
  1306.  
  1307.    guidelines for handling the multiple image situation are:
  1308.  
  1309.    1.  There is no pause between images.  Each is processed immediately  as
  1310.  
  1311.        seen by the decoder.
  1312.  
  1313.    2.  Each image explicitly overwrites any image  already  on  the  screen
  1314.  
  1315.        inside  of  its window.  The only screen clears are at the beginning
  1316.  
  1317.        and end of the  GIF  image  process.   See  discussion  on  the  GIF
  1318.  
  1319.        terminator.
  1320.  
  1321.  
  1322.