Repository navigation
Read our own exports back with jpeg-decoder instead of zune-jpeg - #2
Merged
Merged
Conversation
zune-jpeg 0.4 rejects a progressive scan whose MCU count is a whole multiple of the restart interval. The final interval then ends flush with the scan and, per T.81 figure B.2, no RST marker follows it; the decoder still expects one, latches the next DHT or SOS and fails with "Marker DHT found in bitstream". ZenJPEG restarts every four rows of data units, so 1440x1440 and 1440x960 exports trip it at every chroma mode and 1440x2160 does at 4:2:0. The stream is legal; libjpeg-turbo and jpeg-decoder read it, and upstream fixed zune-jpeg in 0.5.13 (etemesi254/zune-image#451 asks about a 0.4 backport). jpeg-decoder lands within one level of djpeg on those files, closer than zune-jpeg 0.5, and drops two crates from the tree. The ICC path through moxcms is unchanged. CMYK and 16-bit lossless now fail at decode rather than being naively converted; neither is a camera format. Two regression tests: a 16x8 jpeg-encoder fixture with a restart interval of one MCU, which needs no ZenJPEG, and a 1440x960 4:4:4 round trip through the source path, guarded so it only counts while the export's interval still divides the scan.
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.
Why
The converter could not read its own 4:4:4 and 4:2:2 output: zune-jpeg 0.4.21 failed with "Marker DHT found in bitstream" or "Marker SOS found in bitstream", while djpeg and jpeg-decoder read the same files.
The stream is legal and the decoder is at fault. Whenever a scan's MCU count is a whole multiple of the restart interval, the last interval ends flush with the scan, and T.81 figure B.2 puts no RST marker after the final ECS. zune-jpeg 0.4 runs its restart check after every data unit, so on the last one it latches the next segment's marker and rejects it. ZenJPEG restarts every 4 rows of data units, so at 1440 wide the interval is 720 blocks at 4:4:4 or 360 MCUs when subsampled. That divides 1440x1440 and 1440x960 at every chroma mode and 1440x2160 at 4:2:0; only 1440x2160 at 4:4:4 survives, at 67.5 intervals, which made it look format-specific. The scan script itself (spectral bands, Ah/Al chains, single-component AC scans, DHT between scans) checks out against the spec.
A 716-byte file from libjpeg-turbo's own
cjpeg -progressive -sample 1x1 -restart 1Bon a 16x8 image reproduces it without ZenJPEG. Upstream fixed this in zune-jpeg 0.5.13 (PR 358), but the 0.4 line has had no release since September 2025 and no branch. Filed as etemesi254/zune-image#451 with the reproducer and a backport ask.What changed
src/decode.rsdecodes JPEG with jpeg-decoder 0.3. The ICC profile still goes throughcolor::profileand moxcms exactly as before. CMYK and 16-bit lossless now fail at decode instead of being naively converted; neither is a camera format.Cargo.tomlswaps zune-jpeg for jpeg-decoder without rayon; the lock drops zune-jpeg and zune-core.decode::open, guarded so it only counts while the export's interval still divides the scan.Evidence
Each real export decoded with three decoders and diffed against djpeg 3.2:
rayon would have made jpeg-decoder about a fifth faster on the 24MP file and cost a dozen crates, so it stays off.
Test plan
cargo fmt --check,cargo clippy --all-targets -- -D warnings,cargo test: clean, 51 passed.