semmixcolor and perception

The LZW algorithm

LZW is a lossless compression algorithm that replaces repeated sequences of data with short codes. Its key property: the dictionary of sequences is built during both compression and decompression, so it never has to be stored in the file.

The name

Abraham Lempel and Jacob Ziv described two dictionary compression methods in 1977 and 1978, LZ77 and LZ78. In 1984 Terry Welch simplified LZ78 into a form easy to implement in hardware and software. That became LZW, after all three authors.

The idea

The dictionary starts with every single symbol. The compressor reads the data and looks for the longest sequence already in the dictionary. It outputs that sequence's code and adds the sequence extended by the next symbol to the dictionary. The longer compression runs, the longer the sequences the dictionary knows, and the more data fits in one code.

alphabet: A = 0, B = 1 data: A B A B A B A output: 0 1 2 4 dictionary along the way: AB = 2, BA = 3, ABA = 4

Seven symbols became four codes. The step-by-step run is on the LZW compression and LZW decompression pages.

Why the dictionary need not be stored

The decompressor starts with the same dictionary as the compressor (single symbols) and adds entries by the same rule as it reads codes. After every step both dictionaries are identical. There is one exception: the compressor can emit a code that the decompressor only adds in that very step. That case, known as KwKwK, is covered on the decompression page.

Properties

Where it is used

FormatNotes
GIFvariable-width codes of 3..12 bits, special "clear" and "end" codes
TIFFone of the compression methods, often with a differencing predictor
PDF, PostScriptthe LZWDecode filter, now largely replaced by Deflate
Unix compress.Z files

The patent

Unisys held a patent on LZW. In 1994 Unisys and CompuServe announced that software writing GIF files had to pay a license fee. In response the PNG format was created in 1995, with patent-free Deflate compression. The patent expired in the US in 2003 and in Europe and Japan in 2004. GIF has been free since then, but PNG had already replaced it in most uses other than animation.