home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Information / Specifications / Graphics / GIF / GIF89a < prev    next >
Encoding:
Text File  |  1994-05-07  |  81.1 KB  |  2,461 lines  |  [TEXT/R*ch]

  1.  encoder chooses to clear it.  The encoder during
  2.     this time sends out codes that are of the maximum Code Size.
  3.  
  4.     As we can see from the above, when the decoder's table is full, it must
  5.     not change the table until a clear code is received.  The Code Size is that
  6.     of the maximum Code Size.  Processing other than this is done normally.
  7.  
  8.     Because of a large base of decoders that do not handle the decompression in
  9.     this manner, we ask developers of GIF encoding software to NOT implement
  10.     this feature until at least January 1991 and later if they see that their
  11.     particular market is not ready for it.  This will give developers of GIF
  12.     decoding software time to implement this feature and to get it into the
  13.     hands of their clients before the decoders start "breaking" on the new
  14.     GIF's.  It is not required that encoders change their software to take
  15.     advantage of the deferred clear code, but it is for decoders.
  16.  
  17.     APPLICATION EXTENSION BLOCK - APPLICATION IDENTIFIER
  18.  
  19.     There will be a Courtesy Directory file located on CompuServe in the PICS
  20.     forum.  This directory will contain Application Identifiers for Application
  21.     Extension Blocks that have been used by developers of GIF applications.
  22.     This file is intended to help keep developers that wish to create
  23.     Application Extension Blocks from using the same Application Identifiers.
  24.     This is not an official directory; it is for voluntary participation only
  25.     and does not guarantee that someone will not use the same identifier.
  26.  
  27.     E-Mail can be sent to Larry Wood (forum manager of PICS) indicating the
  28.     request for inclusion in this file with an identifier.
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                         GRAPHICS INTERCHANGE FORMAT(sm)
  71.  
  72.                                   Version 89a
  73.  
  74.                             (c)1987,1988,1989,1990
  75.  
  76.                                    Copyright
  77.                             CompuServe Incorporated
  78.                                 Columbus, Ohio
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. CompuServe Incorporated                           Graphics Interchange Format
  110. Document Date : 31 July 1990                            Programming Reference
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.                                Table of Contents
  122.  
  123. Disclaimer.................................................................  1
  124.  
  125. Foreword...................................................................  1
  126.  
  127. Licensing..................................................................  1
  128.  
  129. About the Document.........................................................  2
  130.  
  131. General Description........................................................  2
  132.  
  133. Version Numbers............................................................  2
  134.  
  135. The Encoder................................................................  3
  136.  
  137. The Decoder................................................................  3
  138.  
  139. Compliance.................................................................  3
  140.  
  141. About Recommendations......................................................  4
  142.  
  143. About Color Tables.........................................................  4
  144.  
  145. Blocks, Extensions and Scope...............................................  4
  146.  
  147. Block Sizes................................................................  5
  148.  
  149. Using GIF as an embedded protocol..........................................  5
  150.  
  151. Data Sub-blocks............................................................  5
  152.  
  153. Block Terminator...........................................................  6
  154.  
  155. Header.....................................................................  7
  156.  
  157. Logical Screen Descriptor..................................................  8
  158.  
  159. Global Color Table......................................................... 10
  160.  
  161. Image Descriptor........................................................... 11
  162.  
  163. Local Color Table.......................................................... 13
  164.  
  165. Table Based Image Data..................................................... 14
  166.  
  167. Graphic Control Extension.................................................. 15
  168.  
  169. Comment Extension.......................................................... 17
  170.  
  171. Plain Text Extension....................................................... 18
  172.  
  173. Application Extension...................................................... 21
  174.  
  175. Trailer.................................................................... 23
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. Quick Reference Table...................................................... 24
  188.  
  189. GIF Grammar................................................................ 25
  190.  
  191. Glossary................................................................... 27
  192.  
  193. Conventions................................................................ 28
  194.  
  195. Interlaced Images.......................................................... 29
  196.  
  197. Variable-Length-Code LZW Compression....................................... 30
  198.  
  199. On-line Capabilities Dialogue.............................................. 33
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.                                                                          1
  251.  
  252.  
  253. 1. Disclaimer.
  254.  
  255. The information provided herein is subject to change without notice. In no
  256. event will CompuServe Incorporated be liable for damages, including any loss of
  257. revenue, loss of profits or other incidental or consequential damages arising
  258. out of the use or inability to use the information; CompuServe Incorporated
  259. makes no claim as to the suitability of the information.
  260.  
  261.  
  262. 2. Foreword.
  263.  
  264. This document defines the Graphics Interchange Format(sm). The specification
  265. given here defines version 89a, which is an extension of version 87a.
  266.  
  267. The Graphics Interchange Format(sm) as specified here should be considered
  268. complete; any deviation from it should be considered invalid, including but not
  269. limited to, the use of reserved or undefined fields within control or data
  270. blocks, the inclusion of extraneous data within or between blocks, the use of
  271. methods or algorithms not specifically listed as part of the format, etc. In
  272. general, any and all deviations, extensions or modifications not specified in
  273. this document should be considered to be in violation of the format and should
  274. be avoided.
  275.  
  276.  
  277. 3. Licensing.
  278.  
  279. The Graphics Interchange Format(c) is the copyright property of CompuServe
  280. Incorporated. Only CompuServe Incorporated is authorized to define, redefine,
  281. enhance, alter, modify or change in any way the definition of the format.
  282.  
  283. CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-free
  284. license for the use of the Graphics Interchange Format(sm) in computer
  285. software; computer software utilizing GIF(sm) must acknowledge ownership of the
  286. Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in
  287. User and Technical Documentation. Computer software utilizing GIF, which is
  288. distributed or may be distributed without User or Technical Documentation must
  289. display to the screen or printer a message acknowledging ownership of the
  290. Graphics Interchange Format and the Service Mark by CompuServe Incorporated; in
  291. this case, the acknowledgement may be displayed in an opening screen or leading
  292. banner, or a closing screen or trailing banner. A message such as the following
  293. may be used:
  294.  
  295.       "The Graphics Interchange Format(c) is the Copyright property of
  296.       CompuServe Incorporated. GIF(sm) is a Service Mark property of
  297.       CompuServe Incorporated."
  298.  
  299. For further information, please contact :
  300.  
  301.       CompuServe Incorporated
  302.       Graphics Technology Department
  303.       5000 Arlington Center Boulevard
  304.       Columbus, Ohio  43220
  305.       U. S. A.
  306.  
  307. CompuServe Incorporated maintains a mailing list with all those individuals and
  308. organizations who wish to receive copies of this document when it is corrected
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.                                                                          2
  317.  
  318.  
  319. or revised. This service is offered free of charge; please provide us with your
  320. mailing address.
  321.  
  322.  
  323. 4. About the Document.
  324.  
  325. This document describes in detail the definition of the Graphics Interchange
  326. Format.  This document is intended as a programming reference; it is
  327. recommended that the entire document be read carefully before programming,
  328. because of the interdependence of the various parts. There is an individual
  329. section for each of the Format blocks. Within each section, the sub-section
  330. labeled Required Version refers to the version number that an encoder will have
  331. to use if the corresponding block is used in the Data Stream. Within each
  332. section, a diagram describes the individual fields in the block; the diagrams
  333. are drawn vertically; top bytes in the diagram appear first in the Data Stream.
  334. Bits within a byte are drawn most significant on the left end.  Multi-byte
  335. numeric fields are ordered Least Significant Byte first. Numeric constants are
  336. represented as Hexadecimal numbers, preceded by "0x".  Bit fields within a byte
  337. are described in order from most significant bits to least significant bits.
  338.  
  339.  
  340. 5. General Description.
  341.  
  342. The Graphics Interchange Format(sm) defines a protocol intended for the on-line
  343. transmission and interchange of raster graphic data in a way that is
  344. independent of the hardware used in their creation or display.
  345.  
  346. The Graphics Interchange Format is defined in terms of blocks and sub-blocks
  347. which contain relevant parameters and data used in the reproduction of a
  348. graphic. A GIF Data Stream is a sequence of protocol blocks and sub-blocks
  349. representing a collection of graphics. In general, the graphics in a Data
  350. Stream are assumed to be related to some degree, and to share some control
  351. information; it is recommended that encoders attempt to group together related
  352. graphics in order to minimize hardware changes during processing and to
  353. minimize control information overhead. For the same reason, unrelated graphics
  354. or graphics which require resetting hardware parameters should be encoded
  355. separately to the extent possible.
  356.  
  357. A Data Stream may originate locally, as when read from a file, or it may
  358. originate remotely, as when transmitted over a data communications line. The
  359. Format is defined with the assumption that an error-free Transport Level
  360. Protocol is used for communications; the Format makes no provisions for
  361. error-detection and error-correction.
  362.  
  363. The GIF Data Stream must be interpreted in context, that is, the application
  364. program must rely on information external to the Data Stream to invoke the
  365. decoder process.
  366.  
  367.  
  368. 6. Version Numbers.
  369.  
  370. The version number in the Header of a Data Stream is intended to identify the
  371. minimum set of capabilities required of a decoder in order to fully process the
  372. Data Stream.  An encoder should use the earliest possible version number that
  373. includes all the blocks used in the Data Stream. Within each block section in
  374. this document, there is an entry labeled Required Version which specifies the
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.                                                                          3
  383.  
  384.  
  385. earliest version number that includes the corresponding block.  The encoder
  386. should make every attempt to use the earliest version number covering all the
  387. blocks in the Data Stream; the unnecessary use of later version numbers will
  388. hinder processing by some decoders.
  389.  
  390.  
  391. 7. The Encoder.
  392.  
  393. The Encoder is the program used to create a GIF Data Stream. From raster data
  394. and other information, the encoder produces the necessary control and data
  395. blocks needed for reproducing the original graphics.
  396.  
  397. The encoder has the following primary responsibilities.
  398.  
  399.             - Include in the Data Stream all the necessary information to
  400.             reproduce  the graphics.
  401.  
  402.             - Insure that a Data Stream is labeled with the earliest possible
  403.             Version Number that will cover the definition of all the blocks in
  404.             it; this is to ensure that the largest number of decoders can
  405.             process the Data Stream.
  406.  
  407.             - Ensure encoding of the graphics in such a way that the decoding
  408.             process is optimized. Avoid redundant information as much as
  409.             possible.
  410.  
  411.             - To the extent possible, avoid grouping graphics which might
  412.             require resetting hardware parameters during the decoding process.
  413.  
  414.             - Set to zero (off) each of the bits of each and every field
  415.             designated as reserved. Note that some fields in the Logical Screen
  416.             Descriptor and the Image Descriptor were reserved under Version
  417.             87a, but are used under version 89a.
  418.  
  419.  
  420. 8. The Decoder.
  421.  
  422. The Decoder is the program used to process a GIF Data Stream. It processes the
  423. Data Stream sequentially, parsing the various blocks and sub-blocks, using the
  424. control information to set hardware and process parameters and interpreting the
  425. data to render the graphics.
  426.  
  427. The decoder has the following primary responsibilities.
  428.  
  429.             - Process each graphic in the Data Stream in sequence, without
  430.             delays other than those specified in the control information.
  431.  
  432.             - Set its hardware parameters to fit, as closely as possible, the
  433.             control information contained in the Data Stream.
  434.  
  435.  
  436. 9. Compliance.
  437.  
  438. An encoder or a decoder is said to comply with a given version of the Graphics
  439. Interchange Format if and only if it fully conforms with and correctly
  440. implements the definition of the standard associated with that version.  An
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.                                                                          4
  449.  
  450.  
  451. encoder or a decoder may be compliant with a given version number and not
  452. compliant with some subsequent version.
  453.  
  454.  
  455. 10. About Recommendations.
  456.  
  457. Each block section in this document contains an entry labeled Recommendation;
  458. this section lists a set of recommendations intended to guide and organize the
  459. use of the particular blocks. Such recommendations are geared towards making
  460. the functions of encoders and decoders more efficient, as well as making
  461. optimal use of the communications bandwidth.  It is advised that these
  462. recommendations be followed.
  463.  
  464.  
  465. 11. About Color Tables.
  466.  
  467. The GIF format utilizes color tables to render raster-based graphics. A color
  468. table can have one of two different scopes: global or local. A Global Color
  469. Table is used by all those graphics in the Data Stream which do not have a
  470. Local Color Table associated with them. The scope of the Global Color Table is
  471. the entire Data Stream. A Local Color Table is always associated with the
  472. graphic that immediately follows it; the scope of a Local Color Table is
  473. limited to that single graphic. A Local Color Table supersedes a Global Color
  474. Table, that is, if a Data Stream contains a Global Color Table, and an image
  475. has a Local Color Table associated with it, the decoder must save the Global
  476. Color Table, use the Local Color Table to render the image, and then restore
  477. the Global Color Table. Both types of color tables are optional, making it
  478. possible for a Data Stream to contain numerous graphics without a color table
  479. at all. For this reason, it is recommended that the decoder save the last
  480. Global Color Table used until another Global Color Table is encountered. In
  481. this way, a Data Stream which does not contain either a Global Color Table or
  482. a Local Color Table may be processed using the last Global Color Table saved.
  483. If a Global Color Table from a previous Stream is used, that table becomes the
  484. Global Color Table of the present Stream. This is intended to reduce the
  485. overhead incurred by color tables. In particular, it is recommended that an
  486. encoder use only one Global Color Table if all the images in related Data
  487. Streams can be rendered with the same table.  If no color table is available at
  488. all, the decoder is free to use a system color table or a table of its own. In
  489. that case, the decoder may use a color table with as many colors as its
  490. hardware is able to support; it is recommended that such a table have black and
  491. white as its first two entries, so that monochrome images can be rendered
  492. adequately.
  493.  
  494. The Definition of the GIF Format allows for a Data Stream to contain only the
  495. Header, the Logical Screen Descriptor, a Global Color Table and the GIF
  496. Trailer. Such a Data Stream would be used to load a decoder with a Global Color
  497. Table, in preparation for subsequent Data Streams without a color table at all.
  498.  
  499.  
  500. 12. Blocks, Extensions and Scope.
  501.  
  502. Blocks can be classified into three groups : Control, Graphic-Rendering and
  503. Special Purpose.  Control blocks, such as the Header, the Logical Screen
  504. Descriptor, the Graphic Control Extension and the Trailer, contain information
  505. used to control the process of the Data Stream or information  used in setting
  506. hardware parameters.  Graphic-Rendering blocks such as the Image Descriptor and
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.                                                                          5
  515.  
  516.  
  517. the Plain Text Extension contain information and data used to render a graphic
  518. on the display device. Special Purpose blocks such as the Comment Extension and
  519. the Application Extension are neither used to control the process of the Data
  520. Stream nor do they contain information or data used to render a graphic on the
  521. display device. With the exception of the Logical Screen Descriptor and the
  522. Global Color Table, whose scope is the entire Data Stream, all other Control
  523. blocks have a limited scope, restricted to the Graphic-Rendering block that
  524. follows them.  Special Purpose blocks do not delimit the scope of any Control
  525. blocks; Special Purpose blocks are transparent to the decoding process.
  526. Graphic-Rendering blocks and extensions are used as scope delimiters for
  527. Control blocks and extensions. The labels used to identify labeled blocks fall
  528. into three ranges : 0x00-0x7F (0-127) are the Graphic Rendering blocks,
  529. excluding the Trailer (0x3B); 0x80-0xF9 (128-249) are the Control blocks;
  530. 0xFA-0xFF (250-255) are the Special Purpose blocks. These ranges are defined so
  531. that decoders can handle block scope by appropriately identifying block labels,
  532. even when the block itself cannot be processed.
  533.  
  534.  
  535. 13. Block Sizes.
  536.  
  537. The Block Size field in a block, counts the number of bytes remaining in the
  538. block, not counting the Block Size field itself, and not counting the Block
  539. Terminator, if one is to follow.  Blocks other than Data Blocks are intended to
  540. be of fixed length; the Block Size field is provided in order to facilitate
  541. skipping them, not to allow their size to change in the future.  Data blocks
  542. and sub-blocks are of variable length to accommodate the amount of data.
  543.  
  544.  
  545. 14. Using GIF as an embedded protocol.
  546.  
  547. As an embedded protocol, GIF may be part of larger application protocols,
  548. within which GIF is used to render graphics.  In such a case, the application
  549. protocol could define a block within which the GIF Data Stream would be
  550. contained. The application program would then invoke a GIF decoder upon
  551. encountering a block of type GIF.  This approach is recommended in favor of
  552. using Application Extensions, which become overhead for all other applications
  553. that do not process them. Because a GIF Data Stream must be processed in
  554. context, the application must rely on some means of identifying the GIF Data
  555. Stream outside of the Stream itself.
  556.  
  557.  
  558. 15. Data Sub-blocks.
  559.  
  560.       a. Description. Data Sub-blocks are units containing data. They do not
  561.       have a label, these blocks are processed in the context of control
  562.       blocks, wherever data blocks are specified in the format. The first byte
  563.       of the Data sub-block indicates the number of data bytes to follow. A
  564.       data sub-block may contain from 0 to 255 data bytes. The size of the
  565.       block does not account for the size byte itself, therefore, the empty
  566.       sub-block is one whose size field contains 0x00.
  567.  
  568.       b. Required Version.  87a.
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.                                                                          6
  581.  
  582.  
  583.       c. Syntax.
  584.  
  585.       7 6 5 4 3 2 1 0        Field Name                    Type
  586.      +---------------+
  587.   0  |               |       Block Size                    Byte
  588.      +---------------+
  589.   1  |               |
  590.      +-             -+
  591.   2  |               |
  592.      +-             -+
  593.   3  |               |
  594.      +-             -+
  595.      |               |       Data Values                   Byte
  596.      +-             -+
  597.  up  |               |
  598.      +-   . . . .   -+
  599.  to  |               |
  600.      +-             -+
  601.      |               |
  602.      +-             -+
  603. 255  |               |
  604.      +---------------+
  605.  
  606.             i) Block Size - Number of bytes in the Data Sub-block; the size
  607.             must be within 0 and 255 bytes, inclusive.
  608.  
  609.             ii) Data Values - Any 8-bit value. There must be exactly as many
  610.             Data Values as specified by the Block Size field.
  611.  
  612.       d. Extensions and Scope. This type of block always occurs as part of a
  613.       larger unit. It does not have a scope of itself.
  614.  
  615.       e. Recommendation. None.
  616.  
  617.  
  618. 16. Block Terminator.
  619.  
  620.       a. Description. This zero-length Data Sub-block is used to terminate a
  621.       sequence of Data Sub-blocks. It contains a single byte in the position of
  622.       the Block Size field and does not contain data.
  623.  
  624.       b. Required Version.  87a.
  625.  
  626.       c. Syntax.
  627.  
  628.       7 6 5 4 3 2 1 0        Field Name                    Type
  629.      +---------------+
  630.   0  |               |       Block Size                    Byte
  631.      +---------------+
  632.  
  633.             i) Block Size - Number of bytes in the Data Sub-block; this field
  634.             contains the fixed value 0x00.
  635.  
  636.             ii) Data Values - This block does not contain any data.
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.                                                                          7
  647.  
  648.  
  649.       d. Extensions and Scope. This block terminates the immediately preceding
  650.       sequence of Data Sub-blocks. This block cannot be modified by any
  651.       extension.
  652.  
  653.       e. Recommendation. None.
  654.  
  655.  
  656. 17. Header.
  657.  
  658.       a. Description. The Header identifies the GIF Data Stream in context. The
  659.       Signature field marks the beginning of the Data Stream, and the Version
  660.       field identifies the set of capabilities required of a decoder to fully
  661.       process the Data Stream.  This block is REQUIRED; exactly one Header must
  662.       be present per Data Stream.
  663.  
  664.       b. Required Version.  Not applicable. This block is not subject to a
  665.       version number. This block must appear at the beginning of every Data
  666.       Stream.
  667.  
  668.       c. Syntax.
  669.  
  670.  
  671.       7 6 5 4 3 2 1 0        Field Name                    Type
  672.      +---------------+
  673.    0 |               |       Signature                     3 Bytes
  674.      +-             -+
  675.    1 |               |
  676.      +-             -+
  677.    2 |               |
  678.      +---------------+
  679.    3 |               |       Version                       3 Bytes
  680.      +-             -+
  681.    4 |               |
  682.      +-             -+
  683.    5 |               |
  684.      +---------------+
  685.  
  686.             i) Signature - Identifies the GIF Data Stream. This field contains
  687.             the fixed value 'GIF'.
  688.  
  689.             ii) Version - Version number used to format the data stream.
  690.             Identifies the minimum set of capabilities necessary to a decoder
  691.             to fully process the contents of the Data Stream.
  692.  
  693.             Version Numbers as of 10 July 1990 :       "87a" - May 1987
  694.                                                        "89a" - July 1989
  695.  
  696.             Version numbers are ordered numerically increasing on the first two
  697.             digits starting with 87 (87,88,...,99,00,...,85,86) and
  698.             alphabetically increasing on the third character (a,...,z).
  699.  
  700.             iii) Extensions and Scope. The scope of this block is the entire
  701.             Data Stream. This block cannot be modified by any extension.
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.                                                                          8
  713.  
  714.  
  715.       d. Recommendations.
  716.  
  717.             i) Signature - This field identifies the beginning of the GIF Data
  718.             Stream; it is not intended to provide a unique signature for the
  719.             identification of the data. It is recommended that the GIF Data
  720.             Stream be identified externally by the application. (Refer to
  721.             Appendix G for on-line identification of the GIF Data Stream.)
  722.  
  723.             ii) Version - ENCODER : An encoder should use the earliest possible
  724.             version number that defines all the blocks used in the Data Stream.
  725.             When two or more Data Streams are combined, the latest of the
  726.             individual version numbers should be used for the resulting Data
  727.             Stream. DECODER : A decoder should attempt to process the data
  728.             stream to the best of its ability; if it encounters a version
  729.             number which it is not capable of processing fully, it should
  730.             nevertheless, attempt to process the data stream to the best of its
  731.             ability, perhaps after warning the user that the data may be
  732.             incomplete.
  733.  
  734.  
  735. 18. Logical Screen Descriptor.
  736.  
  737.       a. Description.  The Logical Screen Descriptor contains the parameters
  738.       necessary to define the area of the display device within which the
  739.       images will be rendered.  The coordinates in this block are given with
  740.       respect to the top-left corner of the virtual screen; they do not
  741.       necessarily refer to absolute coordinates on the display device.  This
  742.       implies that they could refer to window coordinates in a window-based
  743.       environment or printer coordinates when a printer is used.
  744.  
  745.       This block is REQUIRED; exactly one Logical Screen Descriptor must be
  746.       present per Data Stream.
  747.  
  748.       b. Required Version.  Not applicable. This block is not subject to a
  749.       version number. This block must appear immediately after the Header.
  750.  
  751.       c. Syntax.
  752.  
  753.       7 6 5 4 3 2 1 0        Field Name                    Type
  754.      +---------------+
  755.   0  |               |       Logical Screen Width          Unsigned
  756.      +-             -+
  757.   1  |               |
  758.      +---------------+
  759.   2  |               |       Logical Screen Height         Unsigned
  760.      +-             -+
  761.   3  |               |
  762.      +---------------+
  763.   4  | |     | |     |       <Packed Fields>               See below
  764.      +---------------+
  765.   5  |               |       Background Color Index        Byte
  766.      +---------------+
  767.   6  |               |       Pixel Aspect Ratio            Byte
  768.      +---------------+
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.                                                                          9
  779.  
  780.  
  781.      <Packed Fields>  =      Global Color Table Flag       1 Bit
  782.                              Color Resolution              3 Bits
  783.                              Sort Flag                     1 Bit
  784.                              Size of Global Color Table    3 Bits
  785.  
  786.             i) Logical Screen Width - Width, in pixels, of the Logical Screen
  787.             where the images will be rendered in the displaying device.
  788.  
  789.             ii) Logical Screen Height - Height, in pixels, of the Logical
  790.             Screen where the images will be rendered in the displaying device.
  791.  
  792.             iii) Global Color Table Flag - Flag indicating the presence of a
  793.             Global Color Table; if the flag is set, the Global Color Table will
  794.             immediately follow the Logical Screen Descriptor. This flag also
  795.             selects the interpretation of the Background Color Index; if the
  796.             flag is set, the value of the Background Color Index field should
  797.             be used as the table index of the background color. (This field is
  798.             the most significant bit of the byte.)
  799.  
  800.             Values :    0 -   No Global Color Table follows, the Background
  801.                               Color Index field is meaningless.
  802.                         1 -   A Global Color Table will immediately follow, the
  803.                               Background Color Index field is meaningful.
  804.  
  805.             iv) Color Resolution - Number of bits per primary color available
  806.             to the original image, minus 1. This value represents the size of
  807.             the entire palette from which the colors in the graphic were
  808.             selected, not the number of colors actually used in the graphic.
  809.             For example, if the value in this field is 3, then the palette of
  810.             the original image had 4 bits per primary color available to create
  811.             the image.  This value should be set to indicate the richness of
  812.             the original palette, even if not every color from the whole
  813.             palette is available on the source machine.
  814.  
  815.             v) Sort Flag - Indicates whether the Global Color Table is sorted.
  816.             If the flag is set, the Global Color Table is sorted, in order of
  817.             decreasing importance. Typically, the order would be decreasing
  818.             frequency, with most frequent color first. This assists a decoder,
  819.             with fewer available colors, in choosing the best subset of colors;
  820.             the decoder may use an initial segment of the table to render the
  821.             graphic.
  822.  
  823.             Values :    0 -   Not ordered.
  824.                         1 -   Ordered by decreasing importance, most
  825.                               important color first.
  826.  
  827.             vi) Size of Global Color Table - If the Global Color Table Flag is
  828.             set to 1, the value in this field is used to calculate the number
  829.             of bytes contained in the Global Color Table. To determine that
  830.             actual size of the color table, raise 2 to [the value of the field
  831.             + 1].  Even if there is no Global Color Table specified, set this
  832.             field according to the above formula so that decoders can choose
  833.             the best graphics mode to display the stream in.  (This field is
  834.             made up of the 3 least significant bits of the byte.)
  835.  
  836.             vii) Background Color Index - Index into the Global Color Table for
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.                                                                         10
  845.  
  846.  
  847.             the Background Color. The Background Color is the color used for
  848.             those pixels on the screen that are not covered by an image. If the
  849.             Global Color Table Flag is set to (zero), this field should be zero
  850.             and should be ignored.
  851.  
  852.             viii) Pixel Aspect Ratio - Factor used to compute an approximation
  853.             of the aspect ratio of the pixel in the original image.  If the
  854.             value of the field is not 0, this approximation of the aspect ratio
  855.             is computed based on the formula:
  856.  
  857.             Aspect Ratio = (Pixel Aspect Ratio + 15) / 64
  858.  
  859.             The Pixel Aspect Ratio is defined to be the quotient of the pixel's
  860.             width over its height.  The value range in this field allows
  861.             specification of the widest pixel of 4:1 to the tallest pixel of
  862.             1:4 in increments of 1/64th.
  863.  
  864.             Values :        0 -   No aspect ratio information is given.
  865.                        1..255 -   Value used in the computation.
  866.  
  867.       d. Extensions and Scope. The scope of this block is the entire Data
  868.       Stream. This block cannot be modified by any extension.
  869.  
  870.       e. Recommendations. None.
  871.  
  872.  
  873. 19. Global Color Table.
  874.  
  875.       a. Description. This block contains a color table, which is a sequence of
  876.       bytes representing red-green-blue color triplets. The Global Color Table
  877.       is used by images without a Local Color Table and by Plain Text
  878.       Extensions. Its presence is marked by the Global Color Table Flag being
  879.       set to 1 in the Logical Screen Descriptor; if present, it immediately
  880.       follows the Logical Screen Descriptor and contains a number of bytes
  881.       equal to
  882.                     3 x 2^(Size of Global Color Table+1).
  883.  
  884.       This block is OPTIONAL; at most one Global Color Table may be present
  885.       per Data Stream.
  886.  
  887.       b. Required Version.  87a
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.                                                                         11
  911.  
  912.  
  913.       c. Syntax.
  914.  
  915.       7 6 5 4 3 2 1 0        Field Name                    Type
  916.      +===============+
  917.   0  |               |       Red 0                         Byte
  918.      +-             -+
  919.   1  |               |       Green 0                       Byte
  920.      +-             -+
  921.   2  |               |       Blue 0                        Byte
  922.      +-             -+
  923.   3  |               |       Red 1                         Byte
  924.      +-             -+
  925.      |               |       Green 1                       Byte
  926.      +-             -+
  927.  up  |               |
  928.      +-   . . . .   -+       ...
  929.  to  |               |
  930.      +-             -+
  931.      |               |       Green 255                     Byte
  932.      +-             -+
  933. 767  |               |       Blue 255                      Byte
  934.      +===============+
  935.  
  936.  
  937.       d. Extensions and Scope. The scope of this block is the entire Data
  938.       Stream. This block cannot be modified by any extension.
  939.  
  940.       e. Recommendation. None.
  941.  
  942.  
  943. 20. Image Descriptor.
  944.  
  945.       a. Description. Each image in the Data Stream is composed of an Image
  946.       Descriptor, an optional Local Color Table, and the image data.  Each
  947.       image must fit within the boundaries of the Logical Screen, as defined
  948.       in the Logical Screen Descriptor.
  949.  
  950.       The Image Descriptor contains the parameters necessary to process a table
  951.       based image. The coordinates given in this block refer to coordinates
  952.       within the Logical Screen, and are given in pixels. This block is a
  953.       Graphic-Rendering Block, optionally preceded by one or more Control
  954.       blocks such as the Graphic Control Extension, and may be optionally
  955.       followed by a Local Color Table; the Image Descriptor is always followed
  956.       by the image data.
  957.  
  958.       This block is REQUIRED for an image.  Exactly one Image Descriptor must
  959.       be present per image in the Data Stream.  An unlimited number of images
  960.       may be present per Data Stream.
  961.  
  962.       b. Required Version.  87a.
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.                                                                         12
  977.  
  978.  
  979.       c. Syntax.
  980.  
  981.       7 6 5 4 3 2 1 0        Field Name                    Type
  982.      +---------------+
  983.   0  |               |       Image Separator               Byte
  984.      +---------------+
  985.   1  |               |       Image Left Position           Unsigned
  986.      +-             -+
  987.   2  |               |
  988.      +---------------+
  989.   3  |               |       Image Top Position            Unsigned
  990.      +-             -+
  991.   4  |               |
  992.      +---------------+
  993.   5  |               |       Image Width                   Unsigned
  994.      +-             -+
  995.   6  |               |
  996.      +---------------+
  997.   7  |               |       Image Height                  Unsigned
  998.      +-             -+
  999.   8  |               |
  1000.      +---------------+
  1001.   9  | | | |   |     |       <Packed Fields>               See below
  1002.      +---------------+
  1003.  
  1004.      <Packed Fields>  =      Local Color Table Flag        1 Bit
  1005.                              Interlace Flag                1 Bit
  1006.                              Sort Flag                     1 Bit
  1007.                              Reserved                      2 Bits
  1008.                              Size of Local Color Table     3 Bits
  1009.  
  1010.            i) Image Separator - Identifies the beginning of an Image
  1011.            Descriptor. This field contains the fixed value 0x2C.
  1012.  
  1013.            ii) Image Left Position - Column number, in pixels, of the left edge
  1014.            of the image, with respect to the left edge of the Logical Screen.
  1015.            Leftmost column of the Logical Screen is 0.
  1016.  
  1017.            iii) Image Top Position - Row number, in pixels, of the top edge of
  1018.            the image with respect to the top edge of the Logical Screen. Top
  1019.            row of the Logical Screen is 0.
  1020.  
  1021.            iv) Image Width - Width of the image in pixels.
  1022.  
  1023.            v) Image Height - Height of the image in pixels.
  1024.  
  1025.            vi) Local Color Table Flag - Indicates the presence of a Local Color
  1026.            Table immediately following this Image Descriptor. (This field is
  1027.            the most significant bit of the byte.)
  1028.  
  1029.  
  1030.            Values :    0 -   Local Color Table is not present. Use
  1031.                              Global Color Table if available.
  1032.                        1 -   Local Color Table present, and to follow
  1033.                              immediately after this Image Descriptor.
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.                                                                         13
  1043.  
  1044.  
  1045.            vii) Interlace Flag - Indicates if the image is interlaced. An image
  1046.            is interlaced in a four-pass interlace pattern; see Appendix E for
  1047.            details.
  1048.  
  1049.            Values :    0 - Image is not interlaced.
  1050.                        1 - Image is interlaced.
  1051.  
  1052.             viii) Sort Flag - Indicates whether the Local Color Table is
  1053.             sorted.  If the flag is set, the Local Color Table is sorted, in
  1054.             order of decreasing importance. Typically, the order would be
  1055.             decreasing frequency, with most frequent color first. This assists
  1056.             a decoder, with fewer available colors, in choosing the best subset
  1057.             of colors; the decoder may use an initial segment of the table to
  1058.             render the graphic.
  1059.  
  1060.             Values :    0 -   Not ordered.
  1061.                         1 -   Ordered by decreasing importance, most
  1062.                               important color first.
  1063.  
  1064.             ix) Size of Local Color Table - If the Local Color Table Flag is
  1065.             set to 1, the value in this field is used to calculate the number
  1066.             of bytes contained in the Local Color Table. To determine that
  1067.             actual size of the color table, raise 2 to the value of the field
  1068.             + 1. This value should be 0 if there is no Local Color Table
  1069.             specified. (This field is made up of the 3 least significant bits
  1070.             of the byte.)
  1071.  
  1072.      d. Extensions and Scope. The scope of this block is the Table-based Image
  1073.      Data Block that follows it. This block may be modified by the Graphic
  1074.      Control Extension.
  1075.  
  1076.      e. Recommendation. None.
  1077.  
  1078.  
  1079. 21. Local Color Table.
  1080.  
  1081.      a. Description. This block contains a color table, which is a sequence of
  1082.      bytes representing red-green-blue color triplets. The Local Color Table
  1083.      is used by the image that immediately follows. Its presence is marked by
  1084.      the Local Color Table Flag being set to 1 in the Image Descriptor; if
  1085.      present, the Local Color Table immediately follows the Image Descriptor
  1086.      and contains a number of bytes equal to
  1087.                           3x2^(Size of Local Color Table+1).
  1088.      If present, this color table temporarily becomes the active color table
  1089.      and the following image should be processed using it. This block is
  1090.      OPTIONAL; at most one Local Color Table may be present per Image
  1091.      Descriptor and its scope is the single image associated with the Image
  1092.      Descriptor that precedes it.
  1093.  
  1094.      b. Required Version.  87a.
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.                                                                         14
  1109.  
  1110.  
  1111.      c. Syntax.
  1112.  
  1113.       7 6 5 4 3 2 1 0        Field Name                    Type
  1114.      +===============+
  1115.   0  |               |       Red 0                         Byte
  1116.      +-             -+
  1117.   1  |               |       Green 0                       Byte
  1118.      +-             -+
  1119.   2  |               |       Blue 0                        Byte
  1120.      +-             -+
  1121.   3  |               |       Red 1                         Byte
  1122.      +-             -+
  1123.      |               |       Green 1                       Byte
  1124.      +-             -+
  1125.  up  |               |
  1126.      +-   . . . .   -+       ...
  1127.  to  |               |
  1128.      +-             -+
  1129.      |               |       Green 255                     Byte
  1130.      +-             -+
  1131. 767  |               |       Blue 255                      Byte
  1132.      +===============+
  1133.  
  1134.  
  1135.      d. Extensions and Scope. The scope of this block is the Table-based Image
  1136.      Data Block that immediately follows it. This block cannot be modified by
  1137.      any extension.
  1138.  
  1139.      e. Recommendations. None.
  1140.  
  1141.  
  1142. 22. Table Based Image Data.
  1143.  
  1144.      a. Description. The image data for a table based image consists of a
  1145.      sequence of sub-blocks, of size at most 255 bytes each, containing an
  1146.      index into the active color table, for each pixel in the image.  Pixel
  1147.      indices are in order of left to right and from top to bottom.  Each index
  1148.      must be within the range of the size of the active color table, starting
  1149.      at 0. The sequence of indices is encoded using the LZW Algorithm with
  1150.      variable-length code, as described in Appendix F
  1151.  
  1152.      b. Required Version.  87a.
  1153.  
  1154.      c. Syntax. The image data format is as follows:
  1155.  
  1156.       7 6 5 4 3 2 1 0        Field Name                    Type
  1157.      +---------------+
  1158.      |               |       LZW Minimum Code Size         Byte
  1159.      +---------------+
  1160.  
  1161.      +===============+
  1162.      |               |
  1163.      /               /       Image Data                    Data Sub-blocks
  1164.      |               |
  1165.      +===============+
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.                                                                         15
  1175.  
  1176.  
  1177.             i) LZW Minimum Code Size.  This byte determines the initial number
  1178.             of bits used for LZW codes in the image data, as described in
  1179.             Appendix F.
  1180.  
  1181.      d. Extensions and Scope. This block has no scope, it contains raster
  1182.      data. Extensions intended to modify a Table-based image must appear
  1183.      before the corresponding Image Descriptor.
  1184.  
  1185.      e. Recommendations. None.
  1186.  
  1187.  
  1188. 23. Graphic Control Extension.
  1189.  
  1190.       a. Description. The Graphic Control Extension contains parameters used
  1191.       when processing a graphic rendering block. The scope of this extension is
  1192.       the first graphic rendering block to follow. The extension contains only
  1193.       one data sub-block.
  1194.  
  1195.       This block is OPTIONAL; at most one Graphic Control Extension may precede
  1196.       a graphic rendering block. This is the only limit to the number of
  1197.       Graphic Control Extensions that may be contained in a Data Stream.
  1198.  
  1199.       b. Required Version.  89a.
  1200.  
  1201.       c. Syntax.
  1202.  
  1203.       7 6 5 4 3 2 1 0        Field Name                    Type
  1204.      +---------------+
  1205.   0  |               |       Extension Introducer          Byte
  1206.      +---------------+
  1207.   1  |               |       Graphic Control Label         Byte
  1208.      +---------------+
  1209.  
  1210.      +---------------+
  1211.   0  |               |       Block Size                    Byte
  1212.      +---------------+
  1213.   1  |     |     | | |       <Packed Fields>               See below
  1214.      +---------------+
  1215.   2  |               |       Delay Time                    Unsigned
  1216.      +-             -+
  1217.   3  |               |
  1218.      +---------------+
  1219.   4  |               |       Transparent Color Index       Byte
  1220.      +---------------+
  1221.  
  1222.      +---------------+
  1223.   0  |               |       Block Terminator              Byte
  1224.      +---------------+
  1225.  
  1226.  
  1227.       <Packed Fields>  =     Reserved                      3 Bits
  1228.                              Disposal Method               3 Bits
  1229.                              User Input Flag               1 Bit
  1230.                              Transparent Color Flag        1 Bit
  1231.  
  1232.             i) Extension Introducer - Identifies the beginning of an extension
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.                                                                         16
  1241.  
  1242.  
  1243.             block. This field contains the fixed value 0x21.
  1244.  
  1245.             ii) Graphic Control Label - Identifies the current block as a
  1246.             Graphic Control Extension. This field contains the fixed value
  1247.             0xF9.
  1248.  
  1249.             iii) Block Size - Number of bytes in the block, after the Block
  1250.             Size field and up to but not including the Block Terminator.  This
  1251.             field contains the fixed value 4.
  1252.  
  1253.             iv) Disposal Method - Indicates the way in which the graphic is to
  1254.             be treated after being displayed.
  1255.  
  1256.             Values :    0 -   No disposal specified. The decoder is
  1257.                               not required to take any action.
  1258.                         1 -   Do not dispose. The graphic is to be left
  1259.                               in place.
  1260.                         2 -   Restore to background color. The area used by the
  1261.                               graphic must be restored to the background color.
  1262.                         3 -   Restore to previous. The decoder is required to
  1263.                               restore the area overwritten by the graphic with
  1264.                               what was there prior to rendering the graphic.
  1265.                      4-7 -    To be defined.
  1266.  
  1267.             v) User Input Flag - Indicates whether or not user input is
  1268.             expected before continuing. If the flag is set, processing will
  1269.             continue when user input is entered. The nature of the User input
  1270.             is determined by the application (Carriage Return, Mouse Button
  1271.             Click, etc.).
  1272.  
  1273.             Values :    0 -   User input is not expected.
  1274.                         1 -   User input is expected.
  1275.  
  1276.             When a Delay Time is used and the User Input Flag is set,
  1277.             processing will continue when user input is received or when the
  1278.             delay time expires, whichever occurs first.
  1279.  
  1280.             vi) Transparency Flag - Indicates whether a transparency index is
  1281.             given in the Transparent Index field. (This field is the least
  1282.             significant bit of the byte.)
  1283.  
  1284.             Values :    0 -   Transparent Index is not given.
  1285.                         1 -   Transparent Index is given.
  1286.  
  1287.             vii) Delay Time - If not 0, this field specifies the number of
  1288.             hundredths (1/100) of a second to wait before continuing with the
  1289.             processing of the Data Stream. The clock starts ticking immediately
  1290.             after the graphic is rendered. This field may be used in
  1291.             conjunction with the User Input Flag field.
  1292.  
  1293.             viii) Transparency Index - The Transparency Index is such that when
  1294.             encountered, the corresponding pixel of the display device is not
  1295.             modified and processing goes on to the next pixel. The index is
  1296.             present if and only if the Transparency Flag is set to 1.
  1297.  
  1298.             ix) Block Terminator - This zero-length data block marks the end of
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.                                                                         17
  1307.  
  1308.             the Graphic Control Extension.
  1309.  
  1310.       d. Extensions and Scope. The scope of this Extension is the graphic
  1311.       rendering block that follows it; it is possible for other extensions to
  1312.       be present between this block and its target. This block can modify the
  1313.       Image Descriptor Block and the Plain Text Extension.
  1314.  
  1315.       e. Recommendations.
  1316.  
  1317.             i) Disposal Method - The mode Restore To Previous is intended to be
  1318.             used in small sections of the graphic; the use of this mode imposes
  1319.             severe demands on the decoder to store the section of the graphic
  1320.             that needs to be saved. For this reason, this mode should be used
  1321.             sparingly.  This mode is not intended to save an entire graphic or
  1322.             large areas of a graphic; when this is the case, the encoder should
  1323.             make every attempt to make the sections of the graphic to be
  1324.             restored be separate graphics in the data stream. In the case where
  1325.             a decoder is not capable of saving an area of a graphic marked as
  1326.             Restore To Previous, it is recommended that a decoder restore to
  1327.             the background color.
  1328.  
  1329.             ii) User Input Flag - When the flag is set, indicating that user
  1330.             input is expected, the decoder may sound the bell (0x07) to alert
  1331.             the user that input is being expected.  In the absence of a
  1332.             specified Delay Time, the decoder should wait for user input
  1333.             indefinitely.  It is recommended that the encoder not set the User
  1334.             Input Flag without a Delay Time specified.
  1335.  
  1336.  
  1337. 24. Comment Extension.
  1338.  
  1339.       a. Description. The Comment Extension contains textual information which
  1340.       is not part of the actual graphics in the GIF Data Stream. It is suitable
  1341.       for including comments about the graphics, credits, descriptions or any
  1342.       other type of non-control and non-graphic data.  The Comment Extension
  1343.       may be ignored by the decoder, or it may be saved for later processing;
  1344.       under no circumstances should a Comment Extension disrupt or interfere
  1345.       with the processing of the Data Stream.
  1346.  
  1347.       This block is OPTIONAL; any number of them may appear in the Data Stream.
  1348.  
  1349.       b. Required Version.  89a.
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.                                                                         18
  1373.  
  1374.  
  1375.       c. Syntax.
  1376.  
  1377.       7 6 5 4 3 2 1 0        Field Name                    Type
  1378.      +---------------+
  1379.   0  |               |       Extension Introducer          Byte
  1380.      +---------------+
  1381.   1  |               |       Comment Label                 Byte
  1382.      +---------------+
  1383.  
  1384.      +===============+
  1385.      |               |
  1386.   N  |               |       Comment Data                  Data Sub-blocks
  1387.      |               |
  1388.      +===============+
  1389.  
  1390.      +---------------+
  1391.   0  |               |       Block Terminator              Byte
  1392.      +---------------+
  1393.  
  1394.             i) Extension Introducer - Identifies the beginning of an extension
  1395.             block. This field contains the fixed value 0x21.
  1396.  
  1397.             ii) Comment Label - Identifies the block as a Comment Extension.
  1398.             This field contains the fixed value 0xFE.
  1399.  
  1400.             iii) Comment Data - Sequence of sub-blocks, each of size at most
  1401.             255 bytes and at least 1 byte, with the size in a byte preceding
  1402.             the data.  The end of the sequence is marked by the Block
  1403.             Terminator.
  1404.  
  1405.             iv) Block Terminator - This zero-length data block marks the end of
  1406.             the Comment Extension.
  1407.  
  1408.       d. Extensions and Scope. This block does not have scope. This block
  1409.       cannot be modified by any extension.
  1410.  
  1411.       e. Recommendations.
  1412.  
  1413.             i) Data - This block is intended for humans.  It should contain
  1414.             text using the 7-bit ASCII character set. This block should
  1415.             not be used to store control information for custom processing.
  1416.  
  1417.             ii) Position - This block may appear at any point in the Data
  1418.             Stream at which a block can begin; however, it is recommended that
  1419.             Comment Extensions do not interfere with Control or Data blocks;
  1420.             they should be located at the beginning or at the end of the Data
  1421.             Stream to the extent possible.
  1422.  
  1423.  
  1424. 25. Plain Text Extension.
  1425.  
  1426.       a. Description. The Plain Text Extension contains textual data and the
  1427.       parameters necessary to render that data as a graphic, in a simple form.
  1428.       The textual data will be encoded with the 7-bit printable ASCII
  1429.       characters.  Text data are rendered using a grid of character cells
  1430.  
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438.                                                                         19
  1439.  
  1440.  
  1441.       defined by the parameters in the block fields. Each character is rendered
  1442.       in an individual cell. The textual data in this block is to be rendered
  1443.       as mono-spaced characters, one character per cell, with a best fitting
  1444.       font and size. For further information, see the section on
  1445.       Recommendations below. The data characters are taken sequentially from
  1446.       the data portion of the block and rendered within a cell, starting with
  1447.       the upper left cell in the grid and proceeding from left to right and
  1448.       from top to bottom. Text data is rendered until the end of data is
  1449.       reached or the character grid is filled.  The Character Grid contains an
  1450.       integral number of cells; in the case that the cell dimensions do not
  1451.       allow for an integral number, fractional cells must be discarded; an
  1452.       encoder must be careful to specify the grid dimensions accurately so that
  1453.       this does not happen. This block requires a Global Color Table to be
  1454.       available; the colors used by this block reference the Global Color Table
  1455.       in the Stream if there is one, or the Global Color Table from a previous
  1456.       Stream, if one was saved. This block is a graphic rendering block,
  1457.       therefore it may be modified by a Graphic Control Extension.  This block
  1458.       is OPTIONAL; any number of them may appear in the Data Stream.
  1459.  
  1460.       b. Required Version.  89a.
  1461.  
  1462.  
  1463.  
  1464.  
  1465.  
  1466.  
  1467.  
  1468.  
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.                                                                         20
  1505.  
  1506.  
  1507.       c. Syntax.
  1508.  
  1509.       7 6 5 4 3 2 1 0        Field Name                    Type
  1510.      +---------------+
  1511.   0  |               |       Extension Introducer          Byte
  1512.      +---------------+
  1513.   1  |               |       Plain Text Label              Byte
  1514.      +---------------+
  1515.  
  1516.      +---------------+
  1517.   0  |               |       Block Size                    Byte
  1518.      +---------------+
  1519.   1  |               |       Text Grid Left Position       Unsigned
  1520.      +-             -+
  1521.   2  |               |
  1522.      +---------------+
  1523.   3  |               |       Text Grid Top Position        Unsigned
  1524.      +-             -+
  1525.   4  |               |
  1526.      +---------------+
  1527.   5  |               |       Text Grid Width               Unsigned
  1528.      +-             -+
  1529.   6  |               |
  1530.      +---------------+
  1531.   7  |               |       Text Grid Height              Unsigned
  1532.      +-             -+
  1533.   8  |               |
  1534.      +---------------+
  1535.   9  |               |       Character Cell Width          Byte
  1536.      +---------------+
  1537.  10  |               |       Character Cell Height         Byte
  1538.      +---------------+
  1539.  11  |               |       Text Foreground Color Index   Byte
  1540.      +---------------+
  1541.  12  |               |       Text Background Color Index   Byte
  1542.      +---------------+
  1543.  
  1544.      +===============+
  1545.      |               |
  1546.   N  |               |       Plain Text Data               Data Sub-blocks
  1547.      |               |
  1548.      +===============+
  1549.  
  1550.      +---------------+
  1551.   0  |               |       Block Terminator              Byte
  1552.      +---------------+
  1553.  
  1554.             i) Extension Introducer - Identifies the beginning of an extension
  1555.             block. This field contains the fixed value 0x21.
  1556.  
  1557.             ii) Plain Text Label - Identifies the current block as a Plain Text
  1558.             Extension. This field contains the fixed value 0x01.
  1559.  
  1560.             iii) Block Size - Number of bytes in the extension, after the Block
  1561.             Size field and up to but not including the beginning of the data
  1562.             portion. This field contains the fixed value 12.
  1563.  
  1564.  
  1565.  
  1566.  
  1567.  
  1568.  
  1569.  
  1570.                                                                         21
  1571.  
  1572.  
  1573.             iv) Text Grid Left Position - Column number, in pixels, of the left
  1574.             edge of the text grid, with respect to the left edge of the Logical
  1575.             Screen.
  1576.  
  1577.             v) Text Grid Top Position - Row number, in pixels, of the top edge
  1578.             of the text grid, with respect to the top edge of the Logical
  1579.             Screen.
  1580.  
  1581.             vi) Image Grid Width - Width of the text grid in pixels.
  1582.  
  1583.             vii) Image Grid Height - Height of the text grid in pixels.
  1584.  
  1585.             viii) Character Cell Width - Width, in pixels, of each cell in the
  1586.             grid.
  1587.  
  1588.             ix) Character Cell Height - Height, in pixels, of each cell in the
  1589.             grid.
  1590.  
  1591.             x) Text Foreground Color Index - Index into the Global Color Table
  1592.             to be used to render the text foreground.
  1593.  
  1594.             xi) Text Background Color Index - Index into the Global Color Table
  1595.             to be used to render the text background.
  1596.  
  1597.             xii) Plain Text Data - Sequence of sub-blocks, each of size at most
  1598.             255 bytes and at least 1 byte, with the size in a byte preceding
  1599.             the data.  The end of the sequence is marked by the Block
  1600.             Terminator.
  1601.  
  1602.             xiii) Block Terminator - This zero-length data block marks the end
  1603.             of the Plain Text Data Blocks.
  1604.  
  1605.       d. Extensions and Scope. The scope of this block is the Plain Text Data
  1606.       Block contained in it. This block may be modified by the Graphic Control
  1607.       Extension.
  1608.  
  1609.       e. Recommendations. The data in the Plain Text Extension is assumed to be
  1610.       preformatted. The selection of font and size is left to the discretion of
  1611.       the decoder.  If characters less than 0x20 or greater than 0xf7 are
  1612.       encountered, it is recommended that the decoder display a Space character
  1613.       (0x20). The encoder should use grid and cell dimensions such that an
  1614.       integral number of cells fit in the grid both horizontally as well as
  1615.       vertically.  For broadest compatibility, character cell dimensions should
  1616.       be around 8x8 or 8x16 (width x height); consider an image for unusual
  1617.       sized text.
  1618.  
  1619.  
  1620. 26. Application Extension.
  1621.  
  1622.       a. Description. The Application Extension contains application-specific
  1623.       information; it conforms with the extension block syntax, as described
  1624.       below, and its block label is 0xFF.
  1625.  
  1626.       b. Required Version.  89a.
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.                                                                         22
  1637.  
  1638.  
  1639.       c. Syntax.
  1640.  
  1641.       7 6 5 4 3 2 1 0        Field Name                    Type
  1642.      +---------------+
  1643.   0  |               |       Extension Introducer          Byte
  1644.      +---------------+
  1645.   1  |               |       Extension Label               Byte
  1646.      +---------------+
  1647.  
  1648.      +---------------+
  1649.   0  |               |       Block Size                    Byte
  1650.      +---------------+
  1651.   1  |               |
  1652.      +-             -+
  1653.   2  |               |
  1654.      +-             -+
  1655.   3  |               |       Application Identifier        8 Bytes
  1656.      +-             -+
  1657.   4  |               |
  1658.      +-             -+
  1659.   5  |               |
  1660.      +-             -+
  1661.   6  |               |
  1662.      +-             -+
  1663.   7  |               |
  1664.      +-             -+
  1665.   8  |               |
  1666.      +---------------+
  1667.   9  |               |
  1668.      +-             -+
  1669.  10  |               |       Appl. Authentication Code     3 Bytes
  1670.      +-             -+
  1671.  11  |               |
  1672.      +---------------+
  1673.  
  1674.      +===============+
  1675.      |               |
  1676.      |               |       Application Data              Data Sub-blocks
  1677.      |               |
  1678.      |               |
  1679.      +===============+
  1680.  
  1681.      +---------------+
  1682.   0  |               |       Block Terminator              Byte
  1683.      +---------------+
  1684.  
  1685.             i) Extension Introducer - Defines this block as an extension. This
  1686.             field contains the fixed value 0x21.
  1687.  
  1688.             ii) Application Extension Label - Identifies the block as an
  1689.             Application Extension. This field contains the fixed value 0xFF.
  1690.  
  1691.             iii) Block Size - Number of bytes in this extension block,
  1692.             following the Block Size field, up to but not including the
  1693.             beginning of the Application Data. This field contains the fixed
  1694.             value 11.
  1695.  
  1696.  
  1697.  
  1698.  
  1699.  
  1700.  
  1701.  
  1702.                                                                         23
  1703.  
  1704.  
  1705.             iv) Application Identifier - Sequence of eight printable ASCII
  1706.             characters used to identify the application owning the Application
  1707.             Extension.
  1708.  
  1709.             v) Application Authentication Code - Sequence of three bytes used
  1710.             to authenticate the Application Identifier. An Application program
  1711.             may use an algorithm to compute a binary code that uniquely
  1712.             identifies it as the application owning the Application Extension.
  1713.  
  1714.  
  1715.       d. Extensions and Scope. This block does not have scope. This block
  1716.       cannot be modified by any extension.
  1717.  
  1718.       e. Recommendation. None.
  1719.  
  1720.  
  1721. 27. Trailer.
  1722.  
  1723.       a. Description. This block is a single-field block indicating the end of
  1724.       the GIF Data Stream.  It contains the fixed value 0x3B.
  1725.  
  1726.       b. Required Version.  87a.
  1727.  
  1728.       c. Syntax.
  1729.  
  1730.       7 6 5 4 3 2 1 0        Field Name                    Type
  1731.      +---------------+
  1732.   0  |               |       GIF Trailer                   Byte
  1733.      +---------------+
  1734.  
  1735.       d. Extensions and Scope. This block does not have scope, it terminates
  1736.       the GIF Data Stream. This block may not be modified by any extension.
  1737.  
  1738.       e. Recommendations. None.
  1739.  
  1740.  
  1741.  
  1742.  
  1743.  
  1744.  
  1745.  
  1746.  
  1747.  
  1748.  
  1749.  
  1750.  
  1751.  
  1752.  
  1753.  
  1754.  
  1755.  
  1756.  
  1757.  
  1758.  
  1759.  
  1760.  
  1761.  
  1762.  
  1763.  
  1764.  
  1765.  
  1766.  
  1767.  
  1768.                                                                         24
  1769.  
  1770.  
  1771. Appendix
  1772. A. Quick Reference Table.
  1773.  
  1774. Block Name                  Required   Label       Ext.   Vers.
  1775. Application Extension       Opt. (*)   0xFF (255)  yes    89a
  1776. Comment Extension           Opt. (*)   0xFE (254)  yes    89a
  1777. Global Color Table          Opt. (1)   none        no     87a
  1778. Graphic Control Extension   Opt. (*)   0xF9 (249)  yes    89a
  1779. Header                      Req. (1)   none        no     N/A
  1780. Image Descriptor            Opt. (*)   0x2C (044)  no     87a (89a)
  1781. Local Color Table           Opt. (*)   none        no     87a
  1782. Logical Screen Descriptor   Req. (1)   none        no     87a (89a)
  1783. Plain Text Extension        Opt. (*)   0x01 (001)  yes    89a
  1784. Trailer                     Req. (1)   0x3B (059)  no     87a
  1785.  
  1786. Unlabeled Blocks
  1787. Header                      Req. (1)   none        no     N/A
  1788. Logical Screen Descriptor   Req. (1)   none        no     87a (89a)
  1789. Global Color Table          Opt. (1)   none        no     87a
  1790. Local Color Table           Opt. (*)   none        no     87a
  1791.  
  1792. Graphic-Rendering Blocks
  1793. Plain Text Extension        Opt. (*)   0x01 (001)  yes    89a
  1794. Image Descriptor            Opt. (*)   0x2C (044)  no     87a (89a)
  1795.  
  1796. Control Blocks
  1797. Graphic Control Extension   Opt. (*)   0xF9 (249)  yes    89a
  1798.  
  1799. Special Purpose Blocks
  1800. Trailer                     Req. (1)   0x3B (059)  no     87a
  1801. Comment Extension           Opt. (*)   0xFE (254)  yes    89a
  1802. Application Extension       Opt. (*)   0xFF (255)  yes    89a
  1803.  
  1804. legend:           (1)   if present, at most one occurrence
  1805.                   (*)   zero or more occurrences
  1806.                   (+)   one or more occurrences
  1807.  
  1808. Notes : The Header is not subject to Version Numbers.
  1809. (89a) The Logical Screen Descriptor and the Image Descriptor retained their
  1810. syntax from version 87a to version 89a, but some fields reserved under version
  1811. 87a are used under version 89a.
  1812.  
  1813.  
  1814.  
  1815.  
  1816.  
  1817.  
  1818.  
  1819.  
  1820.  
  1821.  
  1822.  
  1823.  
  1824.  
  1825.  
  1826.  
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832.  
  1833.  
  1834.                                                                         25
  1835.  
  1836.  
  1837. Appendix
  1838. B. GIF Grammar.
  1839.  
  1840. A Grammar is a form of notation to represent the sequence in which certain
  1841. objects form larger objects.  A grammar is also used to represent the number of
  1842. objects that can occur at a given position.  The grammar given here represents
  1843. the sequence of blocks that form the GIF Data Stream. A grammar is given by
  1844. listing its rules.  Each rule consists of the left-hand side, followed by some
  1845. form of equals sign, followed by the right-hand side.  In a rule, the
  1846. right-hand side describes how the left-hand side is defined. The right-hand
  1847. side consists of a sequence of entities, with the possible presence of special
  1848. symbols. The following legend defines the symbols used in this grammar for GIF.
  1849.  
  1850. Legend:           <>    grammar word
  1851.                   ::=   defines symbol
  1852.                   *     zero or more occurrences
  1853.                   +     one or more occurrences
  1854.                   |     alternate element
  1855.                   []    optional element
  1856.  
  1857. Example:
  1858.  
  1859. <GIF Data Stream> ::= Header <Logical Screen> <Data>* Trailer
  1860.  
  1861. This rule defines the entity <GIF Data Stream> as follows. It must begin with a
  1862. Header. The Header is followed by an entity called Logical Screen, which is
  1863. defined below by another rule. The Logical Screen is followed by the entity
  1864. Data, which is also defined below by another rule. Finally, the entity Data is
  1865. followed by the Trailer.  Since there is no rule defining the Header or the
  1866. Trailer, this means that these blocks are defined in the document.  The entity
  1867. Data has a special symbol (*) following it which means that, at this position,
  1868. the entity Data may be repeated any number of times, including 0 times. For
  1869. further reading on this subject, refer to a standard text on Programming
  1870. Languages.
  1871.  
  1872.  
  1873. The Grammar.
  1874.  
  1875. <GIF Data Stream> ::=     Header <Logical Screen> <Data>* Trailer
  1876.  
  1877. <Logical Screen> ::=      Logical Screen Descriptor [Global Color Table]
  1878.  
  1879. <Data> ::=                <Graphic Block>  |
  1880.                           <Special-Purpose Block>
  1881.  
  1882. <Graphic Block> ::=       [Graphic Control Extension] <Graphic-Rendering Block>
  1883.  
  1884. <Graphic-Rendering Block> ::=  <Table-Based Image>  |
  1885.                                Plain Text Extension
  1886.  
  1887. <Table-Based Image> ::=   Image Descriptor [Local Color Table] Image Data
  1888.  
  1889. <Special-Purpose Block> ::=    Application Extension  |
  1890.                                Comment Extension
  1891.  
  1892.  
  1893.  
  1894.  
  1895.  
  1896.  
  1897.  
  1898.  
  1899.  
  1900.                                                                         26
  1901.  
  1902.  
  1903. NOTE : The grammar indicates that it is possible for a GIF Data Stream to
  1904. contain the Header, the Logical Screen Descriptor, a Global Color Table and the
  1905. GIF Trailer. This special case is used to load a GIF decoder with a Global
  1906. Color Table, in preparation for subsequent Data Streams without color tables at
  1907. all.
  1908.  
  1909.  
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.  
  1919.  
  1920.  
  1921.  
  1922.  
  1923.  
  1924.  
  1925.  
  1926.  
  1927.  
  1928.  
  1929.  
  1930.  
  1931.  
  1932.  
  1933.  
  1934.  
  1935.  
  1936.  
  1937.  
  1938.  
  1939.  
  1940.  
  1941.  
  1942.  
  1943.  
  1944.  
  1945.  
  1946.  
  1947.  
  1948.  
  1949.  
  1950.  
  1951.  
  1952.  
  1953.  
  1954.  
  1955.  
  1956.  
  1957.  
  1958.  
  1959.  
  1960.  
  1961.  
  1962.  
  1963.  
  1964.  
  1965.  
  1966.                                                                         27
  1967.  
  1968.  
  1969. Appendix
  1970. C. Glossary.
  1971.  
  1972. Active Color Table - Color table used to render the next graphic. If the next
  1973. graphic is an image which has a Local Color Table associated with it, the
  1974. active color table becomes the Local Color Table associated with that image.
  1975. If the next graphic is an image without a Local Color Table, or a Plain Text
  1976. Extension, the active color table is the Global Color Table associated with the
  1977. Data Stream, if there is one; if there is no Global Color Table in the Data
  1978. Stream, the active color table is a color table saved from a previous Data
  1979. Stream, or one supplied by the decoder.
  1980.  
  1981. Block - Collection of bytes forming a protocol unit. In general, the term
  1982. includes labeled and unlabeled blocks, as well as Extensions.
  1983.  
  1984. Data Stream - The GIF Data Stream is composed of blocks and sub-blocks
  1985. representing images and graphics, together with control information to render
  1986. them on a display device. All control and data blocks in the Data Stream must
  1987. follow the Header and must precede the Trailer.
  1988.  
  1989. Decoder - A program capable of processing a GIF Data Stream to render the
  1990. images and graphics contained in it.
  1991.  
  1992. Encoder - A program capable of capturing and formatting image and graphic
  1993. raster data, following the definitions of the Graphics Interchange Format.
  1994.  
  1995. Extension - A protocol block labeled by the Extension Introducer 0x21.
  1996.  
  1997. Extension Introducer - Label (0x21) defining an Extension.
  1998.  
  1999. Graphic - Data which can be rendered on the screen by virtue of some algorithm.
  2000. The term graphic is more general than the term image; in addition to images,
  2001. the term graphic also includes data such as text, which is rendered using
  2002. character bit-maps.
  2003.  
  2004. Image - Data representing a picture or a drawing; an image is represented by an
  2005. array of pixels called the raster of the image.
  2006.  
  2007. Raster - Array of pixel values representing an image.
  2008.  
  2009.  
  2010.  
  2011.  
  2012.  
  2013.  
  2014.  
  2015.  
  2016.  
  2017.  
  2018.  
  2019.  
  2020.  
  2021.  
  2022.  
  2023.  
  2024.  
  2025.  
  2026.  
  2027.  
  2028.  
  2029.  
  2030.  
  2031.  
  2032.                                                                         28
  2033.  
  2034.  
  2035. Appendix
  2036. D. Conventions.
  2037.  
  2038. Animation - The Graphics Interchange Format is not intended as a platform for
  2039. animation, even though it can be done in a limited way.
  2040.  
  2041. Byte Ordering - Unless otherwise stated, multi-byte numeric fields are ordered
  2042. with the Least Significant Byte first.
  2043.  
  2044. Color Indices - Color indices always refer to the active color table, either
  2045. the Global Color Table or the Local Color Table.
  2046.  
  2047. Color Order - Unless otherwise stated, all triple-component RGB color values
  2048. are specified in Red-Green-Blue order.
  2049.  
  2050. Color Tables - Both color tables, the Global and the Local, are optional; if
  2051. present, the Global Color Table is to be used with every image in the Data
  2052. Stream for which a Local Color Table is not given; if present, a Local Color
  2053. Table overrides the Global Color Table.  However, if neither color table is
  2054. present, the application program is free to use an arbitrary color table. If
  2055. the graphics in several Data Streams are related and all use the same color
  2056. table, an encoder could place the color table as the Global Color Table in the
  2057. first Data Stream and leave subsequent Data Streams without a Global Color
  2058. Table or any Local Color Tables; in this way, the overhead for the table is
  2059. eliminated.  It is recommended that the decoder save the previous Global Color
  2060. Table to be used with the Data Stream that follows, in case it does not contain
  2061. either a Global Color Table or any Local Color Tables. In general, this allows
  2062. the application program to use past color tables, significantly reducing
  2063. transmission overhead.
  2064.  
  2065. Extension Blocks - Extensions are defined using the Extension Introducer code
  2066. to mark the beginning of the block, followed by a block label, identifying the
  2067. type of extension.  Extension Codes are numbers in the range from 0x00 to 0xFF,
  2068. inclusive. Special purpose extensions are transparent to the decoder and may be
  2069. omitted when transmitting the Data Stream on-line. The GIF capabilities
  2070. dialogue makes the provision for the receiver to request the transmission of
  2071. all blocks; the default state in this regard is no transmission of Special
  2072. purpose blocks.
  2073.  
  2074. Reserved Fields - All Reserved Fields are expected to have each bit set to zero
  2075. (off).
  2076.  
  2077.  
  2078.  
  2079.  
  2080.  
  2081.  
  2082.  
  2083.  
  2084.  
  2085.  
  2086.  
  2087.  
  2088.  
  2089.  
  2090.  
  2091.  
  2092.  
  2093.  
  2094.  
  2095.  
  2096.  
  2097.  
  2098.                                                                         29
  2099.  
  2100.  
  2101. Appendix
  2102. E. Interlaced Images.
  2103.  
  2104. The rows of an Interlaced images are arranged in the following order:
  2105.  
  2106.       Group 1 : Every 8th. row, starting with row 0.              (Pass 1)
  2107.       Group 2 : Every 8th. row, starting with row 4.              (Pass 2)
  2108.       Group 3 : Every 4th. row, starting with row 2.              (Pass 3)
  2109.       Group 4 : Every 2nd. row, starting with row 1.              (Pass 4)
  2110.  
  2111. The Following example illustrates how the rows of an interlaced image are
  2112. ordered.
  2113.  
  2114.       Row Number                                        Interlace Pass
  2115.  
  2116.  0    -----------------------------------------       1
  2117.  1    -----------------------------------------                         4
  2118.  2    -----------------------------------------                   3
  2119.  3    -----------------------------------------                         4
  2120.  4    -----------------------------------------             2
  2121.  5    -----------------------------------------                         4
  2122.  6    -----------------------------------------                   3
  2123.  7    -----------------------------------------                         4
  2124.  8    -----------------------------------------       1
  2125.  9    -----------------------------------------                         4
  2126.  10   -----------------------------------------                   3
  2127.  11   -----------------------------------------                         4
  2128.  12   -----------------------------------------             2
  2129.  13   -----------------------------------------                         4
  2130.  14   -----------------------------------------                   3
  2131.  15   -----------------------------------------                         4
  2132.  16   -----------------------------------------       1
  2133.  17   -----------------------------------------                         4
  2134.  18   -----------------------------------------                   3
  2135.  19   -----------------------------------------                         4
  2136.  
  2137.  
  2138.  
  2139.  
  2140.  
  2141.  
  2142.  
  2143.  
  2144.  
  2145.  
  2146.  
  2147.  
  2148.  
  2149.  
  2150.  
  2151.  
  2152.  
  2153.  
  2154.  
  2155.  
  2156.  
  2157.  
  2158.  
  2159.  
  2160.  
  2161.  
  2162.  
  2163.  
  2164.                                                                         30
  2165.  
  2166.  
  2167. Appendix
  2168. F. Variable-Length-Code LZW Compression.
  2169.  
  2170. The Variable-Length-Code LZW Compression is a variation of the Lempel-Ziv
  2171. Compression algorithm in which variable-length codes are used to replace
  2172. patterns detected in the original data. The algorithm uses a code or
  2173. translation table constructed from the patterns encountered in the original
  2174. data; each new pattern is entered into the table and its index is used to
  2175. replace it in the compressed stream.
  2176.  
  2177. The compressor takes the data from the input stream and builds a code or
  2178. translation table with the patterns as it encounters them; each new pattern is
  2179. entered into the code table and its index is added to the output stream; when a
  2180. pattern is encountered which had been detected since the last code table
  2181. refresh, its index from the code table is put on the output stream, thus
  2182. achieving the data compression.  The expander takes input from the compressed
  2183. data stream and builds the code or translation table from it; as the compressed
  2184. data stream is processed, codes are used to index into the code table and the
  2185. corresponding data is put on the decompressed output stream, thus achieving
  2186. data decompression.  The details of the algorithm are explained below.  The
  2187. Variable-Length-Code aspect of the algorithm is based on an initial code size
  2188. (LZW-initial code size), which specifies the initial number of bits used for
  2189. the compression codes.  When the number of patterns detected by the compressor
  2190. in the input stream exceeds the number of patterns encodable with the current
  2191. number of bits, the number of bits per LZW code is increased by one.
  2192.  
  2193. The Raster Data stream that represents the actual output image can be
  2194. represented as:
  2195.  
  2196.          7 6 5 4 3 2 1 0
  2197.         +---------------+
  2198.         | LZW code size |
  2199.         +---------------+
  2200.  
  2201.         +---------------+ ----+
  2202.         |  block size   |     |
  2203.         +---------------+     |
  2204.         |               |     +-- Repeated as many
  2205.         |  data bytes   |     |   times as necessary.
  2206.         |               |     |
  2207.         +---------------+ ----+
  2208.  
  2209.         . . .       . . . ------- The code that terminates the LZW
  2210.                                   compressed data must appear before
  2211.                                   Block Terminator.
  2212.         +---------------+
  2213.         |0 0 0 0 0 0 0 0|  Block Terminator
  2214.         +---------------+
  2215.  
  2216. The conversion of the image from a series of pixel values to a transmitted or
  2217. stored character stream involves several steps. In brief these steps are:
  2218.  
  2219. 1. Establish the Code Size - Define the number of bits needed to represent the
  2220. actual data.
  2221.  
  2222. 2. Compress the Data - Compress the series of image pixels to a series of
  2223.  
  2224.  
  2225.  
  2226.  
  2227.  
  2228.  
  2229.  
  2230.                                                                         31
  2231.  
  2232.  
  2233. compression codes.
  2234.  
  2235. 3. Build a Series of Bytes - Take the set of compression codes and convert to a
  2236. string of 8-bit bytes.
  2237.  
  2238. 4. Package the Bytes - Package sets of bytes into blocks preceded by character
  2239. counts and output.
  2240.  
  2241. ESTABLISH CODE SIZE
  2242.  
  2243. The first byte of the Compressed Data stream is a value indicating the minimum
  2244. number of bits required to represent the set of actual pixel values. Normally
  2245. this will be the same as the number of color bits. Because of some algorithmic
  2246. constraints however, black & white images which have one color bit must be
  2247. indicated as having a code size of 2.
  2248. This code size value also implies that the compression codes must start out one
  2249. bit longer.
  2250.  
  2251. COMPRESSION
  2252.  
  2253. The LZW algorithm converts a series of data values into a series of codes which
  2254. may be raw values or a code designating a series of values. Using text
  2255. characters as an analogy, the output code consists of a character or a code
  2256. representing a string of characters.
  2257.  
  2258. The LZW algorithm used in GIF matches algorithmically with the standard LZW
  2259. algorithm with the following differences:
  2260.  
  2261. 1.  A special Clear code is defined which resets all compression/decompression
  2262. parameters and tables to a start-up state. The value of this code is 2**<code
  2263. size>. For example if the code size indicated was 4 (image was 4 bits/pixel)
  2264. the Clear code value would be 16 (10000 binary). The Clear code can appear at
  2265. any point in the image data stream and therefore requires the LZW algorithm to
  2266. process succeeding codes as if a new data stream was starting. Encoders should
  2267. output a Clear code as the first code of each image data stream.
  2268.  
  2269. 2. An End of Information code is defined that explicitly indicates the end of
  2270. the image data stream. LZW processing terminates when this code is encountered.
  2271. It must be the last code output by the encoder for an image. The value of this
  2272. code is <Clear code>+1.
  2273.  
  2274. 3. The first available compression code value is <Clear code>+2.
  2275.  
  2276. 4. The output codes are of variable length, starting at <code size>+1 bits per
  2277. code, up to 12 bits per code. This defines a maximum code value of 4095
  2278. (0xFFF). Whenever the LZW code value would exceed the current code length, the
  2279. code length is increased by one. The packing/unpacking of these codes must then
  2280. be altered to reflect the new code length.
  2281.  
  2282. BUILD 8-BIT BYTES
  2283.  
  2284. Because the LZW compression used for GIF creates a series of variable length
  2285. codes, of between 3 and 12 bits each, these codes must be reformed into a
  2286. series of 8-bit bytes that will be the characters actually stored or
  2287. transmitted. This provides additional compression of the image. The codes are
  2288. formed into a stream of bits as if they were packed right to left and then
  2289.  
  2290.  
  2291.  
  2292.  
  2293.  
  2294.  
  2295.  
  2296.                                                                         32
  2297.  
  2298.  
  2299. picked off 8 bits at a time to be output.
  2300.  
  2301. Assuming a character array of 8 bits per character and using 5 bit codes to be
  2302. packed, an example layout would be similar to:
  2303.  
  2304.  
  2305.      +---------------+
  2306.   0  |               |    bbbaaaaa
  2307.      +---------------+
  2308.   1  |               |    dcccccbb
  2309.      +---------------+
  2310.   2  |               |    eeeedddd
  2311.      +---------------+
  2312.   3  |               |    ggfffffe
  2313.      +---------------+
  2314.   4  |               |    hhhhhggg
  2315.      +---------------+
  2316.            . . .
  2317.      +---------------+
  2318.   N  |               |
  2319.      +---------------+
  2320.  
  2321.  
  2322. Note that the physical packing arrangement will change as the number of bits
  2323. per compression code change but the concept remains the same.
  2324.  
  2325. PACKAGE THE BYTES
  2326.  
  2327. Once the bytes have been created, they are grouped into blocks for output by
  2328. preceding each block of 0 to 255 bytes with a character count byte. A block
  2329. with a zero byte count terminates the Raster Data stream for a given image.
  2330. These blocks are what are actually output for the GIF image. This block format
  2331. has the side effect of allowing a decoding program the ability to read past the
  2332. actual image data if necessary by reading block counts and then skipping over
  2333. the data.
  2334.  
  2335.  
  2336.  
  2337. FURTHER READING
  2338.  
  2339. [1] Ziv, J. and Lempel, A. : "A Universal Algorithm for Sequential Data
  2340. Compression", IEEE Transactions on Information Theory, May 1977.
  2341. [2] Welch, T. : "A Technique for High-Performance Data Compression", Computer,
  2342. June 1984.
  2343. [3] Nelson, M.R. : "LZW Data Compression", Dr. Dobb's Journal, October 1989.
  2344.  
  2345.  
  2346.  
  2347.  
  2348.  
  2349.  
  2350.  
  2351.  
  2352.  
  2353.  
  2354.  
  2355.  
  2356.  
  2357.  
  2358.  
  2359.  
  2360.  
  2361.  
  2362.                                                                         33
  2363.  
  2364.  
  2365. Appendix
  2366. G. On-line Capabilities Dialogue.
  2367.  
  2368. NOTE : This section is currently (10 July 1990) under revision; the information
  2369. provided here should be used as general guidelines. Code written based on this
  2370. information should be designed in a flexible way to accommodate any changes
  2371. resulting from the revisions.
  2372.  
  2373. The following sequences are defined for use in mediating control between a GIF
  2374. sender and GIF receiver over an interactive communications line. These
  2375. sequences do not apply to applications that involve downloading of static GIF
  2376. files and are not considered part of a GIF file.
  2377.  
  2378. GIF CAPABILITIES ENQUIRY
  2379.  
  2380. The GIF Capabilities Enquiry sequence is issued from a host and requests an
  2381. interactive GIF decoder to return a response message that defines the graphics
  2382. parameters for the decoder. This involves returning information about available
  2383. screen sizes, number of bits/color supported and the amount of color detail
  2384. supported. The escape sequence for the GIF Capabilities Enquiry is defined as:
  2385.  
  2386. ESC[>0g           0x1B 0x5B 0x3E 0x30 0x67
  2387.  
  2388. GIF CAPABILITIES RESPONSE
  2389.  
  2390. The GIF Capabilities Response message is returned by an interactive GIF decoder
  2391. and defines the decoder's display capabilities for all graphics modes that are
  2392. supported by the software. Note that this can also include graphics printers as
  2393. well as a monitor screen. The general format of this message is:
  2394.  
  2395. #version;protocol{;dev, width, height, color-bits, color-res}...<CR>
  2396.  
  2397.  
  2398. '#'            GIF Capabilities Response identifier character.
  2399. version        GIF format version number;  initially '87a'.
  2400. protocol='0'   No end-to-end protocol supported by decoder Transfer as direct
  2401.                8-bit data stream.
  2402. protocol='1'   Can use CIS B+ error correction protocol to transfer GIF data
  2403.                interactively from the host directly to the display.
  2404. dev = '0'      Screen parameter set follows.
  2405. dev = '1'      Printer parameter set follows.
  2406. width          Maximum supported display width in pixels.
  2407. height         Maximum supported display height in pixels.
  2408. color-bits     Number of bits per pixel supported. The number of supported
  2409.                colors is therefore 2**color-bits.
  2410. color-res      Number of bits per color component supported in the hardware
  2411.                color palette. If color-res is '0' then no hardware palette
  2412.                table is available.
  2413.  
  2414. Note that all values in the GIF Capabilities Response are returned as ASCII
  2415. decimal numbers and the message is terminated by a Carriage Return character.
  2416.  
  2417. The following GIF Capabilities Response message describes three standard IBM PC
  2418. Enhanced Graphics Adapter configurations with no printer; the GIF data stream
  2419.  
  2420.  
  2421.  
  2422.  
  2423.  
  2424.  
  2425.  
  2426.  
  2427.  
  2428.                                                                         34
  2429.  
  2430.  
  2431. can be processed within an error correcting protocol:
  2432.  
  2433. #87a;1;0,320,200,4,0;0,640,200,2,2;0,640,350,4,2<CR>
  2434.  
  2435. ENTER GIF GRAPHICS MODE
  2436.  
  2437. Two sequences are currently defined to invoke an interactive GIF decoder into
  2438. action. The only difference between them is that different output media are
  2439. selected. These sequences are:
  2440.  
  2441. ESC[>1g     Display GIF image on screen
  2442.  
  2443.                   0x1B 0x5B 0x3E 0x31 0x67
  2444.  
  2445. ESC[>2g   Display image directly to an attached graphics printer. The image may
  2446. optionally be displayed on the screen as well.
  2447.  
  2448.                   0x1B 0x5B 0x3E 0x32 0x67
  2449.  
  2450. Note that the 'g' character terminating each sequence is in lowercase.
  2451.  
  2452. INTERACTIVE ENVIRONMENT
  2453.  
  2454. The assumed environment for the transmission of GIF image data from an
  2455. interactive application is a full 8-bit data stream from host to micro.  All
  2456. 256 character codes must be transferrable. The establishing of an 8-bit data
  2457. path for communications will normally be taken care of by the host application
  2458. programs. It is however up to the receiving communications programs supporting
  2459. GIF to be able to receive and pass on all 256 8-bit codes to the GIF decoder
  2460. software.
  2461.