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

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