Repository navigation
Decode 12-bit, arithmetic-coded and two-component lossless JPEG - #41
Merged
Merged
Conversation
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>
This was referenced Nov 25, 2026
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.
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
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).decode/arith.rs,decoder/arithmetic.rs). The QM-coder is also checked against the test sequence of T.81 K.4.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 targetdecode_modes, also in CI.No
unsafewas 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().PixelFormatandImageInfoare exhaustive, so they could not grow a 12-bit or a two-component case.Warning::BadArithmeticCode(the enum isnon_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 withdjpeg's defaults (accurate IDCT, fancy upsampling).djpegitself 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.(The other 324 are images so small that our
scale()picks a smaller size than libjpeg'sscale_denom.) libjpeg-turbo's owntestorig12.jpgis identical;testimgari.jpgdiffers on 7.6 % of samples, by at most 3, exactly the figures its Huffman formtestorig.jpghas (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):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):
No panics. We decode 98 cut files libjpeg-turbo refuses. The three header differences are damaged AC Huffman tables (a symbol
0xr0in 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):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.
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
Unsupported(SamplePrecision(12)),Unsupported(ArithmeticEntropyCoding)orUnsupported(ComponentCount(2)).info()isNoneafter a successfulread_infoon a two-component image.set_max_decoding_buffer_sizecounts 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
Unsupported(ArithmeticEntropyCoding).decode_planarfor 12-bit images (Unsupported(SamplePrecision(12))).🤖 Generated with Claude Code