LR-PROF-CAMERACOLOR: only a look with the profile's colour takes its pooled tone curve

The pooled curve maps luminance through the profile's matrix. A photo whose own fit
rejected the profile's colour renders through its own matrix, so continuing its curve
along the pooled one gave it a highlight mapping fitted to other colours. Such a look now
keeps its curve as fitted (with_pooled_tone). LOOK_VERSION 12. Found by CodeRabbit on #2093.
This commit is contained in:
robmdunn committed 2026-10-18 19:01:55 -04:00
1 parent 1105af1d9e
commit 27d65b252b
2 files changed
+29 -6

No files matched your search

+28 -5
View File
@@ -36,7 +36,8 @@ use lightcraft_raw::{
/// 9: the bundled NIKON Z 8 and NIKON D5300 profiles.
/// 10: a photo no look fits keeps its camera profile's colour instead of the neutral fallback.
/// 11: a camera profile's pooled tone curve continues a photo's own past what its JPEG showed.
pub const LOOK_VERSION: u32 = 11;
/// 12: only a look with the profile's colour continues along its pooled tone curve.
pub const LOOK_VERSION: u32 = 12;
#[derive(Clone, Debug)]
pub(crate) struct CameraLook {
@@ -273,6 +274,7 @@ pub(crate) fn fit_preview(raw: &RawImage, bytes: &[u8], transform: &CameraTransf
let profile = raw.metadata.model.as_deref().and_then(crate::camera_profiles::get);
let colour = profile.as_ref().and_then(|p| Some((p.matrix().mul(&transform.matrix.inverse()?), p.hue_sat.clone())));
let pooled = profile.as_ref().and_then(|p| p.tone);
let profile_matrix = colour.as_ref().map(|c| c.0);
let fitted = fit_look(&sensor, &reference, &clipped, colour.clone());
let tone_only = |colour| {
let unclipped = without_clipped(&sensor, &clipped);
@@ -280,10 +282,7 @@ pub(crate) fn fit_preview(raw: &RawImage, bytes: &[u8], transform: &CameraTransf
};
let Some(look) = with_profile_colour(fitted, colour, pooled, tone_only) else { return (None, black_and_white) };
// the camera's own curve past what this photo's JPEG showed
let look = match pooled {
Some(pooled) => CameraLook { tone: extend_tone(&look.tone, &pooled), ..look },
None => look,
};
let look = with_pooled_tone(look, profile_matrix, pooled);
// Sony's Dynamic Range Optimizer (on by default) brightens the camera JPEG's darker regions,
// not the raw: keep the colour fitted to the JPEG, take the tone curve without DRO.
let dro = lightcraft_raw::embedded_preview_dynamic_range_optimized(bytes) == Some(true);
@@ -301,6 +300,16 @@ pub(crate) fn fit_preview(raw: &RawImage, bytes: &[u8], transform: &CameraTransf
(Some(look), black_and_white)
}
/// `look` with its tone curve continued along the camera profile's `pooled` one ([`extend_tone`]), when the look
/// takes the profile's colour (`profile_matrix`). A look fitted with the photo's own matrix keeps its curve: the pooled
/// one maps luminance through the profile's matrix, which that photo's colours don't go through.
fn with_pooled_tone(look: CameraLook, profile_matrix: Option<Mat3>, pooled: Option<CameraTone>) -> CameraLook {
match (pooled, profile_matrix) {
(Some(pooled), Some(matrix)) if look.matrix == matrix => CameraLook { tone: extend_tone(&look.tone, &pooled), ..look },
_ => look,
}
}
/// `fitted`, the photo's own look, or when no look fits it (a black-and-white JPEG has no colour; a dark one too few
/// colour pairs) its camera profile's `colour`: with the JPEG's tone curve when [`fit_tone_only`] finds one
/// (`monochrome_look` does it without a profile), else the profile's pooled tone curve, else the default raw one. Dropping the profile there for the
@@ -4171,6 +4180,20 @@ mod tests {
}
}
#[test]
fn only_a_look_with_the_profiles_colour_continues_along_its_pooled_curve() {
let profile = Mat3([[1.2, -0.1, -0.1], [-0.1, 1.1, 0.0], [0.0, -0.2, 1.2]]);
let pooled = sampled(camera_curve, 0.0005, 0.9);
let dark = sampled(|x| 0.9 * camera_curve(x), 0.0005, 0.02);
let with = |matrix| CameraLook { matrix, tone: dark, hue_sat: None };
// the profile's colour: the curve goes on along the pooled one
assert_eq!(with_pooled_tone(with(profile), Some(profile), Some(pooled)).tone, extend_tone(&dark, &pooled));
// the photo's own matrix (the profile's colour rejected): its curve as fitted
assert_eq!(with_pooled_tone(with(Mat3::IDENTITY), Some(profile), Some(pooled)).tone, dark);
assert_eq!(with_pooled_tone(with(profile), None, Some(pooled)).tone, dark);
assert_eq!(with_pooled_tone(with(profile), Some(profile), None).tone, dark);
}
#[test]
fn a_photo_no_look_fits_takes_the_profiles_pooled_tone_curve() {
let colour = (Mat3::IDENTITY, None);
+1 -1
View File
@@ -195,7 +195,7 @@ When no look fits a photo of a profiled model (step 2 and 3 both fail: a black-a
Before this, such a photo fell to 4 or 5 and the profile did nothing for it. Measured on two bracketed bursts of one NIKON D780 scene (a colourful woven runner in low sun, ISO 100, f/8, frames 1.1 to 3.7 EV under the burst's brightest, each rendered with Exposure raised by its measured offset and compared with the brightest frame, CIEDE2000 and chroma of the colourful areas), with a local profile pooled from 255 of the photographer's D780 photos: the five frames 2.8 EV or more under have no look (their camera JPEGs are nearly black) and rendered at 35-48% of the reference's chroma with hue off by up to 9° whether or not the profile existed; with the profile's colour kept they render at 88-99% of it, hue within 5°. Frames up to 2.3 EV under fit a look and are unchanged. The pushed frames remain 4-7 L\* darker than the reference: a dark photo's tone curve is fitted to its shadows only (or is the default curve), which the profile's colour doesn't change (the pooled tone curve, next, does).
**A pooled tone curve.** A photo's tone curve only reaches as far as its JPEG's own pixels: a dark photo's ends in the shadows, and past its last knot `CameraTone::apply` goes on as a bounded shoulder whose rate comes from the last two knots. Push such a photo's exposure up and most of it lands on that shoulder. `lightcraft-cli calibrate` therefore also pools tone samples (white-balanced camera RGB → the camera JPEG's luminance, every proxy pixel whose channels are above 0.0001 and whose JPEG luminance is in 0.0003-0.85, at most 4,000 per photo), and after fitting the matrix fits one curve through it by quantile matching (`fit_profile_tone`): every photo went through the same camera curve, so the pooled scene and JPEG distributions are still one monotone map apart, and dark photos supply the shadows while bright ones supply the highlights (picture styles and Active D-Lighting are averaged). The profile stores it as `tone` (absent in profiles fitted before; the bundled ones have none yet). A photo of the model keeps its own curve up to its last knot and from there follows the pooled curve, faded from its own level over 1 EV (`extend_tone`); a photo no look fits takes the pooled curve whole instead of the default one.
**A pooled tone curve.** A photo's tone curve only reaches as far as its JPEG's own pixels: a dark photo's ends in the shadows, and past its last knot `CameraTone::apply` goes on as a bounded shoulder whose rate comes from the last two knots. Push such a photo's exposure up and most of it lands on that shoulder. `lightcraft-cli calibrate` therefore also pools tone samples (white-balanced camera RGB → the camera JPEG's luminance, every proxy pixel whose channels are above 0.0001 and whose JPEG luminance is in 0.0003-0.85, at most 4,000 per photo), and after fitting the matrix fits one curve through it by quantile matching (`fit_profile_tone`): every photo went through the same camera curve, so the pooled scene and JPEG distributions are still one monotone map apart, and dark photos supply the shadows while bright ones supply the highlights (picture styles and Active D-Lighting are averaged). The profile stores it as `tone` (absent in profiles fitted before; the bundled ones have none yet). A photo of the model whose look takes the profile's colour keeps its own curve up to its last knot and from there follows the pooled curve, faded from its own level over 1 EV (`extend_tone`); a look fitted with the photo's own matrix (the profile's colour rejected) keeps its curve as fitted, since the pooled one maps luminance through the profile's matrix (`with_pooled_tone`, `LOOK_VERSION` 12); a photo no look fits takes the pooled curve whole instead of the default one.
On the same D780 brackets with the profile refitted with its tone curve (255 photos), the pushed frames match the reference's lightness: ΔL\* −0.2 to +0.5 for every frame (−1 to −7 before), median CIEDE2000 in the L\* 40-70 band 0.9-2.6 (2.0-13.5 before), hue within 5°. Colourfulness in the second burst comes out 16-28% above the reference in midtones. On 40 D780 photos the profile never saw, rendered unedited and compared with their camera JPEGs, the curve changes nothing that matters: mean ΔE 2.67 with the colour-only profile, 2.68 with the curve (2.77 without a profile), mean ΔL\* +0.71 → +0.73, one photo worse by more than 0.25 (0.38).