Contents

The first notable thing about JPEG compression is the way in which the colour of each pixel is stored. Each pixel of the image is assigned 3 bytes to define it's color. All three bytes can have any value from 0 to 255 and every possible combination of the three bytes stands for another colour. In most file formats, the RGB format is used for defining the colour. RGB stands for Red Green Blue. It's named this way, because the first of the three bytes tells you how much red there is in the pixel's colour. The second byte tells you how much green there is in the colour and the third byte how much blue. The higher value the first byte has, the more red the pixel looks.

JPEG also uses three bytes for every pixel each, but it's using the YPbPr (also known as YCbCr) format. Here, the first byte tells us how bright the pixel is. The second byte tells us how blue the pixel is. The third byte tells us how red the pixel is. Using this colour format, the brightness is stored apart from the colour. This is useful, because we are going to compress an image. Because the human eye is better at seeing brightness than seeing colour, we can apply a greater compression to the colour bytes (the Pb-byte and the Pr-byte. Since we see brightness better, we use less compression on the Y-byte, to have the image look better after compression.

Because images are most often stored in RGB format, the first step of JPEG compression is usually to correctly change the RGB format into the YPbPr format.

JPEG uses cosinefunctions to represent an image. Therefor, we are going to talk a little bit about cosine functions. This is what a cosine function could look like:

cos(x)

To have the cosine function represent the colour of a pixel, we say that the higher the value of the cosine function, the brighter the pixel. If we had a set of pixels that went bright-dark-bright, we could use the function above to define them.

But here's where it gets interesting. We can also create different functions by taking the average of different cosine functions. Here is what it would look like if we took the average of the above two functions:

(cos(x) + cos(2x)) / 2

But it gets even more interesting when use a weighted average. Here is what it would look like if we gave the cos(x) a weight of 3 and the cos(2x) a weight of 5:

(3 cos(x) + 5 cos(2x)) / 8

This mathematical principle is used in the second step of JPEG compression: DCT. The Discrete Cosine Transform (DCT) can be used to write a set of numbers differently. Namely: as a weighted average of different cosine functions.

So far, no information has been lost in the process of compressing the image. In this step, we're filtering out information. For that reason, this is the step that lowers the quality of the image. For every block of 8 × 8 pixels, the cosine functions with high frequencies are set to 0. This means that these can no longer have any impact on how the image looks when you decompress it.

A lot of values will now be 0, which means that this can be very easily compressed. This is done using Huffman coding. Huffman coding is the last step of JPEG compression. It is also the only step in which the data is actually compressed.