Skip to content

Split progressive AC scans so every scan range is non-empty - #47

Open
sunsided wants to merge 1 commit into
vstroebel:mainfrom
sunsided:fix/progressive-scan-ranges
Open

sunsided wants to merge 1 commit into
vstroebel:mainfrom
sunsided:fix/progressive-scan-ranges

Conversation

@sunsided

Copy link
Copy Markdown

set_progressive_scans(n) documents 2..=64 as valid, but every n in 34..=64 produces a file that does not decode:

invalid JPEG format: invalid spectral selection parameters: ss=1, se=0

The AC coefficients were split into 64 / (n - 1) per scan with the first scan starting at max(0, 1). Once that per-scan count drops to 1, the first scan is start = 1, end = 1, so its header says Ss=1, Se=0.

Change

ac_scan_ranges(scans) splits the 63 AC coefficients evenly over scans - 1 scans: [1 + k*63/(n-1), 1 + (k+1)*63/(n-1)). Each range is non-empty for every valid n. The scan writer and the optimized-Huffman counting both used their own copy of the old formula; both now call the helper, so they can't drift apart.

Side effect: the default 4 scans now split at 1/22/43 instead of 1/21/42, so the output bytes change but the files still decode correctly.

Tests

  • test_ac_scan_ranges_cover_all_ac_coefficients: for all n in 2..=64, the ranges are contiguous, non-empty and cover exactly 1..64.
  • test_progressive_all_scan_counts: encodes and decodes with every scan count. Fails on main from 34 up.

With set_progressive_scans(n) for n in 34..=64, each AC scan was given
64 / (n - 1) = 1 coefficient, and the first scan started at
max(0, 1) = 1 but ended at 1. Its header was written with Ss=1, Se=0,
which decoders reject ("invalid spectral selection parameters"), so the
whole file failed to decode even though the setter documents 2..=64 as
valid.

The ranges are now computed once in ac_scan_ranges, which splits the 63
AC coefficients evenly over n - 1 scans: [1 + k*63/(n-1),
1 + (k+1)*63/(n-1)). The writer and the optimized Huffman counting both
use it, so they cannot drift apart. The default 4 scans now split at
1/22/43 instead of 1/21/42.
sunsided added a commit to sunsided/jpeg-encoder that referenced this pull request Nov 23, 2026
encode_roundtrip lets the fuzzer pick every encoder setting from an
8-byte header: color type, quality, sampling factor, optimized Huffman
tables, exact quantization, average subsampling, progressive mode with
2..=64 scans, restart interval and width. When encoding succeeds, the
output must decode with jpeg-decoder to the same dimensions, pixel
format and buffer length. Pixel values are not compared, since
arbitrary input has no meaningful error bound.

On main it currently finds two known bugs within minutes: progressive
scan counts of 34..=64 write an empty AC range (fixed by vstroebel#47), and
optimized Huffman tables with a restart interval miss the DC prediction
reset (fixed by vstroebel#42). With both applied, 589k runs pass. CI builds the
target but does not run it until those land.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant