gif graphics interchange format


a standard defining a mechanism
for the storage and transmission
of raster-based graphics information
june 15, 1987
(c) compuserve incorporated, 1987
all rights reserved
while this document is copyrighted, the information
contained within is made available for use in computer
software without royalties, or licensing restrictions.
gif and 'graphics interchange format' are trademarks of
compuserve, incorporated.
an h&r block company
5000 arlington centre blvd.
columbus, ohio 43220
(614) 457-8600
page 2
graphics interchange format (gif) specification
table of contents
introduction . . . . . . . . . . . . . . . . . page 3
general file format . . . . . . . . . . . . . page 3
gif signature . . . . . . . . . . . . . . . . page 4
screen descriptor . . . . . . . . . . . . . . page 4
global color map . . . . . . . . . . . . . . . page 5
image descriptor . . . . . . . . . . . . . . . page 6
local color map . . . . . . . . . . . . . . . page 7
raster data . . . . . . . . . . . . . . . . . page 7
gif terminator . . . . . . . . . . . . . . . . page 8
gif extension blocks . . . . . . . . . . . . . page 8
appendix a - glossary . . . . . . . . . . . . page 9
appendix b - interactive sequences . . . . . . page 10
appendix c - image packaging & compression . . page 12
appendix d - multiple image processing . . . . page 15
graphics interchange format (gif) page 3
specification
introduction
'gif' (tm) is compuserve's standard for defining generalized color
raster images. this 'graphics interchange format' (tm) allows
high-quality, high-resolution graphics to be displayed on a variety of
graphics hardware and is intended as an exchange and display mechanism
for graphics images. the image format described in this document is
designed to support current and future image technology and will in
addition serve as a basis for future compuserve graphics products.
the main focus of this document is to provide the technical
information necessary for a programmer to implement gif encoders and
decoders. as such, some assumptions are made as to terminology relavent
to graphics and programming in general.
the first section of this document describes the gif data format
and its components and applies to all gif decoders, either as standalone
programs or as part of a communications package. appendix b is a
section relavent to decoders that are part of a communications software
package and describes the protocol requirements for entering and exiting
gif mode, and responding to host interrogations. a glossary in appendix
a defines some of the terminology used in this document. appendix c
gives a detailed explanation of how the graphics image itself is
packaged as a series of data bytes.
graphics interchange format data definition
general file format
+-----------------------+
| +-------------------+ |
| | gif signature | |
| +-------------------+ |
| +-------------------+ |
| | screen descriptor | |
| +-------------------+ |
| +-------------------+ |
| | global color map | |
| +-------------------+ |
. . . . . .
| +-------------------+ | ---+
| | image descriptor | | |
| +-------------------+ | |
| +-------------------+ | |
| | local color map | | |- repeated 1 to n times
| +-------------------+ | |
| +-------------------+ | |
| | raster data | | |
| +-------------------+ | ---+
. . . . . .
|- gif terminator -|
+-----------------------+
graphics interchange format (gif) page 4
specification
gif signature
the following gif signature identifies the data following as a
valid gif image stream. it consists of the following six characters:
g i f 8 7 a
the last three characters '87a' may be viewed as a version number
for this particular gif definition and will be used in general as a
reference in documents regarding gif that address any version
dependencies.
screen descriptor
the screen descriptor describes the overall parameters for all gif
images following. it defines the overall dimensions of the image space
or logical screen required, the existance of color mapping information,
background../jpg/di1.JPGn color, and color depth information. this information
is stored in a series of 8-bit bytes as described below.
bits
7 6 5 4 3 2 1 0 byte #
+---------------+
| | 1
+-screen width -+ raster width in pixels (lsb first)
| | 2
+---------------+
| | 3
+-screen height-+ raster height in pixels (lsb first)
| | 4
+-+-----+-+-----+ m = 1, global color map follows descriptor
|m| cr |0|pixel| 5 cr+1 = # bits of color resolution
+-+-----+-+-----+ pixel+1 = # bits/pixel in image
| background | 6 background=../jpg/di1.JPG index of screen background
+---------------+ (color is defined from the global color
|0 0 0 0 0 0 0 0| 7 map or default map if none specified)
+---------------+
the logical screen width and height can both be larger than the
physical display. how images larger than the physical display are
handled is implementation dependent and can take advantage of hardware
characteristics (e.g. macintosh scrolling windows). otherwise images
can be clipped to the edges of the display.
the value of 'pixel' also defines the maximum number of colors
within an image. the range of values for 'pixel' is 0 to 7 which
represents 1 to 8 bits. this translates to a range of 2 (b & w) to 256
colors. bit 3 of word 5 is reserved for future definition and must be
zero.
graphics interchange format (gif) page 5
specification
global color map
the global color map is optional but recommended for images where
accurate color rendition is desired. the existence of this color map is
indicated in the 'm' field of byte 5 of the screen descriptor. a color
map can also be associated with each image in a gif file as described
later. however this global map will normally be used because of
hardware restrictions in equipment available today. in the individual
image descriptors the 'm' flag will normally be zero. if the global
color map is present, it's definition immediately follows the screen
descriptor. the number of color map entries following a screen
descriptor is equal to 2**(# bits per pixel), where each entry consists
of three byte values representing the relative intensities of red, green
and blue respectively. the structure of the color map block is:
bits
7 6 5 4 3 2 1 0 byte #
+---------------+
| red intensity | 1 red value for color index 0
+---------------+
|green intensity| 2 green value for color index 0
+---------------+
| blue intensity| 3 blue value for color index 0
+---------------+
| red intensity | 4 red value for color index 1
+---------------+
|green intensity| 5 green value for color index 1
+---------------+
| blue intensity| 6 blue value for color index 1
+---------------+
: : (continues for remaining colors)
each image pixel value received will be displayed according to its
closest match with an available color of the display based on this color
map. the color components represent a fractional intensity value from
none (0) to full (255). white would be represented as (255,255,255),
black as (0,0,0) and medium yellow as (180,180,0). for display, if the
device supports fewer than 8 bits per color component, the higher order
bits of each component are used. in the creation of a gif color map
entry with hardware supporting fewer than 8 bits per component, the
component values for the hardware should be converted to the 8-bit
format with the following calculation:
<map_value> = <component_value>*255/(2**<nbits> -1)
this assures accurate translation of colors for all displays. in
the cases of creating gif images from hardware without color palette
capability, a fixed palette should be created based on the available
display colors for that hardware. if no global color map is indicated,
a default color map is generated internally which maps each possible
incoming color index to the same hardware color index modulo <n> where
<n> is the number of available hardware colors.
graphics interchange format (gif) page 6
specification
image descriptor
the image descriptor defines the actual placement and extents of
the following image within the space defined in the screen descriptor.
also defined are flags to indicate the presence of a local color lookup
map, and to define the pixel display sequence. each image descriptor is
introduced by an image separator character. the role of the image
separator is simply to provide a synchronization character to introduce
an image descriptor. this is desirable if a gif file happens to contain
more than one image. this character is defined as 0x2c hex or ','
(comma). when this character is encountered between images, the image
descriptor will follow immediately.
any characters encountered between the end of a previous image and
the image separator character are to be ignored. this allows future gif
enhancements to be present in newer image formats and yet ignored safely
by older software decoders.
bits
7 6 5 4 3 2 1 0 byte #
+---------------+
|0 0 1 0 1 1 0 0| 1 ',' - image separator character
+---------------+
| | 2 start of image in pixels from the
+- image left -+ left side of the screen (lsb first)
| | 3
+---------------+
| | 4
+- image top -+ start of image in pixels from the
| | 5 top of the screen (lsb first)
+---------------+
| | 6
+- image width -+ width of the image in pixels (lsb first)
| | 7
+---------------+
| | 8
+- image height-+ height of the image in pixels (lsb first)
| | 9
+-+-+-+-+-+-----+ m=0 - use global color map, ignore 'pixel'
|m|i|0|0|0|pixel| 10 m=1 - local color map follows, use 'pixel'
+-+-+-+-+-+-----+ i=0 - image formatted in sequential order
i=1 - image formatted in interlaced order
pixel+1 - # bits per pixel for this image
the specifications for the image position and size must be confined
to the dimensions defined by the screen descriptor. on the other hand
it is not necessary that the image fill the entire screen defined.
local color map
graphics interchange format (gif) page 7
specification
a local color map is optional and defined here for future use. if
the 'm' bit of byte 10 of the image descriptor is set, then a color map
follows the image descriptor that applies only to the following image.
at the end of the image, the color map will revert to that defined after
the screen descriptor. note that the 'pixel' field of byte 10 of the
image descriptor is used only if a local color map is indicated. this
defines the parameters not only for the image pixel size, but determines
the number of color map entries that follow. the bits per pixel value
will also revert to the value specified in the screen descriptor when
processing of the image is complete.
raster data
the format of the actual image is defined as the series of pixel
color index values that make up the image. the pixels are stored left
to right sequentially for an image row. by default each image row is
written sequentially, top to bottom. in the case that the interlace or
'i' bit is set in byte 10 of the image descriptor then the row order of
the image display follows a four-pass process in which the image is
filled in by widely spaced rows. the first pass writes every 8th row,
starting with the top row of the image window. the second pass writes
every 8th row starting at the fifth row from the top. the third pass
writes every 4th row starting at the third row from the top. the fourth
pass completes the image, writing every other row, starting at the
second row from the top. a graphic description of this process follows:
image
row pass 1 pass 2 pass 3 pass 4 result
---------------------------------------------------
0 **1a** **1a**
1 **4a** **4a**
2 **3a** **3a**
3 **4b** **4b**
4 **2a** **2a**
5 **4c** **4c**
6 **3b** **3b**
7 **4d** **4d**
8 **1b** **1b**
9 **4e** **4e**
10 **3c** **3c**
11 **4f** **4f**
12 **2b** **2b**
. . .
the image pixel values are processed as a series of color indices
which map into the existing color map. the resulting color value from
the map is what is actually displayed. this series of pixel indices,
the number of which is equal to image-width*image-height pixels, are
passed to the gif image data stream one value per pixel, compressed and
packaged according to a version of the lzw compression algorithm as
defined in appendix c.
graphics interchange format (gif) page 8
specification
gif terminator
in order to provide a synchronization for the termination of a gif
image file, a gif decoder will process the end of gif mode when the
character 0x3b hex or ';' is found after an image has been processed.
by convention the decoding software will pause and wait for an action
indicating that the user is ready to continue. this may be a carriage
return entered at the keyboard or a mouse click. for interactive
applications this user action must be passed on to the host as a
carriage return character so that the host application can continue.
the decoding software will then typically leave graphics mode and resume
any previous process.
gif extension blocks
to provide for orderly extension of the gif definition, a mechanism
for defining the packaging of extensions within a gif data stream is
necessary. specific gif extensions are to be defined and documented by
compuserve in order to provide a controlled enhancement path.
gif extension blocks are packaged in a manner similar to that used
by the raster data though not compressed. the basic structure is:
7 6 5 4 3 2 1 0 byte #
+---------------+
|0 0 1 0 0 0 0 1| 1 '!' - gif extension block introducer
+---------------+
| function code | 2 extension function code (0 to 255)
+---------------+ ---+
| byte count | |
+---------------+ |
: : +-- repeated as many times as necessary
|func data bytes| |
: : |
+---------------+ ---+
. . . . . .
+---------------+
|0 0 0 0 0 0 0 0| zero byte count (terminates block)
+---------------+
a gif extension block may immediately preceed any image descriptor
or occur before the gif terminator.
all gif decoders must be able to recognize the existence of gif
extension blocks and read past them if unable to process the function
code. this ensures that older decoders will be able to process extended
gif image files in the future, though without the additional
functionality.
graphics interchange format (gif) page 9
appendix a - glossary
glossary
pixel - the smallest picture element of a graphics image. this usually
corresponds to a single dot on a graphics screen. image resolution is
typically given in units of pixels. for example a fairly standard
graphics screen format is one 320 pixels across and 200 pixels high.
each pixel can appear as one of several colors depending on the
capabilities of the graphics hardware.
raster - a horizontal row of pixels representing one line of an image. a
typical method of working with images since most hardware is oriented to
work most efficiently in this manner.
lsb - least significant byte. refers to a convention for two byte numeric
values in which the less significant byte of the value preceeds the more
significant byte. this convention is typical on many microcomputers.
color map - the list of definitions of each color used in a gif image.
these desired colors are converted to available colors through a table
which is derived by assigning an incoming color index (from the image)
to an output color index (of the hardware). while the color map
definitons are specified in a gif image, the output pixel colors will
vary based on the hardware used and its ability to match the defined
color.
interlace - the method of displaying a gif image in which multiple passes
are made, outputting raster lines spaced apart to provide a way of
visualizing the general content of an entire image before all of the
data has been processed.
b protocol - a compuserve-developed error-correcting file transfer protocol
available in the public domain and implemented in compuserve vidtex
products. this error checking mechanism will be used in transfers of
gif images for interactive applications.
lzw - a sophisticated data compression algorithm based on work done by
lempel-ziv & welch which has the feature of very efficient one-pass
encoding and decoding. this allows the image to be decompressed and
displayed at the same time. the original article from which this
technique was adapted is:
terry a. welch, "a technique for high performance data
compression", ieee computer, vol 17 no 6 (june 1984)
this basic algorithm is also used in the public domain arc file
compression utilities. the compuserve adaptation of lzw for gif is
described in appendix c.
graphics interchange format (gif) page 10
appendix b - interactive sequences
gif sequence exchanges for an interactive environment
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 gce 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 gce is defined as:
esc [ > 0 g (g is lower case, spaces inserted for clarity)
(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}... <cr>
'#' - gcr identifier character (number sign)
version - gif format version number; initially '87a'
protocol='0' - no end-to-end protocol supported by decoder
transfer as direct 8-bit data stream.
protocol='1' - can use an error correction protocol to transfer gif data
interactively from the host directly to the display.
dev = '0' - screen parameter set follows
dev = '1' - printer parameter set follows
width - maximum supported display width in pixels
height - maximum supported display height in pixels
color-bits - number of bits per pixel supported. the number of
supported colors is therefore 2**color-bits.
color-res - 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 gcr are returned as ascii decimal
numbers and the message is terminated by a carriage return character.
graphics interchange format (gif) page 11
appendix b - interactive sequences
the following gcr message describes three standard ega
configurations with no printer; the gif data stream 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<cr>
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 [ > 1 g display gif image on screen
(0x1b 0x5b 0x3e 0x31 0x67)
esc [ > 2 g 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 lower
case.
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.
graphics interchange format (gif) page 12
appendix c - image packaging & compression
the raster data stream that represents the actual output image can
be represented as:
7 6 5 4 3 2 1 0
+---------------+
| code size |
+---------------+ ---+
|blok byte count| |
+---------------+ |
: : +-- repeated as many times as necessary
| data bytes | |
: : |
+---------------+ ---+
. . . . . .
+---------------+
|0 0 0 0 0 0 0 0| zero byte count (terminates data stream)
+---------------+
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 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 preceeded by
character counts and output.
establish code size
the first byte of the gif raster 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.
graphics interchange format (gif) page 13
appendix c - image packaging & compression
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**<code size>. 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 <clear code>+1.
3. the first available compression code value is <clear code>+2.
4. the output codes are of variable length, starting at <code size>+1
bits per code, up to 12 bits per code. this defines a maximum code
value of 4095 (hex fff). 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 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:
byte n byte 5 byte 4 byte 3 byte 2 byte 1
+-.....-----+--------+--------+--------+--------+--------+
| and so on |hhhhhggg|ggfffffe|eeeedddd|dcccccbb|bbbaaaaa|
+-.....-----+--------+--------+--------+--------+--------+
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 preceeding 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
graphics interchange format (gif) page 14
appendix c - image packaging & compression
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.
graphics interchange format (gif) page 15
appendix d - multiple image processing
since a gif data stream can contain multiple images, it is
necessary to describe processing and display of such a file. because
the image descriptor allows for placement of the image within the
logical screen, it is possible to define a sequence of images that may
each be a partial screen, but in total fill the entire screen. the
guidelines for handling the multiple image situation are:
1. there is no pause between images. each is processed immediately as
seen by the decoder.
2. each image explicitly overwrites any image already on the screen
inside of its window. the only screen clears are at the beginning
and end of the gif image process. see discussion on the gif
terminator.


返回




月光软件源码下载编程文档电脑教程网站优化网址导航网络文学游戏天地生活休闲写作范文安妮宝贝站内搜索
电脑技术编程开发网络专区谈天说地情感世界游戏元素分类游戏热门游戏体育运动手机专区业余爱好影视沙龙
音乐天地数码广场教育园地科学大观古今纵横谈股论金人文艺术医学保健动漫图酷二手专区地方风情各行各业

月光软件站·版权所有