Image Compression
THE
DIGITAL CORNER
The
concept in any kind of data compression is to eliminate unnecessary or redundant
information. There are many ways of
doing this, varying in computational complexity and effectiveness.
There is, at least theoretically, a minimum amount of data (or maximum
compression ratio) for any given set of data to be able to perfectly reconstruct
the original. Practical compression
techniques generally do not achieve this limit, but in many cases it is possible
to use a "lossy" technique which exceeds the maximum compression by
allowing that the reconstructed data may have small errors.
Photographic
images can usually be compressed to quite high ratios because they usually have
large areas containing more or less similar data. Unlike most other data sets (such as text files), images have
similarity in two dimensions. A
pixel is just as likely to be similar to the one above it as to the one to the
left. The better image compression
techniques make use of this two-dimensional nature to improve compression.
One
of the most popular image compression techniques is JPEG (Joint Photographic
Experts Group). This international
standard actually encompasses several methods for image compression, but the
most common is "baseline" JPEG, which is a lossy compression.
While JPEG was being developed I worked at C-Cube Microsystems, which was
a member of the JPEG committee and developed the first JPEG chip.
The
basic idea in JPEG is to analyze an image in terms of frequency content.
Since many portions of an image contain no high frequency components,
this data can be discarded. To
increase the compression ratio, the frequency data is "quantized"
(divided by some value in compression and multiplied by the same value in
decompression). This causes small values to become zero (when rounded), thus
increasing the amount of data which can be discarded. Another part of JPEG compression is that the original data,
which is usually in RGB format, is often converted to YUV format and
"sub-sampled". YUV format
expresses image data as a monochrome luminance (brightness) value
("Y"), and two "color difference" signals ("U" and
"V"). Because we do not
see colors as sharply as we do luminance, the U and V signals can be reduced,
typically keeping every other or every fourth value.
The same principle is used in color television signals.
This means that very sharp color edges may not reproduce accurately with
JPEG compression. For example, a
bright red line on a white background may be sharp in terms of the brightness
change from white to red, but the color will bleed into the white in some
places. Fortunately, such sharp
transitions rarely occur in photographs, but it does explain why JPEG should not
be used for line graphics or computer generated images where such transitions
are common.
So
the first step in deciding how to compress an image is to determine whether to
use JPEG or another type. Of the
commonly used types, most are one dimensional techniques based on LZW or a
similar data compression. The
popular GIF format is a common example. It
is commonly used on 256-color graphics, and is quite good for simple graphics
where there are not too many colors and a lot of repetition.
The Windows native image file format, BMP, uses an even simpler technique
called "run length encoding". Neither
of these works well on typical photographic images.
Photos in GIF format are usually reduced to 256 colors, which is itself a
3:1 compression, but generally results in a very visible loss of quality.
Additional compression from LZW coding generally yields only about 2:1
and can, in some cases, produce a larger file.
If you have an image which started as a photo but has been reduced to a
more "graphical" image (for example, by application of a "posterize"
filter) it might work well as a GIF or BMP file.
But for most photos, these are not good choices.
Photoshop version 5 supports a format called "PNG" which stands
for Portable Network Graphics. This
is a lossless compression format intended to replace GIF, especially for
internet applications. When used on
photographic images it achieves very little compression, but does not require
conversion to 256 colors as GIF does, so the image quality is better (in fact,
since it is lossless compression, you could say that its perfect).
Most
imaging applications which support writing JPEG files provide the user with some
kind of control for the compression ratio or image quality, which are inversely
proportional. In the simplest case
you may have a choice of 3 or 4 levels. Others
may provide a more-or-less continuous scale.
In the early days of JPEG some applications actually let the user select
the chroma sub-sampling (1:1, 2:1, or 4:1) and the quantization level.
This is now considered too complex for most users, but be aware that as
you choose different levels you may be affecting different aspects of quality,
so the results are not predictable. For
example, a 4-level choice might have 4 different quantization levels, but also
change the sub-sampling. In some
cases, the highest quality setting uses 1:1 (or no sub-sampling), which I have
found may not work properly when decompressed by another application.
To
determine what is the best compression/quality level you have to experiment by
compressing and decompressing at each level and examining the results.
Here's what to look for: Starting
from the highest quality level you should see no difference from the original,
although a mathematical analysis would show some small errors.
For a typical image you would get somewhere around 5:1 compression.
As you gradually increase the compression ratio the first artifact you
might notice is "blockiness". JPEG
works on image data in chunks of 8 x 8 pixels, and sometimes the boundaries
become visible as an edge even though the pixels within each block are smooth.
This can also indicate a poor implementation of the JPEG standard (note
that the standard does not specify the details of the compression algorithm, so
programmers often use mathematical shortcuts to make the process faster).
Further increase in compression ratio may produce artifacts near sharp
edges. This may look like bits of
the image have broken off the sharp edge and landed a few pixels away.
As noted above, you may also see color bleeding on sharp, colorful edges.
Finally, the image starts to look "contoured".
That is, instead of smooth transitions of shade you see bands of solid
color with a distinct edge where it changes from one color to another.
For
larger images (at least 640 x 480 pixels) good quality can usually be achieved
with a compression ratio of around 20:1. When
archiving high resolution photos I always use the highest quality setting and am
happy with a 10:1 compression ratio.
That
brings up another important point: Use
JPEG for archiving and transmitting images, but don't use it for images that you
are working on in Photoshop or other applications.
This is because each cycle of compression and decompression introduces
additional error. Good JPEG
implementations will "settle" after a few cycles and not generate any
more errors, but why accept any errors if you don't have to?
In
addition to JPEG there are some other compression techniques which work well for
photographs. I am not so familiar
with these, but I'll share what I know: Most
use mathematical analysis to find "natural" patterns in the data (that
is, patterns that can be easily expressed by equations).
This can be very efficient for compression, but is much more
computationally complex than JPEG, especially on the compression side.
JPEG is described as "symmetrical", meaning that it takes
roughly the same amount of effort to compress as to decompress.
Standards for this type of compression are just beginning to emerge and
will probably not be in wide use for several years.
One more thing: Don't try to compress a compressed file. If it was properly compressed in the first pass you won't get any additional compression. And its more likely that you'll actually end up with a larger file. This is pretty much true regardless of the type of data or compression technique.
Copyright (C) 2004 Greg Marshall
No portion of this web site may be copied or reproduced in any manner without express written consent.