Skip to content

Camera look: on NEFs with a saturated LED wall in frame, the fitted tone curve rises steeply then plateaus and posterises skin and hair #4898

Backlog

Description

@Amarows

Updated 2026-12-01. The original guess (the plateau after the step, missed by limit_elasticity) was incomplete. The step and the plateau are one artefact with one cause: the saturated LED wall takes part in the tone fit. Details and measurements below.

What happens

On Nikon D90 NEFs shot on a stage in front of a saturated red/blue LED wall (flash on), the default render posterises skin, clothing and hair: faces and a beige jacket become a flat orange/yellow fill, and dark hair and stubble break into black blotches. The camera JPEG embedded in the same NEF, and an independent raw converter's render of the same file, show normal skin and fabric texture.

Hall shots from the same body and session (no LED wall in frame) render fine.

Columns: main (84b296e) | with saturated pixels left out of the tone fit (experiment below) | camera JPEG. Rows: frame A, frame B (faces cropped).

Screenshot from 2026-12-01 16-13-02

A synthetic repro that needs no private photos, the experimental patch and open questions for maintainers are in this comment.

Version and repro

Cause

The per-photo tone curve is fitted by quantile matching of scene luminance to JPEG luminance over all training pixels. That assumes the JPEG's luminance depends on scene luminance alone. It doesn't for saturated colours: the camera renders the red LED wall much brighter than skin or hair of the same scene luminance. Quantile matching sorts the two populations apart and joins them with a steep step followed by a plateau (the curves in the first version of this issue: elasticity ~3.6 over 5–8 knots, then 0.1–0.2 over more than 1 EV of mid-tones). Hair falls on the step, skin and fabric on the plateau.

The mechanism is the one #1904 describes (two pixel populations rendered at different brightness for the same scene luminance), but limit_elasticity can't catch it here: no single segment is over the bound, and smoothing the step leaves the plateau.

Measurements

Metric: gradient spread (p99/p50 of the log-luminance gradient) in a hair + skin box, render at --size 1072. Posterisation pushes it up. A dcraw reference scores 3.9 / 4.8 and the camera JPEG 5.0 / 5.2.

Experiment (on main 84b296e) Frame A Frame B
main 21.4 15.3
plateaus lifted to a minimum elasticity of 0.35 16.4 12.1
last segment's elasticity capped at 1.0–1.5 (the extrapolation) 21.4 15.6–15.8
fitted colour matrix refused 21.4 18.8
conditional-median tone fit (fit_tone_conditional) instead of quantile 3.8 13.7
JPEG pixels with saturation > 0.9 left out of the tone training (fit_tone_with_shadows, both the original and the shadow pairs) 5.7 4.9

