Skip to content

M11.4: raws from 52 camera models start from matrices fitted to measured spectral sensitivities - #560

Merged
echelon merged 3 commits into
storytold:mainfrom
rossaaiofficial:rossa/spectral-camera-color
Oct 10, 2026
Merged

echelon merged 3 commits into
storytold:mainfrom
rossaaiofficial:rossa/spectral-camera-color

Conversation

@rossaaiofficial

Copy link
Copy Markdown
Contributor

What it does

A raw that carries no colour matrix now gets DNG ColorMatrix1/2 and ForwardMatrix1/2 for its model, when the model is one of the 52 cameras in rawtoaces-data (Academy Software Foundation, Apache-2.0, tag v1.1.0, commit 5cfa5370). That is 22 Canon, 11 Nikon, 11 Sony, 3 Fujifilm and 5 other bodies, plus the 23 other names in rawtoaces-data's data/aliases.json (Rebel T3i and Kiss X5 for the EOS 600D, for example, and bodies it lists as sharing a sensor, such as the GFX100S with the GFX 100).

  • tools/spectral_camera_table.py fits, under CIE Standard Illuminant A and D65, a white-preserving 3x3 from white-balanced camera RGB to XYZ, by least mean CIE76 error over rawtoaces-data's 190 training reflectances. It writes crates/raw/src/spectral_table.rs. The method follows the description in Academy P-2013-001. No rawtoaces code is used. Run on rawtoaces-data v1.1.0 with the pinned packages, it reproduces the committed spectral_table.rs byte for byte.

  • lightcraft_raw::spectral::find looks a camera up by Exif make and model, then Camera::fill writes the matrices into ColorData. The existing DNG colour model in crates/raw/src/color.rs does the rest.

  • camera_preview::starting_colour takes the first available of these:

    1. the file's own matrices (DNG)
    2. a camera profile for the model, bundled or local, fitted to the JPEG for tone
    3. the spectral matrices
    4. the file-local JPEG fit
    5. the neutral fallback

    If a profiled model's profile fit fails, the model gets its spectral matrices.

  • White balance stays relative to the as-shot look for every raw without matrices of its own. Photo::relative_wb, the WB controls and old catalogs are unchanged. lightcraft-cli calibrate still collects pairs from covered models, so a profile fitted later takes precedence.

  • RENDER_CACHE_VERSION goes to 23.

No number in this change comes from Adobe, from a DNG Converter matrix or from dcraw, LibRaw, rawspeed, darktable, RawTherapee, rawloader or rawler. The only inputs are rawtoaces-data's measured spectral sensitivities, its training reflectances, its alias list and the CIE 1931 colour-matching functions it ships. This follows the clean-room rule in AGENTS.md. Attribution is in assets/ATTRIBUTION.md and NOTICE.

Why an embedding app needs it

An app that embeds the engine renders CR2, NEF, ARW and RAF files from these bodies with a colour model that has the same values on every photo of a body. The values do not depend on the embedded JPEG, the picture style or the scene. Before this change, colour came from a per-file JPEG fit when the fit passed its gates. Otherwise it came from the neutral fallback. This also addresses the first step named in #380 ("per-camera calibration comes first") for the 52 bodies.

What changes for bodies in the table

For a body in the table without an accepted profile fit, the spectral matrices now come before the file-local JPEG fit. Such a raw starts from the spectral matrices and LightCraft's default tone curve, and no longer from a colour and tone curve fitted to its own JPEG. Two corpus files show this. raf-fuji-gfx100s-4503.raf reaches the GFX 100 row through the alias list, and arw-sony-a7rm2-12bit-uncompressed.arw (#232) has its own row. Both had an accepted JPEG fit before this change, and their tests now expect the spectral matrices.

No colour-difference measurement of the spectral matrices against the camera JPEG, or against the file-local fit, is part of this change. The order of steps 3 and 4 is therefore a choice, not a measured result. If you want the JPEG fit to come first, moving step 3 after step 4 is a one-line change in starting_colour, and the corpus tests above would then expect the JPEG fit again.

How it was tested

