Introduction to the JPEG Compression
The JPEG compression method is usually lossy, meaning that some original image information is lost and cannot be restored (possibly affecting image quality.) There is an optional lossless mode defined in the JPEG standard; however, that mode is not widely supported.
The encoding process consists of several steps:
- Color space conversion. The color space is converted from RGB to Y'CBCR (Brightness and blue and red chrominance). This step can be skipped.
- Chroma reduction. The resolution of the chroma data is reduced, usually by a factor of 2 in the horizontal direction or in both directions, depending on the compression factor. This exploits the fact that the eye is less sensitive to fine color details than to fine brightness details.
- DCT Transform. The image is split into blocks of 8x8 pixels, and for each block, each of the Y, CB, and CR data undergoes a discrete cosine transform (DCT). A DCT is similar to a Fourier transform in the sense that it produces a kind of spatial frequency spectrum.
- Quantization. The amplitudes of the frequency components are quantized. Human vision is much more sensitive to small variations in color or brightness over large areas than to the strength of high-frequency brightness variations. Therefore, the magnitudes of the high-frequency components are stored with a lower accuracy than the low-frequency components. The quality setting of the encoder (for example 50 or 95 on a scale of 0-100 in the jpeglib) affects to what extent the resolution of each frequency component is reduced. If an excessively low quality setting is used, the high-frequency components are discarded altogether. This is the lossy step of the compression, provided that the DCT is performed with sufficiently high precision.
- Compression. The resulting data for all 8x8 blocks is further compressed with a loss-less algorithm, a variant of Huffman encoding.
The deconding process performs the inverse of the encoding steps in reversed order. Note that there is no lossy step anymore, except for maybe for integer rounding errors, since the quantization step becomes a matrix multiplication and the step 2 makes no sense during the decompression.
Effects of JPEG compression
JPEG compression artifacts blend well into photographs with detailed non-uniform textures, allowing higher compression ratios. Higher compression ratio first affects the high-frequency textures of the image, and the edges become more fuzzy. The very high compression ratio severely affects the quality of the image, although the overall colors and image form are still recognizable. However, the precision of colors suffer less (for a human eye) than the precision of contours (based on luminance). This justifies the fact that images should be first transformed in a color model separating the luminance from the chromatic information, before subsampling the chromatic planes (which may also use lower quality quantization) in order to preserve the precision of the luminance plane with more information bits.
Does repeated JPEG compression degrade the image quality?
The short answer is no, provided that the 8x8 input tiles do not change (with other words the image dimensions remain the same and the whole image content is not significantely changed each time the image is opend and saved). Just by opening and saving a jpeg a large number of times, there will be no visible loss of quality since the de-compression algorithm is practically lossless and the compression algorithm will work on the same input data again and again, giving out the same results again and again.
To show this, we opened and saved 50x the same image for 4 different compression ratios using AZImage. The results are displayed in the table below (click to open large images in new windows):
Changing the image dimensions will severely affect the JPEG compression since the 8x8 squares will contain other pixels, leading to completely different input data during the compression.
Changing the colors of the whole image, for example by applying a smooth operation or any sort of filter working on the whole image will possibly affect the loss of data during the jpeg compression as well, since the input data will change significantely.
We hope that this article answered your question. If not, please send us a feedbak on how to improve! Email us at email@example.com.