Salta al contenuto
Note per Studenti Image Coding - DCT and JPEG Report

Image Coding - DCT and JPEG

In questa pagina 6

A colour image takes 24 bits per pixel (12 in 4:2:0, see Digital Representation of Audio and Image SignalsAnalog-to-digital conversion has three steps, sampling at Fs at least twice the signal bandwidth after an anti-aliasing low-pass filter, uniform quantization on L = 2^b levels, and binarization of each level into b bits, giving a nominal bit-rate R = Fs times b times the number of channels (speech 64 kbit/s, CD audio 1.411 Mbit/s); an image is a two-dimensional sampled signal with 8 bits per grey level or 24 bits per RGB pixel (12 in YCbCr 4:2:0), and quality is measured with MSE and PSNR.Digital Representation of Audio and Image Signals →). Lossless coding (Source Coding and Compression - Entropy, Huffman and the Quality-Bitrate Trade-offThe information of an event of probability p is log2(1/p) bits and the entropy H = sum p log2(1/p) is the average information of a source; lossless source coding maps source words to variable-length prefix codewords (shorter for likelier words), the Kraft-McMillan inequality constrains the lengths, Shannon's theorem gives H/log2(My) <= L <= H/log2(My) + 1, Huffman coding is optimal among prefix codes; lossy compression gives up exactness to reach a lower rate, and rate, quality, complexity, delay and robustness are conflicting parameters.Source Coding and Compression - Entropy, Huffman and the Quality-Bitrate Trade-off →) on images does not reduce the size much: grey levels are almost equiprobable. To reduce it by a factor of 10 or more a loss must be accepted: this note explains how the JPEG standard, the most widespread image format, does it, passing through linear transforms and the DCT. (The program of the laboratory lists the principles of JPEG coding; the content below follows the site's certified notes on image compression.)

1. Why a linear transform

The only tool to reduce the bit-rate with loss is quantization. But samples have already been quantized in the ADC: quantizing them again (and directly) degrades quality very fast, because in the original domain multimedia signals are not sparse. For a dataset of grey images the distribution of the brightness of one pixel covers all levels; instead the joint distribution of two adjacent pixels is very concentrated (near the diagonal: neighbouring pixels look alike), a sign of strong dependence.

The idea: find a sparse representation, in which some coefficients "are not important" and quantizing them coarsely introduces an imperceptible degradation. One way is to apply an orthogonal linear transform (a rotation of the axes) to the vector of NN samples x=[s(n+1),…,s(n+N)]T\mathbf x=[s(n+1),\dots,s(n+N)]^T:

y=Ax,y~=[Q(y1,Δ1),…,Q(yN,ΔN)],x~=A−1y~=ATy~.\mathbf y=A\mathbf x,\qquad \tilde{\mathbf y}=\bigl[Q(y_1,\Delta_1),\dots,Q(y_N,\Delta_N)\bigr],\qquad\tilde{\mathbf x}=A^{-1}\tilde{\mathbf y}=A^T\tilde{\mathbf y}.

Each transform component has its own step Δi\Delta_i. If the transform is "sparsifying", many yiy_i are small and approximating them by zero costs little error.

Why orthogonal. For an orthogonal matrix A−1=ATA^{-1}=A^T (so it is inverted at low cost), and above all the transform preserves distances. The reconstruction error is

MSE=1N∥x−x~∥2=1N(ATy−ATy~)T(ATy−ATy~)=1N(y−y~)TAAT(y−y~)=1N∥y−y~∥2.\mathrm{MSE}=\frac1N\|\mathbf x-\tilde{\mathbf x}\|^2=\frac1N(A^T\mathbf y-A^T\tilde{\mathbf y})^T(A^T\mathbf y-A^T\tilde{\mathbf y})=\frac1N(\mathbf y-\tilde{\mathbf y})^TAA^T(\mathbf y-\tilde{\mathbf y})=\frac1N\|\mathbf y-\tilde{\mathbf y}\|^2.

So a small error on the coefficients gives a small error on the pixels, and quantization can be optimised directly on the transform.

