Skip to content

feat: camera raw import — clean-room pure-Rust decoder with a LibRaw fallback - #94

Merged
IAmJSD merged 2 commits into
mainfrom
t3code/48fc8ff9
Sep 3, 2026
Merged

IAmJSD merged 2 commits into
mainfrom
t3code/48fc8ff9

Conversation

@IAmJSD

@IAmJSD IAmJSD commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

Two commits: the first adds camera raw import through a runtime-loaded LibRaw; the second adds crates/codec-raw, a pure-Rust, clean-room raw decoder that the plugin now tries first, falling back to LibRaw for whatever it declines. Most files no longer need LibRaw at all; nothing regresses where it is still needed.

Commit 1 — LibRaw via dlopen (the HEIC pattern)

  • LibRaw is dlopen'd at runtime (system copy; libraw_r preferred, plain build serialised). Every pure-Rust raw crate is LGPL/AGPL, hence no static link.
  • Develop: camera as-shot WB, sRGB, 16-bit, an exposure lift (99th percentile to white, capped +2 EV) with a soft shoulder instead of dcraw's 1%-clip stretch.
  • Thumbnails/Quick Look use the embedded camera JPEG, turned upright. Unedited raws archive verbatim. ORF/RW2/RAF EXIF via container shims. Packaging: weak LibRaw deps, raw MIME types, public.camera-raw-image.

Commit 2 — schist-codec-raw