The tests ran on macOS arm64 with rustc 1.99.0, at 88ea1a8 plus this commit. LIGHTCRAFT_CORPUS pointed at a folder whose raw/ held these CC0 corpus files, downloaded from the URLs in xtask/src/main.rs: arw-sony-a7m4-14bit.arw, arw-sony-a7rm2-12bit-uncompressed.arw, cr2-canon-5d3.cr2, cr2-canon-5d2.cr2, cr2-canon-7d.cr2, cr3-canon-r100-raw.cr3, dng-canon-5d3-16bit.dng, nef-nikon-d5100-lossless.nef, nef-nikon-d7500-lossless14.nef, raf-fuji-gfx100s-4503.raf, raf-fuji-xt2-865.raf and raf-fuji-xt20-compressed.raf.

cargo fmt --all -- --check                                                         # ok
cargo clippy -p lightcraft-raw -p lightcraft-engine --all-targets -- -D warnings   # ok
cargo test --locked -p lightcraft-raw -p lightcraft-engine                         # ok: engine 485 passed, raw 190 passed, raw integration suites 54 passed
cargo xtask parity                                                                 # ok: 552 rows, 1037 references
cargo xtask layers                                                                 # ok
cargo xtask assets                                                                 # ok: 42 asset files, all attributed
cargo check --target wasm32-unknown-unknown -p lightcraft-raw                      # ok
cargo check --target wasm32-unknown-unknown -p lightcraft-engine                   # ok

With these files present, corpus_colour_precedence, corpus_raf_colour_and_white_balance and corpus_arw_with_a_lens_corrected_preview_gets_a_camera_look read their files and skipped none.

The full cargo xtask ci was not run. It covers the whole workspace, and this change touches lightcraft-raw and lightcraft-engine internals, plus one new public module, lightcraft_raw::spectral.

New tests:

  • crates/raw/src/spectral.rs: Exif names find their rows (Canon EOS-1Ds, the DIGITAL suffix, NIKON CORPORATION, PowerShot), every alias reaches its target, other bodies and other makes find nothing, every row is a finite and invertible DNG profile whose forward matrices map white to D50, and fill keeps a file's own matrices.

  • crates/engine/src/camera_preview.rs, one test per precedence step:

    • a_files_own_matrices_come_first
    • a_camera_profile_is_tried_before_the_spectral_matrices (ILCE-7M4, bundled profile, no JPEG, so the spectral matrices follow)
    • spectral_matrices_come_before_the_photos_own_fit
    • a_camera_outside_the_table_keeps_the_fit_or_the_fallback
  • corpus_colour_precedence checks the order on real files:

    • ILCE-7M4 takes its profile.
    • EOS 5D Mark III and D5100 take the spectral matrices.
    • EOS 7D keeps its own fit.
    • The DNG keeps its matrices.
    • White balance stays relative for every raw.

    As a control, removing the profile step from starting_colour makes this test fail on the ILCE-7M4.

