Codec algorithms
How JPEG Compression Uses the Discrete Cosine Transform
Follow a JPEG image from pixel blocks through the DCT, quantization, and entropy coding.
The short version
JPEG makes a photo smaller by changing how image information is stored. One important step is the discrete cosine transform, usually shortened to DCT. It describes small blocks of pixels as a combination of smooth patterns, making it easier for JPEG to reduce details that are less noticeable to the eye.
The DCT is not the only step, and it does not by itself make a file smaller. Quantization, followed by entropy coding, is what turns those transformed values into a compact stream.
From pixels to blocks
Before the transform, an encoder may convert the image from RGB color into a brightness channel and two color-difference channels. This is called a YCbCr color model. Because people tend to notice brightness detail more than tiny color changes, some JPEG settings store less detail in the color channels. This optional step is called chroma subsampling.
The encoder then divides each channel into small blocks, commonly 8 by 8 samples. The DCT processes each block and produces 64 values called coefficients. The first coefficient represents the block's average level. The others describe patterns of increasing spatial detail.
flowchart TD
A[Image pixels] --> B[RGB to YCbCr]
B --> C[Optional chroma subsampling]
C --> D[8 by 8 sample blocks]
D --> E[Discrete cosine transform]
E --> F[64 frequency coefficients]
Why quantization affects quality
After the DCT, JPEG divides each coefficient by a value from a quantization table and rounds the result. Larger divisors usually turn more coefficients into zero. That makes the later data easier to compress, but it also discards information.
This is the main lossy step in baseline JPEG. Fine textures, subtle gradients, and sharp edges can look different when too much information is removed. Re-encoding an already compressed JPEG can add more loss, so keep an original when quality matters.
Quality sliders are not a universal measurement. Different encoders can use different tables and settings, so compare the exported image at the size where it will actually be viewed.
Turning coefficients into a file
The remaining coefficients are arranged in a zigzag-like order so runs of zero values are easier to describe. The encoder then uses run-length and entropy coding, commonly Huffman coding in baseline JPEG, to represent the data with fewer bits.
flowchart TD
A[DCT coefficients] --> B[Quantize and round]
B --> C[Order coefficients]
C --> D[Describe runs of zeros]
D --> E[Entropy coding]
E --> F[JPEG data stream]
The decoder reverses the coding steps, applies the inverse transform, and rebuilds the image blocks. It cannot restore information discarded during quantization.
When this matters
The DCT helps explain why JPEG is often effective for photographs and why strong compression can create visible artifacts. If an image contains small text, crisp interface lines, or transparency, compare another format such as PNG or WebP before deciding.
To keep a good result, start from the best original you have, export once at a sensible setting, and inspect both the whole image and fine details.