JPEG Compression

JPEG is the standard lossy compression method for photographs. It rests on three observations. Low-frequency content dominates images of the real world. The human visual system is less sensitive to detail in chromaticity than in luminance. It is also less sensitive to error at high frequencies. JPEG therefore puts its errors in the chroma channels and at high frequencies.

The encoder first converts RGB to Y’CbCr. The luma is the lightness, with the prime marking gamma encoding. The chroma channels and measure the blue-yellow and red-green difference from gray. The encoder then subsamples the chroma. In 4:2:2, and keep half the horizontal resolution, so of every values remain, a fraction of . In 4:2:0, they keep half the resolution in both directions, so of every remain, a fraction of . The luma always stays at full resolution.

Each channel is then cut into blocks, and the discrete cosine transform (DCT) writes each block as a weighted sum of basis patterns,

Here is a pixel position in the block, with counting rows, and . The indices and count roughly how many half-periods the pattern makes down and across the block. The weight of the constant pattern , the DC coefficient, is proportional to the block’s average. Under 4:2:0 an chroma block covers image pixels. Each DCT coefficient is then quantized with a quantization matrix :

The entries of grow toward high frequencies, so most high-frequency coefficients become . The quality setting in a photo app changes , and lower quality means larger entries. Last, the encoder reorders each block’s coefficients in a zigzag from low to high frequency. It then run-length encodes the runs of zeros and Huffman encodes the nonzero values. These last steps are lossless. Information is lost in exactly two places, chroma subsampling and quantization. The decoder runs the steps backward and multiplies each by before the inverse DCT. The lecture shows one photo saved at kB and at kB.

Intuition

A -megapixel photo stored as 8-bit RGB takes MB. Most of those bytes describe differences that a viewer cannot see. JPEG is named after the Joint Photographic Experts Group, the committee that wrote the standard. The lecture uses it as an example of a broader theme, in which visual computing systems gain efficiency by exploiting human perception. Color detail is cheap to discard, so it is subsampled. Fine detail is cheap to approximate, so it is rounded coarsely.

\usepackage{tikz}
\begin{document}
\definecolor{loss}{HTML}{D9483B}
\definecolor{keep}{HTML}{2F80ED}
\begin{tikzpicture}[>=latex,font=\small,
  stage/.style={draw=keep,fill=keep!10,rounded corners,minimum width=2.7cm,minimum height=1cm,align=center},
  lossy/.style={draw=loss,fill=loss!15,rounded corners,minimum width=2.7cm,minimum height=1cm,align=center,thick}]
