Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
set_progressive_scans(n)documents2..=64as valid, but everynin34..=64produces a file that does not decode:The AC coefficients were split into
64 / (n - 1)per scan with the first scan starting atmax(0, 1). Once that per-scan count drops to 1, the first scan isstart = 1, end = 1, so its header saysSs=1, Se=0.Change
ac_scan_ranges(scans)splits the 63 AC coefficients evenly overscans - 1scans:[1 + k*63/(n-1), 1 + (k+1)*63/(n-1)). Each range is non-empty for every validn. 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 allnin 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 onmainfrom 34 up.