Skip to content

rusty_jpeg: 12-bit, arithmetic, lossless (SOF3) and CMYK/YCCK decode, and tolerant decoding of damaged JPEGs #28

Open

Description

@Ttimmahlax

Goal

rusty_jpeg now covers 8-bit baseline and progressive JPEG in most of the Freehold desktop apps. Four JPEG variants and one behaviour keep other JPEG decoders in place:

  • Freehold Photo keeps the upstream craft-jpeg decoder for 12-bit and arithmetic-coded JPEGs, and zune-jpeg for its raw pipeline.
  • Freehold Deck keeps image's JPEG decoder only for JPEGs with damaged data, which it still shows (as PowerPoint does) where a strict decoder refuses them.
  • Freehold Light and Photo need lossless JPEG (SOF3) for DNG raw tiles.
  • Print-workflow JPEGs are Adobe CMYK/YCCK, and the PDF and layout apps meet them in placed and embedded images.

With these, Photo drops craft-jpeg and the remaining zune-jpeg uses, and Deck drops the image JPEG path.

What's needed

  1. 12-bit baseline and extended sequential (SOF1) and progressive (SOF2) decode.
  2. Arithmetic coding: SOF9, SOF10, SOF11 (and DAC marker handling).
  3. Lossless JPEG (SOF3) with predictors 1-7 and point transform, 2-16 bit, as used in DNG (including the DNG tiles where a 2-component stream encodes a 2xN CFA block).
  4. Adobe CMYK and YCCK: honour the APP14 transform flag and the inverted-CMYK convention Photoshop writes; hand back CMYK, not a guessed RGB.
  5. Damaged-data tolerance like zune-jpeg / libjpeg: on a truncated file or a corrupt scan segment, decode what can be decoded, fill the rest (grey or the last good MCU row), and report it, instead of refusing the whole image.

API shape

  • The existing decoder, with an output sample type that can be u16 (12-bit and lossless up to 16-bit), and a ColorSpace::Cmyk output.
  • A strict: bool option (default as today), or a returned warning list (Vec<Warning { kind, mcu_row }>) when tolerant decoding filled something.

