Skip to content

Read our own exports back with jpeg-decoder instead of zune-jpeg - #2

Merged
oddharsh merged 1 commit into
mainfrom
claude/quirky-brahmagupta-b4fc85
Sep 20, 2026
Merged

oddharsh merged 1 commit into
mainfrom
claude/quirky-brahmagupta-b4fc85

Conversation

@oddharsh

Copy link
Copy Markdown
Owner

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 1B on 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.rs decodes JPEG with jpeg-decoder 0.3. The ICC profile still goes through color::profile and moxcms exactly as before. CMYK and 16-bit lossless now fail at decode instead of being naively converted; neither is a camera format.
  • Cargo.toml swaps zune-jpeg for jpeg-decoder without rayon; the lock drops zune-jpeg and zune-core.
  • The encode tests verify ZenJPEG output with jpeg-decoder.
  • Two regression tests: a 16x8 jpeg-encoder fixture with a restart interval of one MCU, and the 1440x960 4:4:4 gradient-plus-noise round trip through 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:

decoder reads the failing exports max diff mean diff 24MP decode
zune-jpeg 0.4.21 no n/a n/a 90 ms
zune-jpeg 0.5.15 yes 1 0.005 not timed
jpeg-decoder 0.3.2, no rayon yes 1 0.001 170 ms

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.
  • Release binary round-trips every export that failed before (1440x1440 at 4:4:4, 4:2:2 and 4:2:0; 1440x2160 at 4:2:0) and still decodes the Leica and Fujifilm originals.

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.
@oddharsh
oddharsh merged commit 2f454cd into main Sep 20, 2026
4 checks passed
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