Example of a sparse signal. The pure tone x(n)=cos⁡(2πf0n)x(n)=\cos(2\pi f_0n) has all samples "important"; transformed with a matrix that performs frequency analysis it gives one large coefficient and N−1N-1 negligible ones.

2. From the 1-D to the 2-D transform

A 1-D transform is extended to images by applying it to the columns and then to the rows. For an N×NN\times N image, X=[x1 … xN]X=[\mathbf x_1\ \dots\ \mathbf x_N] (columns): transform on the columns, Y~=AX\tilde Y=AX; transform on the rows of Y~\tilde Y by multiplying on the right by ATA^T: Y=Y~ATY=\tilde YA^T. In the end

Y=A X AT,X=AT Y A.\boxed{Y=A\,X\,A^T},\qquad X=A^T\,Y\,A.

As in the discrete Fourier transform (see Discrete Fourier TransformHeuristic derivation of the discrete Fourier transform for N-periodic discrete-time signals: complex exponentials with period N, the finite basis of N modes obtained from the difference equation x(k)-x(k-N)=0, orthogonality, the coefficient formula and the definition of the DFT with its periodicity and matrix form.Discrete Fourier Transform →), which writes a sampled signal as a sum of sinusoids, the coefficients represent spatial frequencies: rate of variation horizontally and vertically. Natural images are low-pass signals: the energy is at low frequencies (high frequencies are edges and fine details).

3. The DCT

The DCT (discrete cosine transform) is a variant of the DFT: it produces real values, uses only positive frequencies and sparsifies better (it concentrates even more energy in the first coefficients; the reason is that the DCT is equivalent to a DFT of a symmetrically extended signal, so it has none of the edge discontinuities that the DFT of a block produces). For an N×NN\times N block (here N=8N=8):

Y(u,v)=2N c(u) c(v)∑m=0N−1∑n=0N−1x(m,n)cos⁡(2m+1)uπ2Ncos⁡(2n+1)vπ2N,c(0)=12, c(k)=1 (k>0).Y(u,v)=\frac2N\,c(u)\,c(v)\sum_{m=0}^{N-1}\sum_{n=0}^{N-1}x(m,n)\cos\frac{(2m+1)u\pi}{2N}\cos\frac{(2n+1)v\pi}{2N},\quad c(0)=\tfrac1{\sqrt2},\ c(k)=1\ (k>0).

In matrix form it is Y=AXATY=AXA^T with Aum=2Nc(u)cos⁡(2m+1)uπ2NA_{um}=\sqrt{\tfrac2N}c(u)\cos\frac{(2m+1)u\pi}{2N}, which is orthogonal. The index uu is the vertical frequency (the row), vv the horizontal one (the column). The DCT bases are cosines of increasing frequency: each coefficient Y(u,v)Y(u,v) is the inner product of the block with the base image Bu,v(m,n)=cos⁡(2m+1)uπ2Ncos⁡(2n+1)vπ2NB_{u,v}(m,n)=\cos\frac{(2m+1)u\pi}{2N}\cos\frac{(2n+1)v\pi}{2N} (up to normalisation). Low frequencies are in the top left (the coefficient (0,0)(0,0) is proportional to the mean value and is called DC; the others are the AC coefficients).

1-D example (ramp). The vector x=[10,12,14,16,18,20,22,24]\mathbf x=[10,12,14,16,18,20,22,24] has energy ∑xi2=2480\sum x_i^2=2480. Its orthonormal DCT is [48.08, −12.88, 0, −1.35, 0, −0.40, 0, −0.10][48.08,\ -12.88,\ 0,\ -1.35,\ 0,\ -0.40,\ 0,\ -0.10]: all the energy (the sum of squares is still 24802480, by orthogonality) is in the first two coefficients (48.082+12.882=247848.08^2+12.88^2=2478), the others are almost zero. Quantizing with a large step the small coefficients become 0 and only 2-3 values out of 8 are transmitted.

