Written by Christian Ahmer | 11/20/2023

JPEG

The JPEG (Joint Photographic Experts Group) format is one of the most widely used image compression standards in the world. It was created by the group of the same name, which aimed to standardize the compression of photographic images.

JPEG is a lossy compression algorithm, which means that it reduces file size by selectively discarding data. The process of JPEG compression involves several steps, and understanding each helps to appreciate why it's so effective for photographs but not always ideal for other types of images like line drawings or text.

Here's a simplified overview of the JPEG compression process:

  1. Color Space Conversion: JPEG typically converts images from the RGB color space to a YCbCr color space. This separation of luminance (Y) and chrominance (Cb and Cr) allows the algorithm to exploit the fact that the human eye is less sensitive to fine color details than to fine brightness details.

  2. Downsampling: The chrominance channels (Cb and Cr) can be downsampled, reducing the resolution of the color information without significantly affecting the perceived quality of the image.

  3. Discrete Cosine Transform (DCT): The image is divided into blocks, often 8x8 pixels, and a DCT is applied to each block. The DCT transforms the pixel values from the spatial domain into the frequency domain, resulting in a matrix of frequency coefficients, where the top-left corner represents the lowest frequencies.

  4. Quantization: The human eye is not equally sensitive to all frequencies, so the DCT coefficients are quantized, meaning they are divided by a quantization matrix. This matrix is designed to retain the lower-frequency information while reducing the higher-frequency information, which is less noticeable to the eye. The resulting quantized coefficients are rounded to integers, with many of the higher frequencies often rounded to zero, which is key to achieving compression.

  5. Entropy Encoding: The quantized blocks are then processed using entropy encoding, which includes run-length encoding and Huffman coding. This step takes advantage of the fact that after quantization, many coefficients are zero, and the non-zero coefficients often follow predictable patterns. Huffman coding assigns shorter bit strings to more common patterns, thus reducing the overall size.

The level of compression can be adjusted, allowing a choice between image quality and file size. Higher compression results in smaller files but can introduce artifacts such as blocking, where the 8x8 block boundaries become visible, and "ringing" around sharp edges.

When a JPEG image is opened, the inverse of these processes occurs (decompression), reconstructing an approximation of the original image. Some information is lost irretrievably in the compression process, which is why editing and re-saving a JPEG image multiple times can result in a noticeable degradation of quality.

JPEG has been the de facto standard for photographic image compression due to its flexibility and efficiency. However, it's not without limitations, and there are scenarios where other formats like PNG (lossless but typically larger files) or newer formats like WebP or HEIF may be more suitable due to their advanced compression algorithms and features such as support for transparency or higher dynamic range.