Thresholds from 0.6 to 0.9 all give 3.8–5.9. A synthetic test (neutral subject on a known camera curve, 40% of the frame a red wall whose sensor RGB is [1, 0.25, 0.2] and JPEG [1, 0.03, 0.03] at 1.6× the subject's brightness) reproduces it: main puts subject tones off the camera curve by 0.084 in display gamma (≈ 21 levels of 255).

Side effect, from 47 NEFs (16 from this event, 31 D90 frames from other shoots): 23 render identically, the non-stage ones change by at most 0.5/255. Five stage frames change visibly, all through the LED wall: it renders darker and a deeper red than in the camera JPEG, because the brightness the camera gave it is no longer learned anywhere.

Possible fix

Leaving saturated pixels out of the tone fit fixes the posterisation, but as a hard cutoff it drops the wall's extra brightness. A more complete fix would fit the tone curve on near-neutral pixels and let the colour model carry the hue-dependent brightness, so the subject's curve stays clean and the wall keeps the camera JPEG's brightness. Today the colour model can't: HueSat changes hue and saturation only and restores luminance ("the camera tone curve owns it"). DCP profiles separate the two this way, with a single tone curve plus per-hue value scales in their HueSatMap/LookTable; doing the same here would mean giving the table a luminance gain per hue and saturation.

Related

Sample

The frames show people at a private event, so I won't post them publicly. I can send one or two NEFs privately to a maintainer.

Activity

  1. Amarows commented on Dec 1, 2026

    @Amarows
    Author

    Updated the description with a corrected root cause: the step and plateau come from the saturated LED wall taking part in the quantile tone fit, not from a gap in limit_elasticity. Leaving JPEG pixels with saturation > 0.9 out of the tone training brings both frames to the camera JPEG's level (measurements and side effects in the description). A before/after image follows.

  2. Amarows commented on Dec 1, 2026

    @Amarows
    Author

    Before/after and a synthetic repro – handing this over

    I've taken this as far as I can as a reporter. Below: what the experimental change does on the real frames, a synthetic repro that needs none of my photos, the patch, and questions where I need your judgement.

    Real frames (faces cropped)

    Columns: main (84b296e) | experimental patch below | camera JPEG embedded in the NEF. Rows: frame A, frame B.

    Frame A and B: main, patched, camera JPEG

    On main, skin is a flat fill with a hard edge (A) or a contoured highlight (B), and hair breaks into black specks. With the patch both look like the camera JPEG in texture. The LED wall comes out darker and a deeper red than in the JPEG (see the trade-off below).

    Synthetic repro (no private photos)

    A 960×640 stage: a textured "head" (skin + dark hair) in front of a red LED wall. Sensor RGB of the wall [1, 0.25, 0.2], its JPEG [1, 0.03, 0.03] at 1.6× the subject's brightness for the same scene luminance; the subject follows a known camera curve. The tone curve is fitted with collect_pairs + fit_tone_with_shadows on a 96×64 box-averaged proxy, then applied to the full frame. To isolate the tone curve, colour is taken from the JPEG and only brightness comes from the fitted curve.

    Columns: main | patch | camera JPEG.

    Synthetic LED wall stage: main, patched, camera JPEG

    On main the hair's texture is flattened and lifted to grey (it lands on the plateau); with the patch it matches the JPEG, and the wall is darker than in the JPEG.

    Unit test that fails on main (subject tones off the camera curve by 0.084 in display gamma) and passes with the patch:

    Test (crates/engine/src/camera_preview/tests.rs)
    fn led_wall_stage() -> (Rgb32f, Rgb32f, impl Fn(f32) -> f32) {
        let camera = |y: f32| 0.9 * (y / (y + 0.12)).powf(0.8);
        let mut sensor = Rgb32f::new(96, 64);
        let mut reference = sensor.clone();
        for (i, (src, dst)) in sensor.data.iter_mut().zip(&mut reference.data).enumerate() {
            let wall = i % 5 < 2;
            let (y, raw, jpeg, shown) = if wall {
                let y = 0.006 + ((i * 7919) % 2459) as f32 / 2459.0 * 0.006;
                (y, [1.0, 0.25, 0.2], [1.0, 0.03, 0.03], 1.6 * camera(y))
            } else {
                let y = 0.01 + ((i * 7919) % 3685) as f32 / 3685.0 * 0.24;
                (y, [1.0, 0.85, 0.7], [1.0, 0.85, 0.7], camera(y))
            };
            *src = raw.map(|v| v * y / luminance_2020(raw));
            *dst = jpeg.map(|v| v * shown / luminance_2020(jpeg));
        }
        assert!(reference.data.iter().flatten().all(|v| *v < 0.95));
        (sensor, reference, camera)
    }
    
    #[test]
    fn a_saturated_led_wall_does_not_bend_the_tone_curve_of_the_subject() {
        let (sensor, reference, camera) = led_wall_stage();
        let (pairs, _) = collect_pairs(&sensor, &reference, 0.005, None, false).unwrap();
        let tone = fit_tone_with_shadows(ToneFit::Quantile, &pairs, &sensor, &reference, None, |x| x).unwrap();
        let worst =
            (0..=48).map(|i| 0.01 + 0.005 * i as f32).map(|y| (tone.apply(y).powf(1.0 / 2.2) - camera(y).powf(1.0 / 2.2)).abs()).fold(0.0, f32::max);
        assert!(worst < 0.02, "subject tones off the camera curve by {worst} (display gamma)");
    }

    Experimental patch

    JPEG pixels with HSV saturation above 0.9 are left out of the tone training, in both pair sets of fit_tone_with_shadows. The held-out split is kept: the index still advances for a skipped pixel.

    Diff (crates/engine/src/camera_preview/pairs.rs)
    diff --git a/crates/engine/src/camera_preview/pairs.rs b/crates/engine/src/camera_preview/pairs.rs
    index ce7198caf..fe7457d23 100644
    --- a/crates/engine/src/camera_preview/pairs.rs
    +++ b/crates/engine/src/camera_preview/pairs.rs
    @@ -106,6 +106,13 @@ fn colour_pair_above(input: [f32; 3], output: [f32; 3], (ylo, clo): (f32, f32))
             && (ylo..0.85).contains(&luminance_2020(output))
     }
     
    +pub(super) const MAX_TONE_SATURATION: f64 = 0.9;
    +
    +fn saturation(rgb: [f64; 3]) -> f64 {
    +    let (hi, lo) = (rgb[0].max(rgb[1]).max(rgb[2]), rgb[0].min(rgb[1]).min(rgb[2]));
    +    if hi > 0.0 { (hi - lo) / hi } else { 0.0 }
    +}
    +
     /// Fit tone independently of colour eligibility when enough dark JPEG signal is available.
     /// The original pairs and their split still own the colour fit and its acceptance gates.
     /// Luminance needs no channel ratios, so the binned proxy can use a lower sensor floor and
    @@ -119,7 +126,14 @@ pub(super) fn fit_tone_with_shadows(
         colour: impl Fn([f64; 3]) -> [f64; 3],
     ) -> Option<CameraTone> {
         let luminances = |x: [f64; 3], y: [f64; 3]| (luma(colour(x).map(|v| v.max(0.0))), luma(y));
    -    let original = fit.fit(pairs.iter().enumerate().filter(|(i, _)| i % 3 != 0).map(|(_, (x, y))| luminances(*x, *y)).collect());
    +    let original = fit.fit(
    +        pairs
    +            .iter()
    +            .enumerate()
    +            .filter(|(i, (_, y))| i % 3 != 0 && saturation(*y) <= MAX_TONE_SATURATION)
    +            .map(|(_, (x, y))| luminances(*x, *y))
    +            .collect(),
    +    );
         let mut eligible = Vec::new();
         let (mut colour_index, mut extra_index) = (0, 0);
         for (i, (input, output)) in sensor.data.iter().zip(&reference.data).enumerate() {
    @@ -140,7 +154,7 @@ pub(super) fn fit_tone_with_shadows(
             let held = *index % 3 == 0;
             *index += 1;
             let (x, y) = luminances(input.map(f64::from), output.map(f64::from));
    -        if x.is_finite() && x > 0.0 {
    +        if x.is_finite() && x > 0.0 && saturation(output.map(f64::from)) <= MAX_TONE_SATURATION {
                 eligible.push((x, y, held));
             }
         }

    Checks: cargo fmt and clippy clean; 114/114 camera_preview tests pass; full engine suite 2029/2030. The one failure, smart_previews_from_an_older_look_fit_are_refreshed ("lens database changed while decoding the smart preview"), passes when run alone, 3/3 on both main and the patch. It looks like a race under the parallel suite.

    Effect on 47 D90 NEFs (16 from this event, 31 from other shoots): 23 render identically, non-stage frames change by at most 0.5/255, five stage frames change visibly, all through the LED wall.

    Questions

    1. Is leaving saturated JPEG pixels out of the tone fit acceptable as a fix? If so, how would you like the threshold validated? I only have my own D90 photos, not the CC0 corpus you use. Thresholds 0.6–0.9 all fixed both frames.
    2. Is the darker LED wall acceptable? The camera's extra brightness on saturated colours is no longer learned anywhere, because HueSat restores luminance ("the camera tone curve owns it").
    3. Would you take the fuller approach instead? That means fitting the tone curve on near-neutral pixels and letting the table learn per-hue brightness (the third slot of the DCP-style table, today fixed at 1 and undone by the luminance restore), so the wall keeps the JPEG's brightness. That reverses a design decision, so it's your call, and it touches fit order (tone before table), HueSat::apply, pooled profiles and LOOK_VERSION.
    4. Should the same filter apply elsewhere? Candidates are fit_tone_conditional and the pooled tone of calibrate (fit_profile_tone): a camera profile pooled from stage photos would learn the same step.
    5. Who carries it forward? I'm not in a position to develop this further myself. If the patch and test are useful as a starting point, feel free to take them; I'm happy to test builds on these frames and can send one or two NEFs privately.
  3. Amarows commented on Dec 1, 2026

    @Amarows
    Author

    @radialmonster, since you worked on #1904 (same two-population mechanism in NEFs), could you take a look at the questions in the comment above when you have time? @r3d42-git, cc as you've been working in camera_preview recently. I'm happy to test builds against these frames.

  4. r3d42-git commented on Dec 1, 2026

    @r3d42-git
    Contributor

    @Amarows, thanks for tracing this to the LED wall and sharing the patch and regression test. PR #4975 is ready for review. The GitHub checks passed and CodeRabbit completed its review.

    The PR keeps the existing tone curve if too few midtones remain or if the filtered curve gives no measurable improvement on held-out subject pixels. In the 462-entry RAW corpus, three native renders changed. Two moved closer to the camera JPEG overall and the Canon R100 C-RAW moved slightly farther away.

    Could you try commit 3aa75ce9f on your original D90 frames A and B at 1,072 px? It would help to know whether the hair and skin gradients recover on both and how bright the LED wall looks against the camera JPEG. The frames can stay private.

    If it's handy, you can build the PR in a separate worktree alongside your existing LightCraft checkout. From the root of that checkout, run:

    git fetch https://github.com/storytold/lightcraft.git refs/pull/4975/head
    git worktree add --detach ../lightcraft-pr4975 FETCH_HEAD
    (cd ../lightcraft-pr4975 && cargo build --release --locked -p lightcraft-cli)
    

    With Cargo's default target directory, you can use ../lightcraft-pr4975/target/release/lightcraft-cli in your original render … --size 1072 command for frames A and B.

  5. Amarows commented on Dec 1, 2026

    @Amarows
    Author

    @r3d42-git, thanks for picking this up. I tested 3aa75ce9f on frames A and B, rendered with lightcraft-cli render … --size 1072, a clean HOME and no sidecar.

    Hair and skin recover on both frames. Gradient spread (p99/p50) in the hair + skin box (before vs patch, before vs Camera JPEG):
    before-after

    Frame A Frame B
    camera JPEG 5.0 5.2
    main 84b296e 21.4 15.3
    PR #4975 3aa75ce9f 5.7 4.9

    On both frames the filtered curve is selected. The log shows filtered true, and the held-out subject error drops from 17.5 to 4.4 codes on A and from 9.5 to 3.3 on B. Both renders are pixel-identical to my experimental patch, so the before/after crops in my earlier comment also show this PR.

    The LED wall is darker than in the camera JPEG. Mean display luma (0–255) over the saturated-red wall pixels (JPEG saturation > 0.85, 22% of frame A, 30% of frame B):

    sample2
    Frame A Frame B
    camera JPEG 52.5 68.1
    main 63.9 62.3
    PR #4975 37.3 49.6

    That's about 15–19 levels below the JPEG, about 0.5 stop in display terms. The wall still reads as a deep red stage backdrop, and I find that acceptable for these photos: the subject matters more than the wall.

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