Block DCT. The image is divided into blocks and the DCT is computed on each: so local characteristics are analysed (the signal is sparser where there are no details or edges; a block of sky has almost only the DC). JPEG uses 8×88\times8 blocks, adequate to the resolutions of the time (1991-92); the drawback is that dependencies between distant pixels are not exploited (except the mean of the blocks, see the coding of the DC).

4. JPEG baseline

The JPEG standard (defined in 1991, adopted in 1992) represents colour images (3 channels) or grey images (1 channel). Every channel is treated the same way: one channel is described. The standard defines the decoder and leaves freedom to encoders (different implementations compete but interoperate).

Scheme. One channel (a matrix of values between 0 and 255):

  1. centre it: subtract 128 (values between −128-128 and 127127);
  2. divide it into 8×88\times8 blocks;
  3. each block undergoes DCT, quantization, lossless coding;
  4. other syntactic elements (headers, tables) are added.

For colour images one converts to YCbCr, subsamples the chrominance (4:2:0) and codes each component separately; a different table is used for CbC_b and CrC_r.

4.1 Quantization

Uniform quantization turns the real value xx into the quantization index xind=round(x/Δ)x_{\text{ind}}=\mathrm{round}(x/\Delta); in decoding the reconstructed value is x~=Δ xind\tilde x=\Delta\,x_{\text{ind}}, the nearest multiple of Δ\Delta; the maximum error is Δ/2\Delta/2. Large step: strong quantization and low rate; small step: better quality and higher rate.

JPEG allows a different step for each spatial frequency: a quantization table q(i,j)∈{1,…,256}q(i,j)\in\{1,\dots,256\} and DCTind(i,j)=round ⁣(DCT(i,j)q(i,j))\mathrm{DCT}_{\text{ind}}(i,j)=\mathrm{round}\!\left(\frac{\mathrm{DCT}(i,j)}{q(i,j)}\right). The standard does not define the table: the encoder chooses it and writes it in the file (64 bytes per table), because the decoder needs it to rebuild the values. Why not fix it? So that every implementation can pick the best quantization strategy.

The table used de facto for luminance, obtained from subjective tests of perceived quality, is:

q∗=(1611101624405161121214192658605514131624405769561417222951878062182237566810910377243555648110411392496478871031211201017292959811210010399)q^*=\begin{pmatrix}16&11&10&16&24&40&51&61\\12&12&14&19&26&58&60&55\\14&13&16&24&40&57&69&56\\14&17&22&29&51&87&80&62\\18&22&37&56&68&109&103&77\\24&35&55&64&81&104&113&92\\49&64&78&87&103&121&120&101\\72&92&95&98&112&100&103&99\end{pmatrix}

Another table is used for chrominance. Small values at the top left, large at the bottom right, for two reasons: (1) the DCT concentrates information at low frequencies (so fine steps are needed there, and almost no bits at high frequencies); (2) the eye is more sensitive to low and medium-low frequencies, so small steps are placed where the error is most visible.

Quality factor. A single table is not enough (to compress more or have more quality): it is multiplied by a scale factor. A parameter Q∈[1,100]Q\in[1,100] is defined and

SF={5000/QQ≤50200−2Q50<Q≤991Q=100,q(i,j)=min⁡ ⁣(256, ⌈SF100 q∗(i,j)⌉).\mathrm{SF}=\begin{cases}5000/Q&Q\le50\\200-2Q&50<Q\le99\\1&Q=100\end{cases},\qquad q(i,j)=\min\!\left(256,\ \left\lceil\frac{\mathrm{SF}}{100}\,q^*(i,j)\right\rceil\right).

With Q=50Q=50 the table does not change; with Q<50Q<50 it is multiplied by a number greater than 1 (worse quality); with Q=1Q=1 it is multiplied by 50; with Q=100Q=100 all values are 1. Rounding is upwards (values must be positive integers) and truncated at 256 to fit in a byte.

4.2 A complete example: one 8×88\times8 block

Centred luminance block (values already decreased by 128):

X=(−192−5−2−1217−14−145−4−321636−34−52−3−362231−4970−4−2112910−42140−622033−11−3312−7−792421−11−263−1101627−8−6−52−1072027−2833)X=\begin{pmatrix}-19&2&-5&-2&-1&2&17&-14\\-14&5&-4&-3&2&16&36&-34\\-5&2&-3&-3&6&22&31&-49\\7&0&-4&-2&11&29&10&-42\\14&0&-6&2&20&33&-11&-33\\12&-7&-7&9&24&21&-11&-26\\3&-11&0&16&27&-8&-6&-5\\2&-10&7&20&27&-28&3&3\end{pmatrix}

Its DCT Y=AXATY=AXA^T has these coefficients (the DC is at the top left):

Y≈(8.58.3−58.759.1−20.319.5−21.413.2−11.1−15.07.617.3−61.612.8−15.7−9.7−3.6−18.413.1−52.219.62.0−20.815.2−2.61.6−0.64.513.0−16.115.6−13.4−0.53.65.1−7.98.8−7.05.7−1.1−4.2−1.20.5−5.14.8−0.61.50.7−2.32.6−1.52.0−0.32.0−0.30.4−0.70.9−0.8−2.01.7−2.60.70.1)Y\approx\begin{pmatrix}8.5&8.3&-58.7&59.1&-20.3&19.5&-21.4&13.2\\-11.1&-15.0&7.6&17.3&-61.6&12.8&-15.7&-9.7\\-3.6&-18.4&13.1&-52.2&19.6&2.0&-20.8&15.2\\-2.6&1.6&-0.6&4.5&13.0&-16.1&15.6&-13.4\\-0.5&3.6&5.1&-7.9&8.8&-7.0&5.7&-1.1\\-4.2&-1.2&0.5&-5.1&4.8&-0.6&1.5&0.7\\-2.3&2.6&-1.5&2.0&-0.3&2.0&-0.3&0.4\\-0.7&0.9&-0.8&-2.0&1.7&-2.6&0.7&0.1\end{pmatrix}

Quality Q=30Q=30: SF=5000/30≈166.7\mathrm{SF}=5000/30\approx166.7, factor 1.6671.667; the table becomes q=⌈1.667 q∗⌉q=\lceil1.667\,q^*\rceil, for example q(0,0)=⌈26.7⌉=27q(0,0)=\lceil26.7\rceil=27, q(0,1)=⌈18.3⌉=19q(0,1)=\lceil18.3\rceil=19:

q=(27191727406785102202024324497100922422274067951159424293749851451341043037629411418217212940599210713517418915482107130145172202200169120154159164187167172165)q=\begin{pmatrix}27&19&17&27&40&67&85&102\\20&20&24&32&44&97&100&92\\24&22&27&40&67&95&115&94\\24&29&37&49&85&145&134&104\\30&37&62&94&114&182&172&129\\40&59&92&107&135&174&189&154\\82&107&130&145&172&202&200&169\\120&154&159&164&187&167&172&165\end{pmatrix}

Quantization indices round(Y/q)\mathrm{round}(Y/q) (for example round(−58.7/17)=round(−3.45)=−3\mathrm{round}(-58.7/17)=\mathrm{round}(-3.45)=-3; round(59.1/27)=2\mathrm{round}(59.1/27)=2; the DC round(8.5/27)=0\mathrm{round}(8.5/27)=0):

(00−32−1000−1−101−10000−10−100000000000000000000000000000000000000000000)\begin{pmatrix}0&0&-3&2&-1&0&0&0\\-1&-1&0&1&-1&0&0&0\\0&-1&0&-1&0&0&0&0\\0&0&0&0&0&0&0&0\\0&0&0&0&0&0&0&0\\0&0&0&0&0&0&0&0\\0&0&0&0&0&0&0&0\\0&0&0&0&0&0&0&0\end{pmatrix}

Only 9 indices out of 64 are non-zero: "few values remain to be coded losslessly".

Reconstruction (decoder). Multiply by qq (Y~=indices⋅q\tilde Y=\text{indices}\cdot q, for example −3⋅17=−51-3\cdot17=-51), apply the inverse DCT and add 128 again; in practice one obtains a block that "resembles" the original (for example the first element −19+128=109-19+128=109 becomes 101101). The mean squared error of the block is MSE≈85.6\mathrm{MSE}\approx85.6, i.e. PSNR≈48.13−19.32≈28.8\mathrm{PSNR}\approx48.13-19.32\approx28.8 dB (below 30 dB, "poor" in the empirical scale: a block with a lot of detail compressed at Q=30Q=30). At high quality the error has no recognisable structure; at intermediate quality it is larger near edges (high frequencies); at low quality blocking effects appear (discontinuities between adjacent blocks).

Quality versus bits. On the same block: Q=10Q=10: 2 non-zero indices, 25.225.2 dB; Q=30Q=30: 9 indices, 28.828.8 dB; Q=50Q=50: 13, 30.030.0 dB; Q=70Q=70: 16, 31.631.6 dB; Q=90Q=90: 34, 40.840.8 dB; Q=100Q=100: 58, 56.756.7 dB. More quality means more bits and less error: this is the quality-bitrate trade-off of the codec.

4.3 Lossless coding of the coefficients

Lossless coding does not alter the values, only the number of bits.

Zig-zag scan. Non-zero values are concentrated at the top left: to put them first and leave a long run of zeros at the end the 64 coefficients are read with a zig-zag scan (alternating diagonals starting from (0,0)(0,0): (0,0),(0,1),(1,0),(2,0),(1,1),(0,2),(0,3),…(0,0),(0,1),(1,0),(2,0),(1,1),(0,2),(0,3),\dots). In the example:

0  0  -1  0  -1  -3  2  0  -1  0  0  0  0  1  -1  0  -1  -1  EOB0\ \ 0\ \ \text{-}1\ \ 0\ \ \text{-}1\ \ \text{-}3\ \ 2\ \ 0\ \ \text{-}1\ \ 0\ \ 0\ \ 0\ \ 0\ \ 1\ \ \text{-}1\ \ 0\ \ \text{-}1\ \ \text{-}1\ \ \text{EOB}

When there are no more non-zero coefficients the symbol EOB (end of block) is used. The first coefficient is the DC, the others are the AC.

DC coefficient. Two steps: prediction and category/value coding. The predictor is the DC of the previous block (the DCs of neighbouring blocks are similar: the difference is coded, a DPCM). In the example the previous block had DC =1=1, the current 00: error e=0−1=−1e=0-1=-1. The category of ee is k=⌈log⁡2(∣e∣+1)⌉k=\lceil\log_2(|e|+1)\rceil, i.e. the number of bits of ∣e∣|e| (31→31\to category 5 because 31=11111231=11111_2; −33→-33\to category 6 because 33=100001233=100001_2). The code of the category (a prefix code) is emitted, then ∣e∣|e| on kk bits, complemented bit by bit if e<0e<0. For e=−1e=-1: category 1 (code 010010), value 1→1\to complemented 00: 01000100. Another example, e=−6e=-6: category 3 (code 100100), 6=1102→0016=110_2\to001: 100001100001. DC category codes (de facto standard): 0: 0000; 1: 010010; 2: 011011; 3: 100100; 4: 101101; 5: 110110; 6: 11101110; 7: 1111011110; and so on.

AC coefficients. Two steps: run-length coding and run-category/value coding. The run RR is the number of zeros preceding the next non-zero coefficient; the symbol (R,C)(R,C) is formed with CC the coefficient value. If R≥16R\ge16 the special symbol (15,0)(15,0) (ZRL, zero run length: 16 consecutive zeros) is used first and then the rest of the run follows; if there are no more non-zero coefficients the symbol (0,0)(0,0) = EOB is used. In the example, from the first AC:

(1,−1) (1,−1) (0,−3) (0,2) (1,−1) (4,1) (0,−1) (1,−1) (0,−1) (0,0).(1,-1)\ (1,-1)\ (0,-3)\ (0,2)\ (1,-1)\ (4,1)\ (0,-1)\ (1,-1)\ (0,-1)\ (0,0).

For each symbol the category kk of CC is computed and the pair (R,k)(R,k) is coded with a prefix code (not standardised as for the DC: the two Huffman tables, DC and AC, are written in the file); the value of CC is coded as for the DC (on kk bits, complemented if negative). With the example tables:

# (R,C)(R,C) (R,k)(R,k) code of (R,k)(R,k) value bits
DC e=−1e=-1 cat. 1 010010 00 01000100
1 (1,−1)(1,-1) (1,1)(1,1) 11001100 00 1100011000
2 (1,−1)(1,-1) (1,1)(1,1) 11001100 00 1100011000
3 (0,−3)(0,-3) (0,2)(0,2) 0101 0000 01000100
4 (0,2)(0,2) (0,2)(0,2) 0101 1010 01100110
5 (1,−1)(1,-1) (1,1)(1,1) 11001100 00 1100011000
6 (4,1)(4,1) (4,1)(4,1) 111011111011 11 11101111110111
7 (0,−1)(0,-1) (0,1)(0,1) 0000 00 000000
8 (1,−1)(1,-1) (1,1)(1,1) 11001100 00 1100011000
9 (0,−1)(0,-1) (0,1)(0,1) 0000 00 000000
EOB (0,0)(0,0) 10101010

(For −3-3: 3=1123=11_2, complemented 0000; for 22: 1010.) The total stream of the block is the concatenation 0100 11000 11000 0100 0110 11000 1110111 000 11000 000 1010: 49 bits for 64 pixels, i.e. R=4964=0.766R=\frac{49}{64}=0.766 bit/pixel. The original block took 8 bits/pixel (512 bits): compression ratio 51249≈10.4\frac{512}{49}\approx10.4. (Check: 4+5+5+4+4+5+7+3+5+3+4=494+5+5+4+4+5+7+3+5+3+4=49.)

Why category/value coding makes the code instantaneous: the category tells how many bits follow; after the category code one knows exactly how many bits to read. If the first bit of the value is 1 the sign is positive; if it is 0 one complements and changes sign.

4.4 File syntax and robustness

An image is represented in a frame: the frame header contains dimensions, colour space and subsampling (4:2:0 or other). The image is divided into one or more scans, each with one component (Y, Cb, Cr); the scan header identifies the component and the quantization table. A scan consists of one or more segments (so that segments can be decoded in parallel); the segment header contains the Huffman tables; each segment is a sequence of blocks in raster order.

Since the bits are produced by variable-length codes, an error on a bit can make the decoder lose synchronisation: a JPEG image is much more compact than an uncompressed one but also much more sensitive to errors (in an uncompressed image a wrong bit changes one pixel only).

5. Extensions and other formats

JPEG has other parts: baseline (sequential, the one seen), progressive, hierarchical, lossless, JPEG-LS, Motion JPEG. In progressive JPEG the image appears in versions of increasing quality, by spectral selection (first the DC, then the AC in order) and successive approximation (by bit-plane, from the most significant).

Format Idea Notes
JPEG (1992) DCT, quantization, run-length and Huffman simple, excellent around 1 bpp, universal
PNG lossless: spatial prediction, dictionary and Huffman graphics, logos, transparency
JPEG 2000 DWT (wavelet) instead of the DCT; scalable no blocks (at high compression a slight global blur); high complexity, digital cinema, medical imaging
WebP, HEIF, AVIF derived from video codecs: variable blocks, multi-directional spatial prediction, DCT, arithmetic coding AVIF about 50% smaller than JPEG at equal quality
JPEG XL variable-size DCT; lossless transcoding of JPEG (-20% size); HDR support recovering

Typical mistakes

  • Forgetting to subtract 128.
  • Saying that the quantization table is fixed by the standard.
  • Confusing indices and reconstructed coefficients (they must be multiplied by qq).
  • Not complementing negative values; counting the EOB as a coefficient.
  • Swapping vertical and horizontal (first column = vertical variations).
  • Forgetting that the DC is coded as a difference.

Versione ripasso

Why a transform. Direct quantization of pixels degrades quality fast (samples not sparse: the joint distribution of adjacent pixels is concentrated, so they are dependent). Orthogonal transform: y=Ax\mathbf y=A\mathbf x, y~i=Q(yi,Δi)\tilde y_i=Q(y_i,\Delta_i), x~=ATy~\tilde{\mathbf x}=A^T\tilde{\mathbf y}; orthogonality (A−1=ATA^{-1}=A^T) preserves distances: MSE=1N∥x−x~∥2=1N∥y−y~∥2\mathrm{MSE}=\frac1N\|\mathbf x-\tilde{\mathbf x}\|^2=\frac1N\|\mathbf y-\tilde{\mathbf y}\|^2. 2-D: Y=AXATY=AXA^T (columns, then rows with ATA^T). Natural images are low-pass: energy at low spatial frequencies.

DCT. Y(u,v)=2Nc(u)c(v)∑∑x(m,n)cos⁡(2m+1)uπ2Ncos⁡(2n+1)vπ2NY(u,v)=\frac2Nc(u)c(v)\sum\sum x(m,n)\cos\frac{(2m+1)u\pi}{2N}\cos\frac{(2n+1)v\pi}{2N}, c(0)=12c(0)=\frac1{\sqrt2}. Real, positive frequencies only, sparsifies better than the DFT (symmetric extension, no edge discontinuities). uu = vertical, vv = horizontal frequency; low frequencies at top left; (0,0)(0,0) = DC, the rest AC. 8×88\times8 blocks for local stationarity. Ramp 10,12,…,2410,12,\dots,24: DCT [48.08,−12.88,0,−1.35,0,−0.40,0,−0.10][48.08,-12.88,0,-1.35,0,-0.40,0,-0.10], energy 2480 preserved, concentrated in two coefficients.

JPEG baseline (per channel): subtract 128, 8×88\times8 blocks, DCT, quantization, lossless coding. The decoder is standard, the encoder is free.

  • Quantization: xind=round(x/q)x_{\text{ind}}=\mathrm{round}(x/q), rebuilt x~=q xind\tilde x=q\,x_{\text{ind}}, maximum error q/2q/2. Table q(i,j)q(i,j) chosen by the encoder and written in the file (64 B); de facto luminance table q∗q^* from subjective tests, small values at top left (information and sensitivity at low frequencies); another for chrominance.
  • Quality factor QQ: SF=5000/Q\mathrm{SF}=5000/Q (Q≤50Q\le50), 200−2Q200-2Q (50<Q≤9950<Q\le99), 11 (Q=100Q=100); q=min⁡(256,⌈SF100q∗⌉)q=\min(256,\lceil\frac{\mathrm{SF}}{100}q^*\rceil). Q=50Q=50: unchanged; Q=1Q=1: ×50\times50; Q=100Q=100: all 1. Q=30Q=30: SF=166.7\mathrm{SF}=166.7, q(0,0)=27q(0,0)=27, q(0,1)=19q(0,1)=19.
  • Example: centred block with DC 8.58.5; indices at Q=30Q=30: rows [0,0,−3,2,−1][0,0,-3,2,-1], [−1,−1,0,1,−1][-1,-1,0,1,-1], [0,−1,0,−1][0,-1,0,-1], rest 0 (9 non-zero). Decoding: indices ⋅q\cdot q, IDCT, +128+128; MSE≈85.6\mathrm{MSE}\approx85.6, PSNR≈28.8\mathrm{PSNR}\approx28.8 dB; low quality shows blocking.
  • Quality vs bits (same block): Q=10Q=10: 2 indices, 25.2 dB; 30: 9, 28.8; 50: 13, 30.0; 70: 16, 31.6; 90: 34, 40.8; 100: 58, 56.7.
  • Zig-zag: (0,0),(0,1),(1,0),(2,0),(1,1),(0,2),…(0,0),(0,1),(1,0),(2,0),(1,1),(0,2),\dots; example sequence 0 0 −1 0 −1 −3 2 0 −1 0 0 0 0 1 −1 0 −1 −10\,0\,{-1}\,0\,{-1}\,{-3}\,2\,0\,{-1}\,0\,0\,0\,0\,1\,{-1}\,0\,{-1}\,{-1} EOB.
  • DC: difference from the previous block's DC (DPCM), category k=⌈log⁡2(∣e∣+1)⌉k=\lceil\log_2(|e|+1)\rceil, category code plus ∣e∣|e| on kk bits (bitwise complement if negative). e=−1→0100e=-1\to0100; e=−6→100 001e=-6\to100\,001. Category codes: 00,010,011,100,101,110,1110,…00,010,011,100,101,110,1110,\dots
  • AC: symbols (R,C)(R,C), RR = preceding zeros; (R,k)(R,k) with a prefix code plus value on kk bits; ZRL (15,0)(15,0) = 16 zeros; EOB (0,0)=1010(0,0)=1010. Example: (1,−1)(1,−1)(0,−3)(0,2)(1,−1)(4,1)(0,−1)(1,−1)(0,−1)(1,-1)(1,-1)(0,-3)(0,2)(1,-1)(4,1)(0,-1)(1,-1)(0,-1), EOB; codes 1100,1100,01,01,1100,111011,00,1100,001100,1100,01,01,1100,111011,00,1100,00. Stream: 49 bits ⇒0.766\Rightarrow0.766 bit/pixel, compression 51249≈10.4\frac{512}{49}\approx10.4. DC and AC Huffman tables are written in the file.
  • Syntax: frame (dimensions, colour, subsampling), scan (one component), segments (parallel decoding, Huffman tables), blocks in raster order. Variable-length codes ⇒\Rightarrow sensitive to bit errors.

Formats. Progressive (spectral selection, successive approximation by bit-plane), hierarchical, lossless, JPEG-LS, Motion JPEG. PNG lossless (prediction, dictionary, Huffman). JPEG 2000 (DWT, scalable, no blocks). WebP/HEIF/AVIF (from video codecs; AVIF about −50%-50\% vs JPEG). JPEG XL (lossless JPEG transcoding −20%-20\%, HDR).

Typical mistakes: forgetting to subtract 128; table believed fixed by the standard; indices confused with reconstructed coefficients (multiply by qq); not complementing negative values; counting EOB as a coefficient; swapping vertical and horizontal (first column = vertical variations); forgetting the DC is differential.

More reminders.

  • Why orthogonal: inversion is cheap (ATA^T) and distances are preserved, so quantization can be optimised on the coefficients; each coefficient has its own step Δi\Delta_i.
  • Block size: 8×88\times8 suited the resolutions of 1991-92; dependencies between far pixels are not used (except through the differential DC).
  • Why small steps top left: the DCT puts the information at low frequencies, and the eye is more sensitive to low and medium-low frequencies; subjective tests gave q∗q^*. A different table is used for chrominance (YCbCr 4:2:0, each component coded separately).
  • Quantizer step: large step means low rate and strong loss; small step means better quality and more bits.
  • Decoder: indices ×q\times q, inverse DCT, +128+128; blocking appears at low quality, the error is larger near edges at intermediate quality.
  • DC categories: 31→531\to5 (31=11111231=11111_2), −33→6-33\to6 (33=100001233=100001_2); the category tells how many bits follow, which makes the code instantaneous; first value bit 1 means positive, 0 means complement and negate.
  • AC table excerpt: (0,1)=00(0,1)=00, (0,2)=01(0,2)=01, (0,3)=100(0,3)=100, (1,1)=1100(1,1)=1100, (4,1)=111011(4,1)=111011, EOB =1010=1010, ZRL =11111111001=11111111001.
  • Block bit count: 4+5+5+4+4+5+7+3+5+3+4=494+5+5+4+4+5+7+3+5+3+4=49 bits for 64 pixels, 512 bits uncompressed.
  • Robustness: variable-length codes and the differential DC make JPEG sensitive to bit errors (a wrong bit can desynchronise the decoder), unlike raw images.

Teoria collegata