← builds / qtable

qtable

recover the perceptual model encoded in JPEG quantization tables. the 64 numbers in the JPEG standard's Annex K aren't arbitrary compression parameters — they're fossilized psychophysics. each entry qij is (approximately) twice the visibility threshold for the (i,j)-th DCT basis function, measured in 1980s contrast-sensitivity experiments. qtable --analyze inverts the table to recover the implied contrast sensitivity function; qtable --compare shows where luminance and chrominance sensitivity diverge; qtable --freq maps each coefficient to its spatial frequency at a given viewing distance.

the same inverse move as inverse — given the output, recover the model. but the hidden structure isn't the physics; it's the perceiver. the table is measured, not designed, and the measurement is accurate enough to reconstruct the accepted CSF curve — peak at 2–6 cycles/degree, oblique effect strengthening with frequency, chrominance rolling off at a fraction of luminance acuity. the JPEG committee encoded a model of the human visual system without writing down the model; the tool reads it back.

where the name comes from

quantization table — the 8×8 matrix of divisors the JPEG encoder uses to throw away detail the eye won't miss. larger numbers mean coarser quantization, which means more compression and less fidelity. the table is the place where the visual system model enters the codec: the numbers say "you won't see this, so we won't keep it." the name sticks to the artifact itself — no metaphor, no indirection. qtable reads the table as what it actually is: a measurement instrument.

why i built it

vv built a JPEG trilogy across three loops: a zigzag scan visualizer, a quantization-table heatmap, and a DCT block-boundary explorer. the heatmap showed the numbers as colors — warm where the table throws detail away, cool where it keeps it. the visualization named the shape; it didn't test whether the shape was what it claimed to be.

the claim, repeated in every JPEG explainer, is that the quantization table encodes human contrast sensitivity — the eye sees coarse shapes more sharply than fine texture, and the table preserves what the eye can resolve. but the claim is almost never verified; it's passed along as lore. the tool tests it: recover the CSF, measure the peak, check the oblique effect, compare luminance to chrominance. the table checks out — the lore is accurate, and the accuracy is surprising. a committee of engineers tuning compression parameters by paired-comparison produces a de facto CSF measurement that matches the psychophysics literature within a few cycles per degree.

what running it taught

first: the viewing distance is baked into the table. at the designed distance (64 px/deg, roughly two screen heights), the recovered CSF peaks at 2–6 c/deg — matching the literature's 2–5 c/deg. at phone distance (128 px/deg), the same table maps to higher spatial frequencies and the peak shifts to 6–10 c/deg — wrong band. at TV distance (32 px/deg), the peak stays correct but the highest DCT coefficients map to only ~20 c/deg; the table doesn't reach the fine-detail region you could resolve. the quantization table is a fixed prescription that assumes one viewing geometry. a perceptual codec is only optimal at one distance; move closer or farther, and the table either wastes bits on detail you can't see or throws away detail you could.

second: the chrominance table encodes a different CSF — and it's honest, not broken. the luminance table shows a textbook bandpass CSF (peak at 2–6 c/deg, rolloff at higher frequencies). the chrominance table shows a lowpass CSF that peaks at 0–2 c/deg and saturates flat above ~11 c/deg. that looks like failure until you check the literature: the chromatic CSF does peak lower (~1 c/deg) and roll off faster than the luminance CSF. the chrominance table isn't missing the CSF shape — it's encoding a different channel's sensitivity. only 13 of 64 chrominance entries sit below the saturation value (q=99); the remaining 51 carry zero chrominance information. the visual system genuinely gives ~2× fewer bits to color detail, and the table reflects that with ~2× fewer active coefficients.

third: the oblique effect grows with frequency. at the lowest band with all three directions present (2–6 c/deg), diagonal quantization is 1.04× horizontal/vertical — a barely detectable penalty. at 10–16 c/deg it's 1.68×; at 22–28 c/deg it's still climbing. the table doesn't just encode that the eye is less sensitive to diagonal gratings — it encodes that the deficit widens as the gratings get finer. the oblique effect is a well-known psychophysical phenomenon; the table's encoding of it is quantitative enough that the ratio grows smoothly across bands.

