Skip to content

Decode 12-bit, arithmetic-coded and two-component lossless JPEG - #41

Merged
Ttimmahlax merged 1 commit into
mainfrom
feat/issue-28
Nov 25, 2026
Merged

Ttimmahlax merged 1 commit into
mainfrom
feat/issue-28

Conversation

@Ttimmahlax

Copy link
Copy Markdown
Contributor

Refs #28

Decodes 12-bit lossy JPEG (SOF1, SOF2), arithmetic-coded JPEG (SOF9, SOF10, DAC) and the two-component lossless layout DNG uses (SOF3), and compares CMYK/YCCK output with libjpeg-turbo.

What was added

  • 12-bit DCT, sequential and progressive. A 12-bit SOF0 stays an error (B.2.2). Output is two bytes per sample in native byte order, unscaled (0..=4095), as lossless images above 8 bits already were. IDCT (also 1/2, 1/4, 1/8), upsampling and colour conversion are libjpeg-turbo's 12-bit arithmetic, in scalar safe code (decoder/twelve.rs).
  • Arithmetic coding at 8 and 12 bits, with DAC and libjpeg-turbo's handling of corrupt data (decode/arith.rs, decoder/arithmetic.rs). The QM-coder is also checked against the test sequence of T.81 K.4.
  • Two-component lossless, interleaved samples, full-size components only. Every other process still refuses two components.
  • Tests: tests/extended_modes.rs: 12 fixtures libjpeg-turbo wrote from synthetic pictures (tools/generate_modes_libjpeg.c), expected hashes from libjpeg-turbo, truncation at every byte, two bit flips in every byte, bad DAC/SOF/precision combinations, allocation limits. New fuzz target decode_modes, also in CI.

No unsafe was added.

API additions

  • Decoder::decode_u16() -> Result<Vec<u16>>
  • Decoder::frame_header() -> Option<FrameHeader>: width, height, components, precision, coding_process, arithmetic_coding, bytes_per_sample(). PixelFormat and ImageInfo are exhaustive, so they could not grow a 12-bit or a two-component case.
  • Warning::BadArithmeticCode (the enum is non_exhaustive)

Comparison with libjpeg-turbo 3.1.0

Oracle: libjpeg-turbo 3.1.0 built from the release tarball (sha256 9564c72b1dfd1d6fe6274c5f95a8d989b59854575d4bbee44ade7bc17aa9bc93), through a small C program on its library API with djpeg's defaults (accurate IDCT, fancy upsampling). djpeg itself cannot write CMYK or two components. The files were encoded by the same library from synthetic pictures: nine sizes from 1x1 to 200x133, qualities 40/90/100, six samplings (4:4:4, 4:2:2, 4:2:0, 4:4:0, 4x1, 4x2), grey/YCbCr/RGB/CMYK/YCCK, restart intervals, separate scans, default and non-default DAC.

intact files files identical
12-bit Huffman, sequential 1,233 1,233
12-bit Huffman, progressive 936 936
12-bit arithmetic, sequential 1,233 1,233
12-bit arithmetic, progressive 936 936
lossless, 1 to 4 components, predictors 1-7, Pt 0-2, 8/10/12/14/16 bits 3,465 3,465
... of those, two components 945 945
arithmetic decode == our decode of the Huffman twin (8 and 12 bits, strict, worker and single-threaded) 4,338 4,338
12-bit at 1/2, 1/4, 1/8, 4:4:4 and grey only 2,592 (of 2,916) 2,592

