A scan is one pass over some or all of the components. It is an SOS segment, with the scan's tables and coefficients, followed straight away by the compressed data. A baseline colour image usually has one scan; a progressive image has several.
SOS segment
0MarkerFF DA2 bytes2Ls length, 6 + 2 × Ns2 bytes4Ns components in this scan1 byte5Component 1 identifier, then its tables2 bytes…Further components2 bytes each…Ss first coefficient, in zig-zag order1 byte…Se last coefficient1 byte…Ah, Al bit positions, 4 bits each1 byte…Entropy-coded data up to the next markervaries
| Field | Size | Description |
|---|---|---|
| Ns | 1 | Number of components, 1 to 4. The components appear in the same order as in the frame header. |
| Csj | 1 | Component identifier from the frame header. |
| Tdj (high 4 bits) | ½ | DC Huffman table number. |
| Taj (low 4 bits) | ½ | AC Huffman table number. |
| Ss | 1 | First coefficient of the band, 0 to 63. Sequential images: 0. |
| Se | 1 | Last coefficient of the band. Sequential images: 63. |
| Ah (high 4 bits) | ½ | The bit position the previous scan of these coefficients ended at, or 0. Sequential images: 0. |
| Al (low 4 bits) | ½ | The bit position of this scan. Sequential images: 0. |
The order of the data
A scan with one component codes its blocks one by one, left to right and top to bottom. Each block counts as one unit.
A scan with more than one component codes its MCUs in the same order, and in each MCU the blocks of each component in order, as the frame header page describes.
The codes run together as a stream of bits, with no gaps. Bits are read from the top of each byte. A code or value can start in one byte and finish in the next.
FF bytes and padding
The data must not look like a marker. Where a FF byte falls in the data,
the encoder writes a 00 after it, and a decoder drops that byte. This is byte
stuffing, and it counts towards the bytes of the scan.
The last code before a marker rarely ends on a byte boundary. The remaining bits of the
byte are filled with 1 bits. Fill bytes of FF may come before a marker, as
anywhere else.
Data 3F 2A FF 00 12 7F FF D9
Restart intervals
A DRI segment (FF DD, length 4, 2-byte interval) sets how many MCUs lie
between restart markers in the scans after it. 0 turns them off.
After every interval the data is padded to a byte boundary, then comes a restart
marker, RST0 to RST7 (FF D0 to
FF D7), counting 0 to 7 and round again. No restart marker comes after the
last interval.
At each restart marker the decoder sets each component's DC prediction to 0 and resets the end-of-band run in progressive scans. The bits of one interval are independent of the others, so damage stays inside its interval, and intervals can be decoded in parallel.