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. Decoders should try to read a file with an unknown version.
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. It means nothing without a global colour table. |
| 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, minus 1. Most decoders ignore it. |
| 3 | sort | 1 if the table is sorted, most important colour first. |
| 2–0 | table size | The size of the global colour table: 2n + 1 colours. |
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 largest value, 255, is 4 : 1; the smallest, 1, is 1 : 4, in steps of 1 ÷ 64. 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. An image with no table of either kind has no defined colours; the explorer shows it in greys and reports it.