Lossless JPEG Predictor Functions Explained
The original JPEG standard (ITU-T T.81 / ISO/IEC 10918-1) defines a fully lossless mode of operation based on Differential Pulse Code Modulation (DPCM) instead of the traditional Discrete Cosine Transform (DCT). In this lossless mode, the encoder predicts the value of each pixel using the values of up to three neighboring, previously reconstructed pixels, and then encodes only the difference (prediction error) using Huffman or arithmetic coding. The standard defines eight distinct predictor selection values—ranging from 0 to 7—giving encoders flexibility to optimize compression depending on image characteristics.
The Neighboring Sample Layout
Predictors calculate the estimated value of the current sample, denoted as \(X\), using up to three neighboring reconstructed samples:
- \(A\): The sample immediately to the left of \(X\).
- \(B\): The sample immediately above \(X\).
- \(C\): The sample diagonally above and to the left of \(X\) (the sample immediately to the left of \(B\)).
The Eight Predictor Functions
The JPEG specification designates the predictor via a 3-bit selection value (Ss) in the scan header:
- Selection Value 0 (No Prediction): Differential coding is disabled. This mode is typically reserved for hierarchical progression and implies a prediction value of zero.
- Selection Value 1 (Horizontal Predictor): \[P_x = A\] Predicts the current pixel using the neighbor directly to the left. This works best for images with strong horizontal correlation.
- Selection Value 2 (Vertical Predictor): \[P_x = B\] Predicts the current pixel using the neighbor directly above. This is ideal for images with strong vertical correlation.
- Selection Value 3 (Diagonal Predictor): \[P_x = C\] Predicts the current pixel using the neighbor diagonally above and to the left.
- Selection Value 4 (Planar Predictor): \[P_x = A + B - C\] A two-dimensional linear extrapolation that assumes a planar surface across the local gradient.
- Selection Value 5 (Horizontal + Diagonal Difference): \[P_x = A + \lfloor (B - C) / 2 \rfloor\] Adjusts the horizontal prediction (\(A\)) based on the local vertical gradient between \(B\) and \(C\).
- Selection Value 6 (Vertical + Diagonal Difference): \[P_x = B + \lfloor (A - C) / 2 \rfloor\] Adjusts the vertical prediction (\(B\)) based on the local horizontal gradient between \(A\) and \(C\).
- Selection Value 7 (Average Predictor): \[P_x = \lfloor (A + B) / 2 \rfloor\] Uses the arithmetic mean of the horizontal and vertical neighbors, providing effective noise smoothing in uniform regions.
Boundary Conditions
For edges where neighboring pixels do not exist, the standard defines specific fallbacks. For the first pixel in an image, a fixed baseline value determined by the sample precision is used (e.g., \(2^{P-1}\) where \(P\) is precision). For the remainder of the first row, predictor 1 (\(A\)) is enforced because samples \(B\) and \(C\) are unavailable. For the first column of subsequent rows, predictor 2 (\(B\)) is enforced because samples \(A\) and \(C\) do not exist.