Quantisation is where JPEG loses information. Each of a block's 64 DCT coefficients is divided by a value and rounded to a whole number, and the decoder can only multiply back. The values are in tables the file carries in DQT segments.
DQT segment
A segment (marker FF DB, section B.2.4.1 of T.81) holds one or more tables,
one after another, until its length runs out.
0MarkerFF DB2 bytes2Lq segment length2 bytes4Pq, Tq precision and table number1 byte5Q0 … Q63 the 64 values, in zig-zag order64 or 128 bytes…Further tables the same againvaries
| Field | Size | Description |
|---|---|---|
| Pq (high 4 bits) | ½ | 0: values are 8-bit. 1: values are 16-bit, most significant byte first. Baseline and all 8-bit sample images use 8-bit values. |
| Tq (low 4 bits) | ½ | Table number, 0 to 3. A later table with the same number replaces this one. |
| Qk | 1 or 2 each | 64 values. None may be 0. |
A component's table is chosen by the Tqi byte in the frame header.
Zig-zag order
A block's coefficients, and the 64 values of a table, are stored in zig-zag order: a walk from the top-left corner (lowest frequencies) to the bottom-right (highest), going along diagonals. Each number below is a coefficient's place in the file, from 0 to 63, for a block drawn with the DC coefficient at the top left. Row number increases downwards with vertical frequency, and column number increases across with horizontal frequency.
| 0 | 1 | 5 | 6 | 14 | 15 | 27 | 28 |
| 2 | 4 | 7 | 13 | 16 | 26 | 29 | 42 |
| 3 | 8 | 12 | 17 | 25 | 30 | 41 | 43 |
| 9 | 11 | 18 | 24 | 31 | 40 | 44 | 53 |
| 10 | 19 | 23 | 32 | 39 | 45 | 52 | 54 |
| 20 | 22 | 33 | 38 | 46 | 51 | 55 | 60 |
| 21 | 34 | 37 | 47 | 50 | 56 | 59 | 61 |
| 35 | 36 | 48 | 49 | 57 | 58 | 62 | 63 |
The order puts the low frequencies first. Their quantised values are usually the only non-zero ones, so the rest of a block is a long run of zeros that codes to very little.
What the values do
With Svu the DCT coefficient at row v and column u, and Qvu the table value there, the encoder stores
Sq(v, u) = round(S(v, u) / Q(v, u))
The decoder multiplies back:
R(v, u) = Sq(v, u) × Q(v, u)
A larger value throws away more of that frequency. Tables are not fixed by the format: encoders choose them. Annex K of T.81 gives example tables for luminance and chrominance, and libjpeg's quality setting scales those.
The explorer shows an image's quality number only when its tables are exactly those Annex K tables as scaled by libjpeg's quality setting, because otherwise a quality figure has no defined meaning.
| 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 |
The DC coefficient, at the top left, is 8 × the block's mean sample value (after level shifting). Dividing it by 16, as this table does, keeps the mean to within about 1 grey level.