A progressive JPEG (frame marker FF C2, SOF2) sends the same quantised
coefficients as a sequential one, but in several scans, so a viewer can show a rough picture
and sharpen it as more data arrives. Each
scan carries a part of the coefficients, set by its
Ss, Se, Ah and Al bytes.
Spectral selection and successive approximation
Spectral selection splits the 64 coefficients into bands. The band of a scan runs from Ss to Se, in zig-zag order. Successive approximation sends the high bits of the coefficients first and refines them in later scans: Al is the lowest bit a scan sends, and Ah is the bit position the band had reached before.
| Scan type | Ss | Se | Ah | Components | What it does |
|---|---|---|---|---|---|
| DC, first | 0 | 0 | 0 | one or several | Each DC coefficient, shifted down by Al bits |
| DC, refinement | 0 | 0 | not 0 | one or several | One more low bit of each DC coefficient |
| AC, first | 1 or more | Ss to 63 | 0 | one | The coefficients of the band, each divided by 2Al and rounded towards zero |
| AC, refinement | 1 or more | Ss to 63 | not 0 | one | One more low bit of the coefficients of the band |
In a refinement scan, Al is Ah − 1, so one bit is added. An AC scan has a single component, so its blocks are not gathered into MCUs. DC scans may be interleaved, in MCUs, as a sequential scan is.
An example set of scans
The default that libjpeg uses for a colour image is:
| Scan | Components | Coefficients | Bits |
|---|---|---|---|
| 1 | Y, Cb, Cr | 0–0 | all but the lowest (Al = 1) |
| 2 | Y | 1–5 | high bits (Al = 2) |
| 3 | Cr | 1–63 | high bits (Al = 1) |
| 4 | Cb | 1–63 | high bits (Al = 1) |
| 5 | Y | 6–63 | high bits (Al = 2) |
| 6 | Y | 1–63 | refines to Al = 1 |
| 7 | Y, Cb, Cr | 0–0 | refines to Al = 0 |
| 8 | Cr | 1–63 | refines to Al = 0 |
| 9 | Cb | 1–63 | refines to Al = 0 |
| 10 | Y | 1–63 | refines to Al = 0 |
DC scans
A first DC scan codes each block's DC coefficient, arithmetically shifted right by Al, as a difference from the previous block, just as a sequential scan does. A later DC scan sends one bit per block, with no Huffman code: the bit at position Al of the coefficient. It is added to the value so far.
AC scans
A first AC scan codes each block's band with the same run and size symbols as a sequential scan, starting at coefficient Ss, except that a block can end early with an end-of-band run.
End-of-band runs
A symbol n0, with n from 0 to 14 (00 to E0), means
that this block and some blocks after it have no more non-zero coefficients in this band.
n further bits follow the code. The number of blocks is
EOBRUN = 2^n + (the n bits, read as a number)
In a first AC scan, the blocks counted in the run after the first have no bits at all, so a long run takes only a few bits.
Refinement
In a refinement scan each coefficient of the band is either already non-zero, from earlier scans, or still zero. The scan's symbols describe only the still-zero coefficients:
- A symbol with size 1 means: skip R still-zero coefficients, then make the next one a new
non-zero value of ±2Al. Its one bit, straight after the code, gives the sign:
1for positive,0for negative. - ZRL (
F0) means: skip 16 still-zero coefficients. - An EOB run symbol works as before. The blocks in the run have no further codes.
Every coefficient of the band that is already non-zero gets one
correction bit in the data, read in order, after the codes before it and
the sign bit of any new coefficient. A 1 adds 2Al to the
coefficient's magnitude. Blocks in
an end-of-band run get these correction bits too, so a run is not always free of bits.