Skip to content
Crop DeskFree tools for photos, PDFs & everyday work Search tools…/Search tools…/Search tools…/Get apps

Home › Guides

How JPEG Compression Works: YCbCr, Chroma Subsampling, DCT and Quantization

By Mahendra Singh Deora · Published 23 November 2026 · 6 min read

Every time you press "compress" on a photo, a JPEG encoder runs a pipeline defined in ITU-T Recommendation T.81 (also published as ISO/IEC 10918-1). It is worth understanding if you study computer science, electronics or IT. It also explains everyday problems: why a photo gets blurry at small KB sizes, why text and signatures get "halos", and why saving a JPG again and again makes it worse. This article walks through the baseline JPEG pipeline step by step, with small worked numbers.

In short: JPEG converts colour to brightness plus two colour-difference channels, often stores the colour channels at lower resolution, cuts each channel into 8 × 8 blocks, and transforms each block with the discrete cosine transform (DCT). It then divides the coefficients by a quantization table and rounds them – the lossy step – and finally packs the result with entropy coding.

Step 1: RGB to YCbCr

Camera and screen pixels are red, green and blue. JPEG files using the common JFIF format store them as Y (luminance, or brightness) and Cb and Cr (blue-difference and red-difference chroma). The JFIF specification gives the conversion for 8-bit values:

Y  =  0.299 R + 0.587 G + 0.114 B
Cb = −0.1687 R − 0.3313 G + 0.5 B + 128
Cr =  0.5 R − 0.4187 G − 0.0813 B + 128

Worked example (example pixel, a skin tone R = 200, G = 150, B = 120): Y ≈ 161.5, Cb ≈ 104.6, Cr ≈ 155.4. For a neutral grey (128, 128, 128) you get Y = 128 and Cb = Cr = 128 exactly. Grey has no colour difference, which is why 128 is the "zero" point of the chroma channels. See RGB, CMYK and YCbCr colour models for more on why this split is useful.

Step 2: chroma subsampling

Human vision is more sensitive to fine detail in brightness than in colour. T.81 lets each component have its own horizontal and vertical sampling factors. A very common choice gives Y factors of 2 × 2 and Cb and Cr factors of 1 × 1. That means one Cb and one Cr sample for every 2 × 2 block of Y samples, usually called 4:2:0.

LayoutSamples per pixelData compared with full RGB
Full resolution, 3 channels1 + 1 + 1 = 3100%
Y full, Cb and Cr halved horizontally (4:2:2)1 + 0.5 + 0.5 = 2≈ 67%
Y full, Cb and Cr halved both ways (4:2:0)1 + 0.25 + 0.25 = 1.550%

Half the data has gone before any "real" compression starts. This is also why thin red text or a coloured stamp can look smeared in a JPG: its colour is stored at a quarter of the resolution.

Step 3: 8 × 8 blocks and the DCT

Each component is divided into blocks of 8 × 8 samples. Values are level-shifted (128 is subtracted from 8-bit samples). Each block then goes through the forward discrete cosine transform, which T.81 defines in its Annex A. The DCT does not lose information by itself. It rewrites the 64 sample values as 64 coefficients, each giving how much of one cosine pattern is in the block:

  • The top-left coefficient is the DC coefficient, which is proportional to the block's average brightness.
  • The other 63 are AC coefficients for higher and higher spatial frequencies: slow gradients near the top-left, fine detail and sharp edges towards the bottom-right.

In a typical photo most of the energy sits in a few low-frequency coefficients. Smooth areas such as skin, sky or a plain background produce blocks where nearly all the high-frequency coefficients are close to zero.

Step 4: quantization – where quality is lost

T.81 defines a quantization table as "the set of 64 quantization values used to quantize the DCT coefficients". Each coefficient is divided by its table entry and rounded to the nearest integer. The decoder later multiplies it back. Rounding is the only step that throws information away on purpose.

Example coefficientExample table valueStored (rounded)Decoder rebuildsError
260 (DC)16round(16.25) = 162564
−45 (low AC)12round(−3.75) = −4−483
−7 (high AC)40round(−0.175) = 007 (detail gone)

Larger table values mean more coefficients become zero, so the file gets smaller and the image loses fine detail. The "quality" slider in an app is not part of T.81. It is an encoder setting, and encoders normally use it to choose how coarse the quantization tables are. On the web, the canvas toBlob() method takes a quality number between 0 and 1 for lossy formats such as image/jpeg.

Step 5: zig-zag order and entropy coding

The quantized coefficients are read in the zig-zag sequence, which T.81 describes as an ordering "from (approximately) lowest spatial frequency to highest". This puts the many zeros at the end of the list, where they can be coded very compactly. The DC value is coded as the difference from the previous block's DC. The AC values are coded as runs of zeros plus a value. Baseline JPEG uses Huffman coding for this step. T.81 also defines arithmetic coding, and progressive and lossless modes, though most everyday JPGs are baseline or progressive Huffman-coded files.

What this explains in practice

What you seeCause in the pipelineWhat to do
Blocky squares at very small KB sizesEach 8 × 8 block quantized so hard it keeps only its DC averageReduce pixel dimensions first, then use a higher quality setting – see KB, MB and pixels
Ringing or "halos" around signature strokes and textSharp edges need high-frequency coefficients, which quantization removesIncrease contrast, use a slightly larger pixel size, or use PNG where allowed
Smeared red stampsChroma subsamplingKeep stamps large in the frame
Quality drops after each re-saveEvery re-encode quantizes againAlways start from the original photo
Plain background compresses wellFlat blocks have almost no AC energyA plain, evenly lit background gives smaller, cleaner files

A quick lab exercise

  1. Take one photo and export it at quality 0.9, 0.7, 0.5 and 0.3 using the image compressor. Note the KB each time.
  2. Zoom to 400% on an edge (hair against the background, or a signature stroke) and compare.
  3. Repeat with a flat graphic, such as a screenshot of text. You will see far stronger artifacts at the same quality. Compare the result with a PNG of the same image; PNG and DEFLATE explains why the PNG wins for that kind of image.

File size is roughly proportional to the number of non-zero coefficients left after quantization, not just to the pixel count. That is why two photos with the same dimensions can produce very different KB sizes at the same quality setting.

See quantization in action: export one photo at several quality levels.

Open the image compressor

Frequently asked questions

Which step of JPEG loses quality?

Quantization. The colour conversion and the DCT are (almost) reversible; dividing DCT coefficients by the quantization table and rounding is what discards detail. Chroma subsampling also discards colour resolution.

Why does JPEG work on 8×8 blocks?

T.81 partitions each component into 8×8 blocks and transforms each block with the DCT. Small blocks keep the computation simple and let the encoder adapt to local detail.

Is the quality slider part of the JPEG standard?

No. T.81 defines quantization tables, not a quality number. Encoders map their quality setting to how coarse the tables are, so the same number can give different results in different apps.

Sources

Sources checked on 23 November 2026. Official notices and orders change; the latest version on the issuing body's website is final.