home *** CD-ROM | disk | FTP | other *** search
/ Fractal Frenzy 1 / WalnutCreekFractalFrenzy-1.iso / pc / macutils / gif_utils / gif_format.txt < prev   
Text File  |  1992-08-21  |  32KB  |  856 lines

  1. 15-Mar-89 19:43:20-GMT,32427;000000000001
  2. Return-Path: <SXWRR%ALASKA.BITNET@forsythe.stanford.edu>
  3. Received: from Forsythe.Stanford.EDU by sumex-aim.stanford.edu (4.0/inc-1.0)
  4.     id AA14020; Wed, 15 Mar 89 11:43:20 PST
  5. Message-Id: <8903151943.AA14020@sumex-aim.stanford.edu>
  6. Received: by Forsythe.Stanford.EDU; Wed, 15 Mar 89 11:44:20 PST
  7. Date:     Wed, 15 Mar 89 10:37:59 -0900
  8. To: info-mac@sumex-aim.stanford.edu
  9. Reply-To: <SXWRR%ALASKA.BITNET@forsythe.stanford.edu>
  10. Sender: <SXWRR%ALASKA.BITNET@forsythe.stanford.edu>
  11. From: Reed Rector                      <SXWRR%ALASKA.BITNET@forsythe.stanford.edu>
  12. Subject:  GIF specifications
  13.  
  14.     A few weeks ago, I asked for someone to send me the Graphics Interchange
  15. Format (GIF) specifications. The replys were mixed.... half of the people had
  16. the specs, and the other half wanted a copy, so it seems that the Archive
  17. is a good place for it.
  18.  
  19.     I also recieved some 'C' source code to do the encoding and decoding,
  20. but it is not specifically for the Mac. If there is sufficient demand,
  21. I could also send the code to Info-Mac.
  22.  
  23.     For all the people that responded, thanks for your interest and help.
  24.  
  25.         -Reed
  26.  
  27.         SXWRR@ALASKA (BITNET)
  28.         SXWRR@acad3.fai.alaska.edu (Internet)
  29.  
  30. ------------------------------Cut Here-------------------------------------
  31.  
  32.  
  33.  
  34.  
  35.                                 G I F (tm)
  36.  
  37.                      Graphics Interchange Format (tm)
  38.  
  39.  
  40.  
  41.  
  42.                       A standard defining a mechanism
  43.                      for the storage and transmission
  44.                    of raster-based graphics information
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.                                June 15, 1987
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                      (c) CompuServe Incorporated, 1987
  69.                             All rights reserved
  70.  
  71.             While this document is copyrighted, the information
  72.           contained within is made available for use in computer
  73.           software without royalties, or licensing restrictions.
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.           GIF and 'Graphics Interchange Format' are trademarks of
  81.                          CompuServe, Incorporated.
  82.                            an H&R Block Company
  83.  
  84.                         5000 Arlington Centre Blvd.
  85.                            Columbus, Ohio 43220
  86.                               (614) 457-8600
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.                                                                      Page 2
  96.  
  97.  
  98.               Graphics Interchange Format (GIF) Specification
  99.  
  100.  
  101.                              Table of Contents
  102.  
  103.         INTRODUCTION . . . . . . . . . . . . . . . . . page 3
  104.  
  105.         GENERAL FILE FORMAT  . . . . . . . . . . . . . page 3
  106.  
  107.         GIF SIGNATURE  . . . . . . . . . . . . . . . . page 4
  108.  
  109.         SCREEN DESCRIPTOR  . . . . . . . . . . . . . . page 4
  110.  
  111.         GLOBAL COLOR MAP . . . . . . . . . . . . . . . page 5
  112.  
  113.         IMAGE DESCRIPTOR . . . . . . . . . . . . . . . page 6
  114.  
  115.         LOCAL COLOR MAP  . . . . . . . . . . . . . . . page 7
  116.  
  117.         RASTER DATA  . . . . . . . . . . . . . . . . . page 7
  118.  
  119.         GIF TERMINATOR . . . . . . . . . . . . . . . . page 8
  120.  
  121.         GIF EXTENSION BLOCKS . . . . . . . . . . . . . page 8
  122.  
  123.         APPENDIX A - GLOSSARY  . . . . . . . . . . . . page 9
  124.  
  125.         APPENDIX B - INTERACTIVE SEQUENCES . . . . . . page 10
  126.  
  127.         APPENDIX C - IMAGE PACKAGING & COMPRESSION . . page 12
  128.  
  129.         APPENDIX D - MULTIPLE IMAGE PROCESSING . . . . page 15
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. Graphics Interchange Format (GIF)                                    Page 3
  139. Specification
  140.  
  141.  
  142. INTRODUCTION
  143.  
  144.         'GIF' (tm) is CompuServe's standard for defining generalized  color
  145.    raster   images.    This   'Graphics  Interchange  Format'  (tm)  allows
  146.    high-quality, high-resolution graphics to be displayed on a  variety  of
  147.    graphics  hardware  and is intended as an exchange and display mechanism
  148.    for graphics images.  The image format described  in  this  document  is
  149.    designed  to  support  current  and  future image technology and will in
  150.    addition serve as a basis for future CompuServe graphics products.
  151.  
  152.         The main focus  of  this  document  is  to  provide  the  technical
  153.    information  necessary  for  a  programmer to implement GIF encoders and
  154.    decoders.  As such, some assumptions are made as to terminology relavent
  155.    to graphics and programming in general.
  156.  
  157.         The first section of this document describes the  GIF  data  format
  158.    and its components and applies to all GIF decoders, either as standalone
  159.    programs or as part of  a  communications  package.   Appendix  B  is  a
  160.    section  relavent to decoders that are part of a communications software
  161.    package and describes the protocol requirements for entering and exiting
  162.    GIF mode, and responding to host interrogations.  A glossary in Appendix
  163.    A defines some of the terminology used in  this  document.   Appendix  C
  164.    gives  a  detailed  explanation  of  how  the  graphics  image itself is
  165.    packaged as a series of data bytes.
  166.  
  167.  
  168.                 Graphics Interchange Format Data Definition
  169.  
  170.  
  171.  GENERAL FILE FORMAT
  172.  
  173.         +-----------------------+
  174.         | +-------------------+ |
  175.         | |   GIF Signature   | |
  176.         | +-------------------+ |
  177.         | +-------------------+ |
  178.         | | Screen Descriptor | |
  179.         | +-------------------+ |
  180.         | +-------------------+ |
  181.         | | Global Color Map  | |
  182.         | +-------------------+ |
  183.         . . .               . . .
  184.         | +-------------------+ |    ---+
  185.         | |  Image Descriptor | |       |
  186.         | +-------------------+ |       |
  187.         | +-------------------+ |       |
  188.         | |  Local Color Map  | |       |-   Repeated 1 to n times
  189.         | +-------------------+ |       |
  190.         | +-------------------+ |       |
  191.         | |    Raster Data    | |       |
  192.         | +-------------------+ |    ---+
  193.         . . .               . . .
  194.         |-    GIF Terminator   -|
  195.         +-----------------------+
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. Graphics Interchange Format (GIF)                                    Page 4
  205. Specification
  206.  
  207.  
  208.  GIF SIGNATURE
  209.  
  210.         The following GIF Signature identifies  the  data  following  as  a
  211.    valid GIF image stream.  It consists of the following six characters:
  212.  
  213.              G I F 8 7 a
  214.  
  215.         The last three characters '87a' may be viewed as a  version  number
  216.    for  this  particular  GIF  definition  and will be used in general as a
  217.    reference  in  documents  regarding  GIF  that   address   any   version
  218.    dependencies.
  219.  
  220.  SCREEN DESCRIPTOR
  221.  
  222.         The Screen Descriptor describes the overall parameters for all  GIF
  223.    images  following.  It defines the overall dimensions of the image space
  224.    or logical screen required, the existance of color mapping  information,
  225.    background  screen color, and color depth information.  This information
  226.    is stored in a series of 8-bit bytes as described below.
  227.  
  228.               bits
  229.          7 6 5 4 3 2 1 0  Byte #
  230.         +---------------+
  231.         |               |  1
  232.         +-Screen Width -+      Raster width in pixels (LSB first)
  233.         |               |  2
  234.         +---------------+
  235.         |               |  3
  236.         +-Screen Height-+      Raster height in pixels (LSB first)
  237.         |               |  4
  238.         +-+-----+-+-----+      M = 1, Global color map follows Descriptor
  239.         |M|  cr |0|pixel|  5   cr+1 = # bits of color resolution
  240.         +-+-----+-+-----+      pixel+1 = # bits/pixel in image
  241.         |   background  |  6   background=Color index of screen background
  242.         +---------------+          (color is defined from the Global color
  243.         |0 0 0 0 0 0 0 0|  7        map or default map if none specified)
  244.         +---------------+
  245.  
  246.  
  247.         The logical screen width and height can both  be  larger  than  the
  248.    physical  display.   How  images  larger  than  the physical display are
  249.    handled is implementation dependent and can take advantage  of  hardware
  250.    characteristics  (e.g.   Macintosh scrolling windows).  Otherwise images
  251.    can be clipped to the edges of the display.
  252.  
  253.         The value of 'pixel' also defines  the  maximum  number  of  colors
  254.    within  an  image.   The  range  of  values  for 'pixel' is 0 to 7 which
  255.    represents 1 to 8 bits.  This translates to a range of 2 (B & W) to  256
  256.    colors.   Bit  3 of word 5 is reserved for future definition and must be
  257.    zero.
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266. Graphics Interchange Format (GIF)                                    Page 5
  267. Specification
  268.  
  269.  
  270.  GLOBAL COLOR MAP
  271.  
  272.         The Global Color Map is optional but recommended for  images  where
  273.    accurate color rendition is desired.  The existence of this color map is
  274.    indicated in the 'M' field of byte 5 of the Screen Descriptor.  A  color
  275.    map  can  also  be associated with each image in a GIF file as described
  276.    later.  However this  global  map  will  normally  be  used  because  of
  277.    hardware  restrictions  in equipment available today.  In the individual
  278.    Image Descriptors the 'M' flag will normally be  zero.   If  the  Global
  279.    Color  Map  is  present,  it's definition immediately follows the Screen
  280.    Descriptor.   The  number  of  color  map  entries  following  a  Screen
  281.    Descriptor  is equal to 2**(# bits per pixel), where each entry consists
  282.    of three byte values representing the relative intensities of red, green
  283.    and blue respectively.  The structure of the Color Map block is:
  284.  
  285.               bits
  286.          7 6 5 4 3 2 1 0  Byte #
  287.         +---------------+
  288.         | red intensity |  1    Red value for color index 0
  289.         +---------------+
  290.         |green intensity|  2    Green value for color index 0
  291.         +---------------+
  292.         | blue intensity|  3    Blue value for color index 0
  293.         +---------------+
  294.         | red intensity |  4    Red value for color index 1
  295.         +---------------+
  296.         |green intensity|  5    Green value for color index 1
  297.         +---------------+
  298.         | blue intensity|  6    Blue value for color index 1
  299.         +---------------+
  300.         :               :       (Continues for remaining colors)
  301.  
  302.         Each image pixel value received will be displayed according to  its
  303.    closest match with an available color of the display based on this color
  304.    map.  The color components represent a fractional intensity  value  from
  305.    none  (0)  to  full (255).  White would be represented as (255,255,255),
  306.    black as (0,0,0) and medium yellow as (180,180,0).  For display, if  the
  307.    device  supports fewer than 8 bits per color component, the higher order
  308.    bits of each component are used.  In the creation of  a  GIF  color  map
  309.    entry  with  hardware  supporting  fewer  than 8 bits per component, the
  310.    component values for the hardware  should  be  converted  to  the  8-bit
  311.    format with the following calculation:
  312.  
  313.         <map_value> = <component_value>*255/(2**<nbits> -1)
  314.  
  315.         This assures accurate translation of colors for all  displays.   In
  316.    the  cases  of  creating  GIF images from hardware without color palette
  317.    capability, a fixed palette should be created  based  on  the  available
  318.    display  colors for that hardware.  If no Global Color Map is indicated,
  319.    a default color map is generated internally  which  maps  each  possible
  320.    incoming  color  index to the same hardware color index modulo <n> where
  321.    <n> is the number of available hardware colors.
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330. Graphics Interchange Format (GIF)                                    Page 6
  331. Specification
  332.  
  333.  
  334.  IMAGE DESCRIPTOR
  335.  
  336.         The Image Descriptor defines the actual placement  and  extents  of
  337.    the  following  image within the space defined in the Screen Descriptor.
  338.    Also defined are flags to indicate the presence of a local color  lookup
  339.    map, and to define the pixel display sequence.  Each Image Descriptor is
  340.    introduced by an image separator  character.   The  role  of  the  Image
  341.    Separator  is simply to provide a synchronization character to introduce
  342.    an Image Descriptor.  This is desirable if a GIF file happens to contain
  343.    more  than  one  image.   This  character  is defined as 0x2C hex or ','
  344.    (comma).  When this character is encountered between images,  the  Image
  345.    Descriptor will follow immediately.
  346.  
  347.         Any characters encountered between the end of a previous image  and
  348.    the image separator character are to be ignored.  This allows future GIF
  349.    enhancements to be present in newer image formats and yet ignored safely
  350.    by older software decoders.
  351.  
  352.  
  353.               bits
  354.          7 6 5 4 3 2 1 0  Byte #
  355.         +---------------+
  356.         |0 0 1 0 1 1 0 0|  1    ',' - Image separator character
  357.         +---------------+
  358.         |               |  2    Start of image in pixels from the
  359.         +-  Image Left -+       left side of the screen (LSB first)
  360.         |               |  3
  361.         +---------------+
  362.         |               |  4
  363.         +-  Image Top  -+       Start of image in pixels from the
  364.         |               |  5    top of the screen (LSB first)
  365.         +---------------+
  366.         |               |  6
  367.         +- Image Width -+       Width of the image in pixels (LSB first)
  368.         |               |  7
  369.         +---------------+
  370.         |               |  8
  371.         +- Image Height-+       Height of the image in pixels (LSB first)
  372.         |               |  9
  373.         +-+-+-+-+-+-----+       M=0 - Use global color map, ignore 'pixel'
  374.         |M|I|0|0|0|pixel| 10    M=1 - Local color map follows, use 'pixel'
  375.         +-+-+-+-+-+-----+       I=0 - Image formatted in Sequential order
  376.                                 I=1 - Image formatted in Interlaced order
  377.                                 pixel+1 - # bits per pixel for this image
  378.  
  379.         The specifications for the image position and size must be confined
  380.    to  the  dimensions defined by the Screen Descriptor.  On the other hand
  381.    it is not necessary that the image fill the entire screen defined.
  382.  
  383.  
  384.  LOCAL COLOR MAP
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393. Graphics Interchange Format (GIF)                                    Page 7
  394. Specification
  395.  
  396.  
  397.         A Local Color Map is optional and defined here for future use.   If
  398.    the  'M' bit of byte 10 of the Image Descriptor is set, then a color map
  399.    follows the Image Descriptor that applies only to the  following  image.
  400.    At the end of the image, the color map will revert to that defined after
  401.    the Screen Descriptor.  Note that the 'pixel' field of byte  10  of  the
  402.    Image  Descriptor  is used only if a Local Color Map is indicated.  This
  403.    defines the parameters not only for the image pixel size, but determines
  404.    the  number  of color map entries that follow.  The bits per pixel value
  405.    will also revert to the value specified in the  Screen  Descriptor  when
  406.    processing of the image is complete.
  407.  
  408.  RASTER DATA
  409.  
  410.         The format of the actual image is defined as the  series  of  pixel
  411.    color  index  values that make up the image.  The pixels are stored left
  412.    to right sequentially for an image row.  By default each  image  row  is
  413.    written  sequentially, top to bottom.  In the case that the Interlace or
  414.    'I' bit is set in byte 10 of the Image Descriptor then the row order  of
  415.    the  image  display  follows  a  four-pass process in which the image is
  416.    filled in by widely spaced rows.  The first pass writes every  8th  row,
  417.    starting  with  the top row of the image window.  The second pass writes
  418.    every 8th row starting at the fifth row from the top.   The  third  pass
  419.    writes every 4th row starting at the third row from the top.  The fourth
  420.    pass completes the image, writing  every  other  row,  starting  at  the
  421.    second row from the top.  A graphic description of this process follows:
  422.  
  423.  
  424.    Image
  425.    Row  Pass 1  Pass 2  Pass 3  Pass 4          Result
  426.    ---------------------------------------------------
  427.      0  **1a**                                  **1a**
  428.      1                          **4a**          **4a**
  429.      2                  **3a**                  **3a**
  430.      3                          **4b**          **4b**
  431.      4          **2a**                          **2a**
  432.      5                          **4c**          **4c**
  433.      6                  **3b**                  **3b**
  434.      7                          **4d**          **4d**
  435.      8  **1b**                                  **1b**
  436.      9                          **4e**          **4e**
  437.     10                  **3c**                  **3c**
  438.     11                          **4f**          **4f**
  439.     12          **2b**                          **2b**
  440.    . . .
  441.  
  442.  
  443.  
  444.         The image pixel values are processed as a series of  color  indices
  445.    which  map  into the existing color map.  The resulting color value from
  446.    the map is what is actually displayed.  This series  of  pixel  indices,
  447.    the  number  of  which  is equal to image-width*image-height pixels, are
  448.    passed to the GIF image data stream one value per pixel, compressed  and
  449.    packaged  according  to  a  version  of the LZW compression algorithm as
  450.    defined in Appendix C.
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459. Graphics Interchange Format (GIF)                                    Page 8
  460. Specification
  461.  
  462.  
  463.  GIF TERMINATOR
  464.  
  465.         In order to provide a synchronization for the termination of a  GIF
  466.    image  file,  a  GIF  decoder  will process the end of GIF mode when the
  467.    character 0x3B hex or ';' is found after an image  has  been  processed.
  468.    By  convention  the  decoding software will pause and wait for an action
  469.    indicating that the user is ready to continue.  This may be  a  carriage
  470.    return  entered  at  the  keyboard  or  a  mouse click.  For interactive
  471.    applications this user action must  be  passed  on  to  the  host  as  a
  472.    carriage  return  character  so  that the host application can continue.
  473.    The decoding software will then typically leave graphics mode and resume
  474.    any previous process.
  475.  
  476.  
  477.  GIF EXTENSION BLOCKS
  478.  
  479.         To provide for orderly extension of the GIF definition, a mechanism
  480.    for  defining  the  packaging  of extensions within a GIF data stream is
  481.    necessary.  Specific GIF extensions are to be defined and documented  by
  482.    CompuServe in order to provide a controlled enhancement path.
  483.  
  484.         GIF Extension Blocks are packaged in a manner similar to that  used
  485.    by the raster data though not compressed.  The basic structure is:
  486.  
  487.          7 6 5 4 3 2 1 0  Byte #
  488.         +---------------+
  489.         |0 0 1 0 0 0 0 1|  1       '!' - GIF Extension Block Introducer
  490.         +---------------+
  491.         | function code |  2       Extension function code (0 to 255)
  492.         +---------------+    ---+
  493.         |  byte count   |       |
  494.         +---------------+       |
  495.         :               :       +-- Repeated as many times as necessary
  496.         |func data bytes|       |
  497.         :               :       |
  498.         +---------------+    ---+
  499.         . . .       . . .
  500.         +---------------+
  501.         |0 0 0 0 0 0 0 0|       zero byte count (terminates block)
  502.         +---------------+
  503.  
  504.         A GIF Extension Block may immediately preceed any Image  Descriptor
  505.    or occur before the GIF Terminator.
  506.  
  507.         All GIF decoders must be able to recognize  the  existence  of  GIF
  508.    Extension  Blocks  and  read past them if unable to process the function
  509.    code.  This ensures that older decoders will be able to process extended
  510.    GIF   image   files   in  the  future,  though  without  the  additional
  511.    functionality.
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520. Graphics Interchange Format (GIF)                                    Page 9
  521. Appendix A - Glossary
  522.  
  523.  
  524.                                  GLOSSARY
  525.  
  526. Pixel - The smallest picture element of a  graphics  image.   This  usually
  527.    corresponds  to  a single dot on a graphics screen.  Image resolution is
  528.    typically given in units of  pixels.   For  example  a  fairly  standard
  529.    graphics  screen  format  is  one 320 pixels across and 200 pixels high.
  530.    Each pixel can  appear  as  one  of  several  colors  depending  on  the
  531.    capabilities of the graphics hardware.
  532.  
  533. Raster - A horizontal row of pixels representing one line of an  image.   A
  534.    typical method of working with images since most hardware is oriented to
  535.    work most efficiently in this manner.
  536.  
  537. LSB - Least Significant Byte.  Refers to a convention for two byte  numeric
  538.    values in which the less significant byte of the value preceeds the more
  539.    significant byte.  This convention is typical on many microcomputers.
  540.  
  541. Color Map - The list of definitions of each color  used  in  a  GIF  image.
  542.    These  desired  colors are converted to available colors through a table
  543.    which is derived by assigning an incoming color index (from  the  image)
  544.    to  an  output  color  index  (of  the  hardware).   While the color map
  545.    definitons are specified in a GIF image, the output  pixel  colors  will
  546.    vary  based  on  the  hardware used and its ability to match the defined
  547.    color.
  548.  
  549. Interlace - The method of displaying a GIF image in which  multiple  passes
  550.    are  made,  outputting  raster  lines  spaced  apart to provide a way of
  551.    visualizing the general content of an entire image  before  all  of  the
  552.    data has been processed.
  553.  
  554. B Protocol - A CompuServe-developed error-correcting file transfer protocol
  555.    available  in  the  public  domain  and implemented in CompuServe VIDTEX
  556.    products.  This error checking mechanism will be used  in  transfers  of
  557.    GIF images for interactive applications.
  558.  
  559. LZW - A sophisticated data compression algorithm  based  on  work  done  by
  560.    Lempel-Ziv  &  Welch  which  has  the feature of very efficient one-pass
  561.    encoding and decoding.  This allows the image  to  be  decompressed  and
  562.    displayed  at  the  same  time.   The  original  article from which this
  563.    technique was adapted is:
  564.  
  565.           Terry  A.   Welch,  "A  Technique  for  High   Performance   Data
  566.           Compression", IEEE Computer, vol 17 no 6 (June 1984)
  567.  
  568.         This basic algorithm is also used in the  public  domain  ARC  file
  569.    compression  utilities.   The  CompuServe  adaptation  of LZW for GIF is
  570.    described in Appendix C.
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579. Graphics Interchange Format (GIF)                                   Page 10
  580. Appendix B - Interactive Sequences
  581.  
  582.  
  583.            GIF Sequence Exchanges for an Interactive Environment
  584.  
  585.  
  586.         The following sequences are defined for use  in  mediating  control
  587.    between a GIF sender and GIF receiver over an interactive communications
  588.    line.  These  sequences  do  not  apply  to  applications  that  involve
  589.    downloading  of  static  GIF  files and are not considered part of a GIF
  590.    file.
  591.  
  592.  GIF CAPABILITIES ENQUIRY
  593.  
  594.         The GCE sequence is issued from a host and requests an  interactive
  595.    GIF  decoder  to  return  a  response  message that defines the graphics
  596.    parameters for the decoder.  This involves returning  information  about
  597.    available screen sizes, number of bits/color supported and the amount of
  598.    color detail supported.  The escape sequence for the GCE is defined as:
  599.  
  600.         ESC [ > 0 g     (g is lower case, spaces inserted for clarity)
  601.                          (0x1B 0x5B 0x3E 0x30 0x67)
  602.  
  603.  
  604.  GIF CAPABILITIES RESPONSE
  605.  
  606.         The GIF Capabilities Response message is returned by an interactive
  607.    GIF  decoder  and  defines  the  decoder's  display capabilities for all
  608.    graphics modes that are supported by the software.  Note that  this  can
  609.    also include graphics printers as well as a monitor screen.  The general
  610.    format of this message is:
  611.  
  612.  
  613.      #version;protocol{;dev, width, height, color-bits, color-res}... <CR>
  614.  
  615.    '#'          - GCR identifier character (Number Sign)
  616.    version      - GIF format version number;  initially '87a'
  617.    protocol='0' - No end-to-end protocol supported by decoder
  618.                   Transfer as direct 8-bit data stream.
  619.    protocol='1' - Can use an error correction protocol to transfer GIF data
  620.                interactively from the host directly to the display.
  621.  
  622.    dev = '0'    - Screen parameter set follows
  623.    dev = '1'    - Printer parameter set follows
  624.  
  625.    width        - Maximum supported display width in pixels
  626.    height       - Maximum supported display height in pixels
  627.    color-bits   - Number of  bits  per  pixel  supported.   The  number  of
  628.                supported colors is therefore 2**color-bits.
  629.    color-res    - Number of bits  per  color  component  supported  in  the
  630.                hardware  color  palette.   If  color-res  is  '0'  then  no
  631.                hardware palette table is available.
  632.  
  633.  
  634.         Note that all values in the  GCR  are  returned  as  ASCII  decimal
  635.    numbers and the message is terminated by a Carriage Return character.
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644. Graphics Interchange Format (GIF)                                   Page 11
  645. Appendix B - Interactive Sequences
  646.  
  647.  
  648.         The  following   GCR   message   describes   three   standard   EGA
  649.    configurations  with  no  printer;  the GIF data stream can be processed
  650.    within an error correcting protocol:
  651.  
  652.         #87a;1 ;0,320,200,4,0 ;0,640,200,2,2 ;0,640,350,4,2<CR>
  653.  
  654.  
  655.  ENTER GIF GRAPHICS MODE
  656.  
  657.         Two sequences are currently defined to invoke  an  interactive  GIF
  658.    decoder into action.  The only difference between them is that different
  659.    output media are selected.  These sequences are:
  660.  
  661.      ESC [ > 1 g   Display GIF image on screen
  662.                    (0x1B 0x5B 0x3E 0x31 0x67)
  663.  
  664.      ESC [ > 2 g   Display image directly to an attached graphics  printer.
  665.                    The  image  may optionally be displayed on the screen as
  666.                    well.
  667.                    (0x1B 0x5B 0x3E 0x32 0x67)
  668.  
  669.         Note that the 'g' character terminating each sequence is  in  lower
  670.    case.
  671.  
  672.  
  673.  INTERACTIVE ENVIRONMENT
  674.  
  675.         The assumed environment for the transmission of GIF image data from
  676.    an  interactive  application  is  a  full 8-bit data stream from host to
  677.    micro.  All 256 character codes must be transferrable.  The establishing
  678.    of  an 8-bit data path for communications will normally be taken care of
  679.    by the host application programs.  It is however  up  to  the  receiving
  680.    communications programs supporting GIF to be able to receive and pass on
  681.    all 256 8-bit codes to the GIF decoder software.
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690. Graphics Interchange Format (GIF)                                   Page 12
  691. Appendix C - Image Packaging & Compression
  692.  
  693.  
  694.         The Raster Data stream that represents the actual output image  can
  695.    be represented as:
  696.  
  697.          7 6 5 4 3 2 1 0
  698.         +---------------+
  699.         |   code size   |
  700.         +---------------+     ---+
  701.         |blok byte count|        |
  702.         +---------------+        |
  703.         :               :        +-- Repeated as many times as necessary
  704.         |  data bytes   |        |
  705.         :               :        |
  706.         +---------------+     ---+
  707.         . . .       . . .
  708.         +---------------+
  709.         |0 0 0 0 0 0 0 0|       zero byte count (terminates data stream)
  710.         +---------------+
  711.  
  712.         The conversion of the image from a series  of  pixel  values  to  a
  713.    transmitted or stored character stream involves several steps.  In brief
  714.    these steps are:
  715.  
  716.    1.  Establish the Code Size -  Define  the  number  of  bits  needed  to
  717.        represent the actual data.
  718.  
  719.    2.  Compress the Data - Compress the series of image pixels to a  series
  720.        of compression codes.
  721.  
  722.    3.  Build a Series of Bytes - Take the  set  of  compression  codes  and
  723.        convert to a string of 8-bit bytes.
  724.  
  725.    4.  Package the Bytes - Package sets of bytes into blocks  preceeded  by
  726.        character counts and output.
  727.  
  728.  
  729.  
  730. ESTABLISH CODE SIZE
  731.  
  732.         The first byte of the GIF Raster Data stream is a value  indicating
  733.    the minimum number of bits required to represent the set of actual pixel
  734.    values.  Normally this will be the same as the  number  of  color  bits.
  735.    Because  of  some  algorithmic constraints however, black & white images
  736.    which have one color bit must be indicated as having a code size  of  2.
  737.    This  code size value also implies that the compression codes must start
  738.    out one bit longer.
  739.  
  740.  
  741. COMPRESSION
  742.  
  743.         The LZW algorithm converts a series of data values into a series of
  744.    codes  which may be raw values or a code designating a series of values.
  745.    Using text characters as an analogy,  the  output  code  consists  of  a
  746.    character or a code representing a string of characters.
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755. Graphics Interchange Format (GIF)                                   Page 13
  756. Appendix C - Image Packaging & Compression
  757.  
  758.  
  759.         The LZW algorithm used in  GIF  matches  algorithmically  with  the
  760.    standard LZW algorithm with the following differences:
  761.  
  762.    1.  A   special   Clear   code   is    defined    which    resets    all
  763.        compression/decompression parameters and tables to a start-up state.
  764.        The value of this code is 2**<code size>.  For example if  the  code
  765.        size  indicated  was 4 (image was 4 bits/pixel) the Clear code value
  766.        would be 16 (10000 binary).  The Clear code can appear at any  point
  767.        in the image data stream and therefore requires the LZW algorithm to
  768.        process succeeding codes as if  a  new  data  stream  was  starting.
  769.        Encoders  should output a Clear code as the first code of each image
  770.        data stream.
  771.  
  772.    2.  An End of Information code is defined that explicitly indicates  the
  773.        end  of  the image data stream.  LZW processing terminates when this
  774.        code is encountered.  It must be the last code output by the encoder
  775.        for an image.  The value of this code is <Clear code>+1.
  776.  
  777.    3.  The first available compression code value is <Clear code>+2.
  778.  
  779.    4.  The output codes are of variable length, starting  at  <code size>+1
  780.        bits  per code, up to 12 bits per code.  This defines a maximum code
  781.        value of 4095 (hex FFF).  Whenever the LZW code value  would  exceed
  782.        the  current  code length, the code length is increased by one.  The
  783.        packing/unpacking of these codes must then be altered to reflect the
  784.        new code length.
  785.  
  786.  
  787. BUILD 8-BIT BYTES
  788.  
  789.         Because the LZW compression  used  for  GIF  creates  a  series  of
  790.    variable  length  codes, of between 3 and 12 bits each, these codes must
  791.    be reformed into a series of 8-bit bytes that  will  be  the  characters
  792.    actually stored or transmitted.  This provides additional compression of
  793.    the image.  The codes are formed into a stream of bits as if  they  were
  794.    packed  right to left and then picked off 8 bits at a time to be output.
  795.    Assuming a character array of 8 bits per character and using 5 bit codes
  796.    to be packed, an example layout would be similar to:
  797.  
  798.          byte n       byte 5   byte 4   byte 3   byte 2   byte 1
  799.         +-.....-----+--------+--------+--------+--------+--------+
  800.         | and so on |hhhhhggg|ggfffffe|eeeedddd|dcccccbb|bbbaaaaa|
  801.         +-.....-----+--------+--------+--------+--------+--------+
  802.  
  803.         Note that the physical  packing  arrangement  will  change  as  the
  804.    number  of  bits per compression code change but the concept remains the
  805.    same.
  806.  
  807. PACKAGE THE BYTES
  808.  
  809.         Once the bytes have been created, they are grouped into blocks  for
  810.    output by preceeding each block of 0 to 255 bytes with a character count
  811.    byte.  A block with a zero byte count terminates the Raster Data  stream
  812.    for  a  given  image.  These blocks are what are actually output for the
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821. Graphics Interchange Format (GIF)                                   Page 14
  822. Appendix C - Image Packaging & Compression
  823.  
  824.  
  825.    GIF image.  This block format has the side effect of allowing a decoding
  826.    program  the  ability to read past the actual image data if necessary by
  827.    reading block counts and then skipping over the data.
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836. Graphics Interchange Format (GIF)                                   Page 15
  837. Appendix D - Multiple Image Processing
  838.  
  839.  
  840.         Since a  GIF  data  stream  can  contain  multiple  images,  it  is
  841.    necessary  to  describe  processing and display of such a file.  Because
  842.    the image descriptor allows  for  placement  of  the  image  within  the
  843.    logical  screen,  it is possible to define a sequence of images that may
  844.    each be a partial screen, but in total  fill  the  entire  screen.   The
  845.    guidelines for handling the multiple image situation are:
  846.  
  847.    1.  There is no pause between images.  Each is processed immediately  as
  848.        seen by the decoder.
  849.  
  850.    2.  Each image explicitly overwrites any image  already  on  the  screen
  851.        inside  of  its window.  The only screen clears are at the beginning
  852.        and end of the  GIF  image  process.   See  discussion  on  the  GIF
  853.        terminator.
  854.  
  855.  
  856.