🖼️ DCT Image Compression (JPEG Principle)

Discrete Cosine Transform compresses an 8×8 pixel block like JPEG. DCT-II: X_k = 2·Σx_n·cos(π(2n+1)k/2N). Quantize high-frequency coefficients. See compression ratio vs quality.

AlgorithmsInteractive
Original · DCT coefficients · Reconstructed · Difference · Toggle for coefficient view

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.

DCT-II: X[u][v] = (2/N) c(u)c(v) ΣΣ f[x][y] cos(π(2x+1)u/2N) cos(π(2y+1)v/2N) c(0) = 1/√2, c(k) = 1 for k>0 Quality → scale: s = (100−Q)/50 if Q<50 else 50/Q Quantized: Q[u][v] = round(X[u][v] / (step[u][v] · s))

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.

About this simulation

This simulation runs the exact core of JPEG compression on a single 8×8 pixel block: a 2D DCT-II transform, division by the standard JPEG luminance quantization matrix scaled by your chosen quality factor, and an inverse DCT to reconstruct the lossy result. Switching pattern types shows how gradients, edges, checkerboards, and random noise compress completely differently under the same quality setting.

🔬 What it shows

Four side-by-side 8×8 panels — Original, DCT coefficients (log-scaled), Reconstructed, and Error×5 — or, after toggling view, a red/blue heatmap of the quantized coefficients themselves with their integer values printed on each cell.

🎮 How to use

Pick a block pattern from the dropdown, drag the Quality Factor slider from 1 to 100 to watch compression ratio and PSNR trade off in real time, adjust Zoom for a closer look, and press Toggle View (or the I key) to inspect the quantized coefficient grid directly.

💡 Did you know?

The Random Noise pattern is the worst case for this whole scheme — because DCT only concentrates energy well when nearby pixels are correlated, noise scatters roughly equal energy across all 64 coefficients, so even at high quality settings the compression ratio barely improves, unlike the Gradient pattern where almost the entire block collapses onto the single DC coefficient.

Frequently asked questions

Why does the Sharp Edge pattern produce visible "Error×5" artefacts that Gradient doesn't?

A hard edge inside the block has energy spread across many high-frequency DCT coefficients, and those are exactly the coefficients the quantization matrix divides by the largest step sizes — once quantized to zero, the inverse DCT can't recover the sharp transition, producing the Gibbs-like ringing visible in the error panel.

Why does lowering the Quality slider increase "Non-zero coeff" compression so much?

The quantization scale factor grows sharply below quality 50 (scale = 5000/Q), multiplying every entry of the JPEG quantization matrix and pushing more and more of the smaller AC coefficients to round to exactly zero — the Non-zero coeff and Compression stats update live to show fewer surviving coefficients at low quality.

What does the PSNR number actually measure here?

PSNR (peak signal-to-noise ratio) is computed directly from the mean squared error between the original and reconstructed 8×8 block using 10·log10(255²/MSE) — higher PSNR means a more faithful reconstruction, and you can watch it climb toward infinity as you raise quality and the MSE shrinks toward zero.

Why do only a handful of cells in the coefficient heatmap have big numbers?

For structured patterns, the 2D DCT concentrates most of the block's energy into the low-frequency coefficients near the top-left corner (u=0, v=0 is the DC term), which is precisely why the JPEG quantization matrix assigns those cells small step sizes — the large surviving numbers you see are almost always clustered there.

Why does the Checkerboard pattern behave so differently from Gradient at the same quality?

A checkerboard alternates every pixel, which is the highest possible spatial frequency and maps almost entirely onto the single highest-frequency DCT basis function rather than the DC term — so at low quality this coefficient gets quantized away entirely and the reconstruction collapses to a flat grey block, unlike the gradient which survives because its energy sits in low, well-preserved frequencies.