CompuServe GIF (Graphics Interchange Format) image file format
- Typical file name extension
.gif
- Magic bytes
0x47 0x49 0x46 0x38 at file offset 0x00.
- Version 87a GIFs are followed by the two bytes
0x37 0x61.
- Version 89a GIFs are followed by the two bytes
0x39 0x61.
- So GIF files start with one of the two six byte ASCII character sequences
GIF87a and
GIF89a.
- MIME type
image/gif
- Popularity
- Very high. Standard file format for the exchange of graphics on the World Wide Web.
- History
- Internet service provider CompuServe
introduced GIF (Graphics Interchange Format) in 1987 as a format to transmit
images with up to 256 different colors.
In 1989 a revised specification was published that added some features to the format.
- It was only later that people found out that LZW, the compression algorithm used
to store the image data within GIF, was patented by Unisys.
- When GIF finally became popular, Unisys started charging license fees
for creating software that reads or writes GIF files, even website owners who
used GIF images on their site.
This eventually triggered the development of
PNG, the Portable Network Graphics
image file format, in 1995, meant as a replacement for GIF.
- However, mediocre support of PNG in browsers (especially Microsoft's Internet
Explorer with its huge market share) kept GIF from becoming extinct.
- On June 20, 2003, the LZW patent expires in the US. In 2004 (supposedly July 7th)
the patent will expire in the
other countries where Unisys holds it.
- File structure
- GIF streams start with a screen descriptor that defines image resolution and color depth,
followed by an optional global color map (palette).
- Now follow one or more image descriptors.
These are data chunks within GIF that contain actual image data.
Each image descriptor contains image data for a rectangular part of the complete image.
That rectangular part can be (and often is) the complete image as defined in the screen descriptor.
- With GIF version 89a extension blocks were introduced.
These can be used to store text comments or other additional information.
Some of these extension blocks can be used to tell decoder software to delay the decoding
of subsequent image descriptors.
That's how GIF can be used to store animations.
- Compression types
- LZW (Lempel-Ziv-Welch, 1984) is the only compression type to be used in GIF.
It is a dictionary compression algorithm - codes in the compressed data stream
represent symbol sequences that have previously occurred in the uncompressed
data stream.
The algorithm is asymmetrical (encoding is slower than decoding).
- Uncompressed GIFs.
Because of the patent issues, people have created programming code
(e.g. the library libungif) that writes
GIF files which are compliant with the specification, but which do not actually make use of LZW's ability to compress.
LZW translates sequences of symbols (here: pixels) into codes.
Uncompressed GIFs contain only codes that translate into single pixels.
Such GIFs do not save space, they are even a bit bigger than the uncompressed amount of image data.
While this approach seems to avoid using Unisys's patented LZW technology, it has never
been proven in a court of law.
- Tom Lane explains uncompressed and RLE-compressed GIFs in his
1999 Usenet posting
(message ID <uo9ogk084m4.fsf@netcom10.netcom.com>) and speculates
on legal implications. In a nutshell: these two variants probably do not make use
of LZW, but this theory has to be proven in a court of law, most likely with a jury of
people who have never heard of compression algorithms.
- Image types
- Paletted images with up to 256 colors. Each color is a 24 bit RGB value.
- Animated GIFs.
A GIF file can contain more than one image.
With certain software packages (most web browsers and image viewers),
multiple-image GIFs can be played back as animations.
There is no way to include sound, though.
- Truecolor GIFs.
Actually, it is possible to store arbitrary 24 bit RGB truecolor images in GIF files.
But this is a dirty hack, so it is included here just for the sake of completeness.
In the description of GIF's file structure it was explained
how more than one image can be stored in a GIF file.
Subsequent images replace the complete image or a rectangular part of it.
Each of those subsequent images can define its own palette.
Thus, a truecolor image can be encoded as GIF as follows:
- Define the image resolution as the resolution of the truecolor image to be stored.
- The global palette contains the first 256 different colors used in the truecolor image.
- The first image in the GIF file stores pixels from the source truecolor image
(from top to bottom, left to right within each row) until a 257th color would be required.
- From that point on, all pixels in the first image are set (1) to the correct color
if it is in the global palette or (2) they are set to some arbitrary color from the palette.
All pixels that fall under (2) will be replaced in subsequent images with pixels of the correct
color value.
- As was said before, subsequent images in the GIF files can be created to store rectangular
parts of the image.
Each of those images comes with a local palette and defines pixels not
correctly stored by the images that precede it in the GIF file.
Such images are stored until the complete original truecolor image is represented.
- Thus, the original truecolor image is - much like a mosaic or a quilt -
patched together from smaller paletted images.
Also see True-Color GIF Example.
- Metadata
- GIF version 89a provides support for textual comments.
Each character is one byte large, nothing is said about the character set to be used,
that's why it is recommendable to use only ASCII characters.
- Libraries
- libungif - creates
uncompressed GIFs
.
- GD - a version of this
library that creates GIF will be provided July 7th, 2004, when the
LZW patent expires world-wide.
- Also see the GIF section of DataCompression.info linked to below.
- Software
- Almost every web browser and image viewer can read GIF. Most major image editing software can
read and write GIF files.
- There is GIF optimization software available that is specialized on creating
small GIF files for websites.
- The NetPBM utility ppmtogif.
- Also see the GIF section of DataCompression.info linked to below.
- Specification
- Specifications for both GIF versions are available online:
GIF 87a,
GIF 89a.
- Apart from the file format specs it is important to understand the LZW algorithm.
It is explained in the specs, but also in various other texts:
- Sites
- GIF section of DataCompression.info
- Google directory GIF section
- The GIF Controversy: A Software Developer's Perspective -
detailed history of GIF.
- Burn All GIFs - anti-GIF site.
- W3C page on GIF - currently unavailable
- Wikipedia LZW entry
- Discussion
- Kuro5hin discussion on LZW patent expiry (June 2003)
- Slashdot discussion on LZW patent expiry (June 2003)