cover sheet for the gif89a specification deferred clear code in lzw compression there has been confusion about where clear codes can be found in the data stream. as the specification says, they may appear at anytime. there is not a requirement to send a clear code when the string table is full. it is the encoder's decision as to when the table should be cleared. when the table is full, the encoder can chose to use the table as is, making no changes to it until the encoder chooses to clear it. the encoder during this time sends out codes that are of the maximum code size. as we can see from the above, when the decoder's table is full, it must not change the table until a clear code is received. the code size is that of the maximum code size. processing other than this is done normally. because of a large base of decoders that do not handle the decompression in this manner, we ask developers of gif encoding software to not implement this feature until at least january 1991 and later if they see that their particular market is not ready for it. this will give developers of gif decoding software time to implement this feature and to get it into the hands of their clients before the decoders start "breaking" on the new gif's. it is not required that encoders change their software to take advantage of the deferred clear code, but it is for decoders. application extension block - application identifier there will be a courtesy directory file located on compuserve in the pics forum. this directory will contain application identifiers for application extension blocks that have been used by developers of gif applications. this file is intended to help keep developers that wish to create application extension blocks from using the same application identifiers. this is not an official directory; it is for voluntary participation only and does not guarantee that someone will not use the same identifier. e-mail can be sent to larry wood (forum manager of pics) indicating the request for inclusion in this file with an identifier.
graphics interchange format(sm) version 89a (c)1987,1988,1989,1990 copyright compuserve incorporated columbus, ohio
compuserve incorporated document date : 31 july 1990
graphics interchange format programming reference
table of contents disclaimer.................................................................
1
foreword...................................................................
1
licensing..................................................................
1
about the document.........................................................
2
general description........................................................
2
version numbers............................................................
2
the encoder................................................................
3
the decoder................................................................
3
compliance.................................................................
3
about recommendations......................................................
4
about color tables.........................................................
4
blocks, extensions and scope...............................................
4
block sizes................................................................
5
using gif as an embedded protocol..........................................
5
data sub-blocks............................................................
5
block terminator...........................................................
6
header.....................................................................
7
logical screen descriptor..................................................
8
global color table......................................................... 10 image descriptor........................................................... 11 local color table.......................................................... 13
table based image data..................................................... 14 graphic control extension.................................................. 15 comment extension.......................................................... 17 plain text extension....................................................... 18 application extension...................................................... 21 trailer.................................................................... 23
quick reference table...................................................... 24 gif grammar................................................................ 25 glossary................................................................... 27 conventions................................................................ 28 interlaced images.......................................................... 29 variable-length-code lzw compression....................................... 30 on-line capabilities dialogue.............................................. 33
1 1. disclaimer. the information provided herein is subject to change without notice. in no event will compuserve incorporated be liable for damages, including any loss of revenue, loss of profits or other incidental or consequential damages arising out of the use or inability to use the information; compuserve incorporated makes no claim as to the suitability of the information. 2. foreword. this document defines the graphics interchange format(sm). the specification given here defines version 89a, which is an extension of version 87a. the graphics interchange format(sm) as specified here should be considered complete; any deviation from it should be considered invalid, including but not limited to, the use of reserved or undefined fields within control or data blocks, the inclusion of extraneous data within or between blocks, the use of methods or algorithms not specifically listed as part of the format, etc. in general, any and all deviations, extensions or modifications not specified in this document should be considered to be in violation of the format and should be avoided. 3. licensing. the graphics interchange format(c) is the copyright property of compuserve
incorporated. only compuserve incorporated is authorized to define, redefine, enhance, alter, modify or change in any way the definition of the format. compuserve incorporated hereby grants a limited, non-exclusive, royalty-free license for the use of the graphics interchange format(sm) in computer software; computer software utilizing gif(sm) must acknowledge ownership of the graphics interchange format and its service mark by compuserve incorporated, in user and technical documentation. computer software utilizing gif, which is distributed or may be distributed without user or technical documentation must display to the screen or printer a message acknowledging ownership of the graphics interchange format and the service mark by compuserve incorporated; in this case, the acknowledgement may be displayed in an opening screen or leading banner, or a closing screen or trailing banner. a message such as the following may be used: "the graphics interchange format(c) is the copyright property of compuserve incorporated. gif(sm) is a service mark property of compuserve incorporated." for further information, please contact : compuserve incorporated graphics technology department 5000 arlington center boulevard columbus, ohio 43220 u. s. a. compuserve incorporated maintains a mailing list with all those individuals and organizations who wish to receive copies of this document when it is corrected
2 or revised. this service is offered free of charge; please provide us with your mailing address. 4. about the document. this document describes in detail the definition of the graphics interchange format. this document is intended as a programming reference; it is recommended that the entire document be read carefully before programming, because of the interdependence of the various parts. there is an individual section for each of the format blocks. within each section, the sub-section labeled required version refers to the version number that an encoder will have to use if the corresponding block is used in the data stream. within each section, a diagram describes the individual fields in the block; the diagrams are drawn vertically; top bytes in the diagram appear first in the data stream. bits within a byte are drawn most significant on the left end. multi-byte numeric fields are ordered least significant byte first. numeric constants are represented as hexadecimal numbers, preceded by "0x". bit fields within a byte are described in order from most significant bits to least significant bits.
5. general description. the graphics interchange format(sm) defines a protocol intended for the on-line transmission and interchange of raster graphic data in a way that is independent of the hardware used in their creation or display. the graphics interchange format is defined in terms of blocks and sub-blocks which contain relevant parameters and data used in the reproduction of a graphic. a gif data stream is a sequence of protocol blocks and sub-blocks representing a collection of graphics. in general, the graphics in a data stream are assumed to be related to some degree, and to share some control information; it is recommended that encoders attempt to group together related graphics in order to minimize hardware changes during processing and to minimize control information overhead. for the same reason, unrelated graphics or graphics which require resetting hardware parameters should be encoded separately to the extent possible. a data stream may originate locally, as when read from a file, or it may originate remotely, as when transmitted over a data communications line. the format is defined with the assumption that an error-free transport level protocol is used for communications; the format makes no provisions for error-detection and error-correction. the gif data stream must be interpreted in context, that is, the application program must rely on information external to the data stream to invoke the decoder process. 6. version numbers. the version number in the header of a data stream is intended to identify the minimum set of capabilities required of a decoder in order to fully process the data stream. an encoder should use the earliest possible version number that includes all the blocks used in the data stream. within each block section in this document, there is an entry labeled required version which specifies the
3 earliest version number that includes the corresponding block. the encoder should make every attempt to use the earliest version number covering all the blocks in the data stream; the unnecessary use of later version numbers will hinder processing by some decoders. 7. the encoder. the encoder is the program used to create a gif data stream. from raster data and other information, the encoder produces the necessary control and data blocks needed for reproducing the original graphics.
the encoder has the following primary responsibilities. - include in the data stream all the necessary information to reproduce the graphics. - insure that a data stream is labeled with the earliest possible version number that will cover the definition of all the blocks in it; this is to ensure that the largest number of decoders can process the data stream. - ensure encoding of the graphics in such a way that the decoding process is optimized. avoid redundant information as much as possible. - to the extent possible, avoid grouping graphics which might require resetting hardware parameters during the decoding process. - set to zero (off) each of the bits of each and every field designated as reserved. note that some fields in the logical screen descriptor and the image descriptor were reserved under version 87a, but are used under version 89a. 8. the decoder. the decoder is the program used to process a gif data stream. it processes the data stream sequentially, parsing the various blocks and sub-blocks, using the control information to set hardware and process parameters and interpreting the data to render the graphics. the decoder has the following primary responsibilities. - process each graphic in the data stream in sequence, without delays other than those specified in the control information. - set its hardware parameters to fit, as closely as possible, the control information contained in the data stream. 9. compliance. an encoder or a decoder is said to comply with a given version of the graphics interchange format if and only if it fully conforms with and correctly implements the definition of the standard associated with that version. an
4 encoder or a decoder may be compliant with a given version number and not compliant with some subsequent version.
10. about recommendations. each block section in this document contains an entry labeled recommendation; this section lists a set of recommendations intended to guide and organize the use of the particular blocks. such recommendations are geared towards making the functions of encoders and decoders more efficient, as well as making optimal use of the communications bandwidth. it is advised that these recommendations be followed. 11. about color tables. the gif format utilizes color tables to render raster-based graphics. a color table can have one of two different scopes: global or local. a global color table is used by all those graphics in the data stream which do not have a local color table associated with them. the scope of the global color table is the entire data stream. a local color table is always associated with the graphic that immediately follows it; the scope of a local color table is limited to that single graphic. a local color table supersedes a global color table, that is, if a data stream contains a global color table, and an image has a local color table associated with it, the decoder must save the global color table, use the local color table to render the image, and then restore the global color table. both types of color tables are optional, making it possible for a data stream to contain numerous graphics without a color table at all. for this reason, it is recommended that the decoder save the last global color table used until another global color table is encountered. in this way, a data stream which does not contain either a global color table or a local color table may be processed using the last global color table saved. if a global color table from a previous stream is used, that table becomes the global color table of the present stream. this is intended to reduce the overhead incurred by color tables. in particular, it is recommended that an encoder use only one global color table if all the images in related data streams can be rendered with the same table. if no color table is available at all, the decoder is free to use a system color table or a table of its own. in that case, the decoder may use a color table with as many colors as its hardware is able to support; it is recommended that such a table have black and white as its first two entries, so that monochrome images can be rendered adequately. the definition of the gif format allows for a data stream to contain only the header, the logical screen descriptor, a global color table and the gif trailer. such a data stream would be used to load a decoder with a global color table, in preparation for subsequent data streams without a color table at all. 12. blocks, extensions and scope. blocks can be classified into three groups : control, graphic-rendering and special purpose. control blocks, such as the header, the logical screen descriptor, the graphic control extension and the trailer, contain information used to control the process of the data stream or information used in setting hardware parameters. graphic-rendering blocks such as the image descriptor and
5 the plain text extension contain information and data used to render a graphic on the display device. special purpose blocks such as the comment extension and the application extension are neither used to control the process of the data stream nor do they contain information or data used to render a graphic on the display device. with the exception of the logical screen descriptor and the global color table, whose scope is the entire data stream, all other control blocks have a limited scope, restricted to the graphic-rendering block that follows them. special purpose blocks do not delimit the scope of any control blocks; special purpose blocks are transparent to the decoding process. graphic-rendering blocks and extensions are used as scope delimiters for control blocks and extensions. the labels used to identify labeled blocks fall into three ranges : 0x00-0x7f (0-127) are the graphic rendering blocks, excluding the trailer (0x3b); 0x80-0xf9 (128-249) are the control blocks; 0xfa-0xff (250-255) are the special purpose blocks. these ranges are defined so that decoders can handle block scope by appropriately identifying block labels, even when the block itself cannot be processed. 13. block sizes. the block size field in a block, counts the number of bytes remaining in the block, not counting the block size field itself, and not counting the block terminator, if one is to follow. blocks other than data blocks are intended to be of fixed length; the block size field is provided in order to facilitate skipping them, not to allow their size to change in the future. data blocks and sub-blocks are of variable length to accommodate the amount of data. 14. using gif as an embedded protocol. as an embedded protocol, gif may be part of larger application protocols, within which gif is used to render graphics. in such a case, the application protocol could define a block within which the gif data stream would be contained. the application program would then invoke a gif decoder upon encountering a block of type gif. this approach is recommended in favor of using application extensions, which become overhead for all other applications that do not process them. because a gif data stream must be processed in context, the application must rely on some means of identifying the gif data stream outside of the stream itself. 15. data sub-blocks. a. description. data sub-blocks are units containing data. they do not have a label, these blocks are processed in the context of control blocks, wherever data blocks are specified in the format. the first byte of the data sub-block indicates the number of data bytes to follow. a data sub-block may contain from 0 to 255 data bytes. the size of the block does not account for the size byte itself, therefore, the empty sub-block is one whose size field contains 0x00. b. required version.
87a.
6 c. syntax.
0 1 2 3
up to
255
7 6 5 4 3 2 1 0 +---------------+ | | +---------------+ | | +-+ | | +-+ | | +-+ | | +-+ | | +. . . . -+ | | +-+ | | +-+ | | +---------------+
field name
type
block size
byte
data values
byte
i) block size - number of bytes in the data sub-block; the size must be within 0 and 255 bytes, inclusive. ii) data values - any 8-bit value. there must be exactly as many data values as specified by the block size field. d. extensions and scope. this type of block always occurs as part of a larger unit. it does not have a scope of itself. e. recommendation. none. 16. block terminator. a. description. this zero-length data sub-block is used to terminate a sequence of data sub-blocks. it contains a single byte in the position of the block size field and does not contain data. b. required version. c. syntax.
87a.
7 6 5 4 3 2 1 0 +---------------+ | | +---------------+
0
field name
type
block size
byte
i) block size - number of bytes in the data sub-block; this field contains the fixed value 0x00. ii) data values - this block does not contain any data.
7 d. extensions and scope. this block terminates the immediately preceding sequence of data sub-blocks. this block cannot be modified by any extension. e. recommendation. none. 17. header. a. description. the header identifies the gif data stream in context. the signature field marks the beginning of the data stream, and the version field identifies the set of capabilities required of a decoder to fully process the data stream. this block is required; exactly one header must be present per data stream. b. required version. not applicable. this block is not subject to a version number. this block must appear at the beginning of every data stream. c. syntax.
0 1 2 3 4 5
7 6 5 4 3 2 1 0 +---------------+ | | +-+ | | +-+ | | +---------------+ | | +-+ | | +-+ | | +---------------+
field name
type
signature
3 bytes
version
3 bytes
i) signature - identifies the gif data stream. this field contains the fixed value 'gif'. ii) version - version number used to format the data stream. identifies the minimum set of capabilities necessary to a decoder to fully process the contents of the data stream. version numbers as of 10 july 1990 :
"87a" - may 1987 "89a" - july 1989
version numbers are ordered numerically increasing on the first two digits starting with 87 (87,88,...,99,00,...,85,86) and alphabetically increasing on the third character (a,...,z). iii) extensions and scope. the scope of this block is the entire data stream. this block cannot be modified by any extension.
8 d. recommendations. i) signature - this field identifies the beginning of the gif data stream; it is not intended to provide a unique signature for the identification of the data. it is recommended that the gif data stream be identified externally by the application. (refer to appendix g for on-line identification of the gif data stream.) ii) version - encoder : an encoder should use the earliest possible version number that defines all the blocks used in the data stream. when two or more data streams are combined, the latest of the individual version numbers should be used for the resulting data stream. decoder : a decoder should attempt to process the data stream to the best of its ability; if it encounters a version number which it is not capable of processing fully, it should nevertheless, attempt to process the data stream to the best of its ability, perhaps after warning the user that the data may be incomplete. 18. logical screen descriptor. a. description. the logical screen descriptor contains the parameters necessary to define the area of the display device within which the images will be rendered. the coordinates in this block are given with respect to the top-left corner of the virtual screen; they do not necessarily refer to absolute coordinates on the display device. this implies that they could refer to window coordinates in a window-based environment or printer coordinates when a printer is used.
this block is required; exactly one logical screen descriptor must be present per data stream. b. required version. not applicable. this block is not subject to a version number. this block must appear immediately after the header. c. syntax.
0 1 2 3 4 5 6
7 6 5 4 3 2 1 0 +---------------+ | | +-+ | | +---------------+ | | +-+ | | +---------------+ | | | | | +---------------+ | | +---------------+ | | +---------------+
field name
type
logical screen width
unsigned
logical screen height
unsigned
<packed fields>
see below
background color index
byte
pixel aspect ratio
byte
9 <packed fields>
=
global color table flag color resolution sort flag size of global color table
1 3 1 3
bit bits bit bits
i) logical screen width - width, in pixels, of the logical screen where the images will be rendered in the displaying device. ii) logical screen height - height, in pixels, of the logical screen where the images will be rendered in the displaying device. iii) global color table flag - flag indicating the presence of a global color table; if the flag is set, the global color table will immediately follow the logical screen descriptor. this flag also selects the interpretation of the background color index; if the flag is set, the value of the background color index field should be used as the table index of the background color. (this field is the most significant bit of the byte.) values :
0 -
no global color table follows, the background color index field is meaningless.
1 -
a global color table will immediately follow, the background color index field is meaningful.
iv) color resolution - number of bits per primary color available to the original image, minus 1. this value represents the size of the entire palette from which the colors in the graphic were selected, not the number of colors actually used in the graphic. for example, if the value in this field is 3, then the palette of the original image had 4 bits per primary color available to create the image. this value should be set to indicate the richness of the original palette, even if not every color from the whole palette is available on the source machine. v) sort flag - indicates whether the global color table is sorted. if the flag is set, the global color table is sorted, in order of decreasing importance. typically, the order would be decreasing frequency, with most frequent color first. this assists a decoder, with fewer available colors, in choosing the best subset of colors; the decoder may use an initial segment of the table to render the graphic. values :
0 1 -
not ordered. ordered by decreasing importance, most important color first.
vi) size of global color table - if the global color table flag is set to 1, the value in this field is used to calculate the number of bytes contained in the global color table. to determine that actual size of the color table, raise 2 to [the value of the field + 1]. even if there is no global color table specified, set this field according to the above formula so that decoders can choose the best graphics mode to display the stream in. (this field is made up of the 3 least significant bits of the byte.) vii) background color index - index into the global color table for
10 the background color. the background color is the color used for those pixels on the screen that are not covered by an image. if the global color table flag is set to (zero), this field should be zero and should be ignored. viii) pixel aspect ratio - factor used to compute an approximation of the aspect ratio of the pixel in the original image. if the value of the field is not 0, this approximation of the aspect ratio is computed based on the formula: aspect ratio = (pixel aspect ratio + 15) / 64 the pixel aspect ratio is defined to be the quotient of the pixel's
width over its height. the value range in this field allows specification of the widest pixel of 4:1 to the tallest pixel of 1:4 in increments of 1/64th. values :
0 1..255 -
no aspect ratio information is given. value used in the computation.
d. extensions and scope. the scope of this block is the entire data stream. this block cannot be modified by any extension. e. recommendations. none. 19. global color table. a. description. this block contains a color table, which is a sequence of bytes representing red-green-blue color triplets. the global color table is used by images without a local color table and by plain text extensions. its presence is marked by the global color table flag being set to 1 in the logical screen descriptor; if present, it immediately follows the logical screen descriptor and contains a number of bytes equal to 3 x 2^(size of global color table+1). this block is optional; at most one global color table may be present per data stream. b. required version.
87a
11 c. syntax.
0
7 6 5 4 3 2 1 0 +===============+ | |
field name
type
red 0
byte
1 2 3
up to
767
+-+ | | +-+ | | +-+ | | +-+ | | +-+ | | +. . . . -+ | | +-+ | | +-+ | | +===============+
green 0
byte
blue 0
byte
red 1
byte
green 1
byte
... green 255
byte
blue 255
byte
d. extensions and scope. the scope of this block is the entire data stream. this block cannot be modified by any extension. e. recommendation. none. 20. image descriptor. a. description. each image in the data stream is composed of an image descriptor, an optional local color table, and the image data. each image must fit within the boundaries of the logical screen, as defined in the logical screen descriptor. the image descriptor contains the parameters necessary to process a table based image. the coordinates given in this block refer to coordinates within the logical screen, and are given in pixels. this block is a graphic-rendering block, optionally preceded by one or more control blocks such as the graphic control extension, and may be optionally followed by a local color table; the image descriptor is always followed by the image data. this block is required for an image. exactly one image descriptor must be present per image in the data stream. an unlimited number of images may be present per data stream. b. required version.
87a.
12 c. syntax.
0 1 2 3 4 5 6 7 8 9
7 6 5 4 3 2 1 0 +---------------+ | | +---------------+ | | +-+ | | +---------------+ | | +-+ | | +---------------+ | | +-+ | | +---------------+ | | +-+ | | +---------------+ | | | | | | +---------------+
field name
type
image separator
byte
image left position
unsigned
image top position
unsigned
image width
unsigned
image height
unsigned
<packed fields>
see below
<packed fields>
local color table flag interlace flag sort flag reserved size of local color table
1 1 1 2 3
=
bit bit bit bits bits
i) image separator - identifies the beginning of an image descriptor. this field contains the fixed value 0x2c. ii) image left position - column number, in pixels, of the left edge of the image, with respect to the left edge of the logical screen. leftmost column of the logical screen is 0. iii) image top position - row number, in pixels, of the top edge of the image with respect to the top edge of the logical screen. top row of the logical screen is 0. iv) image width - width of the image in pixels. v) image height - height of the image in pixels. vi) local color table flag - indicates the presence of a local color table immediately following this image descriptor. (this field is the most significant bit of the byte.) values :
0 1 -
local color table is not present. use global color table if available. local color table present, and to follow immediately after this image descriptor.
13 vii) interlace flag - indicates if the image is interlaced. an image is interlaced in a four-pass interlace pattern; see appendix e for details. values :
0 - image is not interlaced. 1 - image is interlaced.
viii) sort flag - indicates whether the local color table is sorted. if the flag is set, the local color table is sorted, in order of decreasing importance. typically, the order would be decreasing frequency, with most frequent color first. this assists a decoder, with fewer available colors, in choosing the best subset of colors; the decoder may use an initial segment of the table to render the graphic. values :
0 1 -
not ordered. ordered by decreasing importance, most important color first.
ix) size of local color table - if the local color table flag is set to 1, the value in this field is used to calculate the number of bytes contained in the local color table. to determine that actual size of the color table, raise 2 to the value of the field + 1. this value should be 0 if there is no local color table specified. (this field is made up of the 3 least significant bits of the byte.) d. extensions and scope. the scope of this block is the table-based image data block that follows it. this block may be modified by the graphic control extension. e. recommendation. none. 21. local color table. a. description. this block contains a color table, which is a sequence of bytes representing red-green-blue color triplets. the local color table is used by the image that immediately follows. its presence is marked by the local color table flag being set to 1 in the image descriptor; if present, the local color table immediately follows the image descriptor and contains a number of bytes equal to 3x2^(size of local color table+1). if present, this color table temporarily becomes the active color table and the following image should be processed using it. this block is optional; at most one local color table may be present per image descriptor and its scope is the single image associated with the image
descriptor that precedes it. b. required version.
87a.
14 c. syntax.
0 1 2 3
up to
767
7 6 5 4 3 2 1 0 +===============+ | | +-+ | | +-+ | | +-+ | | +-+ | | +-+ | | +. . . . -+ | | +-+ | | +-+ | | +===============+
field name
type
red 0
byte
green 0
byte
blue 0
byte
red 1
byte
green 1
byte
... green 255
byte
blue 255
byte
d. extensions and scope. the scope of this block is the table-based image data block that immediately follows it. this block cannot be modified by any extension. e. recommendations. none. 22. table based image data. a. description. the image data for a table based image consists of a sequence of sub-blocks, of size at most 255 bytes each, containing an index into the active color table, for each pixel in the image. pixel indices are in order of left to right and from top to bottom. each index must be within the range of the size of the active color table, starting at 0. the sequence of indices is encoded using the lzw algorithm with
variable-length code, as described in appendix f b. required version.
87a.
c. syntax. the image data format is as follows: 7 6 5 4 3 2 1 0 +---------------+ | | +---------------+ +===============+ | | / / | | +===============+
field name
type
lzw minimum code size
byte
image data
data sub-blocks
15 i) lzw minimum code size. this byte determines the initial number of bits used for lzw codes in the image data, as described in appendix f. d. extensions and scope. this block has no scope, it contains raster data. extensions intended to modify a table-based image must appear before the corresponding image descriptor. e. recommendations. none. 23. graphic control extension. a. description. the graphic control extension contains parameters used when processing a graphic rendering block. the scope of this extension is the first graphic rendering block to follow. the extension contains only one data sub-block. this block is optional; at most one graphic control extension may precede a graphic rendering block. this is the only limit to the number of graphic control extensions that may be contained in a data stream. b. required version.
89a.
c. syntax.
0 1
7 6 5 4 3 2 1 0 +---------------+ | | +---------------+ | |
field name
type
extension introducer
byte
graphic control label
byte
+---------------+ 0 1 2 3 4
0
+---------------+ | | +---------------+ | | | | | +---------------+ | | +-+ | | +---------------+ | | +---------------+ +---------------+ | | +---------------+ <packed fields>
=
block size
byte
<packed fields>
see below
delay time
unsigned
transparent color index
byte
block terminator
byte
reserved disposal method user input flag transparent color flag
3 3 1 1
bits bits bit bit
i) extension introducer - identifies the beginning of an extension
16 block. this field contains the fixed value 0x21. ii) graphic control label - identifies the current block as a graphic control extension. this field contains the fixed value 0xf9. iii) block size - number of bytes in the block, after the block size field and up to but not including the block terminator. this field contains the fixed value 4. iv) disposal method - indicates the way in which the graphic is to be treated after being displayed. values :
0 1 2 3 4-7 -
no disposal specified. the decoder is not required to take any action. do not dispose. the graphic is to be left in place. restore to background color. the area used by the graphic must be restored to the background color. restore to previous. the decoder is required to restore the area overwritten by the graphic with what was there prior to rendering the graphic. to be defined.
v) user input flag - indicates whether or not user input is expected before continuing. if the flag is set, processing will continue when user input is entered. the nature of the user input is determined by the application (carriage return, mouse button click, etc.). values :
0 1 -
user input is not expected. user input is expected.
when a delay time is used and the user input flag is set, processing will continue when user input is received or when the delay time expires, whichever occurs first. vi) transparency flag - indicates whether a transparency index is given in the transparent index field. (this field is the least significant bit of the byte.) values :
0 1 -
transparent index is not given. transparent index is given.
vii) delay time - if not 0, this field specifies the number of hundredths (1/100) of a second to wait before continuing with the processing of the data stream. the clock starts ticking immediately after the graphic is rendered. this field may be used in conjunction with the user input flag field. viii) transparency index - the transparency index is such that when encountered, the corresponding pixel of the display device is not modified and processing goes on to the next pixel. the index is present if and only if the transparency flag is set to 1. ix) block terminator - this zero-length data block marks the end of
17 the graphic control extension. d. extensions and scope. the scope of this extension is the graphic rendering block that follows it; it is possible for other extensions to be present between this block and its target. this block can modify the image descriptor block and the plain text extension. e. recommendations. i) disposal method - the mode restore to previous is intended to be used in small sections of the graphic; the use of this mode imposes severe demands on the decoder to store the section of the graphic that needs to be saved. for this reason, this mode should be used sparingly. this mode is not intended to save an entire graphic or large areas of a graphic; when this is the case, the encoder should make every attempt to make the sections of the graphic to be
restored be separate graphics in the data stream. in the case where a decoder is not capable of saving an area of a graphic marked as restore to previous, it is recommended that a decoder restore to the background color. ii) user input flag - when the flag is set, indicating that user input is expected, the decoder may sound the bell (0x07) to alert the user that input is being expected. in the absence of a specified delay time, the decoder should wait for user input indefinitely. it is recommended that the encoder not set the user input flag without a delay time specified. 24. comment extension. a. description. the comment extension contains textual information which is not part of the actual graphics in the gif data stream. it is suitable for including comments about the graphics, credits, descriptions or any other type of non-control and non-graphic data. the comment extension may be ignored by the decoder, or it may be saved for later processing; under no circumstances should a comment extension disrupt or interfere with the processing of the data stream. this block is optional; any number of them may appear in the data stream. b. required version.
89a.
18 c. syntax.
0 1
7 6 5 4 3 2 1 0 +---------------+ | | +---------------+ | |
field name
type
extension introducer
byte
comment label
byte
+---------------+
n
+===============+ | | | | | | +===============+
comment data
data sub-blocks
0
+---------------+ | | +---------------+
block terminator
byte
i) extension introducer - identifies the beginning of an extension block. this field contains the fixed value 0x21. ii) comment label - identifies the block as a comment extension. this field contains the fixed value 0xfe. iii) comment data - sequence of sub-blocks, each of size at most 255 bytes and at least 1 byte, with the size in a byte preceding the data. the end of the sequence is marked by the block terminator. iv) block terminator - this zero-length data block marks the end of the comment extension. d. extensions and scope. this block does not have scope. this block cannot be modified by any extension. e. recommendations. i) data - this block is intended for humans. it should contain text using the 7-bit ascii character set. this block should not be used to store control information for custom processing. ii) position - this block may appear at any point in the data stream at which a block can begin; however, it is recommended that comment extensions do not interfere with control or data blocks; they should be located at the beginning or at the end of the data stream to the extent possible. 25. plain text extension. a. description. the plain text extension contains textual data and the parameters necessary to render that data as a graphic, in a simple form. the textual data will be encoded with the 7-bit printable ascii characters. text data are rendered using a grid of character cells
19
defined by the parameters in the block fields. each character is rendered in an individual cell. the textual data in this block is to be rendered as mono-spaced characters, one character per cell, with a best fitting font and size. for further information, see the section on recommendations below. the data characters are taken sequentially from the data portion of the block and rendered within a cell, starting with the upper left cell in the grid and proceeding from left to right and from top to bottom. text data is rendered until the end of data is reached or the character grid is filled. the character grid contains an integral number of cells; in the case that the cell dimensions do not allow for an integral number, fractional cells must be discarded; an encoder must be careful to specify the grid dimensions accurately so that this does not happen. this block requires a global color table to be available; the colors used by this block reference the global color table in the stream if there is one, or the global color table from a previous stream, if one was saved. this block is a graphic rendering block, therefore it may be modified by a graphic control extension. this block is optional; any number of them may appear in the data stream. b. required version.
89a.
20 c. syntax. field name
type
extension introducer
byte
plain text label
byte
block size
byte
text grid left position
unsigned
text grid top position
unsigned
text grid width
unsigned
text grid height
unsigned
character cell width
byte
character cell height
byte
text foreground color index
byte
text background color index
byte
n
+===============+ | | | | | | +===============+
plain text data
data sub-blocks
0
+---------------+ | | +---------------+
block terminator
byte
0 1
0 1 2 3 4 5 6 7 8 9 10 11 12
7 6 5 4 3 2 1 0 +---------------+ | | +---------------+ | | +---------------+ +---------------+ | | +---------------+ | | +-+ | | +---------------+ | | +-+ | | +---------------+ | | +-+ | | +---------------+ | | +-+ | | +---------------+ | | +---------------+ | | +---------------+ | | +---------------+ | | +---------------+
i) extension introducer - identifies the beginning of an extension block. this field contains the fixed value 0x21.
ii) plain text label - identifies the current block as a plain text extension. this field contains the fixed value 0x01. iii) block size - number of bytes in the extension, after the block size field and up to but not including the beginning of the data portion. this field contains the fixed value 12.
21 iv) text grid left position - column number, in pixels, of the left edge of the text grid, with respect to the left edge of the logical screen. v) text grid top position - row number, in pixels, of the top edge of the text grid, with respect to the top edge of the logical screen. vi) image grid width - width of the text grid in pixels. vii) image grid height - height of the text grid in pixels. viii) character cell width - width, in pixels, of each cell in the grid. ix) character cell height - height, in pixels, of each cell in the grid. x) text foreground color index - index into the global color table to be used to render the text foreground. xi) text background color index - index into the global color table to be used to render the text background. xii) plain text data - sequence of sub-blocks, each of size at most 255 bytes and at least 1 byte, with the size in a byte preceding the data. the end of the sequence is marked by the block terminator. xiii) block terminator - this zero-length data block marks the end of the plain text data blocks. d. extensions and scope. the scope of this block is the plain text data block contained in it. this block may be modified by the graphic control extension. e. recommendations. the data in the plain text extension is assumed to be preformatted. the selection of font and size is left to the discretion of the decoder. if characters less than 0x20 or greater than 0xf7 are encountered, it is recommended that the decoder display a space character (0x20). the encoder should use grid and cell dimensions such that an
integral number of cells fit in the grid both horizontally as well as vertically. for broadest compatibility, character cell dimensions should be around 8x8 or 8x16 (width x height); consider an image for unusual sized text. 26. application extension. a. description. the application extension contains application-specific information; it conforms with the extension block syntax, as described below, and its block label is 0xff. b. required version.
89a.
22 c. syntax.
0 1
0 1 2 3 4 5 6 7 8 9 10 11
7 6 5 4 3 2 1 0 +---------------+ | | +---------------+ | | +---------------+ +---------------+ | | +---------------+ | | +-+ | | +-+ | | +-+ | | +-+ | | +-+ | | +-+ | | +-+ | | +---------------+ | | +-+ | | +-+ | |
field name
type
extension introducer
byte
extension label
byte
block size
byte
application identifier
8 bytes
appl. authentication code
3 bytes
+---------------+ +===============+ | | | | | | | | +===============+ 0
+---------------+ | | +---------------+
application data
data sub-blocks
block terminator
byte
i) extension introducer - defines this block as an extension. this field contains the fixed value 0x21. ii) application extension label - identifies the block as an application extension. this field contains the fixed value 0xff. iii) block size - number of bytes in this extension block, following the block size field, up to but not including the beginning of the application data. this field contains the fixed value 11.
23 iv) application identifier - sequence of eight printable ascii characters used to identify the application owning the application extension. v) application authentication code - sequence of three bytes used to authenticate the application identifier. an application program may use an algorithm to compute a binary code that uniquely identifies it as the application owning the application extension. d. extensions and scope. this block does not have scope. this block cannot be modified by any extension. e. recommendation. none. 27. trailer. a. description. this block is a single-field block indicating the end of the gif data stream. it contains the fixed value 0x3b. b. required version. c. syntax.
87a.
0
7 6 5 4 3 2 1 0 +---------------+ | | +---------------+
field name
type
gif trailer
byte
d. extensions and scope. this block does not have scope, it terminates the gif data stream. this block may not be modified by any extension. e. recommendations. none.
24 appendix a. quick reference table. block name application extension comment extension global color table graphic control extension header image descriptor local color table logical screen descriptor plain text extension trailer
required opt. (*) opt. (*) opt. (1) opt. (*) req. (1) opt. (*) opt. (*) req. (1) opt. (*) req. (1)
label 0xff (255) 0xfe (254) none 0xf9 (249) none 0x2c (044) none none 0x01 (001) 0x3b (059)
ext. yes yes no yes no no no no yes no
vers. 89a 89a 87a 89a n/a 87a (89a) 87a 87a (89a) 89a 87a
unlabeled blocks header
req. (1)
none
no
n/a
logical screen descriptor global color table local color table
req. (1) opt. (1) opt. (*)
none none none
no no no
87a (89a) 87a 87a
graphic-rendering blocks plain text extension image descriptor
opt. (*) opt. (*)
0x01 (001) 0x2c (044)
yes no
89a 87a (89a)
control blocks graphic control extension
opt. (*)
0xf9 (249)
yes
89a
special purpose blocks trailer comment extension application extension
req. (1) opt. (*) opt. (*)
0x3b (059) 0xfe (254) 0xff (255)
no yes yes
87a 89a 89a
legend:
(1) (*) (+)
if present, at most one occurrence zero or more occurrences one or more occurrences
notes : the header is not subject to version numbers. (89a) the logical screen descriptor and the image descriptor retained their syntax from version 87a to version 89a, but some fields reserved under version 87a are used under version 89a.
25 appendix b. gif grammar. a grammar is a form of notation to represent the sequence in which certain objects form larger objects. a grammar is also used to represent the number of objects that can occur at a given position. the grammar given here represents the sequence of blocks that form the gif data stream. a grammar is given by listing its rules. each rule consists of the left-hand side, followed by some form of equals sign, followed by the right-hand side. in a rule, the
right-hand side describes how the left-hand side is defined. the right-hand side consists of a sequence of entities, with the possible presence of special symbols. the following legend defines the symbols used in this grammar for gif. legend:
<> ::= * + | []
grammar word defines symbol zero or more occurrences one or more occurrences alternate element optional element
example:
::= header * trailer this rule defines the entity as follows. it must begin with a header. the header is followed by an entity called logical screen, which is defined below by another rule. the logical screen is followed by the entity data, which is also defined below by another rule. finally, the entity data is followed by the trailer. since there is no rule defining the header or the trailer, this means that these blocks are defined in the document. the entity data has a special symbol (*) following it which means that, at this position, the entity data may be repeated any number of times, including 0 times. for further reading on this subject, refer to a standard text on programming languages. the grammar. ::=
header * trailer
::=
logical screen descriptor [global color table]
::=
| <special-purpose block>
::=
[graphic control extension]
::= ::=
| plain text extension
image descriptor [local color table] image data
<special-purpose block> ::=
application extension comment extension
|
26 note : the grammar indicates that it is possible for a gif data stream to
contain the header, the logical screen descriptor, a global color table and the gif trailer. this special case is used to load a gif decoder with a global color table, in preparation for subsequent data streams without color tables at all.
27 appendix c. glossary. active color table - color table used to render the next graphic. if the next graphic is an image which has a local color table associated with it, the active color table becomes the local color table associated with that image. if the next graphic is an image without a local color table, or a plain text extension, the active color table is the global color table associated with the data stream, if there is one; if there is no global color table in the data stream, the active color table is a color table saved from a previous data stream, or one supplied by the decoder. block - collection of bytes forming a protocol unit. in general, the term includes labeled and unlabeled blocks, as well as extensions. data stream - the gif data stream is composed of blocks and sub-blocks representing images and graphics, together with control information to render them on a display device. all control and data blocks in the data stream must follow the header and must precede the trailer. decoder - a program capable of processing a gif data stream to render the images and graphics contained in it. encoder - a program capable of capturing and formatting image and graphic raster data, following the definitions of the graphics interchange format. extension - a protocol block labeled by the extension introducer 0x21. extension introducer - label (0x21) defining an extension. graphic - data which can be rendered on the screen by virtue of some algorithm. the term graphic is more general than the term image; in addition to images, the term graphic also includes data such as text, which is rendered using character bit-maps. image - data representing a picture or a drawing; an image is represented by an array of pixels called the raster of the image. raster - array of pixel values representing an image.
28 appendix d. conventions. animation - the graphics interchange format is not intended as a platform for animation, even though it can be done in a limited way. byte ordering - unless otherwise stated, multi-byte numeric fields are ordered with the least significant byte first. color indices - color indices always refer to the active color table, either the global color table or the local color table. color order - unless otherwise stated, all triple-component rgb color values are specified in red-green-blue order. color tables - both color tables, the global and the local, are optional; if present, the global color table is to be used with every image in the data stream for which a local color table is not given; if present, a local color table overrides the global color table. however, if neither color table is present, the application program is free to use an arbitrary color table. if the graphics in several data streams are related and all use the same color table, an encoder could place the color table as the global color table in the first data stream and leave subsequent data streams without a global color table or any local color tables; in this way, the overhead for the table is eliminated. it is recommended that the decoder save the previous global color table to be used with the data stream that follows, in case it does not contain either a global color table or any local color tables. in general, this allows the application program to use past color tables, significantly reducing transmission overhead. extension blocks - extensions are defined using the extension introducer code to mark the beginning of the block, followed by a block label, identifying the type of extension. extension codes are numbers in the range from 0x00 to 0xff, inclusive. special purpose extensions are transparent to the decoder and may be omitted when transmitting the data stream on-line. the gif capabilities dialogue makes the provision for the receiver to request the transmission of all blocks; the default state in this regard is no transmission of special purpose blocks. reserved fields - all reserved fields are expected to have each bit set to zero (off).
29 appendix e. interlaced images. the rows of an interlaced images are arranged in the following order: group group group group
1 2 3 4
: : : :
every every every every
8th. 8th. 4th. 2nd.
row, row, row, row,
starting starting starting starting
with with with with
row row row row
0. 4. 2. 1.
(pass (pass (pass (pass
1) 2) 3) 4)
the following example illustrates how the rows of an interlaced image are ordered. row number 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
interlace pass 1 3
4 4
2 3
4 4
1 3
4 4
2 3
4 4
1 3
4 4
30 appendix f. variable-length-code lzw compression. the variable-length-code lzw compression is a variation of the lempel-ziv compression algorithm in which variable-length codes are used to replace patterns detected in the original data. the algorithm uses a code or translation table constructed from the patterns encountered in the original data; each new pattern is entered into the table and its index is used to replace it in the compressed stream. the compressor takes the data from the input stream and builds a code or translation table with the patterns as it encounters them; each new pattern is entered into the code table and its index is added to the output stream; when a pattern is encountered which had been detected since the last code table refresh, its index from the code table is put on the output stream, thus achieving the data compression. the expander takes input from the compressed data stream and builds the code or translation table from it; as the compressed data stream is processed, codes are used to index into the code table and the corresponding data is put on the decompressed output stream, thus achieving data decompression. the details of the algorithm are explained below. the variable-length-code aspect of the algorithm is based on an initial code size (lzw-initial code size), which specifies the initial number of bits used for the compression codes. when the number of patterns detected by the compressor in the input stream exceeds the number of patterns encodable with the current number of bits, the number of bits per lzw code is increased by one. the raster data stream that represents the actual output image can be
represented as: 7 6 5 4 3 2 1 0 +---------------+ | lzw code size | +---------------+ +---------------+ ----+ | block size | | +---------------+ | | | +-- repeated as many | data bytes | | times as necessary. | | | +---------------+ ----+ . . .
. . . ------- the code that terminates the lzw compressed data must appear before block terminator. +---------------+ |0 0 0 0 0 0 0 0| block terminator +---------------+ the conversion of the image from a series of pixel values to a transmitted or stored character stream involves several steps. in brief these steps are: 1. establish the code size - define the number of bits needed to represent the actual data. 2. compress the data - compress the series of image pixels to a series of
31 compression codes. 3. build a series of bytes - take the set of compression codes and convert to a string of 8-bit bytes. 4. package the bytes - package sets of bytes into blocks preceded by character counts and output. establish code size the first byte of the compressed data stream is a value indicating the minimum number of bits required to represent the set of actual pixel values. normally this will be the same as the number of color bits. because of some algorithmic constraints however, black & white images which have one color bit must be indicated as having a code size of 2. this code size value also implies that the compression codes must start out one bit longer. compression
the lzw algorithm converts a series of data values into a series of codes which may be raw values or a code designating a series of values. using text characters as an analogy, the output code consists of a character or a code representing a string of characters. the lzw algorithm used in gif matches algorithmically with the standard lzw algorithm with the following differences: 1. a special clear code is defined which resets all compression/decompression parameters and tables to a start-up state. the value of this code is 2**. for example if the code size indicated was 4 (image was 4 bits/pixel) the clear code value would be 16 (10000 binary). the clear code can appear at any point in the image data stream and therefore requires the lzw algorithm to process succeeding codes as if a new data stream was starting. encoders should output a clear code as the first code of each image data stream. 2. an end of information code is defined that explicitly indicates the end of the image data stream. lzw processing terminates when this code is encountered. it must be the last code output by the encoder for an image. the value of this code is +1. 3. the first available compression code value is +2. 4. the output codes are of variable length, starting at +1 bits per code, up to 12 bits per code. this defines a maximum code value of 4095 (0xfff). whenever the lzw code value would exceed the current code length, the code length is increased by one. the packing/unpacking of these codes must then be altered to reflect the new code length. build 8-bit bytes because the lzw compression used for gif creates a series of variable length codes, of between 3 and 12 bits each, these codes must be reformed into a series of 8-bit bytes that will be the characters actually stored or transmitted. this provides additional compression of the image. the codes are formed into a stream of bits as if they were packed right to left and then
32 picked off 8 bits at a time to be output. assuming a character array of 8 bits per character and using 5 bit codes to be packed, an example layout would be similar to:
0 1
+---------------+ | | +---------------+ | | +---------------+
bbbaaaaa dcccccbb
2 3 4
n
| | +---------------+ | | +---------------+ | | +---------------+ . . . +---------------+ | | +---------------+
eeeedddd ggfffffe hhhhhggg
note that the physical packing arrangement will change as the number of bits per compression code change but the concept remains the same. package the bytes once the bytes have been created, they are grouped into blocks for output by preceding each block of 0 to 255 bytes with a character count byte. a block with a zero byte count terminates the raster data stream for a given image. these blocks are what are actually output for the gif image. this block format has the side effect of allowing a decoding program the ability to read past the actual image data if necessary by reading block counts and then skipping over the data.
further reading [1] ziv, j. and lempel, a. : "a universal algorithm for sequential data compression", ieee transactions on information theory, may 1977. [2] welch, t. : "a technique for high-performance data compression", computer, june 1984. [3] nelson, m.r. : "lzw data compression", dr. dobb's journal, october 1989.
33 appendix g. on-line capabilities dialogue.
note : this section is currently (10 july 1990) under revision; the information provided here should be used as general guidelines. code written based on this information should be designed in a flexible way to accommodate any changes resulting from the revisions. the following sequences are defined for use in mediating control between a gif sender and gif receiver over an interactive communications line. these sequences do not apply to applications that involve downloading of static gif files and are not considered part of a gif file. gif capabilities enquiry the gif capabilities enquiry sequence is issued from a host and requests an interactive gif decoder to return a response message that defines the graphics parameters for the decoder. this involves returning information about available screen sizes, number of bits/color supported and the amount of color detail supported. the escape sequence for the gif capabilities enquiry is defined as: esc[>0g
0x1b 0x5b 0x3e 0x30 0x67
gif capabilities response the gif capabilities response message is returned by an interactive gif decoder and defines the decoder's display capabilities for all graphics modes that are supported by the software. note that this can also include graphics printers as well as a monitor screen. the general format of this message is: #version;protocol{;dev, width, height, color-bits, color-res}... '#' version protocol='0' protocol='1' dev = '0' dev = '1' width height color-bits color-res
gif capabilities response identifier character. gif format version number; initially '87a'. no end-to-end protocol supported by decoder transfer as direct 8-bit data stream. can use cis b+ error correction protocol to transfer gif data interactively from the host directly to the display. screen parameter set follows. printer parameter set follows. maximum supported display width in pixels. maximum supported display height in pixels. number of bits per pixel supported. the number of supported colors is therefore 2**color-bits. number of bits per color component supported in the hardware color palette. if color-res is '0' then no hardware palette table is available.
note that all values in the gif capabilities response are returned as ascii decimal numbers and the message is terminated by a carriage return character. the following gif capabilities response message describes three standard ibm pc enhanced graphics adapter configurations with no printer; the gif data stream
34 can be processed within an error correcting protocol: #87a;1;0,320,200,4,0;0,640,200,2,2;0,640,350,4,2 enter gif graphics mode two sequences are currently defined to invoke an interactive gif decoder into action. the only difference between them is that different output media are selected. these sequences are: esc[>1g
display gif image on screen 0x1b 0x5b 0x3e 0x31 0x67
esc[>2g display image directly to an attached graphics printer. the image may optionally be displayed on the screen as well. 0x1b 0x5b 0x3e 0x32 0x67 note that the 'g' character terminating each sequence is in lowercase. interactive environment the assumed environment for the transmission of gif image data from an interactive application is a full 8-bit data stream from host to micro. all 256 character codes must be transferrable. the establishing of an 8-bit data path for communications will normally be taken care of by the host application programs. it is however up to the receiving communications programs supporting gif to be able to receive and pass on all 256 8-bit codes to the gif decoder software. .