The GIF format: introduction
GIF is one of the simplest image formats and one of the oldest still in use. This part of the section takes it apart byte by byte, using a real 65-byte file you can download and inspect in any hex editor.
History
CompuServe developed the format in 1987 as GIF87a, to send images over modems. In 1989 came GIF89a with extension blocks: transparency, delay between frames, and comments. Animated GIFs are built on them.
| Feature | GIF |
|---|---|
| colors | palette of up to 256 colors, each 24 bits |
| compression | LZW, lossless |
| transparency | one palette color (binary) |
| interlacing | optional, 4 passes |
| animation | multiple images with delays (GIF89a) |
| byte order | little-endian: least significant byte first |
The example file
A 4 × 2 pixel image in four colors: #c85028, white, black, #1b98e0.
File: przyklad.gif (65 bytes). Enlarged it looks like this: ![]()
The whole file byte by byte
| Offset | Bytes | Meaning | More |
|---|---|---|---|
| 00-05 | 47 49 46 38 39 61 | "GIF89a" | header |
| 06-07 | 04 00 | width 4 | logical screen descriptor |
| 08-09 | 02 00 | height 2 | |
| 0A | F1 | global color table, 4 colors | |
| 0B | 00 | background color: index 0 | |
| 0C | 00 | pixel aspect: not given | |
| 0D-18 | C8 50 28 FF FF FF 00 00 00 1B 98 E0 | palette: 4 × 3 bytes | global color table |
| 19-25 | 21 FE 09 73 65 6D 6D 69 78 2E 70 6C 00 | comment "semmix.pl" | comments |
| 26-2D | 21 F9 04 00 00 00 00 00 | graphic control | graphic control |
| 2E-37 | 2C 00 00 00 00 04 00 02 00 00 | image descriptor | image block |
| 38 | 02 | LZW minimum code size | |
| 39-3F | 05 04 12 30 13 05 00 | sub-block of 5 LZW bytes, terminator | |
| 40 | 3B | end of file (trailer) | block order |
The most interesting bytes are 04 12 30 13 05: the image's eight pixels compressed by LZW into 36 bits. Decoding them step by step is on the LZW compression in GIF and LZW decompression in GIF pages.
The file was generated by a program written for this section and checked with an independent system decoder: every pixel reads back as intended.