(The other 324 are images so small that our scale() picks a smaller size than libjpeg's scale_denom.) libjpeg-turbo's own testorig12.jpg is identical; testimgari.jpg differs on 7.6 % of samples, by at most 3, exactly the figures its Huffman form testorig.jpg has (7.6 %, 3).

8-bit arithmetic is not compared with djpeg sample by sample for exactness: our 8-bit IDCT and upsampling are not libjpeg-turbo's and differ by the same small amounts with either entropy coder. The twin comparison above is the test for it.

CMYK / YCCK, 8 bits (baseline, 35x23, 64x64 and 200x133, all compared with the complement of libjpeg's JCS_CMYK):

max difference samples that differ
CMYK, Adobe transform 0, 4:4:4 1 2.7 %
CMYK, subsampled 2 2.7-8.6 %
YCCK, Adobe transform 2, 4:4:4 3 5.3 %
YCCK, subsampled 3 5.3-13.8 %
CMYK without an Adobe marker 2 3.2-9.2 %

At 12 bits all three are identical. Two things to know: we always return the complement of the stored values (0 = no ink), also without an Adobe marker, where libjpeg returns what is stored; and on images whose subsampled planes are 1 or 2 samples wide libjpeg stops using fancy upsampling while our 8-bit path does not (up to 30 off on 3x5 images). Both are as they were. These are synthetic files; no Photoshop-written print file was available.

Damaged files (8,820: cuts, bit flips, 5-byte garbles in scan data, header bit flips, of 420 of the new-mode files):

12-bit and two-component lossless both decode identical
cut short 2,629 2,629
one bit flipped in scan data 1,754 1,754
five bytes garbled in scan data 524 524
one header bit flipped 746 743

No panics. We decode 98 cut files libjpeg-turbo refuses. The three header differences are damaged AC Huffman tables (a symbol 0xr0 in a sequential scan, read as a progressive EOB run; the 8-bit path does the same). What we refuse and libjpeg-turbo decodes are header defects this decoder already refused at 8 bits (spectral selection in a sequential scan, undefined tables). 8-bit arithmetic files were in the run for refusals and panics only.

Fuzzing: decode_modes, 4.39 million runs in 151 s locally, no findings.

Existing 8-bit paths

Output. Base (e36f635) against this branch on 4,689 intact Huffman 8-bit and lossless files and eight damaged variants of each (42,201 decodes; pixels, warnings and errors hashed): the only differences are lossless files with separate scans (a bug, below) and streams where a flipped bit makes a DAC or a 12-bit header, refused before and after with another message.

Instructions (callgrind, whole process, 1920x1200 photos, decode, one thread, rusty_alloc; a second build of base reads within 0.006 % of base):

base this branch
grey 38,771,878 38,810,241 +0.10 %
4:2:0 78,669,357 78,674,066 +0.01 %
4:2:2, restarts 85,063,267 85,023,019 -0.05 %
4:4:4 98,946,660 98,978,303 +0.03 %
4:2:0 progressive 116,474,410 116,468,927 -0.00 %

Clock (pinned, arms rotated and reversed over 20 rounds, each reading the best of 3-12 decodes; the box was busy): median of the per-round ratio to base, with the null arm (base built again) beside it.

null this branch
grey 1.015 (faster 7/20) 1.011 (8/20)
4:2:0 1.002 (9/20) 0.994 (11/20)
4:2:2, restarts 1.015 (8/20) 0.995 (13/20)
4:4:4 0.988 (13/20) 1.008 (6/20)
4:2:0 progressive 0.992 (10/20) 1.038 (8/20)
lossless 8-bit 0.997 (10/20) 0.987 (12/20)

No row is outside what 20 rounds of sign test call noise, but the clock here resolves no better than 1-4 %; the instruction counts are the evidence.

New modes against libjpeg-turbo (pinned, 8 alternating rounds, best of 5 each, 1920x1200): median ratio of our time to its: arithmetic 8-bit sequential 1.13, progressive 0.87; 12-bit Huffman sequential 1.97, progressive 1.48; 12-bit arithmetic 1.27. 12-bit keeps the whole frame's coefficients and renders afterwards; nothing has been vectorised.

Behaviour changes

  • The streams above decode instead of Unsupported(SamplePrecision(12)), Unsupported(ArithmeticEntropyCoding) or Unsupported(ComponentCount(2)).
  • Lossless images with one scan per component decoded wrongly on intact files (all planes stored over component 0; 630 of 630 test files): they now match libjpeg-turbo.
  • A DAC segment in a Huffman file is checked and ignored; it was refused.
  • info() is None after a successful read_info on a two-component image.
  • set_max_decoding_buffer_size counts a 12-bit sample as two bytes and bounds the stored coefficients of 12-bit and arithmetic frames. The error text says "coefficient buffer" without "progressive".

Not done

  • SOF11 (lossless arithmetic): libjpeg-turbo does not implement it, so there is no oracle. Still Unsupported(ArithmeticEntropyCoding).
  • decode_planar for 12-bit images (Unsupported(SamplePrecision(12))).
  • Not tested on real DNG tiles or real 12-bit medical files, only on libjpeg-turbo's output.
  • Seen, not changed: lossless frames with subsampled components are read as if full-size; a 3-component file whose ids are 1, 2, 3 is taken as YCbCr even when Adobe's transform says RGB (libjpeg-turbo lets Adobe decide); 8-bit scans of one component with sampling 2 of 4 cover padding blocks.

🤖 Generated with Claude Code

SOF1/SOF2 at 12 bits, SOF9/SOF10 with DAC at 8 and 12 bits, and the
two-component SOF3 layout of DNG CFA tiles. 12-bit and lossless output
matches libjpeg-turbo 3.1.0 exactly; arithmetic files decode to what their
Huffman twins do. Also stores the planes of a lossless image with one scan
per component where they belong.

Refs #28

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ttimmahlax
Ttimmahlax merged commit 95a5d64 into main Nov 25, 2026
11 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.

2 participants