LZW Compression


LZW compression is an acronym for Lemple-Ziv-Welch compression. This is a classical, lossless dictionary-based compression system.

Prior, to compression any GIF image must be reduced to 256 colors. This can be done using quantization followed by dithering as discussed earlier. This is the where the loss occurs in GIF compression.

After the image is quantized then LZW compression is applied to reduce the image size. LZW compression starts with a dictionary that contains all possible symbols (in our case numbers from 0 to 255). For the sake of illustration, I will use characters in this explanation.

Basically, build up a dictionary of longer and longer strings based on the input stream. The encoder compares the incoming characters for the longest possible match and returns the index for that dictionary entry. It then then adds a new dictionary entry with the current character concatenated to the longest match.
LZW Compression Algorithm:
   	w = NIL;
   	while ( read a character k )
       {
         if wk exists in the dictionary
          w = wk;
         else
           add wk to the dictionary;
           output the code for w;
           w = k;
       }
	
Lecture 3   Slide 29   6.837 Fall '01