Each of these eight changes to the new code made at least one new or changed test fail: not filling the spectral matrices, removing the profile step, putting the JPEG fit before the spectral matrices, overwriting a file's own matrices, deriving relative white balance from the transform, ignoring the alias list, keeping a leading EOS in the model name, and ignoring the make.

  • corpus_arw_with_a_lens_corrected_preview_gets_a_camera_look (Odd colors when importing photos into LightCraft. #232) still checks that the away-from-edges fit is accepted on the ILCE-7RM2, now by calling the fit directly. It also asserts that loading the file now uses the spectral matrices. corpus_raf_colour_and_white_balance now expects the GFX100S to use the spectral matrices.

What it does not change

  • DNG files and every raw that carries its own matrices.
  • The file-local JPEG fit, its gates and the pooled-profile path, for every body outside the table.
  • Bodies with a bundled or local profile (ILCE-7M4, X-T4, X-H2S) while their profile fit is accepted.
  • White-balance semantics, catalog data, XMP, smart-preview headers and lightcraft-cli calibrate.
  • HDR and panorama merge (crates/merge) and DNG conversion and export, which read lightcraft_raw::decode directly and still see the file's own colour data.
  • The camera's picture style, which the spectral matrices do not model.

Licence

The table is derived from Apache-2.0 data. This change keeps lightcraft-raw under the workspace licence and meets Apache-2.0 §4 through LICENSE-APACHE, the file header, NOTICE and assets/ATTRIBUTION.md. If you prefer to mark derived data the way crates/segment is marked, as Apache-2.0 only, say so and I will change it. The CIE 1931 colour-matching functions that rawtoaces-data ships are marked CC-BY-4.0, and they are credited in both places.

Issues

Refers to #380 and the LR-PROF-CAMERACOLOR row in docs/parity.md. Follows #310. No open issue asks for exactly this change, so it closes none.

🤖 Generated with Claude Code

…red spectral sensitivities

A raw without colour matrices of its own now gets DNG ColorMatrix1/2 and
ForwardMatrix1/2 for its model when the model is one of the 52 cameras
in rawtoaces-data (Academy Software Foundation, Apache-2.0): 22 Canon,
11 Nikon, 11 Sony, 3 Fujifilm and 5 others, plus the other names its
alias list gives (Rebel T3i and Kiss X5 for the EOS 600D; bodies it
lists as sharing a sensor). tools/spectral_camera_table.py fits, under
Standard Illuminant A and D65, a white-preserving 3x3 to the measured
spectral sensitivities over rawtoaces-data's 190 training reflectances
and writes crates/raw/src/spectral_table.rs. No number comes from Adobe
or from a raw decoder. lightcraft_raw::spectral looks a camera up by
Exif make and model; the DNG colour model does the rest.

camera_preview::starting_colour takes the first available of: the
file's own matrices, a camera profile for the model (fitted to the
JPEG for tone), the spectral matrices, the file-local JPEG fit, the
neutral fallback. A profiled model whose profile fit fails takes its
spectral matrices. White balance stays relative to the as-shot look for
every raw without matrices of its own, so Photo::relative_wb and the
controls are unchanged; calibrate still pools covered models.

Tests: one per precedence step (synthetic), the table's lookup, aliases
and DNG shape, and a corpus test of the order on ILCE-7M4 (profile),
EOS 5D Mark III and D5100 (spectral), EOS 7D (own fit) and a DNG. The
ILCE-7RM2 and GFX100S corpus tests now expect the spectral matrices.
Attribution in assets/ATTRIBUTION.md and NOTICE. RENDER_CACHE_VERSION 23.

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

echelon commented Oct 10, 2026

Copy link
Copy Markdown
Member

Thank you @rossaai! Licensing is clean: rawtoaces-data is Apache-2.0 per file, the training set is too, the CMFs are CC-BY-4.0, and NOTICE and ATTRIBUTION are in place. Thanks for documenting that so carefully.

The ordering stops me from landing it. starting_colour uses the spectral matrices before the per-file JPEG fit, so for 52 popular bodies (ILCE-7M3, ILCE-7RM4, 5D Mark III, …) a JPEG-fitted tone and colour becomes a bare matrix plus the default curve. The description says that hasn't been measured. Either:

  • use the spectral matrix only when the per-file JPEG fit fails (before the neutral fallback); or
  • show ΔE against the camera JPEG, and ideally against Lightroom, on the CC0 corpus for those bodies, showing the spectral start is better.

#657 also landed #528, #583, #568 and #616, which refactor fit_preview and the camera-look code, so please rebase onto main. Also bump RENDER_CACHE_VERSION past main's.

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

echelon and others added 2 commits October 10, 2026 03:27
Merge origin/main into storytold#560 (fit_preview / camera-look refactors of storytold#528, storytold#583, storytold#568, storytold#616).
`starting_colour` now tries the per-file camera-JPEG fit (with the camera profile when there is
one) first and uses the spectral-sensitivity matrices only when that fit fails, before the neutral
fallback, so no photo with an accepted JPEG fit changes. Tests pin the order through an injectable
fit; docs, parity, ROADMAP and what's new describe it. RENDER_CACHE_VERSION 37 -> 38.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… by mistake)

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

echelon commented Oct 10, 2026

Copy link
Copy Markdown
Member

Thank you @rossaai! This landed in #692. I merged main into your branch and reordered the starting colour: the file's own matrices, then the per-file JPEG fit, then your spectral matrices, then neutral. So photos with an accepted JPEG fit render exactly as before, and your matrices replace the neutral fallback for 52 bodies. A test pins that order. A ΔE comparison of the spectral start against the camera JPEGs would still be very welcome.

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

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