How it Works
JPEG breaks the image into 8×8 pixel blocks, shifts values by subtracting 128, then applies the 2D DCT-II. The resulting 64 coefficients are divided by a quantization matrix (scaled by quality factor) and rounded to integers. The inverse DCT reconstructs the block — with lossy approximation. The DC coefficient represents the block average; the AC coefficients encode spatial frequency content.
The standard JPEG luminance quantization matrix assigns low step sizes to low frequencies (top-left) and high step sizes to high frequencies (bottom-right). At low quality, most AC coefficients become zero, producing block artefacts but small file size.
Frequently Asked Questions
What is the Discrete Cosine Transform (DCT)?
The DCT expresses a sequence of values in terms of cosine basis functions at different frequencies. DCT-II is used in JPEG: X_k = 2·Σ x_n·cos(π(2n+1)k/2N) for k=0…N-1.
Why does JPEG use 8×8 blocks?
8×8 blocks balance computational cost and correlation: nearby pixels are highly correlated, so the 64-coefficient DCT efficiently concentrates energy in low-frequency terms. Larger blocks increase artefacts at low quality.
What is quantization in JPEG?
Quantization divides each DCT coefficient by a step size and rounds to an integer. High-frequency coefficients use larger step sizes, so they become zero — the main source of lossy compression. Lower quality → larger steps → more zeros.
What are JPEG compression artefacts?
Block artefacts appear at block boundaries because adjacent 8×8 blocks are compressed independently. Ringing artefacts (Gibbs phenomenon) appear near sharp edges as high-frequency coefficients are discarded.
Why does DCT concentrate energy better than DFT?
The DCT implicitly assumes even symmetric extension of the signal, eliminating the discontinuity at block boundaries that the DFT would see. This makes the DCT basis a near-optimal Karhunen-Loève transform for typical image statistics.
What is the DC coefficient?
The DC coefficient (k=0) is proportional to the block's average pixel value. It is the largest coefficient and is typically coded with DPCM (difference from the previous block's DC) in JPEG.
What is zigzag scanning in JPEG?
After quantization, the 8×8 coefficient matrix is read in a zigzag order from low to high frequency. This groups the non-zero low-frequency coefficients at the start and produces long runs of zeros for entropy coding.
How does chroma subsampling improve JPEG compression?
Human vision is more sensitive to luminance (Y) than chrominance (Cb, Cr). JPEG converts RGB to YCbCr and downsamples Cb/Cr (e.g., 4:2:0 halves both dimensions), reducing data before DCT even runs.
What is the difference between JPEG and JPEG 2000?
JPEG uses 8×8 block DCT; JPEG 2000 uses wavelet transform (DWT) on the whole image. JPEG 2000 avoids block artefacts, supports lossless mode, and provides better quality at high compression ratios, but is slower.
What quality factor should I use for web images?
Quality 75–85 is standard for web use, offering a good balance between file size and visual quality. Quality below 60 causes visible block artefacts. For thumbnails, quality 50–65 is often acceptable.