Victor Image Processing Library Application Notes

JPEG Image File Compression

What is JPEG?

JPEG is an image file compression technique for 8-bit grayscale and 24-bit color images. It combines several compression steps (conversion to frequency domain by discrete cosine transform, discardng low frequency components, differential pulse code modulation to encode differences, and Huffman coding) to yield highly compressed image files. JPEG compression yields smaller files than all other standard image file formats (TIFF, LZW, PackBits, GIF, PCX, TGA, BMP, DIB, or BIF).

The JPEG compression is adjustable -- depending on the desired quality of the decompressed image -- almost any degree of compression can be achieved. Commonly an image size is brought down by about 20 to 1. There is a price to pay for this high degree of compression and that is the resultant loss of image quality as compression increases. There is also a lossless JPEG mode, but it is no more efficient than other compression techniques and is seldom encountered.

Why use JPEG?

JPEG is highly efficient at squeezing big images into small spaces. For conserving disk space and minimizing transmission time JPEG is often the method of choice.

How to use JPEG

The Victor Library implements powerful JPEG file support for ultra-high compression of images. The loadjpg functions read 24-bit color or 8-bit grayscale images in the JPEG File Interchange Format (JFIF). The JPEG lossless mode format is not supported.

To load an image from a JPEG file use the following code in your Victor application:

   loadjpg("test.jpg",&image);

Or to load an image from a memory buffer that contains JPEG file data use:

   loadjpgfrombuffer(bufferaddress,&image);

Images can be compressed to a small fraction of their original size. To compress an image and save it in a JPEG file use:

   savejpg("test.jpg",&image,quality);

The quality parameter can range from 1 (lowest) to 100 (highest). This parameter determines the amount of image data to be retained and the resulting compression efficiency.

An Example

Here is a 24-bit color image 314 x 237 saved in JPEG format with quality set to 50.

The original image size is 223,254 bytes, but at quality 50 the JPEG file occupies only 9413 bytes. This represents a compression ratio of about 35:1. And as you can see, a quality of 50 creates a highly compressed file with little visible degradation.

Grayscale, palette color, and RGB images can be saved in the JPEG file format. Since some image information is lost each time an image is saved as a JPEG file, this is not the file format to use if images are to be loaded and saved repeatedly, as for example, when intermediate images are saved to disk during a lengthy image processing sequence.

The table below shows typical JPEG compression efficiencies for saving a typical 24-bit color image at several quality settings.

Table 1. JPEG Efficiency for a 24-bit Image
Quality factor File Size    Compression ratio 
---------------------------------------------
original image   723 kb           1:1 
    100          309 kb           2:1 
     80           83 kb           9:1 
     60           54 kb          13:1 
     40           40 kb          18:1 
     20           24 kb          30:1 

The values in the table are rather conservative. The compression efficiency varies from image to image. As we saw above, the yellow flower picture compressed to about 35:1 at a quality of 50 that is much more efficient than this table would predict.




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.