A GIF87a image is one stream of variable-width LZW codes, joined together from its raster data blocks. The method is Welch’s LZW, with variable code widths from a small starting size up to 12 bits.
Codes and the code size
The byte before the raster data is the code size m. Black-and-white images normally use a code size of 2, so that the two codes below have room to start with. A few writers use 1; a code size of 1 works, and is decoded by the explorer, Chrome, macOS’s ImageIO and ffmpeg alike (see Code widths).
| Code | Value | Meaning |
|---|---|---|
| colour codes | 0 to 2m − 1 | A single pixel value. |
| clear code | 2m | Reset the dictionary and the code width. |
| end code | 2m + 1 | End of the raster data. |
| dictionary codes | 2m + 2 up to 4095 | Strings added while decoding. |
The specification says codes are between 3 and 12 bits long, that the end code must be the last code of the image, and that encoders should send a clear code first. The packed bytes are cut into blocks of 0 to 255 bytes, each with a count byte in front; a block with count 0 ends the raster data, so a reader can skip the data without decoding it. Codes start at m + 1 bits. The stream normally starts with a clear code, and the width grows by one bit each time the dictionary would otherwise run out of codes, up to 12 bits.
Code widths
After a code is read, if the number of the next free dictionary entry has reached 2width and the width is below 12, the width goes up by one. With a code size of 2 the first new entry is 6 and the width becomes 4 once entry 7 is made, so the next code number, 8, is the first that needs 4 bits. With a code size of 1 the first new entry, 4, already needs 3 bits, so the width grows straight after the first code, before any entry exists. That is how giflib and the browsers read it.
How codes are packed
Codes are packed into bytes low bit first: the first code takes the low bits of the first byte, the next code continues from the bit after, and the last byte is padded with spare bits. A code can cross from one data block into the next.
The dictionary
Decoding begins with entries for the colour codes only. The first code after a clear code is a colour code. After each later code a new entry is added: the string of the previous code, followed by the first value of the string the current code stands for. If the current code is the next free number it stands for the previous string followed by that string’s first value, and that is the new entry too.
When the dictionary reaches 4,096 entries, the code that follows is usually a clear code. A decoder should also allow an encoder to keep using the full dictionary, as later GIF89a files may do.
End of the data
The end code, 2m + 1, ends the codes. Unused bits of the last byte are padding. The data block with a count of 0 follows.
Not described
What this page leaves out, and what the explorer does not do with the codes.
Known, not yet covered
- Reference: the GIF87a specification (spec-gif87.txt) was read in full on 2026-11-18 and Appendix C checked against this page; it is the same algorithm as the GIF89a appendix.
- Reference: the code size 1 case is described from decoders (the explorer, Chrome, ImageIO and ffmpeg agree on a built file), not from the specification; giflib’s source shows how it reads it.
- Viewer: the dictionary is shown only as the entry each code adds, not as a whole table at one moment.
Unknown
- Reference: how decoders recover from a code that is not in the dictionary or a stream with no end code: the specification is silent and decoders differ.
- Reference and viewer: a code size above 8: no valid file uses it. The viewer lists the codes (up to 11) but decodes no pixels; above 11 it lists nothing.