A GIF87a file starts with a 6-byte signature and a 7-byte screen descriptor, followed at once by the global colour map when there is one. Numbers of more than one byte are little-endian, low byte first.
Signature
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 3 | ASCII | signature | GIF. |
| 3 | 3 | ASCII | version | 87a. |
Screen descriptor
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 6 | 2 | u16 | width | The width of the screen in pixels. |
| 8 | 2 | u16 | height | The height of the screen in pixels. |
| 10 | 1 | u8 | flags | Colour map flag, colour resolution and bits per pixel. |
| 11 | 1 | u8 | background | The colour index of the screen’s background. |
| 12 | 1 | u8 | reserved | Zero. GIF89a later used this byte for the pixel aspect ratio. |
Flags
| Bits | Name | Meaning |
|---|---|---|
| 7 | global colour map | 1 if a global colour map follows the descriptor. |
| 6–4 | colour resolution | Bits of colour resolution, minus 1. |
| 3 | reserved | Zero. GIF89a later used it as the sort flag. |
| 2–0 | pixel | Bits per pixel, minus 1: 0 to 7, for 2 to 256 colours. |
Global colour map
Straight after the descriptor when bit 7 of the flags is set: 2pixel + 1 entries of three bytes each, red, green and blue. A pixel value is an index into it.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 1 | u8 | red | Colour 0, red, 0 to 255. |
| 1 | 1 | u8 | green | Colour 0, green, 0 to 255. |
| 2 | 1 | u8 | blue | Colour 0, blue, 0 to 255. |