Skip to content

LR-IMP-FORMATS: decode Hasselblad lossless JPEG with selection value 8 (FFF, H3D/H4D/CFV-50c 3FR) - #555

Merged
echelon merged 4 commits into
storytold:mainfrom
radialmonster:hasselblad-p8
Oct 10, 2026
Merged

echelon merged 4 commits into
storytold:mainfrom
radialmonster:hasselblad-p8

Conversation

@radialmonster

Copy link
Copy Markdown
Contributor

TL;DR: LightCraft now decodes the Hasselblad raws that store their sensor data in Hasselblad's own
variant of lossless JPEG: every FFF file and the H3D, H4D and CFV-50c 3FR files (the other 14 of the 20 CC0
Hasselblad samples on raw.pixls.us, so all 20 now decode). The coding rule was first recalled by the AI model that wrote this change,
then tested against 17,279 alternatives per file with a tool built for the purpose, and confirmed by
measurements on the files. The measurements corrected the recalled rule in one respect (byte order).

Follows #448 (uncompressed 3FR, now on main); together they cover all 20 CC0 Hasselblad samples. Part of #420.

Change

  • crates/raw/src/ljpeg.rs: a lossless-JPEG scan whose SOS header gives predictor selection value 8 (which the
    JPEG standard, T.81, does not define) is decoded with Hasselblad's rule (decode_hasselblad):
    • the coded data are 32-bit little-endian words, each read from its most significant bit, with no byte stuffing
      (FF bytes are ordinary data);
    • each row is coded in pairs of samples: the two Huffman categories come first, then the two differences;
    • categories 1 to 15 work as in T.81; category 16 is followed by 16 bits that are ignored, and means -32768;
    • each sample is the previous sample of the same column parity in its row plus the difference, modulo 2^16;
      both start at 32768 at the beginning of every row.
  • Only the layout that every measured file has is accepted: one component, 16 bits, no point transform, no
    restart markers, even width. Anything else with selection value 8, and any selection value above 8, stays an
    "unsupported" error and the file stays preview-only, as before.
  • The Huffman decoder now works over any bit source (a small Bits trait), so the existing T.81 path is
    unchanged.
  • RENDER_CACHE_VERSION 22 to 23 (these files now render from the raw data).
  • docs/parity.md: the LR-IMP-FORMATS row lists the newly supported files.

Provenance

This needs to be stated plainly. The AI model that wrote this change (Claude) recalled the outline of this
coding from its training data, which includes other raw converters' code
. It said so before any work started,
and the work was stopped until the contributor directing it (not a LightCraft maintainer) decided how to proceed. The terms were: the rule must
be proven from the files by a tool we build; the recalled rule may be one candidate among many; no other
converter's source may be consulted; and nothing may be copied from memory as code.

That is what was done. No other converter's source, documentation or web page was opened, and no code was
copied.
The recalled rule was treated as one hypothesis, tested against alternatives with the verification
tool described below, and corrected where the measurements disagreed with it. The decoder was written fresh
from the measured rule, in the style of the existing lossless-JPEG code.

How the rule was verified

ljpeg-explorer is a standalone Rust command-line tool written for this (its own Cargo project, not part of the
product or of this pull request; I can share it and the full per-file hypothesis table if that helps review). It reads the JPEG markers, decodes the T.81 Huffman categories, and decodes a scan under any
combination of:

  • byte order (plain, or 2-, 4- or 8-byte little-endian words), bit order, byte stuffing on or off;
  • 1, 2 or 4 samples per code group, with categories interleaved with the difference bits or read first;
  • six treatments of category 16, and rows aligned to bytes or not;
  • 30 predictors: none, the 7 T.81 predictors, the same 7 on same-colour neighbours of the colour filter, and
    running predictors with 1, 2 or 4 lanes started from several values or from the row above.

That is 576 x 30 = 17,280 hypotheses per file. For each one the tool reports only scores computed from the file:

  • (a) whether it decodes the whole frame and where its stream ends relative to the end of the scan;
  • (b) smoothness: the mean difference between same-colour neighbours;
  • (c) the histogram: black level against the file's tag, and samples that wrap around the 16-bit range;
  • (d) correlation of colour ratios with the camera's own embedded preview, for each of the 4 colour-filter phases;
  • (e) black level and noise compared with the 6 uncompressed 3FR files of the same sensors.

It ran on all 14 files, full frames (243,298 rows logged).

question result
entropy layer Of 576 entropy hypotheses per file, only the selected one (and category-16 variants of it) decodes the whole frame in the 13 files whose Huffman table has invalid codes. Everything else fails, almost always in the first row. The selected rule's stream ends in the last 32-bit word before the end-of-image marker in 8 files, exactly at the strip end or at the start of filler in 3, and 1 to 2 words before it in 3. In the H4D 3FR, whose Huffman table has no invalid codes, 268 wrong hypotheses decode a full frame, but none ends at the scan end.
recalled rule as stated It assumed plain byte order. It fails within the first 4 samples in the 13 files whose Huffman table has invalid codes, and does not end at the scan end in the H4D 3FR. The files use 32-bit little-endian words. The rest of the recalled rule (pairs with categories first, no stuffing, two running predictors started at 32768 every row, a special case for category 16) was confirmed.
category 16 The 16 bits that follow are a constant per file (0x7FFF in the H3D, 0xFFFF elsewhere). Reading them as a value turns rows red in the CFV-50 and lowers chroma agreement (0.88 -> 0.44); ignoring them and using -32768 is right for both.
predictor Against the best different predictor, smoothness is 1.38 to 2.09 times better, and chroma agreement with the preview is clearly higher in every file. RGGB is the best colour-filter phase in all 14.

