A GIF89a file starts with 13 bytes that come before every image: a 6-byte header and a 7-byte logical screen descriptor, followed at once by the global colour table when there is one. Numbers of more than one byte are little-endian, low byte first.
00000000 47 49 46 38 39 61 0A 00 0A 00 91 00 00 FF FF FF GIF89a..........
Header
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 3 | ASCII | signature | The letters GIF. |
| 3 | 3 | ASCII | version | 89a. |
The specification asks encoders to use the earliest version that covers the blocks in
the file. A file with no extensions at all can therefore be marked
GIF87a and still be a valid GIF; the header
carries the minimum capability a decoder needs, and using a later version than needed
hinders some decoders. When two streams are combined, the later of the two versions is
used. Versions are ordered by the first two digits starting at 87 (87, 88 … 99, 00 …
86) and by the third character (a to z). The specification lists 87a
(May 1987) and 89a (July 1989) as of 10 July 1990. A decoder meeting a
version it cannot fully process should still try, perhaps warning the user that the
data may be incomplete. The specification calls any use of reserved fields or extra
data between blocks a deviation and invalid.
Logical screen descriptor
The seven bytes at offset 6 describe the canvas on which the images are drawn. The canvas is not itself an image: every picture is placed on it by its own image descriptor.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 6 | 2 | u16 | width | The width of the canvas in pixels. |
| 8 | 2 | u16 | height | The height of the canvas in pixels. |
| 10 | 1 | u8 | flags | Colour table flag, colour resolution, sort flag and table size. |
| 11 | 1 | u8 | background | The index in the global colour table of the background colour: the colour of pixels not covered by an image. It means nothing without a global colour table, and should then be 0. |
| 12 | 1 | u8 | pixel aspect | The shape of a pixel; 0 means it is not given. |
Flags
| Bits | Name | Meaning |
|---|---|---|
| 7 | global colour table | 1 if a global colour table follows. |
| 6–4 | colour resolution | The number of bits for each primary colour in the original image’s palette, minus 1: the richness of the palette the colours were chosen from, not the number of colours used (3 means a 4-bit palette). Most decoders ignore it. |
| 3 | sort | 1 if the table is sorted in decreasing importance, usually most frequent colour first, so a decoder with fewer colours can use an initial segment. |
| 2–0 | table size | The size of the global colour table: 2n + 1 colours. The specification asks for it to be set by this formula even when there is no table, so decoders can choose the best graphics mode. |
In the example the flags byte is 91: binary 1001 0001. Bit 7
is set, so a table follows; the colour resolution is 001, 2 bits; the sort bit is 0; and
the table size is 001, so the table has 22 = 4 colours.
Pixel aspect ratio
A value of 0 means no aspect ratio is given. Any other value is an aspect ratio of (n + 15) ÷ 64: a pixel’s width divided by its height. The specification gives the range as 4 : 1 to 1 : 4, in steps of 1 ÷ 64: the smallest value, 1, gives 16 ÷ 64 = 1 : 4, and the largest, 255, gives 270 ÷ 64, about 4.2 : 1. Few decoders use it.
Global colour table
When bit 7 of the flags is set, a colour table comes straight after the descriptor. It holds 2size + 1 colours, from 2 to 256, each three bytes: red, green and blue, 0 to 255. A table of 256 colours is 768 bytes.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 1 | u8 | red | Colour 0, red. |
| 1 | 1 | u8 | green | Colour 0, green. |
| 2 | 1 | u8 | blue | Colour 0, blue. |
| 3 | … | Colour 1, and so on. |
0000000D FF FF FF FF 00 00 00 00 FF 00 00 00 ............
A file does not need a global colour table if every image has a local colour table. The global table is used by every image without a local table, and by plain text extensions. A local table supersedes it for one image, after which the global table comes back. The specification recommends that a decoder keep the last global table it saw and use it for a later stream that has no table; a stream may even hold only the header, a global table and the trailer, to load a decoder with a table. With no table at all the decoder may use its own (the specification suggests black and white as its first two entries); the explorer shows such an image in greys and reports it. Colour components are in red, green, blue order.
Not described
What this page leaves out, and what the explorer does not do with the header and screen.
Known, not yet covered
- Reference: how programs use the colour resolution field and the sort flag: the GIF89a specification defines them, but only the aspect ratio was checked, and the decoders tried here (Chrome, Pillow, ImageIO, ffmpeg) ignore it.
- Viewer: the pixel aspect ratio is shown as a number and not applied to the picture; the background colour is shown and not painted, so the canvas outside the images stays transparent, as in Chrome. Both are defined by the specification.
Unknown
- Reference: what a version string other than
87aor89ameans: no source defines it and none was found in real files. - Reference and viewer: a background colour index past the end of the global colour table: the specification does not define it. The viewer shows the number and notes that it is past the table.
- Viewer: a global colour table cut short by the end of the file is shown as far as it goes, with a note, and nothing stands in for the missing colours.