Test shape

  • A 12-bit medical or scientific JPEG.
  • An arithmetic-coded JPEG (libjpeg-turbo's testimgari.jpg style).
  • A DNG lossless-JPEG tile.
  • A CMYK JPEG from a print workflow (Photoshop, APP14 transform 0) and a YCCK one (transform 2).
  • A JPEG truncated at 60 %, and one with a corrupted scan segment.

How we'll verify

Decode against libjpeg-turbo (12-bit and arithmetic builds) and zune-jpeg: exact for lossless and for 12-bit where the IDCT matches, within 1 LSB otherwise; for damaged files, compare how many MCU rows each decoder recovers and that none panics. We'll post the numbers here.

Please correct anything rusty_jpeg 0.4.8 already covers. When it's on crates.io, please reply on this issue and we'll run the comparison.

Activity

  1. Ttimmahlax commented on Nov 21, 2026

    @Ttimmahlax
    ContributorAuthor

    Status update; this stays open. Of the five items, tolerant decoding (5) is done, CMYK/YCCK (4) was already there, lossless (3) is done apart from the DNG 2-component tiles, and 12-bit (1) and arithmetic coding (2) are not started.

    5. Damaged-data tolerance: done.

    • fix(decode): recover damaged streams as libjpeg-turbo does; rust-version 1.87 #29, released in 0.4.9: recovers damaged streams the way libjpeg-turbo 3.x does. That covers zero quantisation entries, restart markers out of sequence, reserved codes that look like markers, and stray RSTs between segments. Fill bits after a marker leave the rest of the segment grey instead of decoding noise.
    • Recover cut-short and damaged streams as libjpeg-turbo does (warnings + strict mode) #31, merged, ships in the next release: cut-short files and bad Huffman codes.
      • A truncated stream decodes what it can, as libjpeg-turbo does by inserting its own EOI. Progressive files get libjpeg-turbo's interblock smoothing.
      • The API is the shape you proposed: Decoder::warnings() -> &[Warning] (Truncated, SegmentEndedEarly, BadHuffmanCode, RestartMarkerMismatch, ExtraneousBytes, MissingScan), is_truncated(), and set_strict(true) to get the old refusals back.
      • Against djpeg from libjpeg-turbo 3.1.0 on 5,544 damaged variants of 210 files: we decode 5,200 of the 5,368 that djpeg decodes, and 4,875 match it exactly. 0.4.9 decoded 1,954.
      • Behaviour change: recovery is the default. A caller that relied on Err(UnexpectedEof) to drop a cut-short frame must call set_strict(true) or check is_truncated().

    4. Adobe CMYK and YCCK: already in 0.4.8. The APP14 transform flag is honoured (0 is CMYK, 2 is YCCK), the output is PixelFormat::CMYK32 rather than a guessed RGB, and Photoshop's inverted CMYK is un-inverted. We haven't compared it against libjpeg-turbo on print-workflow files; your CMYK and YCCK test files would settle that.

    3. Lossless (SOF3): done apart from DNG tiles. SOF3 with predictors 1-7 and point transform at 2-16 bits was already in 0.4.8, but #31 found two bugs in it on intact files:

    • restart intervals didn't reset the prediction (89-97% of samples differed from djpeg);
    • point transform > 0 scaled the samples before using them as predictors (about 98% of samples differed on every cjpeg -lossless 1,2 file).

    Both are fixed in #31 and now match djpeg. Not done: the DNG layout where a 2-component stream codes a 2xN CFA block. Frames must be 1, 3 or 4 components, so those tiles are refused. That is also why a DNG's lossless-JPEG raw data doesn't decode through rusty_tiff yet.

    1. 12-bit (SOF1/SOF2): not started. Lossy frames other than 8-bit return Unsupported(SamplePrecision).

    2. Arithmetic coding (SOF9/10/11, DAC): not started. These return Unsupported(ArithmeticEntropyCoding).

    We'll post here as 12-bit, arithmetic and the 2-component tiles land, and close the issue then.

  2. Ttimmahlax commented on Nov 25, 2026

    @Ttimmahlax
    ContributorAuthor

    12-bit, arithmetic coding and the DNG two-component tiles are in #41, released in 0.4.14 (#43 kept it semver-compatible). One item is left, SOF11, so this stays open.

    What changed:

    • 12-bit (1): SOF1 and SOF2 decode, Huffman or arithmetic. Samples are u16, unscaled, 0..=4095.
    • Arithmetic coding (2): SOF9 and SOF10 at 8 and 12 bits, with DAC. New Warning::BadArithmeticCode for damaged segments.
    • DNG tiles (3): two-component SOF3, full-size components, interleaved samples.
    • API: Decoder::decode_u16() and Decoder::frame_header() (components, precision, bytes_per_sample()). info() is None for two-component images, which no PixelFormat describes.
    • Bug fixed on the way: intact lossless files with one scan per component decoded wrongly, 630 of 630 test files. They match now.

    Against libjpeg-turbo 3.1.0 (synthetic pictures it encoded, djpeg defaults):

    check result
    12-bit, Huffman and arithmetic, sequential and progressive 4,338/4,338 identical
    lossless, 1-4 components, 8-16 bits 3,465/3,465 identical (945 two-component)
    8-bit arithmetic file against our decode of its Huffman twin identical, all files
    testorig12.jpg identical
    testimgari.jpg (8-bit) 7.6% of samples differ, by 3 at most; its Huffman form reads the same
    12-bit CMYK and YCCK identical
    8-bit CMYK differs by 1 at 4:4:4, 2 subsampled
    8-bit YCCK differs by 3 at most

    We compared 8-bit CMYK and YCCK at 35x23, 64x64 and 200x133; on tiny subsampled images such as 3x5 the difference reaches 30, which is our existing 8-bit upsampling. CMYK is always output complemented (Adobe's convention), where libjpeg returns the stored values. We then checked 72 12-bit and 162 lossless files against libjpeg-turbo 3.1.3 in a separate build: all identical.

    Of damaged 12-bit and two-component files that both decoders decode, 2,629/2,629 cut-short files, 1,754/1,754 with a flipped scan bit and 524/524 with garbled scan bytes are identical. With a flipped header bit, 743 of 746: the other three have a damaged AC table our 8-bit path reads the same way. No panics; a new fuzz target runs in CI.

    Speed: existing 8-bit decodes are within 0.10% of 0.4.13's instruction counts. The new modes, as our time over libjpeg-turbo's: arithmetic 1.13 sequential and 0.87 progressive, 12-bit 1.97 sequential and 1.48 progressive. 12-bit is scalar and keeps the whole frame's coefficients.

    Left open:

    • SOF11: libjpeg-turbo doesn't implement arithmetic lossless, so we have nothing to check it against. It returns Unsupported. If you have such a file and a decoder that reads it, we'll do it. If nothing you support needs SOF11, say so and we'll close this.
    • decode_planar refuses 12-bit images.
    • Real files: we had no real DNG tile, medical 12-bit or Photoshop print file, so your test set is the check we'd most like to see.

    mata.network · remadewithrust.com

    If this helped, please follow us on X: @matanetwork

    -Thanks, Tim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions