Preprocessing and Quantization

Baseline JPEG first prepares the image, then transforms and quantizes each block. This note covers those stages and the artefacts they cause.

Preprocessing

Preprocessing converts the colour space to YCrCb, reduces the chroma resolution, splits each component into blocks and centres the sample values. The stages run in that order.

Colour Space Conversion

For 8-bit samples, the JFIF conversion is:

Y=0.299 R+0.587 G+0.114 BCb=−0.1687 R−0.3313 G+0.5 B+128Cr=0.5 R−0.4187 G−0.0813 B+128\begin{aligned} Y &= 0.299\,R + 0.587\,G + 0.114\,B \\ C_b &= -0.1687\,R - 0.3313\,G + 0.5\,B + 128 \\ C_r &= 0.5\,R - 0.4187\,G - 0.0813\,B + 128 \end{aligned}

YY is a weighted sum of the primaries, with weights matching the eye’s sensitivity (green highest, blue lowest). CbC_b is a scaled B−YB - Y and CrC_r is a scaled R−YR - Y. The +128 offset keeps CbC_b and CrC_r inside [0,255][0, 255].

The standard itself does not fix the colour space. JFIF and Exif specify the conversion.

The inverse (for the above-mentioned conversion) is:

R=Y+1.402 (Cr−128)G=Y−0.3441 (Cb−128)−0.7141 (Cr−128)B=Y+1.772 (Cb−128)\begin{aligned} R &= Y + 1.402\,(C_r - 128) \\ G &= Y - 0.3441\,(C_b - 128) - 0.7141\,(C_r - 128) \\ B &= Y + 1.772\,(C_b - 128) \end{aligned}

Chroma Subsampling

The eye is less sensitive to color detail than to brightness detail, so reducing chroma resolution removes psychovisual redundancy early.

Subsampling is written as J:a:bJ{:}a{:}b over a block 4 pixels wide and 2 rows high.

  • 4:4:4
    No subsampling. Cb and Cr have full resolution.
  • 4:2:2
    Chroma halved horizontally. Chroma carries half the samples of luma.
  • 4:2:0
    Chroma halved in both directions. Chroma carries a quarter of the samples of luma.

Each chroma sample is the average of the luma-resolution samples it covers. For 4:2:0, a 2x2 average. The decoder upsamples by replication or interpolation.

4:2:0 reduces the sample count to 1+14+14=1.51 + \frac{1}{4} + \frac{1}{4} = 1.5 samples per pixel, from 3. This gives a 2:1 reduction before any DCT work.

Block

Each component is split into 8x8 blocks (also known as tiles). Every later stage processes a block independently of the others.

Each component ii has a sampling factor pair (Hi,Vi)(H_i, V_i). For an image of X×YX \times Y pixels:

xi=⌈X⋅HiHmax⁡⌉yi=⌈Y⋅ViVmax⁡⌉x_i = \left\lceil X \cdot \frac{H_i}{H_{\max}} \right\rceil \qquad y_i = \left\lceil Y \cdot \frac{V_i}{V_{\max}} \right\rceil
Here:
xi,yix_i, y_i
dimensions of component ii in samples
Hmax⁡,Vmax⁡H_{\max}, V_{\max}

largest sampling factors across all components

Minimum Coded Unit

Also known as MCU. Unit of coding. Blocks of the different components are interleaved in the output.

An MCU holds Hi×ViH_i \times V_i blocks of each component, in raster order within the component, components in order.

  • 4:4:4
    MCU covers 8x8 pixels and holds 3 blocks (Y, Cb, Cr).
  • 4:2:0
    MCU covers 16x16 pixels and holds 6 blocks (four Y, one Cb, one Cr).

The image is padded to a whole number of MCUs, in both directions, by replicating the last row and column. Padding with edge values avoids a strong artificial edge, which would inflate the high-frequency coefficients of the border blocks.

Level Shift

For PP-bit samples, subtract 2P−12^{P-1} from every sample. Samples become centred on 0 and signed. The range becomes symmetric, [−2n,2n−1][-2^{n}, 2^{n}-1].

Discrete Cosine Transform

Each level-shifted 8x8 block goes through the 2D DCT with N=8N = 8.

F(u,v)=14C(u)C(v)∑x=07∑y=07f(x,y)cos⁡(2x+1)uπ16cos⁡(2y+1)vπ16F(u, v) = \frac{1}{4} C(u) C(v) \sum_{x=0}^{7} \sum_{y=0}^{7} f(x, y) \cos\frac{(2x+1)u\pi}{16} \cos\frac{(2y+1)v\pi}{16}
f(x,y)=14∑u=07∑v=07C(u)C(v)F(u,v)cos⁡(2x+1)uπ16cos⁡(2y+1)vπ16f(x, y) = \frac{1}{4} \sum_{u=0}^{7} \sum_{v=0}^{7} C(u) C(v) F(u, v) \cos\frac{(2x+1)u\pi}{16} \cos\frac{(2y+1)v\pi}{16}
Here:
x,yx, y
column and row of a sample in the block
u,vu, v
horizontal and vertical frequency indices, from 0 to 7
C(k)C(k)
12\frac{1}{\sqrt{2}} for k=0k = 0, otherwise 1
F(0,0)F(0, 0)
DC coefficient, equal to 8 times the block mean
F(u,v) for (u,v)≠(0,0)F(u, v) \ \text{for} \ (u, v) \neq (0, 0)
AC coefficients

In matrix form, F=T f T⊤\mathbf{F} = \mathbf{T}\, \mathbf{f}\, \mathbf{T}^\top with T(k,n)=C(k)2cos⁡(2n+1)kπ16T(k, n) = \frac{C(k)}{2} \cos\frac{(2n+1)k\pi}{16}. Both arrays are stored row first, so f[y][x]=f(x,y)\mathbf{f}[y][x] = f(x, y) and F[v][u]=F(u,v)\mathbf{F}[v][u] = F(u, v). The zig-zag scan used in entropy coding indexes F\mathbf{F} in this layout.

The transform alone is lossless up to rounding. Energy concentrates in the low-frequency coefficients near the top-left of F\mathbf{F}. Most high-frequency coefficients are small. Since the input is level shifted, the DC coefficient is small and signed.

For 8-bit input, F(0,0)∈[−1024,1016]F(0, 0) \in [-1024, 1016] and every AC coefficient fits in the range [−1023,1023][-1023, 1023], so coefficients fit in 11 bits.

Quantization

Each coefficient is divided by the matching entry of an 8x8 quantization table QQ and rounded:

Fq(u,v)=round(F(u,v)Q(u,v))F_q(u, v) = \text{round}\left(\frac{F(u, v)}{Q(u, v)}\right)
  • Entries of QQ grow towards the high-frequency corner, since the eye tolerates error there better
  • Many high-frequency coefficients become 0
  • A quality setting scales QQ. Larger entries give a smaller file and more visible artefacts
  • The decoder multiplies by Q(u,v)Q(u, v). The rounding error cannot be recovered
  • Fast DCT implementations leave the transform unscaled and fold the scale factors into the divisors

At high compression ratios the 8x8 tile structure becomes visible as blocking artefacts.

The decoder reconstructs F^(u,v)=Fq(u,v) Q(u,v)\hat{F}(u, v) = F_q(u, v)\, Q(u, v). Rounding bounds the error per coefficient:

∣F(u,v)−F^(u,v)∣≤Q(u,v)2\left| F(u, v) - \hat{F}(u, v) \right| \le \frac{Q(u, v)}{2}

A coefficient with ∣F(u,v)∣<Q(u,v)/2|F(u, v)| < Q(u, v)/2 quantizes to 0 and its whole value is lost. For a coefficient that survives, the error is close to uniform, with variance Q(u,v)2/12Q(u, v)^2 / 12.

The DCT preserves energy, so the squared error summed over the coefficients of a tile equals the squared error summed over its pixels, before pixel rounding and clamping.

Quantization Tables

Annex K of the standard gives example tables, tuned for viewing conditions and not mandated. Luminance:

1611101624405161
1212141926586055
1413162440576956
1417222951878062
182237566810910377
243555648110411392
49647887103121120101
7292959811210010399

Chrominance:

1718244799999999
1821266699999999
2426569999999999
4766999999999999
9999999999999999
9999999999999999
9999999999999999
9999999999999999
  • The chrominance table is coarser, since the eye resolves colour detail poorly
  • Entries lie in [1,255][1, 255] for 8-bit tables
  • The table in use is stored in the file, so a decoder never assumes the Annex K values

Quality Scaling

Most encoders expose a quality q∈[1,100]q \in [1, 100] and scale the Annex K tables (the Independent JPEG Group convention):

s={5000/qq<50200−2qq≥50Q′(u,v)=clamp(⌊s Q(u,v)+50100⌋,1,255)s = \begin{cases} 5000 / q & q < 50 \\ 200 - 2q & q \ge 50 \end{cases} \qquad Q'(u, v) = \text{clamp}\left( \left\lfloor \frac{s\, Q(u, v) + 50}{100} \right\rfloor, 1, 255 \right)
  • q=50q = 50 gives s=100s = 100, so the table is unchanged
  • q=100q = 100 gives s=0s = 0, so every entry clamps to 1
  • Lower qq gives a larger ss, larger divisors and a smaller file

Artefacts

  • Blocking
    Visible 8x8 grid in smooth regions. Each block quantizes independently, so neighbouring blocks get different average values.
  • Ringing
    Ripples around sharp edges. Dropping high-frequency coefficients leaves a truncated series for the edge, like the Gibbs effect.
  • Mosquito noise
    Flickering specks around moving edges in video made of such frames. Edge blocks are quantized differently from frame to frame.
  • Colour bleeding
    Chroma smears across edges. Caused by chroma subsampling and the coarse chrominance table.
  • Generation loss
    Re-encoding a decoded image quantizes again and compounds the error. The loss shrinks when the tables and block grid are identical.

Worked Example

Quantizing a DCT Coefficient

A DCT coefficient is F(u,v)=37F(u,v) = 37. Its quantization-table entry is Q(u,v)=12Q(u,v) = 12. Find the quantized and reconstructed coefficients.

Solution Reveal Hide
Fq(u,v)=round⁡(3712)=3F_q(u,v) = \operatorname{round}\left(\frac{37}{12}\right) = 3F^(u,v)=3⋅12=36\hat{F}(u,v) = 3 \cdot 12 = 36

The reconstructed coefficient is 3636. Its error is 11.

Written by November 3, 2026 7 min read
Was this helpful?