What the scores cannot pin down, said plainly:

  • The sign of the category-16 difference: +32768 and -32768 are the same modulo 2^16.
  • In 5 files with no category-16 codes, the category-16 rule is untested.
  • The exact start value 32768: a different constant shifts every sample equally. It is pinned only by the ends of
    the value range in files that reach them. For example, the X1D II's fill columns equal 65535, exactly as in its
    uncompressed siblings, only at 32768; dark and saturated samples elsewhere wrap one step either side.
  • Three files carry one or two unexplained 32-bit words after the stream.
  • Two scores were weak: luminance correlation does not discriminate, and the noise comparison with uncompressed
    relatives (shot at other ISOs) favours the rule in only 6 of 8 files.

Results

  • All 14 files decode. LightCraft's samples equal the verification tool's decode sample for sample in all 14.
  • Geometry and colour-filter phase were checked on the raw mosaic against each file's embedded preview (not on a
    fitted render): RGGB is the best phase in every file, and the preview covers the default crop.
  • Renders compared by eye with the camera previews: same framing and orientation; colours and tones match apart
    from LightCraft's flatter neutral rendering. No row bands or colour casts.
  • Identity: 25 files of other formats decode bit-identically before and after (CR2, CR3, NEF, ARW, RAF, RW2, PEF,
    DNG including Hasselblad L1D/L2D, the 6 uncompressed 3FR). 20 more fail identically on both.

Tests

Hand-made byte streams in ljpeg.rs:

  • selection_value_8_known_answer: a 4 x 2 frame written out bit by bit (category 16 with two different ignored
    payloads, modular wrap, per-row restart, the FF FD bytes that a stuffing reader would take for a marker).
  • selection_value_8_reads_little_endian_words: the same bits in plain byte order are a different stream.
  • selection_value_8_round_trips_through_the_test_writer: a test-only writer of the rule, with extremes.
  • selection_value_8_rejects_bad_streams: truncated data, a category above 16, an odd width, other precisions,
    selection value 9, and every prefix of a stream fail cleanly without a panic.
  • In lib.rs, selection_value_8_cfa_tiff_decodes checks the 3FR/FFF layout end to end, and the
    unknown-predictor test now uses selection value 9.

The three decode tests fail without the change.

CI

  • cargo fmt --check: clean.
  • cargo clippy --workspace --all-targets -D warnings: clean.
  • cargo test --workspace --no-fail-fast: 1,394 passed, 0 failed, 14 ignored in the 67 test binaries that
    completed. The ui-egui library binary (its headless tests are known to be flaky under load) had 4 headless
    failures in the full run and stopped without a summary. Run alone it passed: 363 passed, 2 ignored.
  • cargo xtask parity, layers, assets, wasm: OK.

After merging current main into the branch (the diff is now ljpeg.rs, raw/src/lib.rs, media.rs and the parity row): cargo fmt --check, cargo clippy -p lightcraft-raw -p lightcraft-engine --all-targets -D warnings, cargo test -p lightcraft-raw and cargo test -p lightcraft-engine --lib pass (one engine test known to be flaky under load, tests_denoise::model_installation_…, failed in the full run and passed alone), and cargo xtask parity passes with the same "summary is stale" note as main.

…8 (FFF, H3D/H4D/CFV-50c 3FR)

Lossless JPEG scans whose SOS gives predictor selection value 8 (which T.81 leaves undefined) are read as Hasselblad codes them (ljpeg::decode_hasselblad): 32-bit little-endian words read from their most significant bit, no byte stuffing; each row in pairs of samples, both Huffman categories before both differences; category 16 is followed by 16 ignored bits and means -32768; each sample is the previous sample of its column parity in the row plus the difference, modulo 2^16, both starting at 32768 on every row. Only the layout measured is accepted (one component, 16 bits, no point transform or restarts, even width); other layouts and selection values above 8 stay Unsupported. The rule was measured on 14 CC0 files with the ljpeg-explorer tool. RENDER_CACHE_VERSION 23.
…lad-p8 (render cache version 23, as storytold#448 has 22)

# Conflicts:
#	crates/engine/src/media.rs
#	docs/parity.md
… render cache version 23, as main is at 22)

# Conflicts:
#	docs/parity.md
…nance

Merge origin/main into storytold#555. The decode_hasselblad doc comment records how the coding rule was
established (first hypothesis recalled by the AI author from training data that includes other
converters, corrected for byte order and confirmed by a black-box search over 17,280 layout
hypotheses on the CC0 files; the code is original). The LR-IMP-FORMATS row on main's text now
lists the decoded FFF and lossless-JPEG 3FR files instead of preview-only. RENDER_CACHE_VERSION
37 -> 38.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@echelon

echelon commented Oct 10, 2026

Copy link
Copy Markdown
Member

Thank you @radialmonster! The Hasselblad decoder landed in #692. I added a short provenance note to the decode_hasselblad docs (first hypothesis recalled, then corrected and confirmed by exhaustive search on CC0 files; the code is original), and updated the parity row.

Please join our Discord if you haven't already: https://discord.gg/artcraft

emmertex added a commit to Kaan88/lightcraft that referenced this pull request Oct 26, 2026
LR-PROF-CAMERACOLOR: bundled X20, X100T, X-T1, D700 and D70 profiles (upstream storytold#2160)
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