\node[stage] (a) at (0,0) {RGB image};
\node[stage] (b) at (3.3,0) {convert to\\$Y'C_bC_r$};
\node[lossy] (c) at (6.6,0) {subsample\\$C_b$, $C_r$};
\node[stage] (d) at (9.9,0) {$8 \times 8$ blocks,\\DCT};
\node[lossy] (e) at (0,-1.8) {quantize\\by $Q[i,j]$};
\node[stage] (f) at (3.3,-1.8) {zigzag\\reorder};
\node[stage] (g) at (6.6,-1.8) {run-length code\\the zeros};
\node[stage] (h) at (9.9,-1.8) {Huffman code,\\file};
\draw[->,thick] (a) -- (b);
\draw[->,thick] (b) -- (c);
\draw[->,thick] (c) -- (d);
\draw[->,thick] (d.south) -- ++(0,-0.3) -| (e.north);
\draw[->,thick] (e) -- (f);
\draw[->,thick] (f) -- (g);
\draw[->,thick] (g) -- (h);
\node[loss,anchor=west] at (-1.35,-3.0) {red: information is lost};
\node[keep,anchor=west] at (4.5,-3.0) {blue: invertible, no loss};
\end{tikzpicture}
\end{document}

The DCT step is a change of basis, like the Fourier transform. A smooth block is described almost entirely by a few low-frequency patterns. Quantization rounds the weights of all patterns, more coarsely for patterns the eye notices less. Most weights become , and long runs of zeros cost almost nothing to store.

Properties

(Where Information Is Lost) The DCT and the entropy coding are invertible, while rounding and chroma subsampling are not.

The DCT basis is orthogonal, so the transform is an invertible change of coordinates (2.9c Matrix Orthonormality). It loses nothing apart from floating-point rounding. Quantization maps every value within of a multiple of to the same integer. After the decoder multiplies back by , each coefficient is off by at most . Chroma subsampling discards half or three quarters of the chroma samples, and the decoder can only upsample what remains. Zigzag reordering, run-length coding and Huffman coding are exactly reversible.

(Zigzag Order) Reading the block diagonal by diagonal gathers the zeros into one long run at the end.

After quantization the nonzero coefficients cluster in the top-left corner, at low frequencies. The zigzag visits coefficients in order of increasing , which is roughly increasing frequency. The figure shades the nonzero coefficients of the lecture’s example block and draws the zigzag. The solid part ends at the last nonzero coefficient, the th visited. The remaining coefficients are all and need only a single end-of-block code.

\usepackage{tikz}
\begin{document}
\definecolor{nz}{HTML}{2F80ED}
\definecolor{path}{HTML}{D9483B}
\begin{tikzpicture}[x=0.55cm,y=0.55cm,>=latex,font=\small]
\foreach \j/\i in {0/0,1/0,2/0,3/0,4/0,5/0,1/-1,2/-1,3/-1,4/-1,0/-2,1/-2,2/-2,3/-2,4/-2,0/-3,1/-3,2/-3,3/-3,0/-4} {\fill[nz!35] (\j,\i) rectangle ++(1,-1);}
\draw[gray,step=1] (0,-8) grid (8,0);
\draw[path,thick] (0.5,-0.5) -- (1.5,-0.5) -- (0.5,-1.5) -- (0.5,-2.5) -- (1.5,-1.5) -- (2.5,-0.5) -- (3.5,-0.5) -- (2.5,-1.5) -- (1.5,-2.5) -- (0.5,-3.5) -- (0.5,-4.5) -- (1.5,-3.5) -- (2.5,-2.5) -- (3.5,-1.5) -- (4.5,-0.5) -- (5.5,-0.5) -- (4.5,-1.5) -- (3.5,-2.5) -- (2.5,-3.5) -- (1.5,-4.5) -- (0.5,-5.5) -- (0.5,-6.5) -- (1.5,-5.5) -- (2.5,-4.5) -- (3.5,-3.5) -- (4.5,-2.5);
\draw[path!60,dashed] (4.5,-2.5) -- (5.5,-1.5) -- (6.5,-0.5) -- (7.5,-0.5) -- (6.5,-1.5) -- (5.5,-2.5) -- (4.5,-3.5) -- (3.5,-4.5) -- (2.5,-5.5) -- (1.5,-6.5) -- (0.5,-7.5) -- (1.5,-7.5) -- (2.5,-6.5) -- (3.5,-5.5) -- (4.5,-4.5) -- (5.5,-3.5) -- (6.5,-2.5) -- (7.5,-1.5) -- (7.5,-2.5) -- (6.5,-3.5) -- (5.5,-4.5) -- (4.5,-5.5) -- (3.5,-6.5) -- (2.5,-7.5) -- (3.5,-7.5) -- (4.5,-6.5) -- (5.5,-5.5) -- (6.5,-4.5) -- (7.5,-3.5) -- (7.5,-4.5) -- (6.5,-5.5) -- (5.5,-6.5) -- (4.5,-7.5) -- (5.5,-7.5) -- (6.5,-6.5) -- (7.5,-5.5) -- (7.5,-6.5) -- (6.5,-7.5) -- (7.5,-7.5);
\fill[path] (0.5,-0.5) circle (3pt);
\fill[path] (4.5,-2.5) circle (3pt);
\node[above] at (4,0.1) {$j$ (across) $\rightarrow$};
\node[left,align=center] at (-0.1,-1.5) {$i$\\(down)\\$\downarrow$};
\node[right,align=left] at (8.3,-0.5) {DC coefficient\\(start)};
\node[right,align=left] at (8.3,-2.5) {last nonzero,\\26th visited};
\draw[->,gray] (8.3,-2.5) -- (4.8,-2.5);
\node[right,align=left] at (8.3,-5) {shaded: nonzero\\after quantization};
\end{tikzpicture}
\end{document}

(Blocking and Ringing) At low quality, the block structure and the missing high frequencies become visible.

Each block is quantized on its own. At strong compression many blocks keep little more than their DC coefficient, so they turn into flat tiles. Neighboring tiles get slightly different levels, and the grid shows. This artifact is called blocking. A sharp edge needs many high-frequency cosines to stay sharp. When quantization removes them, ripples appear beside the edge, an artifact called ringing. Text, line art and other images with hard edges show both artifacts most strongly.

(Why Cosines) The DCT uses real cosines that treat each block as if it were mirrored at its borders.

The Fourier transform treats a block as one period of a repeating pattern. Its left edge then meets its right edge, which usually creates a false jump and spreads energy into high frequencies. The cosine basis corresponds to reflecting the block at its borders, which creates no jump. A smooth block then concentrates its energy in a few low-frequency coefficients, which is what quantization needs. The coefficients are also real numbers, with no separate phase to store.

(Chroma Block Footprint) Under 4:2:0, a pixel region is coded as six blocks.

The region contains four blocks of . Its chroma, at half resolution in each direction, fills one block of and one of . That is values, against values in RGB. The ratio is , the 4:2:0 fraction. A color error in chroma can therefore spread over pixels, while luma errors stay within .

Examples

(Easy) Counting Values in a Phone Photo

A camera takes a photo with 8-bit RGB values.

(Medium) Quantizing the Lecture's Block

The lecture’s example block has DCT coefficients , , and (rows and columns counted from ). The quantization matrix has , , and .

(Hard) Zigzag and Run-Length Coding

The lecture’s quantized block has these nonzero rows, with every other entry :

See Also