The LZW algorithm in GIF
GIF uses the LZW algorithm with a few additions: two special codes, variable-width codes and a dictionary limited to 4096 entries. These are what make a home-made GIF decoder "almost work" so often.
Initial dictionary and special codes
In the image block the data is preceded by the minimum code size m. The whole dictionary layout follows from it:
The stream usually starts with CLEAR and should always end with END.
Variable-width codes
Codes start at m + 1 bits, since they must also fit CLEAR and END. When the dictionary reaches a power-of-two boundary, codes grow by 1 bit, up to 12:
The hardest part to implement is exactly when the width changes. The compressor adds an entry when it emits a code; the decompressor adds it one step later, so both must compute the same boundary offset by one:
Using the same condition in both (e.g. "=") produces a compressor that widens one code too early. Such a file usually breaks after the first few pixels.
A full dictionary
When the dictionary holds 4096 entries, no more can be added. The compressor has two options:
- send CLEAR (12 bits wide) and start from an empty dictionary, with codes back to m + 1 bits. The simplest and most common.
- stop adding entries and keep coding with the old dictionary until it decides to clear (a deferred clear). Sometimes better for images whose patterns do not change.
The decoder must handle both: when the dictionary is full, it stops adding entries and waits for CLEAR.
Bit packing
Codes are packed into bytes least significant bit first. The first code takes the lowest bits of the first byte, the next code the following bits, and a code that does not fit continues in the next byte. Finally the bytes are split into 255-byte sub-blocks.
The full run for the example file is on the LZW compression in GIF and LZW decompression in GIF pages.