Guide · Mechanics

What the encoder
actually does

JPEG, WebP and AVIF are different architectures built from the same five stages. Once you can name the stages, every artefact has an obvious cause.

Colour transform · Transform · Quantisation · Entropy coding
5 shared stages1 lossy step÷8 block size
The pipeline

Five stages, one of them irreversible

Every codec in this family is a chain of reversible steps wrapped around a single lossy decision. That decision is quantisation.

Reference

From pixels to coefficients

An encoder reads a grid of pixels and writes a stream of numbers. Between those two states sit five stages, and only one of them destroys information.

A pixel grid is a terrible format for storage. Neighbouring pixels are almost identical, so the raw representation repeats itself endlessly. Compression works by translating the picture into a basis where that redundancy is explicit — usually a frequency basis, where smooth areas produce one large number and dozens of near-zero ones.

What the decoder can undo

Four of the five stages are exactly reversible: the colour transform, the block transform, the entropy decoder and the container framing. Quantisation is not. Once a coefficient is rounded, the decoder has no way to know what it was — it only knows what it became. Every visible artefact traces back to that rounding.

Stage 1–2

Colour transform and subsampling

Before any transform, RGB is converted to a luma/chroma space, and the chroma channels are usually stored at half resolution.

01 01
RGB → YCbCr

Luma (Y) approximates perceived brightness; Cb and Cr carry colour difference. The split is what makes the next step possible.

02 02
Subsample the chroma

4:2:0 keeps one colour sample per 2×2 block of luma. That is an immediate 50% saving on colour data, and it is invisible for most photographs.

03 03
Block the luma

The image is cut into 8×8 blocks (JPEG) or larger transform units (AV1, HEVC), each processed independently — which is why block edges become visible under pressure.

04 04
Transform

A DCT (or an integer approximation of one) converts each block into frequency coefficients. Smooth blocks concentrate energy in the top-left corner.

Subsampling is why thin coloured type suffers first. Luma survives; the colour under it does not.

Stage 3

Quantisation is the lossy step

Each coefficient is divided by a value and rounded. Small coefficients become zero, larger ones lose precision. The quality slider chooses the divisor.

CoefficientAfter quantisationWhat the viewer sees
Large, low frequencyKept almost exactlyOverall shape and tone — preserved
Medium frequencyRounded to a coarse stepFine texture softens
Small high frequencyBecomes zeroDetail disappears; edges ring
All coefficients in a blockCollapse to one valueFlat 8×8 patch — blocking

Because the divisor is applied per coefficient band, quality settings are not linear. Moving from 95 to 85 changes the byte count far more than moving from 85 to 75 — a consequence of how many coefficients are already zero.

See lossy vs lossless for what this means when you need exact pixels.

Stage 4

Entropy coding is the lossless part

After quantisation the stream is mostly zeros and small numbers. Entropy coding stores that pattern in the fewest bits possible — without changing a single value.

pipeline.txt 8·8 block of coefficients → run-length + Huffman symbols2–4× smaller
Run-length coding Long runs of zeros become a count, not a thousand separators.
Huffman coding Frequent symbols get short codes. JPEG's classic tables are tuned for photographs, not screenshots.
Arithmetic and rANS Modern codecs use range coders instead, squeezing out the fraction of a bit per symbol a Huffman table cannot reach.
Predictive coding PNG takes the other route entirely: predict each pixel from its neighbours, then compress the leftover differences with deflate.
Variants

Where the formats actually differ

The five stages are universal. Formats differ in block size, prediction, entropy coder and how many passes they make over the image.

JPEG 8×8 DCT, Huffman, one pass. Ancient, universal, still fine for photographs.
PNG No transform and no quality dial: predictive filters plus deflate. Exact output, poor ratios on photos.
WebP Two codecs in one file: VP8-style DCT for lossy, VP8L prediction for lossless, plus alpha and animation.
AVIF AV1 intra frames: variable transform sizes, directional prediction, range coding. Slowest to encode, smallest output.

The capability differences are tabulated in image formats compared. That divergence explains the practical advice in the compression guide: choose the codec, then spend quality. The reverse order wastes both. For how much each choice saves on real files, see the benchmarks; for the near-lossless workflow, see compressing without visible loss.

FAQ

Mechanics — FAQ

Is the DCT the same in every codec?

Conceptually yes; numerically no. JPEG uses a specific 8×8 floating-point DCT that implementations approximate with integers. AV1 and HEVC allow several transform sizes and integer approximations of their own.

Why do some encoders produce smaller files at the same quality?

Better quantisation matrices, better entropy coding, and psychovisual weighting that spends bits where the eye looks. mozjpeg is a well-tuned JPEG, not a new format.

Can quantisation be undone?

No. The decoder reconstructs an approximation, and the discarded coefficients are gone. Re-encoding can only approximate the approximation.

Does metadata survive compression?

It depends on the tool, not the format. Metadata is a separate container section; strippers remove it entirely, and stripping is often the easiest few kilobytes to save.

Reference · Mechanics

See the stages on your own files

Pick a quality, compare before and after, and watch which stage you are paying for.

Windows 10/11 · Offline · Free