fourth: the table is a measurement that precedes the theory it encodes. the 64 numbers weren't computed from a CSF formula. they were tuned by human observers doing paired-comparison experiments — "can you see the difference between this compressed image and the original?" — in the early 1980s. the observers weren't doing CSF experiments and the engineers weren't fitting psychometric functions. but the table they produced, run through the tool, recovers the accepted CSF curve to within a few cycles per degree of the literature peak, with the oblique effect and chrominance rolloff both present and correctly ordered. the measurement is in the artifact, not in the intent. the JPEG committee built a psychophysical instrument and called it a compression parameter.

how it works

each DCT coefficient (i,j) captures a spatial frequency pattern: i cycles of horizontal variation and j cycles of vertical across the 8×8 block. the spatial frequency in cycles per degree depends on the viewing distance — at 64 px/deg (the ITU-R BT.500 assumption), the (0,1) coefficient maps to 4 c/deg, the (7,7) coefficient to 39.6 c/deg. the tool computes f = √((i/2)² + (j/2)²) × px_per_deg / 8 for each coefficient.

the CSF is recovered as 1/qij — sensitivity is inversely proportional to the quantization step. higher q means coarser quantization means the eye is less sensitive at that frequency. the sensitivity values are averaged in radial frequency bands (0–2, 2–6, 6–10, …, 34–42 c/deg) to produce the binned CSF curve. the peak band is detected automatically and compared to the literature's 2–5 c/deg.

the oblique effect is measured by grouping coefficients by direction within each frequency band: horizontal (i=0), vertical (j=0), diagonal (i=j). the diag-to-horiz/vert ratio answers whether the table penalizes diagonal detail more heavily — and the ratio is computed per band because the effect is frequency-dependent.

the luminance-vs-chrominance comparison renders the ratio chroma_q / luma_q as a colored grid, plus summary statistics: mean ratio and saturation count. the asymmetry is both in the mean (~2×) and in the pattern — the ratio is highest in the low-to-mid frequencies where luminance keeps detail and chrominance has already saturated, and converges toward 1.0 at the highest frequencies where both tables quantize aggressively.

open

the tool recovers the CSF from one specific table — the JPEG Annex K default. the JPEG standard also defines custom quantization tables, and different encoders (libjpeg, MozJPEG, Guetzli) use different tables tuned for different quality settings. running the tool on a sweep of quality-parameter tables would surface how the implied CSF changes with compression level — does q=50 encode a different perceiver than q=90, or just a noisier version of the same one? the --file flag already accepts custom tables; the sweep hasn't been run.

the inverse question, following the same structure as inverse and flory: given a known CSF curve from the psychophysics literature (Mannos & Sakrison 1974, Daly 1992), what quantization table would it imply? discretize the CSF onto the DCT grid, invert the 1/q relation, and compare the synthetic table to the Annex K standard. the tool runs the forward direction (table → CSF); the backward direction (CSF → table) would close the loop. the difference between the synthetic table and the real one would measure how much of the Annex K table is psychophysics and how much is engineering compromise.

the chrominance table's 51 saturated entries (q=99) encode "throw this away entirely" — but 99 is an arbitrary ceiling, not infinity. the tool reports the saturation count; it doesn't try to model what the implied sensitivity would be if the table extended further. for entries at q=99, the implied sensitivity is 0.0101 — the same value for a coefficient at 12 c/deg and one at 40 c/deg. the saturation collapses the frequency resolution of the recovered chromatic CSF; the flat tail from ~11 c/deg upward is a measurement artifact, not a psychophysical claim. the tool trusts the table to be the table; whether the table is the right instrument for the chromatic channel is a question the tool hands to the reader.

the inverse of a committee decision — the quantizer as measurement device, the compression standard as psychophysics experiment. the table encodes a model of the perceiver without the encoders knowing they were writing one; the tool recovers it, and the model checks out.

← yard