catcode.com > Guide to Images > Choosing Graphics Format > Compression

Compression - What it is and Why you want it

Each image on your screen consists of many individual dots, called pixels (which is short for picture elements). If you have an image that's 256 dots wide and 256 dots tall, that's a total of 65,536 pixels.

How much room does each pixel take up in an image? That depends on how many colors each pixel can have. If you restrict each pixel to 256 possible colors, it takes only one byte per pixel, so the picture that we're talking about would take up 256 x 256, or 65,536 bytes to store on disk. A limit of 256 colors per pixel is more than adequate for most corporate logos, block diagrams, etc.

A photograph, however, has a much wider range of color than most of this so-called “line art”. If you want each pixel to be able to have a range of millions of colors (specifically, 16,777,216, which is more than enough to create a pleasing picture to all but the most sensitive of eyes), then you need three bytes per pixel. The 256 by 256 picture would then take up 196,608 bytes of storage space.

Most images aren't completely different in every single pixel; line art consists of large areas of a single color, and photos contain areas with very similar colors. We can exploit these properties to compress an image to take up less room than it would need if we were to record every pixel separately.

To show a crude example of how compression can work, let's take a closeup look at a set of pixels from one line of a picture:
closeup of pixels
We have a run of six blue pixels, four red pixels, and five more blue pixels for a total of 15 pixels, at, say, one byte each. Now let's use this rule:

A compressed line consists of a set of numbers and pixels. The numbers tell how many times to repeat the following pixel.

That changes our line to what you see below, which uses only six bytes total:
6 BLUE 4 RED 5 BLUE

Again, this is a crude example. Modern compression methods (or algorithms) are much more sophisticated, and can compress much more complex patterns than simple runs of color.

^ Choosing a Graphic Format