5 Cover Sheet for the GIF89a Specification
8 DEFERRED CLEAR CODE IN LZW COMPRESSION
10 There has been confusion about where clear codes can be found in the
11 data stream. As the specification says, they may appear at anytime. There
12 is not a requirement to send a clear code when the string table is full.
14 It is the encoder's decision as to when the table should be cleared. When
15 the table is full, the encoder can chose to use the table as is, making no
16 changes to it until the encoder chooses to clear it. The encoder during
17 this time sends out codes that are of the maximum Code Size.
19 As we can see from the above, when the decoder's table is full, it must
20 not change the table until a clear code is received. The Code Size is that
21 of the maximum Code Size. Processing other than this is done normally.
23 Because of a large base of decoders that do not handle the decompression in
24 this manner, we ask developers of GIF encoding software to NOT implement
25 this feature until at least January 1991 and later if they see that their
26 particular market is not ready for it. This will give developers of GIF
27 decoding software time to implement this feature and to get it into the
28 hands of their clients before the decoders start "breaking" on the new
29 GIF's. It is not required that encoders change their software to take
30 advantage of the deferred clear code, but it is for decoders.
32 APPLICATION EXTENSION BLOCK - APPLICATION IDENTIFIER
34 There will be a Courtesy Directory file located on CompuServe in the PICS
35 forum. This directory will contain Application Identifiers for Application
36 Extension Blocks that have been used by developers of GIF applications.
37 This file is intended to help keep developers that wish to create
38 Application Extension Blocks from using the same Application Identifiers.
39 This is not an official directory; it is for voluntary participation only
40 and does not guarantee that someone will not use the same identifier.
42 E-Mail can be sent to Larry Wood (forum manager of PICS) indicating the
43 request for inclusion in this file with an identifier.
85 GRAPHICS INTERCHANGE FORMAT(sm)
89 (c)1987,1988,1989,1990
92 CompuServe Incorporated
124 CompuServe Incorporated Graphics Interchange Format
125 Document Date : 31 July 1990 Programming Reference
138 Disclaimer................................................................. 1
140 Foreword................................................................... 1
142 Licensing.................................................................. 1
144 About the Document......................................................... 2
146 General Description........................................................ 2
148 Version Numbers............................................................ 2
150 The Encoder................................................................ 3
152 The Decoder................................................................ 3
154 Compliance................................................................. 3
156 About Recommendations...................................................... 4
158 About Color Tables......................................................... 4
160 Blocks, Extensions and Scope............................................... 4
162 Block Sizes................................................................ 5
164 Using GIF as an embedded protocol.......................................... 5
166 Data Sub-blocks............................................................ 5
168 Block Terminator........................................................... 6
170 Header..................................................................... 7
172 Logical Screen Descriptor.................................................. 8
174 Global Color Table......................................................... 10
176 Image Descriptor........................................................... 11
178 Local Color Table.......................................................... 13
180 Table Based Image Data..................................................... 14
182 Graphic Control Extension.................................................. 15
184 Comment Extension.......................................................... 17
186 Plain Text Extension....................................................... 18
188 Application Extension...................................................... 21
190 Trailer.................................................................... 23
202 Quick Reference Table...................................................... 24
204 GIF Grammar................................................................ 25
206 Glossary................................................................... 27
208 Conventions................................................................ 28
210 Interlaced Images.......................................................... 29
212 Variable-Length-Code LZW Compression....................................... 30
214 On-line Capabilities Dialogue.............................................. 33
270 The information provided herein is subject to change without notice. In no
271 event will CompuServe Incorporated be liable for damages, including any loss of
272 revenue, loss of profits or other incidental or consequential damages arising
273 out of the use or inability to use the information; CompuServe Incorporated
274 makes no claim as to the suitability of the information.
279 This document defines the Graphics Interchange Format(sm). The specification
280 given here defines version 89a, which is an extension of version 87a.
282 The Graphics Interchange Format(sm) as specified here should be considered
283 complete; any deviation from it should be considered invalid, including but not
284 limited to, the use of reserved or undefined fields within control or data
285 blocks, the inclusion of extraneous data within or between blocks, the use of
286 methods or algorithms not specifically listed as part of the format, etc. In
287 general, any and all deviations, extensions or modifications not specified in
288 this document should be considered to be in violation of the format and should
294 The Graphics Interchange Format(c) is the copyright property of CompuServe
295 Incorporated. Only CompuServe Incorporated is authorized to define, redefine,
296 enhance, alter, modify or change in any way the definition of the format.
298 CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-free
299 license for the use of the Graphics Interchange Format(sm) in computer
300 software; computer software utilizing GIF(sm) must acknowledge ownership of the
301 Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in
302 User and Technical Documentation. Computer software utilizing GIF, which is
303 distributed or may be distributed without User or Technical Documentation must
304 display to the screen or printer a message acknowledging ownership of the
305 Graphics Interchange Format and the Service Mark by CompuServe Incorporated; in
306 this case, the acknowledgement may be displayed in an opening screen or leading
307 banner, or a closing screen or trailing banner. A message such as the following
310 "The Graphics Interchange Format(c) is the Copyright property of
311 CompuServe Incorporated. GIF(sm) is a Service Mark property of
312 CompuServe Incorporated."
314 For further information, please contact :
316 CompuServe Incorporated
317 Graphics Technology Department
318 5000 Arlington Center Boulevard
322 CompuServe Incorporated maintains a mailing list with all those individuals and
323 organizations who wish to receive copies of this document when it is corrected
334 or revised. This service is offered free of charge; please provide us with your
338 4. About the Document.
340 This document describes in detail the definition of the Graphics Interchange
341 Format. This document is intended as a programming reference; it is
342 recommended that the entire document be read carefully before programming,
343 because of the interdependence of the various parts. There is an individual
344 section for each of the Format blocks. Within each section, the sub-section
345 labeled Required Version refers to the version number that an encoder will have
346 to use if the corresponding block is used in the Data Stream. Within each
347 section, a diagram describes the individual fields in the block; the diagrams
348 are drawn vertically; top bytes in the diagram appear first in the Data Stream.
349 Bits within a byte are drawn most significant on the left end. Multi-byte
350 numeric fields are ordered Least Significant Byte first. Numeric constants are
351 represented as Hexadecimal numbers, preceded by "0x". Bit fields within a byte
352 are described in order from most significant bits to least significant bits.
355 5. General Description.
357 The Graphics Interchange Format(sm) defines a protocol intended for the on-line
358 transmission and interchange of raster graphic data in a way that is
359 independent of the hardware used in their creation or display.
361 The Graphics Interchange Format is defined in terms of blocks and sub-blocks
362 which contain relevant parameters and data used in the reproduction of a
363 graphic. A GIF Data Stream is a sequence of protocol blocks and sub-blocks
364 representing a collection of graphics. In general, the graphics in a Data
365 Stream are assumed to be related to some degree, and to share some control
366 information; it is recommended that encoders attempt to group together related
367 graphics in order to minimize hardware changes during processing and to
368 minimize control information overhead. For the same reason, unrelated graphics
369 or graphics which require resetting hardware parameters should be encoded
370 separately to the extent possible.
372 A Data Stream may originate locally, as when read from a file, or it may
373 originate remotely, as when transmitted over a data communications line. The
374 Format is defined with the assumption that an error-free Transport Level
375 Protocol is used for communications; the Format makes no provisions for
376 error-detection and error-correction.
378 The GIF Data Stream must be interpreted in context, that is, the application
379 program must rely on information external to the Data Stream to invoke the
385 The version number in the Header of a Data Stream is intended to identify the
386 minimum set of capabilities required of a decoder in order to fully process the
387 Data Stream. An encoder should use the earliest possible version number that
388 includes all the blocks used in the Data Stream. Within each block section in
389 this document, there is an entry labeled Required Version which specifies the
400 earliest version number that includes the corresponding block. The encoder
401 should make every attempt to use the earliest version number covering all the
402 blocks in the Data Stream; the unnecessary use of later version numbers will
403 hinder processing by some decoders.
408 The Encoder is the program used to create a GIF Data Stream. From raster data
409 and other information, the encoder produces the necessary control and data
410 blocks needed for reproducing the original graphics.
412 The encoder has the following primary responsibilities.
414 - Include in the Data Stream all the necessary information to
415 reproduce the graphics.
417 - Insure that a Data Stream is labeled with the earliest possible
418 Version Number that will cover the definition of all the blocks in
419 it; this is to ensure that the largest number of decoders can
420 process the Data Stream.
422 - Ensure encoding of the graphics in such a way that the decoding
423 process is optimized. Avoid redundant information as much as
426 - To the extent possible, avoid grouping graphics which might
427 require resetting hardware parameters during the decoding process.
429 - Set to zero (off) each of the bits of each and every field
430 designated as reserved. Note that some fields in the Logical Screen
431 Descriptor and the Image Descriptor were reserved under Version
432 87a, but are used under version 89a.
437 The Decoder is the program used to process a GIF Data Stream. It processes the
438 Data Stream sequentially, parsing the various blocks and sub-blocks, using the
439 control information to set hardware and process parameters and interpreting the
440 data to render the graphics.
442 The decoder has the following primary responsibilities.
444 - Process each graphic in the Data Stream in sequence, without
445 delays other than those specified in the control information.
447 - Set its hardware parameters to fit, as closely as possible, the
448 control information contained in the Data Stream.
453 An encoder or a decoder is said to comply with a given version of the Graphics
454 Interchange Format if and only if it fully conforms with and correctly
455 implements the definition of the standard associated with that version. An
466 encoder or a decoder may be compliant with a given version number and not
467 compliant with some subsequent version.
470 10. About Recommendations.
472 Each block section in this document contains an entry labeled Recommendation;
473 this section lists a set of recommendations intended to guide and organize the
474 use of the particular blocks. Such recommendations are geared towards making
475 the functions of encoders and decoders more efficient, as well as making
476 optimal use of the communications bandwidth. It is advised that these
477 recommendations be followed.
480 11. About Color Tables.
482 The GIF format utilizes color tables to render raster-based graphics. A color
483 table can have one of two different scopes: global or local. A Global Color
484 Table is used by all those graphics in the Data Stream which do not have a
485 Local Color Table associated with them. The scope of the Global Color Table is
486 the entire Data Stream. A Local Color Table is always associated with the
487 graphic that immediately follows it; the scope of a Local Color Table is
488 limited to that single graphic. A Local Color Table supersedes a Global Color
489 Table, that is, if a Data Stream contains a Global Color Table, and an image
490 has a Local Color Table associated with it, the decoder must save the Global
491 Color Table, use the Local Color Table to render the image, and then restore
492 the Global Color Table. Both types of color tables are optional, making it
493 possible for a Data Stream to contain numerous graphics without a color table
494 at all. For this reason, it is recommended that the decoder save the last
495 Global Color Table used until another Global Color Table is encountered. In
496 this way, a Data Stream which does not contain either a Global Color Table or
497 a Local Color Table may be processed using the last Global Color Table saved.
498 If a Global Color Table from a previous Stream is used, that table becomes the
499 Global Color Table of the present Stream. This is intended to reduce the
500 overhead incurred by color tables. In particular, it is recommended that an
501 encoder use only one Global Color Table if all the images in related Data
502 Streams can be rendered with the same table. If no color table is available at
503 all, the decoder is free to use a system color table or a table of its own. In
504 that case, the decoder may use a color table with as many colors as its
505 hardware is able to support; it is recommended that such a table have black and
506 white as its first two entries, so that monochrome images can be rendered
509 The Definition of the GIF Format allows for a Data Stream to contain only the
510 Header, the Logical Screen Descriptor, a Global Color Table and the GIF
511 Trailer. Such a Data Stream would be used to load a decoder with a Global Color
512 Table, in preparation for subsequent Data Streams without a color table at all.
515 12. Blocks, Extensions and Scope.
517 Blocks can be classified into three groups : Control, Graphic-Rendering and
518 Special Purpose. Control blocks, such as the Header, the Logical Screen
519 Descriptor, the Graphic Control Extension and the Trailer, contain information
520 used to control the process of the Data Stream or information used in setting
521 hardware parameters. Graphic-Rendering blocks such as the Image Descriptor and
532 the Plain Text Extension contain information and data used to render a graphic
533 on the display device. Special Purpose blocks such as the Comment Extension and
534 the Application Extension are neither used to control the process of the Data
535 Stream nor do they contain information or data used to render a graphic on the
536 display device. With the exception of the Logical Screen Descriptor and the
537 Global Color Table, whose scope is the entire Data Stream, all other Control
538 blocks have a limited scope, restricted to the Graphic-Rendering block that
539 follows them. Special Purpose blocks do not delimit the scope of any Control
540 blocks; Special Purpose blocks are transparent to the decoding process.
541 Graphic-Rendering blocks and extensions are used as scope delimiters for
542 Control blocks and extensions. The labels used to identify labeled blocks fall
543 into three ranges : 0x00-0x7F (0-127) are the Graphic Rendering blocks,
544 excluding the Trailer (0x3B); 0x80-0xF9 (128-249) are the Control blocks;
545 0xFA-0xFF (250-255) are the Special Purpose blocks. These ranges are defined so
546 that decoders can handle block scope by appropriately identifying block labels,
547 even when the block itself cannot be processed.
552 The Block Size field in a block, counts the number of bytes remaining in the
553 block, not counting the Block Size field itself, and not counting the Block
554 Terminator, if one is to follow. Blocks other than Data Blocks are intended to
555 be of fixed length; the Block Size field is provided in order to facilitate
556 skipping them, not to allow their size to change in the future. Data blocks
557 and sub-blocks are of variable length to accommodate the amount of data.
560 14. Using GIF as an embedded protocol.
562 As an embedded protocol, GIF may be part of larger application protocols,
563 within which GIF is used to render graphics. In such a case, the application
564 protocol could define a block within which the GIF Data Stream would be
565 contained. The application program would then invoke a GIF decoder upon
566 encountering a block of type GIF. This approach is recommended in favor of
567 using Application Extensions, which become overhead for all other applications
568 that do not process them. Because a GIF Data Stream must be processed in
569 context, the application must rely on some means of identifying the GIF Data
570 Stream outside of the Stream itself.
575 a. Description. Data Sub-blocks are units containing data. They do not
576 have a label, these blocks are processed in the context of control
577 blocks, wherever data blocks are specified in the format. The first byte
578 of the Data sub-block indicates the number of data bytes to follow. A
579 data sub-block may contain from 0 to 255 data bytes. The size of the
580 block does not account for the size byte itself, therefore, the empty
581 sub-block is one whose size field contains 0x00.
583 b. Required Version. 87a.
600 7 6 5 4 3 2 1 0 Field Name Type
602 0 | | Block Size Byte
621 i) Block Size - Number of bytes in the Data Sub-block; the size
622 must be within 0 and 255 bytes, inclusive.
624 ii) Data Values - Any 8-bit value. There must be exactly as many
625 Data Values as specified by the Block Size field.
627 d. Extensions and Scope. This type of block always occurs as part of a
628 larger unit. It does not have a scope of itself.
630 e. Recommendation. None.
633 16. Block Terminator.
635 a. Description. This zero-length Data Sub-block is used to terminate a
636 sequence of Data Sub-blocks. It contains a single byte in the position of
637 the Block Size field and does not contain data.
639 b. Required Version. 87a.
643 7 6 5 4 3 2 1 0 Field Name Type
645 0 | | Block Size Byte
648 i) Block Size - Number of bytes in the Data Sub-block; this field
649 contains the fixed value 0x00.
651 ii) Data Values - This block does not contain any data.
664 d. Extensions and Scope. This block terminates the immediately preceding
665 sequence of Data Sub-blocks. This block cannot be modified by any
668 e. Recommendation. None.
673 a. Description. The Header identifies the GIF Data Stream in context. The
674 Signature field marks the beginning of the Data Stream, and the Version
675 field identifies the set of capabilities required of a decoder to fully
676 process the Data Stream. This block is REQUIRED; exactly one Header must
677 be present per Data Stream.
679 b. Required Version. Not applicable. This block is not subject to a
680 version number. This block must appear at the beginning of every Data
686 7 6 5 4 3 2 1 0 Field Name Type
688 0 | | Signature 3 Bytes
694 3 | | Version 3 Bytes
701 i) Signature - Identifies the GIF Data Stream. This field contains
702 the fixed value 'GIF'.
704 ii) Version - Version number used to format the data stream.
705 Identifies the minimum set of capabilities necessary to a decoder
706 to fully process the contents of the Data Stream.
708 Version Numbers as of 10 July 1990 : "87a" - May 1987
711 Version numbers are ordered numerically increasing on the first two
712 digits starting with 87 (87,88,...,99,00,...,85,86) and
713 alphabetically increasing on the third character (a,...,z).
715 iii) Extensions and Scope. The scope of this block is the entire
716 Data Stream. This block cannot be modified by any extension.
732 i) Signature - This field identifies the beginning of the GIF Data
733 Stream; it is not intended to provide a unique signature for the
734 identification of the data. It is recommended that the GIF Data
735 Stream be identified externally by the application. (Refer to
736 Appendix G for on-line identification of the GIF Data Stream.)
738 ii) Version - ENCODER : An encoder should use the earliest possible
739 version number that defines all the blocks used in the Data Stream.
740 When two or more Data Streams are combined, the latest of the
741 individual version numbers should be used for the resulting Data
742 Stream. DECODER : A decoder should attempt to process the data
743 stream to the best of its ability; if it encounters a version
744 number which it is not capable of processing fully, it should
745 nevertheless, attempt to process the data stream to the best of its
746 ability, perhaps after warning the user that the data may be
750 18. Logical Screen Descriptor.
752 a. Description. The Logical Screen Descriptor contains the parameters
753 necessary to define the area of the display device within which the
754 images will be rendered. The coordinates in this block are given with
755 respect to the top-left corner of the virtual screen; they do not
756 necessarily refer to absolute coordinates on the display device. This
757 implies that they could refer to window coordinates in a window-based
758 environment or printer coordinates when a printer is used.
760 This block is REQUIRED; exactly one Logical Screen Descriptor must be
761 present per Data Stream.
763 b. Required Version. Not applicable. This block is not subject to a
764 version number. This block must appear immediately after the Header.
768 7 6 5 4 3 2 1 0 Field Name Type
770 0 | | Logical Screen Width Unsigned
774 2 | | Logical Screen Height Unsigned
778 4 | | | | | <Packed Fields> See below
780 5 | | Background Color Index Byte
782 6 | | Pixel Aspect Ratio Byte
796 <Packed Fields> = Global Color Table Flag 1 Bit
797 Color Resolution 3 Bits
799 Size of Global Color Table 3 Bits
801 i) Logical Screen Width - Width, in pixels, of the Logical Screen
802 where the images will be rendered in the displaying device.
804 ii) Logical Screen Height - Height, in pixels, of the Logical
805 Screen where the images will be rendered in the displaying device.
807 iii) Global Color Table Flag - Flag indicating the presence of a
808 Global Color Table; if the flag is set, the Global Color Table will
809 immediately follow the Logical Screen Descriptor. This flag also
810 selects the interpretation of the Background Color Index; if the
811 flag is set, the value of the Background Color Index field should
812 be used as the table index of the background color. (This field is
813 the most significant bit of the byte.)
815 Values : 0 - No Global Color Table follows, the Background
816 Color Index field is meaningless.
817 1 - A Global Color Table will immediately follow, the
818 Background Color Index field is meaningful.
820 iv) Color Resolution - Number of bits per primary color available
821 to the original image, minus 1. This value represents the size of
822 the entire palette from which the colors in the graphic were
823 selected, not the number of colors actually used in the graphic.
824 For example, if the value in this field is 3, then the palette of
825 the original image had 4 bits per primary color available to create
826 the image. This value should be set to indicate the richness of
827 the original palette, even if not every color from the whole
828 palette is available on the source machine.
830 v) Sort Flag - Indicates whether the Global Color Table is sorted.
831 If the flag is set, the Global Color Table is sorted, in order of
832 decreasing importance. Typically, the order would be decreasing
833 frequency, with most frequent color first. This assists a decoder,
834 with fewer available colors, in choosing the best subset of colors;
835 the decoder may use an initial segment of the table to render the
838 Values : 0 - Not ordered.
839 1 - Ordered by decreasing importance, most
840 important color first.
842 vi) Size of Global Color Table - If the Global Color Table Flag is
843 set to 1, the value in this field is used to calculate the number
844 of bytes contained in the Global Color Table. To determine that
845 actual size of the color table, raise 2 to [the value of the field
846 + 1]. Even if there is no Global Color Table specified, set this
847 field according to the above formula so that decoders can choose
848 the best graphics mode to display the stream in. (This field is
849 made up of the 3 least significant bits of the byte.)
851 vii) Background Color Index - Index into the Global Color Table for
862 the Background Color. The Background Color is the color used for
863 those pixels on the screen that are not covered by an image. If the
864 Global Color Table Flag is set to (zero), this field should be zero
865 and should be ignored.
867 viii) Pixel Aspect Ratio - Factor used to compute an approximation
868 of the aspect ratio of the pixel in the original image. If the
869 value of the field is not 0, this approximation of the aspect ratio
870 is computed based on the formula:
872 Aspect Ratio = (Pixel Aspect Ratio + 15) / 64
874 The Pixel Aspect Ratio is defined to be the quotient of the pixel's
875 width over its height. The value range in this field allows
876 specification of the widest pixel of 4:1 to the tallest pixel of
877 1:4 in increments of 1/64th.
879 Values : 0 - No aspect ratio information is given.
880 1..255 - Value used in the computation.
882 d. Extensions and Scope. The scope of this block is the entire Data
883 Stream. This block cannot be modified by any extension.
885 e. Recommendations. None.
888 19. Global Color Table.
890 a. Description. This block contains a color table, which is a sequence of
891 bytes representing red-green-blue color triplets. The Global Color Table
892 is used by images without a Local Color Table and by Plain Text
893 Extensions. Its presence is marked by the Global Color Table Flag being
894 set to 1 in the Logical Screen Descriptor; if present, it immediately
895 follows the Logical Screen Descriptor and contains a number of bytes
897 3 x 2^(Size of Global Color Table+1).
899 This block is OPTIONAL; at most one Global Color Table may be present
902 b. Required Version. 87a
930 7 6 5 4 3 2 1 0 Field Name Type
948 767 | | Blue 255 Byte
952 d. Extensions and Scope. The scope of this block is the entire Data
953 Stream. This block cannot be modified by any extension.
955 e. Recommendation. None.
958 20. Image Descriptor.
960 a. Description. Each image in the Data Stream is composed of an Image
961 Descriptor, an optional Local Color Table, and the image data. Each
962 image must fit within the boundaries of the Logical Screen, as defined
963 in the Logical Screen Descriptor.
965 The Image Descriptor contains the parameters necessary to process a table
966 based image. The coordinates given in this block refer to coordinates
967 within the Logical Screen, and are given in pixels. This block is a
968 Graphic-Rendering Block, optionally preceded by one or more Control
969 blocks such as the Graphic Control Extension, and may be optionally
970 followed by a Local Color Table; the Image Descriptor is always followed
973 This block is REQUIRED for an image. Exactly one Image Descriptor must
974 be present per image in the Data Stream. An unlimited number of images
975 may be present per Data Stream.
977 b. Required Version. 87a.
996 7 6 5 4 3 2 1 0 Field Name Type
998 0 | | Image Separator Byte
1000 1 | | Image Left Position Unsigned
1004 3 | | Image Top Position Unsigned
1008 5 | | Image Width Unsigned
1012 7 | | Image Height Unsigned
1016 9 | | | | | | <Packed Fields> See below
1019 <Packed Fields> = Local Color Table Flag 1 Bit
1020 Interlace Flag 1 Bit
1023 Size of Local Color Table 3 Bits
1025 i) Image Separator - Identifies the beginning of an Image
1026 Descriptor. This field contains the fixed value 0x2C.
1028 ii) Image Left Position - Column number, in pixels, of the left edge
1029 of the image, with respect to the left edge of the Logical Screen.
1030 Leftmost column of the Logical Screen is 0.
1032 iii) Image Top Position - Row number, in pixels, of the top edge of
1033 the image with respect to the top edge of the Logical Screen. Top
1034 row of the Logical Screen is 0.
1036 iv) Image Width - Width of the image in pixels.
1038 v) Image Height - Height of the image in pixels.
1040 vi) Local Color Table Flag - Indicates the presence of a Local Color
1041 Table immediately following this Image Descriptor. (This field is
1042 the most significant bit of the byte.)
1045 Values : 0 - Local Color Table is not present. Use
1046 Global Color Table if available.
1047 1 - Local Color Table present, and to follow
1048 immediately after this Image Descriptor.
1060 vii) Interlace Flag - Indicates if the image is interlaced. An image
1061 is interlaced in a four-pass interlace pattern; see Appendix E for
1064 Values : 0 - Image is not interlaced.
1065 1 - Image is interlaced.
1067 viii) Sort Flag - Indicates whether the Local Color Table is
1068 sorted. If the flag is set, the Local Color Table is sorted, in
1069 order of decreasing importance. Typically, the order would be
1070 decreasing frequency, with most frequent color first. This assists
1071 a decoder, with fewer available colors, in choosing the best subset
1072 of colors; the decoder may use an initial segment of the table to
1075 Values : 0 - Not ordered.
1076 1 - Ordered by decreasing importance, most
1077 important color first.
1079 ix) Size of Local Color Table - If the Local Color Table Flag is
1080 set to 1, the value in this field is used to calculate the number
1081 of bytes contained in the Local Color Table. To determine that
1082 actual size of the color table, raise 2 to the value of the field
1083 + 1. This value should be 0 if there is no Local Color Table
1084 specified. (This field is made up of the 3 least significant bits
1087 d. Extensions and Scope. The scope of this block is the Table-based Image
1088 Data Block that follows it. This block may be modified by the Graphic
1091 e. Recommendation. None.
1094 21. Local Color Table.
1096 a. Description. This block contains a color table, which is a sequence of
1097 bytes representing red-green-blue color triplets. The Local Color Table
1098 is used by the image that immediately follows. Its presence is marked by
1099 the Local Color Table Flag being set to 1 in the Image Descriptor; if
1100 present, the Local Color Table immediately follows the Image Descriptor
1101 and contains a number of bytes equal to
1102 3x2^(Size of Local Color Table+1).
1103 If present, this color table temporarily becomes the active color table
1104 and the following image should be processed using it. This block is
1105 OPTIONAL; at most one Local Color Table may be present per Image
1106 Descriptor and its scope is the single image associated with the Image
1107 Descriptor that precedes it.
1109 b. Required Version. 87a.
1128 7 6 5 4 3 2 1 0 Field Name Type
1146 767 | | Blue 255 Byte
1150 d. Extensions and Scope. The scope of this block is the Table-based Image
1151 Data Block that immediately follows it. This block cannot be modified by
1154 e. Recommendations. None.
1157 22. Table Based Image Data.
1159 a. Description. The image data for a table based image consists of a
1160 sequence of sub-blocks, of size at most 255 bytes each, containing an
1161 index into the active color table, for each pixel in the image. Pixel
1162 indices are in order of left to right and from top to bottom. Each index
1163 must be within the range of the size of the active color table, starting
1164 at 0. The sequence of indices is encoded using the LZW Algorithm with
1165 variable-length code, as described in Appendix F
1167 b. Required Version. 87a.
1169 c. Syntax. The image data format is as follows:
1171 7 6 5 4 3 2 1 0 Field Name Type
1173 | | LZW Minimum Code Size Byte
1178 / / Image Data Data Sub-blocks
1192 i) LZW Minimum Code Size. This byte determines the initial number
1193 of bits used for LZW codes in the image data, as described in
1196 d. Extensions and Scope. This block has no scope, it contains raster
1197 data. Extensions intended to modify a Table-based image must appear
1198 before the corresponding Image Descriptor.
1200 e. Recommendations. None.
1203 23. Graphic Control Extension.
1205 a. Description. The Graphic Control Extension contains parameters used
1206 when processing a graphic rendering block. The scope of this extension is
1207 the first graphic rendering block to follow. The extension contains only
1210 This block is OPTIONAL; at most one Graphic Control Extension may precede
1211 a graphic rendering block. This is the only limit to the number of
1212 Graphic Control Extensions that may be contained in a Data Stream.
1214 b. Required Version. 89a.
1218 7 6 5 4 3 2 1 0 Field Name Type
1220 0 | | Extension Introducer Byte
1222 1 | | Graphic Control Label Byte
1226 0 | | Block Size Byte
1228 1 | | | | | <Packed Fields> See below
1230 2 | | Delay Time Unsigned
1234 4 | | Transparent Color Index Byte
1238 0 | | Block Terminator Byte
1242 <Packed Fields> = Reserved 3 Bits
1243 Disposal Method 3 Bits
1244 User Input Flag 1 Bit
1245 Transparent Color Flag 1 Bit
1247 i) Extension Introducer - Identifies the beginning of an extension
1258 block. This field contains the fixed value 0x21.
1260 ii) Graphic Control Label - Identifies the current block as a
1261 Graphic Control Extension. This field contains the fixed value
1264 iii) Block Size - Number of bytes in the block, after the Block
1265 Size field and up to but not including the Block Terminator. This
1266 field contains the fixed value 4.
1268 iv) Disposal Method - Indicates the way in which the graphic is to
1269 be treated after being displayed.
1271 Values : 0 - No disposal specified. The decoder is
1272 not required to take any action.
1273 1 - Do not dispose. The graphic is to be left
1275 2 - Restore to background color. The area used by the
1276 graphic must be restored to the background color.
1277 3 - Restore to previous. The decoder is required to
1278 restore the area overwritten by the graphic with
1279 what was there prior to rendering the graphic.
1280 4-7 - To be defined.
1282 v) User Input Flag - Indicates whether or not user input is
1283 expected before continuing. If the flag is set, processing will
1284 continue when user input is entered. The nature of the User input
1285 is determined by the application (Carriage Return, Mouse Button
1288 Values : 0 - User input is not expected.
1289 1 - User input is expected.
1291 When a Delay Time is used and the User Input Flag is set,
1292 processing will continue when user input is received or when the
1293 delay time expires, whichever occurs first.
1295 vi) Transparency Flag - Indicates whether a transparency index is
1296 given in the Transparent Index field. (This field is the least
1297 significant bit of the byte.)
1299 Values : 0 - Transparent Index is not given.
1300 1 - Transparent Index is given.
1302 vii) Delay Time - If not 0, this field specifies the number of
1303 hundredths (1/100) of a second to wait before continuing with the
1304 processing of the Data Stream. The clock starts ticking immediately
1305 after the graphic is rendered. This field may be used in
1306 conjunction with the User Input Flag field.
1308 viii) Transparency Index - The Transparency Index is such that when
1309 encountered, the corresponding pixel of the display device is not
1310 modified and processing goes on to the next pixel. The index is
1311 present if and only if the Transparency Flag is set to 1.
1313 ix) Block Terminator - This zero-length data block marks the end of
1323 the Graphic Control Extension.
1325 d. Extensions and Scope. The scope of this Extension is the graphic
1326 rendering block that follows it; it is possible for other extensions to
1327 be present between this block and its target. This block can modify the
1328 Image Descriptor Block and the Plain Text Extension.
1332 i) Disposal Method - The mode Restore To Previous is intended to be
1333 used in small sections of the graphic; the use of this mode imposes
1334 severe demands on the decoder to store the section of the graphic
1335 that needs to be saved. For this reason, this mode should be used
1336 sparingly. This mode is not intended to save an entire graphic or
1337 large areas of a graphic; when this is the case, the encoder should
1338 make every attempt to make the sections of the graphic to be
1339 restored be separate graphics in the data stream. In the case where
1340 a decoder is not capable of saving an area of a graphic marked as
1341 Restore To Previous, it is recommended that a decoder restore to
1342 the background color.
1344 ii) User Input Flag - When the flag is set, indicating that user
1345 input is expected, the decoder may sound the bell (0x07) to alert
1346 the user that input is being expected. In the absence of a
1347 specified Delay Time, the decoder should wait for user input
1348 indefinitely. It is recommended that the encoder not set the User
1349 Input Flag without a Delay Time specified.
1352 24. Comment Extension.
1354 a. Description. The Comment Extension contains textual information which
1355 is not part of the actual graphics in the GIF Data Stream. It is suitable
1356 for including comments about the graphics, credits, descriptions or any
1357 other type of non-control and non-graphic data. The Comment Extension
1358 may be ignored by the decoder, or it may be saved for later processing;
1359 under no circumstances should a Comment Extension disrupt or interfere
1360 with the processing of the Data Stream.
1362 This block is OPTIONAL; any number of them may appear in the Data Stream.
1364 b. Required Version. 89a.
1392 7 6 5 4 3 2 1 0 Field Name Type
1394 0 | | Extension Introducer Byte
1396 1 | | Comment Label Byte
1401 N | | Comment Data Data Sub-blocks
1406 0 | | Block Terminator Byte
1409 i) Extension Introducer - Identifies the beginning of an extension
1410 block. This field contains the fixed value 0x21.
1412 ii) Comment Label - Identifies the block as a Comment Extension.
1413 This field contains the fixed value 0xFE.
1415 iii) Comment Data - Sequence of sub-blocks, each of size at most
1416 255 bytes and at least 1 byte, with the size in a byte preceding
1417 the data. The end of the sequence is marked by the Block
1420 iv) Block Terminator - This zero-length data block marks the end of
1421 the Comment Extension.
1423 d. Extensions and Scope. This block does not have scope. This block
1424 cannot be modified by any extension.
1428 i) Data - This block is intended for humans. It should contain
1429 text using the 7-bit ASCII character set. This block should
1430 not be used to store control information for custom processing.
1432 ii) Position - This block may appear at any point in the Data
1433 Stream at which a block can begin; however, it is recommended that
1434 Comment Extensions do not interfere with Control or Data blocks;
1435 they should be located at the beginning or at the end of the Data
1436 Stream to the extent possible.
1439 25. Plain Text Extension.
1441 a. Description. The Plain Text Extension contains textual data and the
1442 parameters necessary to render that data as a graphic, in a simple form.
1443 The textual data will be encoded with the 7-bit printable ASCII
1444 characters. Text data are rendered using a grid of character cells
1456 defined by the parameters in the block fields. Each character is rendered
1457 in an individual cell. The textual data in this block is to be rendered
1458 as mono-spaced characters, one character per cell, with a best fitting
1459 font and size. For further information, see the section on
1460 Recommendations below. The data characters are taken sequentially from
1461 the data portion of the block and rendered within a cell, starting with
1462 the upper left cell in the grid and proceeding from left to right and
1463 from top to bottom. Text data is rendered until the end of data is
1464 reached or the character grid is filled. The Character Grid contains an
1465 integral number of cells; in the case that the cell dimensions do not
1466 allow for an integral number, fractional cells must be discarded; an
1467 encoder must be careful to specify the grid dimensions accurately so that
1468 this does not happen. This block requires a Global Color Table to be
1469 available; the colors used by this block reference the Global Color Table
1470 in the Stream if there is one, or the Global Color Table from a previous
1471 Stream, if one was saved. This block is a graphic rendering block,
1472 therefore it may be modified by a Graphic Control Extension. This block
1473 is OPTIONAL; any number of them may appear in the Data Stream.
1475 b. Required Version. 89a.
1524 7 6 5 4 3 2 1 0 Field Name Type
1526 0 | | Extension Introducer Byte
1528 1 | | Plain Text Label Byte
1532 0 | | Block Size Byte
1534 1 | | Text Grid Left Position Unsigned
1538 3 | | Text Grid Top Position Unsigned
1542 5 | | Text Grid Width Unsigned
1546 7 | | Text Grid Height Unsigned
1550 9 | | Character Cell Width Byte
1552 10 | | Character Cell Height Byte
1554 11 | | Text Foreground Color Index Byte
1556 12 | | Text Background Color Index Byte
1561 N | | Plain Text Data Data Sub-blocks
1566 0 | | Block Terminator Byte
1569 i) Extension Introducer - Identifies the beginning of an extension
1570 block. This field contains the fixed value 0x21.
1572 ii) Plain Text Label - Identifies the current block as a Plain Text
1573 Extension. This field contains the fixed value 0x01.
1575 iii) Block Size - Number of bytes in the extension, after the Block
1576 Size field and up to but not including the beginning of the data
1577 portion. This field contains the fixed value 12.
1588 iv) Text Grid Left Position - Column number, in pixels, of the left
1589 edge of the text grid, with respect to the left edge of the Logical
1592 v) Text Grid Top Position - Row number, in pixels, of the top edge
1593 of the text grid, with respect to the top edge of the Logical
1596 vi) Image Grid Width - Width of the text grid in pixels.
1598 vii) Image Grid Height - Height of the text grid in pixels.
1600 viii) Character Cell Width - Width, in pixels, of each cell in the
1603 ix) Character Cell Height - Height, in pixels, of each cell in the
1606 x) Text Foreground Color Index - Index into the Global Color Table
1607 to be used to render the text foreground.
1609 xi) Text Background Color Index - Index into the Global Color Table
1610 to be used to render the text background.
1612 xii) Plain Text Data - Sequence of sub-blocks, each of size at most
1613 255 bytes and at least 1 byte, with the size in a byte preceding
1614 the data. The end of the sequence is marked by the Block
1617 xiii) Block Terminator - This zero-length data block marks the end
1618 of the Plain Text Data Blocks.
1620 d. Extensions and Scope. The scope of this block is the Plain Text Data
1621 Block contained in it. This block may be modified by the Graphic Control
1624 e. Recommendations. The data in the Plain Text Extension is assumed to be
1625 preformatted. The selection of font and size is left to the discretion of
1626 the decoder. If characters less than 0x20 or greater than 0xf7 are
1627 encountered, it is recommended that the decoder display a Space character
1628 (0x20). The encoder should use grid and cell dimensions such that an
1629 integral number of cells fit in the grid both horizontally as well as
1630 vertically. For broadest compatibility, character cell dimensions should
1631 be around 8x8 or 8x16 (width x height); consider an image for unusual
1635 26. Application Extension.
1637 a. Description. The Application Extension contains application-specific
1638 information; it conforms with the extension block syntax, as described
1639 below, and its block label is 0xFF.
1641 b. Required Version. 89a.
1656 7 6 5 4 3 2 1 0 Field Name Type
1658 0 | | Extension Introducer Byte
1660 1 | | Extension Label Byte
1664 0 | | Block Size Byte
1670 3 | | Application Identifier 8 Bytes
1684 10 | | Appl. Authentication Code 3 Bytes
1691 | | Application Data Data Sub-blocks
1697 0 | | Block Terminator Byte
1700 i) Extension Introducer - Defines this block as an extension. This
1701 field contains the fixed value 0x21.
1703 ii) Application Extension Label - Identifies the block as an
1704 Application Extension. This field contains the fixed value 0xFF.
1706 iii) Block Size - Number of bytes in this extension block,
1707 following the Block Size field, up to but not including the
1708 beginning of the Application Data. This field contains the fixed
1720 iv) Application Identifier - Sequence of eight printable ASCII
1721 characters used to identify the application owning the Application
1724 v) Application Authentication Code - Sequence of three bytes used
1725 to authenticate the Application Identifier. An Application program
1726 may use an algorithm to compute a binary code that uniquely
1727 identifies it as the application owning the Application Extension.
1730 d. Extensions and Scope. This block does not have scope. This block
1731 cannot be modified by any extension.
1733 e. Recommendation. None.
1738 a. Description. This block is a single-field block indicating the end of
1739 the GIF Data Stream. It contains the fixed value 0x3B.
1741 b. Required Version. 87a.
1745 7 6 5 4 3 2 1 0 Field Name Type
1747 0 | | GIF Trailer Byte
1750 d. Extensions and Scope. This block does not have scope, it terminates
1751 the GIF Data Stream. This block may not be modified by any extension.
1753 e. Recommendations. None.
1787 A. Quick Reference Table.
1789 Block Name Required Label Ext. Vers.
1790 Application Extension Opt. (*) 0xFF (255) yes 89a
1791 Comment Extension Opt. (*) 0xFE (254) yes 89a
1792 Global Color Table Opt. (1) none no 87a
1793 Graphic Control Extension Opt. (*) 0xF9 (249) yes 89a
1794 Header Req. (1) none no N/A
1795 Image Descriptor Opt. (*) 0x2C (044) no 87a (89a)
1796 Local Color Table Opt. (*) none no 87a
1797 Logical Screen Descriptor Req. (1) none no 87a (89a)
1798 Plain Text Extension Opt. (*) 0x01 (001) yes 89a
1799 Trailer Req. (1) 0x3B (059) no 87a
1802 Header Req. (1) none no N/A
1803 Logical Screen Descriptor Req. (1) none no 87a (89a)
1804 Global Color Table Opt. (1) none no 87a
1805 Local Color Table Opt. (*) none no 87a
1807 Graphic-Rendering Blocks
1808 Plain Text Extension Opt. (*) 0x01 (001) yes 89a
1809 Image Descriptor Opt. (*) 0x2C (044) no 87a (89a)
1812 Graphic Control Extension Opt. (*) 0xF9 (249) yes 89a
1814 Special Purpose Blocks
1815 Trailer Req. (1) 0x3B (059) no 87a
1816 Comment Extension Opt. (*) 0xFE (254) yes 89a
1817 Application Extension Opt. (*) 0xFF (255) yes 89a
1819 legend: (1) if present, at most one occurrence
1820 (*) zero or more occurrences
1821 (+) one or more occurrences
1823 Notes : The Header is not subject to Version Numbers.
1824 (89a) The Logical Screen Descriptor and the Image Descriptor retained their
1825 syntax from version 87a to version 89a, but some fields reserved under version
1826 87a are used under version 89a.
1855 A Grammar is a form of notation to represent the sequence in which certain
1856 objects form larger objects. A grammar is also used to represent the number of
1857 objects that can occur at a given position. The grammar given here represents
1858 the sequence of blocks that form the GIF Data Stream. A grammar is given by
1859 listing its rules. Each rule consists of the left-hand side, followed by some
1860 form of equals sign, followed by the right-hand side. In a rule, the
1861 right-hand side describes how the left-hand side is defined. The right-hand
1862 side consists of a sequence of entities, with the possible presence of special
1863 symbols. The following legend defines the symbols used in this grammar for GIF.
1865 Legend: <> grammar word
1867 * zero or more occurrences
1868 + one or more occurrences
1874 <GIF Data Stream> ::= Header <Logical Screen> <Data>* Trailer
1876 This rule defines the entity <GIF Data Stream> as follows. It must begin with a
1877 Header. The Header is followed by an entity called Logical Screen, which is
1878 defined below by another rule. The Logical Screen is followed by the entity
1879 Data, which is also defined below by another rule. Finally, the entity Data is
1880 followed by the Trailer. Since there is no rule defining the Header or the
1881 Trailer, this means that these blocks are defined in the document. The entity
1882 Data has a special symbol (*) following it which means that, at this position,
1883 the entity Data may be repeated any number of times, including 0 times. For
1884 further reading on this subject, refer to a standard text on Programming
1890 <GIF Data Stream> ::= Header <Logical Screen> <Data>* Trailer
1892 <Logical Screen> ::= Logical Screen Descriptor [Global Color Table]
1894 <Data> ::= <Graphic Block> |
1895 <Special-Purpose Block>
1897 <Graphic Block> ::= [Graphic Control Extension] <Graphic-Rendering Block>
1899 <Graphic-Rendering Block> ::= <Table-Based Image> |
1900 Plain Text Extension
1902 <Table-Based Image> ::= Image Descriptor [Local Color Table] Image Data
1904 <Special-Purpose Block> ::= Application Extension |
1918 NOTE : The grammar indicates that it is possible for a GIF Data Stream to
1919 contain the Header, the Logical Screen Descriptor, a Global Color Table and the
1920 GIF Trailer. This special case is used to load a GIF decoder with a Global
1921 Color Table, in preparation for subsequent Data Streams without color tables at
1987 Active Color Table - Color table used to render the next graphic. If the next
1988 graphic is an image which has a Local Color Table associated with it, the
1989 active color table becomes the Local Color Table associated with that image.
1990 If the next graphic is an image without a Local Color Table, or a Plain Text
1991 Extension, the active color table is the Global Color Table associated with the
1992 Data Stream, if there is one; if there is no Global Color Table in the Data
1993 Stream, the active color table is a color table saved from a previous Data
1994 Stream, or one supplied by the decoder.
1996 Block - Collection of bytes forming a protocol unit. In general, the term
1997 includes labeled and unlabeled blocks, as well as Extensions.
1999 Data Stream - The GIF Data Stream is composed of blocks and sub-blocks
2000 representing images and graphics, together with control information to render
2001 them on a display device. All control and data blocks in the Data Stream must
2002 follow the Header and must precede the Trailer.
2004 Decoder - A program capable of processing a GIF Data Stream to render the
2005 images and graphics contained in it.
2007 Encoder - A program capable of capturing and formatting image and graphic
2008 raster data, following the definitions of the Graphics Interchange Format.
2010 Extension - A protocol block labeled by the Extension Introducer 0x21.
2012 Extension Introducer - Label (0x21) defining an Extension.
2014 Graphic - Data which can be rendered on the screen by virtue of some algorithm.
2015 The term graphic is more general than the term image; in addition to images,
2016 the term graphic also includes data such as text, which is rendered using
2019 Image - Data representing a picture or a drawing; an image is represented by an
2020 array of pixels called the raster of the image.
2022 Raster - Array of pixel values representing an image.
2053 Animation - The Graphics Interchange Format is not intended as a platform for
2054 animation, even though it can be done in a limited way.
2056 Byte Ordering - Unless otherwise stated, multi-byte numeric fields are ordered
2057 with the Least Significant Byte first.
2059 Color Indices - Color indices always refer to the active color table, either
2060 the Global Color Table or the Local Color Table.
2062 Color Order - Unless otherwise stated, all triple-component RGB color values
2063 are specified in Red-Green-Blue order.
2065 Color Tables - Both color tables, the Global and the Local, are optional; if
2066 present, the Global Color Table is to be used with every image in the Data
2067 Stream for which a Local Color Table is not given; if present, a Local Color
2068 Table overrides the Global Color Table. However, if neither color table is
2069 present, the application program is free to use an arbitrary color table. If
2070 the graphics in several Data Streams are related and all use the same color
2071 table, an encoder could place the color table as the Global Color Table in the
2072 first Data Stream and leave subsequent Data Streams without a Global Color
2073 Table or any Local Color Tables; in this way, the overhead for the table is
2074 eliminated. It is recommended that the decoder save the previous Global Color
2075 Table to be used with the Data Stream that follows, in case it does not contain
2076 either a Global Color Table or any Local Color Tables. In general, this allows
2077 the application program to use past color tables, significantly reducing
2078 transmission overhead.
2080 Extension Blocks - Extensions are defined using the Extension Introducer code
2081 to mark the beginning of the block, followed by a block label, identifying the
2082 type of extension. Extension Codes are numbers in the range from 0x00 to 0xFF,
2083 inclusive. Special purpose extensions are transparent to the decoder and may be
2084 omitted when transmitting the Data Stream on-line. The GIF capabilities
2085 dialogue makes the provision for the receiver to request the transmission of
2086 all blocks; the default state in this regard is no transmission of Special
2089 Reserved Fields - All Reserved Fields are expected to have each bit set to zero
2117 E. Interlaced Images.
2119 The rows of an Interlaced images are arranged in the following order:
2121 Group 1 : Every 8th. row, starting with row 0. (Pass 1)
2122 Group 2 : Every 8th. row, starting with row 4. (Pass 2)
2123 Group 3 : Every 4th. row, starting with row 2. (Pass 3)
2124 Group 4 : Every 2nd. row, starting with row 1. (Pass 4)
2126 The Following example illustrates how the rows of an interlaced image are
2129 Row Number Interlace Pass
2131 0 ----------------------------------------- 1
2132 1 ----------------------------------------- 4
2133 2 ----------------------------------------- 3
2134 3 ----------------------------------------- 4
2135 4 ----------------------------------------- 2
2136 5 ----------------------------------------- 4
2137 6 ----------------------------------------- 3
2138 7 ----------------------------------------- 4
2139 8 ----------------------------------------- 1
2140 9 ----------------------------------------- 4
2141 10 ----------------------------------------- 3
2142 11 ----------------------------------------- 4
2143 12 ----------------------------------------- 2
2144 13 ----------------------------------------- 4
2145 14 ----------------------------------------- 3
2146 15 ----------------------------------------- 4
2147 16 ----------------------------------------- 1
2148 17 ----------------------------------------- 4
2149 18 ----------------------------------------- 3
2150 19 ----------------------------------------- 4
2183 F. Variable-Length-Code LZW Compression.
2185 The Variable-Length-Code LZW Compression is a variation of the Lempel-Ziv
2186 Compression algorithm in which variable-length codes are used to replace
2187 patterns detected in the original data. The algorithm uses a code or
2188 translation table constructed from the patterns encountered in the original
2189 data; each new pattern is entered into the table and its index is used to
2190 replace it in the compressed stream.
2192 The compressor takes the data from the input stream and builds a code or
2193 translation table with the patterns as it encounters them; each new pattern is
2194 entered into the code table and its index is added to the output stream; when a
2195 pattern is encountered which had been detected since the last code table
2196 refresh, its index from the code table is put on the output stream, thus
2197 achieving the data compression. The expander takes input from the compressed
2198 data stream and builds the code or translation table from it; as the compressed
2199 data stream is processed, codes are used to index into the code table and the
2200 corresponding data is put on the decompressed output stream, thus achieving
2201 data decompression. The details of the algorithm are explained below. The
2202 Variable-Length-Code aspect of the algorithm is based on an initial code size
2203 (LZW-initial code size), which specifies the initial number of bits used for
2204 the compression codes. When the number of patterns detected by the compressor
2205 in the input stream exceeds the number of patterns encodable with the current
2206 number of bits, the number of bits per LZW code is increased by one.
2208 The Raster Data stream that represents the actual output image can be
2216 +---------------+ ----+
2219 | | +-- Repeated as many
2220 | data bytes | | times as necessary.
2222 +---------------+ ----+
2224 . . . . . . ------- The code that terminates the LZW
2225 compressed data must appear before
2228 |0 0 0 0 0 0 0 0| Block Terminator
2231 The conversion of the image from a series of pixel values to a transmitted or
2232 stored character stream involves several steps. In brief these steps are:
2234 1. Establish the Code Size - Define the number of bits needed to represent the
2237 2. Compress the Data - Compress the series of image pixels to a series of
2250 3. Build a Series of Bytes - Take the set of compression codes and convert to a
2251 string of 8-bit bytes.
2253 4. Package the Bytes - Package sets of bytes into blocks preceded by character
2258 The first byte of the Compressed Data stream is a value indicating the minimum
2259 number of bits required to represent the set of actual pixel values. Normally
2260 this will be the same as the number of color bits. Because of some algorithmic
2261 constraints however, black & white images which have one color bit must be
2262 indicated as having a code size of 2.
2263 This code size value also implies that the compression codes must start out one
2268 The LZW algorithm converts a series of data values into a series of codes which
2269 may be raw values or a code designating a series of values. Using text
2270 characters as an analogy, the output code consists of a character or a code
2271 representing a string of characters.
2273 The LZW algorithm used in GIF matches algorithmically with the standard LZW
2274 algorithm with the following differences:
2276 1. A special Clear code is defined which resets all compression/decompression
2277 parameters and tables to a start-up state. The value of this code is 2**<code
2278 size>. For example if the code size indicated was 4 (image was 4 bits/pixel)
2279 the Clear code value would be 16 (10000 binary). The Clear code can appear at
2280 any point in the image data stream and therefore requires the LZW algorithm to
2281 process succeeding codes as if a new data stream was starting. Encoders should
2282 output a Clear code as the first code of each image data stream.
2284 2. An End of Information code is defined that explicitly indicates the end of
2285 the image data stream. LZW processing terminates when this code is encountered.
2286 It must be the last code output by the encoder for an image. The value of this
2287 code is <Clear code>+1.
2289 3. The first available compression code value is <Clear code>+2.
2291 4. The output codes are of variable length, starting at <code size>+1 bits per
2292 code, up to 12 bits per code. This defines a maximum code value of 4095
2293 (0xFFF). Whenever the LZW code value would exceed the current code length, the
2294 code length is increased by one. The packing/unpacking of these codes must then
2295 be altered to reflect the new code length.
2299 Because the LZW compression used for GIF creates a series of variable length
2300 codes, of between 3 and 12 bits each, these codes must be reformed into a
2301 series of 8-bit bytes that will be the characters actually stored or
2302 transmitted. This provides additional compression of the image. The codes are
2303 formed into a stream of bits as if they were packed right to left and then
2314 picked off 8 bits at a time to be output.
2316 Assuming a character array of 8 bits per character and using 5 bit codes to be
2317 packed, an example layout would be similar to:
2337 Note that the physical packing arrangement will change as the number of bits
2338 per compression code change but the concept remains the same.
2342 Once the bytes have been created, they are grouped into blocks for output by
2343 preceding each block of 0 to 255 bytes with a character count byte. A block
2344 with a zero byte count terminates the Raster Data stream for a given image.
2345 These blocks are what are actually output for the GIF image. This block format
2346 has the side effect of allowing a decoding program the ability to read past the
2347 actual image data if necessary by reading block counts and then skipping over
2354 [1] Ziv, J. and Lempel, A. : "A Universal Algorithm for Sequential Data
2355 Compression", IEEE Transactions on Information Theory, May 1977.
2356 [2] Welch, T. : "A Technique for High-Performance Data Compression", Computer,
2358 [3] Nelson, M.R. : "LZW Data Compression", Dr. Dobb's Journal, October 1989.
2381 G. On-line Capabilities Dialogue.
2383 NOTE : This section is currently (10 July 1990) under revision; the information
2384 provided here should be used as general guidelines. Code written based on this
2385 information should be designed in a flexible way to accommodate any changes
2386 resulting from the revisions.
2388 The following sequences are defined for use in mediating control between a GIF
2389 sender and GIF receiver over an interactive communications line. These
2390 sequences do not apply to applications that involve downloading of static GIF
2391 files and are not considered part of a GIF file.
2393 GIF CAPABILITIES ENQUIRY
2395 The GIF Capabilities Enquiry sequence is issued from a host and requests an
2396 interactive GIF decoder to return a response message that defines the graphics
2397 parameters for the decoder. This involves returning information about available
2398 screen sizes, number of bits/color supported and the amount of color detail
2399 supported. The escape sequence for the GIF Capabilities Enquiry is defined as:
2401 ESC[>0g 0x1B 0x5B 0x3E 0x30 0x67
2403 GIF CAPABILITIES RESPONSE
2405 The GIF Capabilities Response message is returned by an interactive GIF decoder
2406 and defines the decoder's display capabilities for all graphics modes that are
2407 supported by the software. Note that this can also include graphics printers as
2408 well as a monitor screen. The general format of this message is:
2410 #version;protocol{;dev, width, height, color-bits, color-res}...<CR>
2413 '#' GIF Capabilities Response identifier character.
2414 version GIF format version number; initially '87a'.
2415 protocol='0' No end-to-end protocol supported by decoder Transfer as direct
2417 protocol='1' Can use CIS B+ error correction protocol to transfer GIF data
2418 interactively from the host directly to the display.
2419 dev = '0' Screen parameter set follows.
2420 dev = '1' Printer parameter set follows.
2421 width Maximum supported display width in pixels.
2422 height Maximum supported display height in pixels.
2423 color-bits Number of bits per pixel supported. The number of supported
2424 colors is therefore 2**color-bits.
2425 color-res Number of bits per color component supported in the hardware
2426 color palette. If color-res is '0' then no hardware palette
2429 Note that all values in the GIF Capabilities Response are returned as ASCII
2430 decimal numbers and the message is terminated by a Carriage Return character.
2432 The following GIF Capabilities Response message describes three standard IBM PC
2433 Enhanced Graphics Adapter configurations with no printer; the GIF data stream
2446 can be processed within an error correcting protocol:
2448 #87a;1;0,320,200,4,0;0,640,200,2,2;0,640,350,4,2<CR>
2450 ENTER GIF GRAPHICS MODE
2452 Two sequences are currently defined to invoke an interactive GIF decoder into
2453 action. The only difference between them is that different output media are
2454 selected. These sequences are:
2456 ESC[>1g Display GIF image on screen
2458 0x1B 0x5B 0x3E 0x31 0x67
2460 ESC[>2g Display image directly to an attached graphics printer. The image may
2461 optionally be displayed on the screen as well.
2463 0x1B 0x5B 0x3E 0x32 0x67
2465 Note that the 'g' character terminating each sequence is in lowercase.
2467 INTERACTIVE ENVIRONMENT
2469 The assumed environment for the transmission of GIF image data from an
2470 interactive application is a full 8-bit data stream from host to micro. All
2471 256 character codes must be transferrable. The establishing of an 8-bit data
2472 path for communications will normally be taken care of by the host application
2473 programs. It is however up to the receiving communications programs supporting
2474 GIF to be able to receive and pass on all 256 8-bit codes to the GIF decoder