Victor Image Processing Library Application Notes

LZW Compression

What is LZW Compression?

LZW (short for Lempel-Ziv-Welch) is a lossless compression technique that is an optional part of the GIF and TIFF image file formats.

The Lempel-Ziv algorithm looks for repeated pixel combinations and builds a lookup table (on-the-fly) of compression codes to represent the combinations. The codes are Huffman-encoded for further compression efficiency.

LZW compression is commonly used for 1- through 8-bit palette color images and less often for 24-bit RGB images. Typically an LZW-compressed palette color image is 60 to 80% of the original size. But for some images LZW can create a larger file than the original uncompressed image requires. Compression efficiency varies based on image complexity.

Why Use LZW Compression?

Two reasons . . .

A Comparison of Compression Efficiencies

This picture of yellow dendrobium flowers began as a 24-bit RGB image (314 x 237 pixels). Table 1 shows the sizes of the resulting files when the image is saved using various compression techniques.

Table 1. Comparison of Compression Efficiencies for a 24-bit Image

Compression     Image bits/pixel  File Format   Size (kb) (% of original)
none                  24             TIFF       223,646       100
LZW                   24             TIFF       243,829       109
run length            24             TGA        224,055       101
JPEG quality=75       24             JPEG        14,572         7
JPEG quality=50       24             JPEG         9,413         4
Notice that for the 24-bit image LZW (or run length encoding) is not really compressing but actually increasing the space requirements. This demonstrates why LZW compression is seldom used for 24-bit images. (Be sure to see the JPEG Application Note.)

Using color reduction (with optimized palette and diffusion scatter) the original image is converted to 8 bits per pixel. Color reduction cuts the image size by 2/3. Now examine the resulting file sizes when the image is saved in 8-bit formats.


Table 2. Comparison of Compression Efficiencies for an 8-bit Image

Compression     Image bits/pixel  File Format   Size (kb) (% of original)
none                   8             TIFF        77,810       100
run length             8             PCX         79,028       102 
LZW                    8             GIF         59,582        77
Table 2 shows typical LZW compression efficiency for palette color images. Now take a look at the 8-bit LZW image. You can compare the quality and speed of transmission to that of the 24-bit JPEG.

License Considerations

The Victor Library includes complete support for LZW compression and decompression within the GIF and TIFF-LZW image file formats. LZW functionality is fully enabled.

The Unisys US patent on lzw compression/decompression expired June 20, 2003. The European and Canadian patents expired July 7, 2004.

LZW Compression with Victor

The Victor Library includes complete support for optional LZW compression and decompression within the GIF and TIFF image file formats.

The GIF and TIFF file load and save functions all perform normally for non-LZW compressed images, that is, no unlocking is necessary.

Sample Code - How to Load an Image from a GIF File

The following small C function shows how to load an image from a GIF file. The image is stored in memory as a device independent bitmap, DIB.
int load_a_gif(char *fname, imgdes *image)
{
   GifData gdat;
   int rcode;

   // Get info on the GIF file we're to load 
   gifinfo(fname, &gdat);
   // Allocate space for the image based on the data in the GIF file
   allocimage(image, gdat.width, gdat.length, gdat.vbitcount);
    
   rcode = loadgif(fname, image); 
   return(rcode);
} 
As soon as the image is loaded it is eligible for use with any of the Victor functions for processing, printing, saving, or display. See the list of Victor functions.

Sample Code - How to Save an Image to an Uncompressed GIF File

The following small C function shows how to save an image to a GIF file. The image will be saved uncompressed with color number 43 transparent.

int save_a_gif(char *fname, imgdes *image)
{
   int rcode;
   int savemode = GIFTRANSPARENT | GIFNOCOMP;  // Transparent, no compression
   int transcolor = 43;
    
   rcode = savegifex(fname, image, savemode, transcolor); 
   return(rcode);
} 




Victor Image Processing Library

Victor Image Processing Library homepage

Copyright © 1995 - 2002 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.