Friday, April 23, 2010

Image Size, File Size, and File Compression

You can adjust the image size, file size, and image quality of your digital camera images. The guidebooks and manuals don't always make it clear that these adjustments are three entirely different things. However, image size affects file size and image quality, and image quality affects file size. File size, while it's dependent on the other two, has no direct effect on image size or quality. No wonder it's confusing! It's a good idea to get these three terms sorted out before we move on, so that we're all talking about exactly the same thing. Here's a quick summary:
  • Image size is the dimensions, in pixels, of the image you capture. For example, if you have a 5MP camera, it might offer a choice of 2560 x 1920, 1600 x 1200, 1280 x 960, and 640 x 480 resolutions. Each reduction in resolution reduces the size of the file stored on your memory card. A TIFF file at 2560 x 1920 pixels might occupy 4MB; a 1600 x 1200-pixel image, 1.7MB; a 1280 x 960-pixel image, 1.3MB, and a 640 x 480-pixel image, less than 1MB.

  • Image quality is the apparent resolution of the image after it's been compressed and then restored in your image editor. The TIFF format can compress the image, somewhat, with no loss of image quality, but JPEG compression does reduce the image quality for reasons that will become clear shortly.
  • File size is the actual space occupied on your memory card, hard disk, or other storage medium, measured in megabytes. The size of the file depends on both the image size (resolution) and quality/compression level. You can reduce the file size by reducing the image size or using a lower-quality/higher-compression setting.
Keep the difference between image size, file size, and image quality in mind as we continue our discussion.
What's Image Compression?

The next thing to clear up is the idea of compression. Compression comes in two varieties: lossless compression, like that provided with the RAW and TIFF formats, and lossy compression, like that afforded by the JPEG format. Understanding a little about how compression works makes these terms a little more understandable.

Images, like all other computer code, are stored as a series of binary numbers, which are the only values a computer can handle. A string of 64 bits might (but probably wouldn't) look like this:

0000000000000011100000000000000001111110000000000000000000000001

Ordinarily, the computer would need 8 full bytes to store that string of 64 binary numbers. A lossless compression scheme, like that used to squeeze a TIFF file, could instead record a value that would designate how many times a particular value is repeated, so, instead of storing all 64 bits, a code that translates to "14 zeroes, followed by 3 ones, followed by 16 zeros, followed by 6 ones..." would be used. Moreover, as the compression algorithm worked, it would notice that certain strings of numbers begin to repeat. Instead of enumerating only the number of runs of ones and zeros, the code would indicate where to find a string of numbers identical to the one that needed to be recorded next. The second time the line of numbers above turned up, a short code representing where that line is stored in the file would be substituted. The larger the file becomes, the fewer actual numbers the compression scheme has to record. More and more of the code consists of pointers to strings of numbers. This method, called Huffman encoding, builds a frequency table of the number strings in a file and assigns the shortest codes to the strings of numbers that occur most often.

Even though all the redundant numbers are eliminated from the file, the decompression algorithm can use the information to reconstruct the original file precisely. Today, more advanced algorithms, such as the Lempel-Ziv Welch (LZW) algorithm used to compress TIFF files, are very efficient.

LZW was originally developed by Israeli researchers Abraham Lempel and Jacob Ziv in 1977 and 1978. Their work was further developed by a Unisys researcher named Terry Welch, who in 1984 published and patented the LZW compression technique.
The Next Step in Compression

Although the compression scheme used with TIFF files works well, TIFF files can still be massive. As transfer of image files over telecommunications lines became popular (this was even before the public Internet), a consortium called the Joint Photographic Experts Group (JPEG) developed a compression scheme particularly for continuous tone images that is efficient and still retains the most valuable image information. JPEG uses three different algorithms: discrete cosine transformation (DCT), a quantization routine, and a numeric compression method like Huffman encoding.

JPEG first divides an image into larger cells, say 8 x 8 pixels, and divides the image into a special color space that separates the luminance values (brightness) from the chrominance (color) values. In that mode, the JPEG algorithm can provide separate compression of each type of data. Because luminance is more important to our eyes, more compression can be applied to the color values. The human eye finds it easier to detect small changes in brightness than equally small changes in color.

Next, the algorithm performs a discrete cosine transformation on the information. This mathematical mumbo jumbo simply analyzes the pixels in the 64-pixel cell and looks for similarities. Redundant pixels—those that have the same value as those around them—are discarded.

Next, quantization occurs, which causes some of the pixels that are nearly white to be represented as all white. Then the grayscale and color information is compressed by recording the differences in tone from one pixel to the next, and the resulting string of numbers is encoded using a combination of math routines. In that way, an 8 x 8 block with 24 bits of information per pixel (192 bytes) can often be squeezed down to 10 to 13 or fewer bytes. JPEG allows specifying various compression ratios, in which case larger amounts of information are discarded to produce higher compression ratios.

Finally, the codes that remain are subjected to a numeric compression process, which can be similar to the Huffman encoding described earlier. This final step is lossless, as all the information that is going to be discarded from the image has already been shed.

Because it discards data, the JPEG algorithm is referred to as "lossy." This means that once an image has been compressed and then decompressed, it will not be identical to the original image. In many cases, the differences between the original and compressed version of the image are difficult to see.

Compressed JPEG images are squeezed down by a factor of between 5:1 and 15:1. The kinds of details in an image affect the compression ratio. Large featureless areas such as areas of sky, blank walls, and so forth compress much better than images with a lot of detail.

This kind of compression is particularly useful for files that will be viewed on web pages and sent as e-mail files. It's also used to squeeze down digital camera files. However, more quality is lost every time the JPEG file is compressed and saved again, so you won't want to keep editing your JPEG files. Instead, save the original file as a TIFF file and edit that, reserving the original as just that, an original version you can return to when necessary.

No comments:

Post a Comment