Start with the pictures we want to keep
JPEG compression can erase texture, expose block boundaries or create ripples beside edges. Follow one block to see where those changes come from. JPEG blocking, ringing and detail loss isolates those visible changes with a GPU quantisation experiment.
JPEG describes each 8×8 component block as a sum of predefined patterns. It stores rounded numbers saying how much of each pattern to add. The decoder adds the weighted patterns to rebuild sample values. Colour JPEG usually codes Y′, Cb and Cr separately; the patterns themselves are not coloured.
An RGB8 picture uses three bytes per pixel: about 6.22 MB for 1920×1080, before headers. Each 8-bit component stores a value from 0 to 255.
Two ways to spend fewer bits
Lossless compression uses repetition and structure to recover the input exactly. Not every input has enough structure to become smaller.
Lossy compression permits changes to the picture. Here, converting samples into pattern weights is reversible; rounding those weights loses information.
Chroma subsampling can remove colour detail before this stage. How digital images represent colour explains RGB, Y′CbCr and the 4:4:4, 4:2:2 and 4:2:0 grids.
Subtract a known starting level
Take an 8×8 block: 64 neighbouring samples of one component. Subtract 128 from each, shifting 0…255 to −128…127. Add it back after reconstruction.
Hats mark reconstructed values. This shift alone loses nothing and saves no bits.
Describe the block as patterns
The discrete cosine transform (DCT) expresses the block as weights of 64 cosine patterns. There are still 64 numbers. Each weight, or coefficient, sets how much of a pattern to add; it can be negative or larger than 255. Here u indexes horizontal variation and v vertical variation, from 0 to 7.
Smooth areas often need mostly the constant pattern and a few slowly varying ones. Many other weights are small enough to round to zero.
Round the coefficients: this is the loss
Divide a coefficient c by a positive step Δ and round to an integer index q. Reconstruction multiplies q by Δ.
With Δ=16, both 21 and 23 become 1 and reconstruct as 16; 6 becomes zero. The original value within each rounding interval is lost. Larger steps can remove more patterns, changing the reconstructed samples.
Here Δ=32 for every coefficient. JPEG instead uses a table of steps.
Pack the remaining symbols without losing more
In baseline sequential JPEG, the constant coefficient (DC) is stored as a difference from the preceding block’s DC in the same component. The other 63 (AC) follow a zigzag from low towards higher spatial frequencies.
Runs of zeros and an end-of-block symbol shorten the description. Huffman codes identify these symbols; extra bits carry the non-zero values. “Category” or “size” counts those extra value bits.
64 quantised coefficients, in zigzag order12 · 0 · 0 · −1 · 60 more zeros
The previous block’s DC is 10, so this block sends 12 − 10 = +2.
| Meaning | Table code | Value bits | Bits |
|---|---|---|---|
| DC difference +2 · category 2 | 011 | 10 | 5 |
| Two zeros, then −1 · run 2 / size 1 | 11100 | 0 | 6 |
| End of block · remaining 60 zeros | 1010 | — | 4 |
Scroll the table horizontally to see all bit fields.
Join the fields → 011101110001010 15 bits
These are real codes from JPEG’s example luminance tables. The decoder reads
a table code, then the number of value bits it specifies. For category 1,
1 means +1 and 0 means −1.
Five signed bits (−16…15) suffice per coefficient in this example. The 15 bits count only the block payload. A complete JPEG also needs headers, shared tables and padding at the end of the scan. Packing preserves the rounded coefficients exactly; it adds no picture loss.
Compare image quality and compressed size
The quality slider spans 1–100% with extra travel at low qualities: 1–20% occupies about 44% of its length. Its readout gives the encoder quality, not the slider position.
Lower JPEG quality and watch the colour boundaries, gradients, feather detail and file size. Slanted edge and Ramp with fine texture isolate edge and texture changes. Both pictures always have the same pixel dimensions. The − and + buttons on either picture change both scales together. Fit shows the complete image; 100% maps each image pixel to one CSS pixel. Drag or scroll either view to inspect the same area. Clicking compares the selected 8×8 RGB fragments under both images and updates the luma-pattern breakdown below. Zoom magnifies the existing samples without adding detail.
The image comparison needs JavaScript; the diagrams above show the static mechanism.
The original size counts raw RGB8 samples: width × height × 3 bytes. The JPEG size includes the complete file. Headers can make a tiny JPEG larger than its bitmap. Quality 100% still need not be lossless.
Predict inside the same picture
Video codecs can subtract a guess from reconstructed neighbours instead of a fixed 128. Nearby samples often leave a smaller difference to encode.
For the first sample, 42 − 40 = +2; adding 40 + 2 recovers 42. Exact differences make prediction reversible. Rounding their transform weights can introduce loss.
Other H.264 modes use left neighbours or averages. The mode tells the decoder how to form the same guess; unavailable neighbours at picture or slice boundaries limit the choices. This is intra prediction, within the current picture.
Video bitstreams also carry prediction modes, reference and motion information, and quantiser settings. The JPEG Huffman codes above do not apply to H.264’s syntax.
Where you have seen this mechanism
| Format | Familiar context | Connection to this experiment |
|---|---|---|
| JPEG (.jpg, .jpeg) | Camera photographs and images saved from websites. | 8×8 DCT, quantisation tables and entropy coding. |
| Motion JPEG (MJPEG) | Some camera recordings and JPEG video feeds. | Each frame is a separate JPEG image. |
| MPEG-2 Video (H.262) | DVD-Video and digital television recordings. | 8×8 DCT for picture data or motion-prediction residuals. |
| H.264/AVC and H.265/HEVC | Compressed camera video, downloaded video and streams. | Integer transforms, prediction and filtering; codec-specific block sizes. |
| Lossy WebP | Images served by websites. | VP8-based prediction and transforms; a separate lossless mode. |
| AVIF / AV1 | AVIF web images and AV1 video. | AVIF stores AV1-coded images; several transform types and sizes. |
MP4 and MOV are containers; check the codec inside. PNG uses lossless filtering and DEFLATE, rather than this DCT-rounding process. “JPEG” here means ordinary lossy DCT JPEG.
What this model leaves out
The static transform uses an orthonormal 8×8 DCT and uniform rounding, with half-step ties away from zero. A constant shifted block of 12 has DC weight 96 and all other weights zero. Values clip and round only for display.
The application uses the browser’s JPEG encoder, which chooses quantisation tables and chroma sampling. The colour test chart is encoded at its native 2048×2048 resolution, preserving the diagnostic patterns before JPEG encoding; procedural targets are 16×16. Fit only changes the displayed scale. The block view reads luma coefficients from that same file. Its floating-point inverse DCT can differ slightly from browser integer rounding. It supports baseline single-scan grey and Y′CbCr JPEG; other processes retain the full image comparison without the block view.
Continue with How video compression uses other frames to predict from other pictures.
References
-
ITU-T T.81 / ISO/IEC 10918-1 — Digital compression and coding of continuous-tone still images: §A.3 defines the 8×8 DCT, quantisation and inverse operations; Annex F specifies sequential DCT coding and decoding, including the block inspector’s Huffman decoding and inverse transform; Tables K.3 and K.5 supply the example luminance Huffman codes used above.
-
Iain Richardson — H.264/AVC Intra Prediction: directional prediction from reconstructed neighbours within the same picture.
-
Iain Richardson — H.264/AVC 4×4 Transform and Quantization: comparison with the integer transforms and scaling used by a video codec; the floating-point 8×8 illustration is not an H.264 implementation.
-
RFC 2435 — RTP Payload Format for JPEG-compressed Video, §1: independently JPEG-compressed frames in a video stream.
-
Library of Congress — MPEG-2 Video Encoding (H.262): DCT and motion-prediction mechanisms, DVD-Video and broadcast use.
-
Iain Richardson — HEVC: An introduction to high efficiency coding: prediction, transform sizes and reconstruction in H.265/HEVC.
-
Google — WebP Compression Techniques: the distinct lossy VP8-based and lossless paths.
-
Alliance for Open Media — AV1 Image File Format and AV1 specification: AVIF’s AV1 image payload and the available transform families.
-
W3C — PNG specification, compression: lossless compression distinct from the DCT model.
-
WHATWG — Canvas image serialization: real JPEG output and the encoder quality argument used by the file-size experiment.