Written by a fan-out of 4 foundation + 10 vendor Opus workers under a clean-room rule (specs, ExifTool tag docs, real files and LibRaw's output as oracle; no copyleft decoder source read or fetched), then 4 adversarial review passes (~30k mutated files) whose findings are fixed here.

Support ("exact" = every sample matches LibRaw's unpacked frame)

container status
DNG 26/26 exact incl. ProRAW/linear where LibRaw fails; uncompressed, lossless JPEG, deflate, lossy, float; GoPro VC-5 declined
Sony ARW/SR2/SRF 24/24, every generation incl. ARW 1.0 and ARW 4 lossless
Nikon NEF/NRW 36 files / 20 bodies incl. all six D850 variants (Huffman trees reconstructed from oracle data); Z 8/9 HE declined
Canon CR2 / CRW 14/14 CR2; sRAW/mRAW and compressed CRW declined
Canon CR3 container, ColorData (found in the CTMD track), previews exact; CRX pixels not decoded → LibRaw
Fujifilm RAF 9 bodies exact uncompressed; compressed RAF declined → LibRaw
Olympus ORF / Panasonic RW2 12/12 (four layouts) and 29/31; RawFormat 8 declined
Pentax PEF / Samsung SRW 10/10 and 15/17; NX1/NX500 codec declined
Minolta / Kodak / Epson / Mamiya 15 bodies; Kodak 65000 reverse-engineered; Epson as-shot WB unknown (declines colour)
Hasselblad / Phase One / Leaf 7/7, 5/11 ("IIQ S" declined), 3/3
Sigma X3F metadata + preview only

Colour: DNG carries its matrix; others use cameras.rs (189 bodies; 64 read from real DNGs, the rest recalled Adobe D65 values cross-checked against each body's own WB presets, with a duplicate-matrix guard). No matrix → the plugin prefers LibRaw.

Native vs LibRaw's own development over the 103 corpus files both handle: median 33 dB, p10 25 dB, worst 19.6 dB (a CHDK DNG). SCHIST_RAW_COMPARE=1 SCHIST_RAW_CORPUS=… cargo test -p schist-codecs-common native_agrees_with_libraw -- --nocapture prints the table; SCHIST_RAW_DECODER=native|libraw pins a decoder.

Robustness

Every decoder sizes its frame through frame_samples (400 MP ceiling, overflow-checked) and requires the file to hold at least a bit per sample; validate is overflow-safe; the plugin wraps native calls in catch_unwind. Reviewer fuzzing: 0 panics after fixes.

For a human to judge (licensing)

  • Reviewers noted the Olympus and Panasonic bit-level decoders use the same magic constants any correct decoder must (format-determined; prose is independent), and the bit-pump type names coincide with rawspeed's. Both flagged, neither judged a copy.
  • The recalled camera matrices are Adobe's published profile values; ~35 entries have no independent check yet.

Known gaps / follow-ups

  • CRX (Canon CR3) and Fuji compressed are the two big codecs still on LibRaw; both workers pinned the entropy layers and documented where they stopped in the module docs.
  • Nikon bodies without CropArea get their masked right-hand columns trimmed by a data-driven detector; one flat test-chart frame slips through.
  • No prebuilt/download offer for LibRaw yet (Windows has no system LibRaw), and CR3 has no EXIF in the gallery.
  • The pure-Rust crate would build for wasm; a web-only raw path is a follow-up.

Test plan

  • cargo test --workspace, cargo fmt --check, clippy --workspace --all-targets -D warnings
  • Crate: 296 tests incl. per-format corpus tests against LibRaw oracles (SCHIST_RAW_CORPUS=/tmp/rawsamples)
  • Plugin sweep over the full corpus (native → LibRaw fallback), gallery EXIF corpus
  • Native-vs-LibRaw PSNR table as above
  • macOS smoke test with Homebrew's LibRaw under the signed bundle

🤖 Generated with Claude Code

IAmJSD and others added 2 commits September 2, 2026 17:45
Opens NEF, ARW, CR2, CR3, DNG, RAF, ORF, RW2, PEF and the rest of what
LibRaw reads. Every pure-Rust raw decoder is LGPL (rawler, rawloader,
quickraw) or AGPL (zenraw), so this makes the choice HEIC made: nothing
links at build time and nothing is bundled; LibRaw is dlopen'd on first
import from a managed directory, else the system (Homebrew, the distro
package), preferring the reentrant build and serialising the plain one.

Development is camera white balance, sRGB primaries, 16 bits a channel,
and an exposure lift with a soft shoulder in place of dcraw's histogram
stretch, so raws open at the camera JPEG's brightness with the
highlights still there. The codec registers before TIFF, since most raws
are TIFF containers; its probe walks the IFDs for sensor data so a plain
TIFF still goes to its own codec.

Thumbnails and Quick Look use the camera's embedded JPEG, turned
upright. Unedited raws archive verbatim; the gallery reads ORF/RW2/RAF
EXIF through container shims (CR3 still has none). Packaging gains weak
LibRaw dependencies, the raw MIME types and the camera-raw UTI.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
A new crate that decodes and develops camera raws without LibRaw:
every vendor container (DNG, NEF, ARW, CR2/CRW/CR3, RAF, ORF, RW2,
PEF, SRW, MRW, Kodak, ERF, MEF, IIQ, 3FR/FFF, MOS, X3F) behind one
RawImage contract, a develop pipeline (levels, white balance,
Hamilton-Adams and X-Trans demosaic, D65-normalised camera matrix,
crop, orientation), and a 189-body colour matrix table with per-entry
provenance. Written from the public specifications and observation of
real files; nothing derives from a copyleft decoder.

Verified sample for sample against LibRaw's unpacked frames on some
250 raw.pixls.us files. Exact: DNG 26, Sony 24, Nikon 36, Canon CR2 14,
Pentax 10, Samsung 15, Olympus 12, Panasonic 29, Fuji 9 (uncompressed),
Hasselblad 7, Phase One 5, Leaf 3, and the Minolta/Kodak/Epson/Mamiya
set. Declined with a reason: Canon CR3 pixels (CRX), Fuji compressed
RAF, sRAW, compressed CRW, Panasonic RawFormat 8, Samsung NX1/NX500,
IIQ S, Foveon, GoPro VC-5.

The codecs-common plugin now tries the native crate first and falls
back to the dlopen'd LibRaw for what it declines or has no matrix for,
so nothing regresses; SCHIST_RAW_DECODER pins either. Against LibRaw's
own development the native output sits at a median 33 dB over the
files both handle.

Reviewed adversarially (four passes, ~30k mutated files): every
frame allocation is now bounded by a 400 MP ceiling and by the bytes
the file holds, arithmetic on file-derived values is checked, and the
findings (Sony overflows, Samsung CFA phase on five bodies, DNG
AsShotWhiteXY, float DNG outside deflate, restart-marker indices) are
fixed with tests.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@IAmJSD IAmJSD changed the title feat: camera raw import through a runtime-loaded LibRaw feat: camera raw import — clean-room pure-Rust decoder with a LibRaw fallback Sep 3, 2026
@IAmJSD
IAmJSD merged commit 839b2f4 into main Sep 3, 2026
4 checks passed
@IAmJSD
IAmJSD deleted the t3code/48fc8ff9 branch September 3, 2026 07:15
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