Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Merge PR #1316

Brandon Thomasaf8843bf012a
diff --cc crates/engine/src/camera_preview.rs
index 60c0bd556,39b0d1b1c..3939ecf1c
--- a/crates/engine/src/camera_preview.rs
+++ b/crates/engine/src/camera_preview.rs
@@@ -114,113 -137,6 +144,113 @@@ fn starting_colour_with
(xy, t, look)
}
+/// The colour a raw starts from ([`starting_colour`]) as DNG colour tags, for a DNG of the raw's camera RGB (Photo
+/// Merge, issue #965): without them a merge of CR2s, whose colour comes from a look fitted to their JPEG, rendered
+/// green and dark. `None` when the file has colour matrices of its own (a DNG keeps them).
+pub(crate) fn starting_colour_tags(raw: &mut RawImage, bytes: &[u8]) -> Option<ColorData> {
+ if lightcraft_raw::color::has_matrix(&raw.color) {
+ return None;
+ }
+ // the white the source shows, decided before `starting_colour` may fill in measured matrices
+ let scale = crate::files::WbScaleOf::new(raw.format, &raw.color, raw.wb_multipliers, &raw.metadata);
+ let white = scale.dng_white(&raw.color, lightcraft_raw::color::as_shot_white_xy(raw));
- let (_, t, look) = starting_colour(raw, bytes);
++ let (_, t, look) = starting_colour(raw, bytes, None);
+ dng_colour(&raw.color, &white, &t, look.as_ref())
+}
+
+/// Give a raw about to be written as a DNG of its own (Export as DNG, Convert / Copy as DNG) the colour it opens
+/// with, when the file has no colour matrices (issue #1745: a Z 9 NEF's DNG had no `ColorMatrix1`, and other raw
+/// converters refused it). The lens corrections stay in the DNG; only the fit sees the raw without them, as when
+/// the photo opens.
+pub(crate) fn with_starting_colour(raw: &mut RawImage, bytes: &[u8]) {
+ if lightcraft_raw::color::has_matrix(&raw.color) {
+ return;
+ }
+ let list3 = raw.opcodes.list3.clone();
+ raw.opcodes.list3.retain(|op| !op.is_lens_correction());
+ let tags = starting_colour_tags(raw, bytes);
+ raw.opcodes.list3 = list3;
+ if let Some(tags) = tags {
+ raw.color = tags;
+ }
+}
+
+/// DNG tags that render white-balanced camera RGB at its as-shot white as `t` with `look` on top does: one forward
+/// matrix (with a colour matrix and as-shot neutral that agree with it at that white), the look's hue/saturation
+/// table as `ProfileHueSatMap` and its tone curve as `ProfileToneCurve`. Not quite the same render: a DNG table
+/// doesn't keep each pixel's luminance as the fitted one does ([`HueSat`]), and the chroma curve has no tag; on two
+/// CC0 CR2s (EOS 550D, 40D) an HDR merge of three copies renders within mean CIE76 ΔE 1.07 and 0.47 of the photo
+/// (1.47 and 0.66 without the table; 26 and 23 with no colour tags at all).
+/// The colour matrices declare `declared`, the white balance the source shows ([`crate::files::WbScaleOf::dng_white`]),
+/// so the DNG opens at the source's As Shot temperature and tint and a white-balance edit starts from it. Matrices
+/// derived from `forward` put the declared white wherever the look and the generic camera model took it: ARW merges
+/// opened at about 7000 K / +115 where the ARWs show 4800 K / +8, and any white-balance edit turned them green.
+/// `forward` is used only with the as-shot neutral, so the render at As Shot doesn't depend on them.
+/// `None` when the white balance or the matrix can't give tags.
+pub(crate) fn dng_colour(tags: &ColorData, declared: &crate::files::DngWhite, t: &CameraTransform, look: Option<&CameraLook>) -> Option<ColorData> {
+ let m = look.map_or(t.matrix, |l| l.matrix.mul(&t.matrix));
+ // the DNG model renders the camera's white at unit brightness: what a look does to white's brightness goes
+ // into the baseline exposure
+ let white = m.apply([1.0; 3]);
+ let k = (white[0] + white[1] + white[2]) / 3.0;
+ if !(k.is_finite() && k > 1e-6 && t.wb.iter().all(|w| w.is_finite() && *w > 0.0)) {
+ return None;
+ }
+ let m = Mat3(m.0.map(|r| r.map(|v| v / k)));
+ let forward = bradford(D65, D50).mul(&REC2020.to_xyz()).mul(&m);
+ let wb = t.wb.map(f64::from);
+ let neutral = wb.map(|w| wb.iter().cloned().fold(f64::MAX, f64::min) / w);
+ let (illuminant, color_matrix) = match declared {
+ crate::files::DngWhite::Matrices(c) if lightcraft_raw::color::has_matrix(c) => (c.illuminant, c.color_matrix),
+ crate::files::DngWhite::Matrices(_) => ([21, 0], [Some(declaring(t.white_xy, neutral)?), None]),
+ crate::files::DngWhite::At(xy) => ([21, 0], [Some(declaring(*xy, neutral)?), None]),
+ };
+ if !color_matrix.iter().flatten().all(|m| m.0.iter().flatten().all(|v| v.is_finite())) {
+ return None;
+ }
+ Some(ColorData {
+ illuminant,
+ color_matrix,
+ // a second colour matrix comes with a second forward matrix (DNG spec): the same one, fitted at one white
+ forward_matrix: [Some(forward), color_matrix[1].map(|_| forward)],
+ camera_calibration: [None, None],
+ analog_balance: None,
+ as_shot_neutral: Some(neutral),
+ as_shot_white_xy: None,
+ baseline_exposure: t.baseline_exposure + k.log2(),
+ baseline_sharpness: tags.baseline_sharpness,
+ kelvin_presets: None,
+ profile: ProfileLook {
+ hue_sat_map: [look.and_then(|l| l.hue_sat.clone()), None],
+ tone_curve: look.and_then(|l| dng_tone_curve(&l.tone)),
+ ..Default::default()
+ },
+ })
+}
+
+/// An XYZ → camera colour matrix under which camera `neutral` is white `xy`: the generic camera model, scaled per
+/// channel. `None` when the model gives no positive neutral there.
+fn declaring(xy: Xy, neutral: [f64; 3]) -> Option<Mat3> {
+ let g = lightcraft_raw::color::xyz_to_camera(&ColorData::default(), xy);
+ let n = g.apply(xy.to_xyz());
+ if !n.iter().all(|v| v.is_finite() && *v > 1e-9) {
+ return None;
+ }
+ Some(Mat3::diag(neutral[0] / n[0], neutral[1] / n[1], neutral[2] / n[2]).mul(&g))
+}
+
+/// A camera tone curve as a DNG `ProfileToneCurve` (linear in, linear out, 1 = white): sampled densely over the
+/// 12 stops below white, the range the curve is read back over (`files::dng_tone_curve`).
+fn dng_tone_curve(tone: &CameraTone) -> Option<ToneCurve> {
+ const N: usize = 256;
+ let samples = (0..=N).map(|i| {
+ let x = 2f32.powf(-12.0 + 12.0 * i as f32 / N as f32);
+ [x, tone.apply(x).min(1.0)]
+ });
+ let flat: Vec<f64> = std::iter::once([0.0, 0.0]).chain(samples).flatten().map(f64::from).collect();
+ ToneCurve::from_tag(&flat)
+}
+
/// The look fitted to the file's camera JPEG, and whether that JPEG is black and white ([`monochrome`]).
pub(crate) fn fit_preview(raw: &RawImage, bytes: &[u8], transform: &CameraTransform) -> (Option<CameraLook>, bool) {
if !transform.matrix_is_fallback || !file_local_look(raw.format) {
@@@ -379,22 -295,171 +409,180 @@@ fn at_most(a: &CameraTone, b: &CameraTo
})
}
+/// Pixel cap for the embedded JPEG decoded for the fit. The codec checks it before decoding and
+/// decodes at full size, so a 66.5 MP full-frame ILCE-7RM6 JPEG transiently needs ~200 MB (3 B/px);
+/// 128 MP also admits 100 MP medium-format previews.
+const FIT_PREVIEW_MAX_PIXELS: u64 = 128_000_000;
++
+ /// A DNG's tone and chroma curves fitted to its own camera JPEG ([`dng_tone_eligible`]); the file's colour model
+ /// (matrix, profile tables, white balance) stays as it is: the proxy goes through the file's tables and the given
+ /// colour of the search is the identity. Same held-out acceptance gates as [`fit_look`]; the preview must be near
+ /// the raw's size, show the same picture ([`structure_agrees`]), and the curve must not clip more highlights than
+ /// the plain render ([`clips_more`]).
+ fn fit_dng_tone(raw: &RawImage, bytes: &[u8], transform: &CameraTransform, lens: Option<&lightcraft_develop::EmbeddedLens>) -> Option<CameraLook> {
+ // Logged under LIGHTCRAFT_PROFILE so a corpus run can count the outcome per gate.
+ let refuse = |why: &str| {
+ if std::env::var_os("LIGHTCRAFT_PROFILE").is_some() {
+ eprintln!("DNGTONE refused {why}");
+ }
+ None
+ };
+ let Some((mut sensor, reference, clipped)) = proxies(raw, bytes, transform, PROXY, MIN_PREVIEW_FRACTION) else { return refuse("preview") };
+ // As the loader renders it: the profile's tables around the baseline exposure.
+ let xy = lightcraft_raw::color::as_shot_white_xy(raw);
+ if let Some(tables) = lightcraft_raw::profile::ProfileTables::new(&raw.color.profile, lightcraft_raw::color::illuminant_weight(&raw.color, xy)) {
+ let gain = 2f32.powf(transform.baseline_exposure as f32);
+ sensor.map_in_place(|p| tables.apply(p.map(|v| v / gain), gain));
+ }
+ correct_vignetting(&mut sensor, lens, raw.orientation);
+ if (sensor.width, sensor.height) != (reference.width, reference.height) {
+ return refuse("preview");
+ }
+ if !structure_agrees(&sensor, &reference) {
+ return refuse("structure");
+ }
+ let unclipped = without_clipped(&sensor, &clipped);
+ let fit_on = unclipped.as_ref().unwrap_or(&sensor);
+ let colour = Some((Mat3::IDENTITY, None));
+ let look = TONE_FITS.iter().find_map(|&tone| {
+ [false, true]
+ .into_iter()
+ .find_map(|away_from_edges| fit_attempt(fit_on, &reference, &colour, Attempt { tone, profile: true, away_from_edges, dark: true }))
+ });
+ let Some(look) = look else { return refuse("fit") };
+ let plain = match raw.color.profile.tone_curve.as_ref().and_then(crate::files::dng_tone_curve) {
+ Some(curve) => ToneMap::camera(&curve, 0.0, 0.0, 0.0),
+ None => ToneMap::new(0.0, 0.0, 0.0),
+ };
+ let fitted = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);
+ if clips_more(&sensor, &reference, &fitted, &plain) {
+ return refuse("clip");
+ }
+ if std::env::var_os("LIGHTCRAFT_PROFILE").is_some() {
+ eprintln!("DNGTONE accepted");
+ }
+ Some(look)
+ }
+
+ /// Whether the fitted tone map puts clearly more of the photo's proxy pixels at or near white than both the plain
+ /// render and the camera's own preview do ([`CLIP_TOLERANCE`]): a fit that clips what the camera clips too is the
+ /// camera's look, one that clips what neither does loses highlight detail.
+ fn clips_more(sensor: &Rgb32f, reference: &Rgb32f, fitted: &ToneMap, plain: &ToneMap) -> bool {
+ let near_white = |tone: &ToneMap| {
+ let n = sensor.data.iter().filter(|p| p.iter().all(|v| v.is_finite()) && tone.apply(luminance_2020(p.map(|v| v.max(0.0)))) >= 0.98).count();
+ n as f64 / sensor.data.len().max(1) as f64
+ };
+ let preview = reference.data.iter().filter(|p| luminance_2020(**p) >= 0.98).count() as f64 / reference.data.len().max(1) as f64;
+ near_white(fitted) > near_white(plain).max(preview) + CLIP_TOLERANCE
+ }
+
+ /// Multiply the sensor proxy (sensor orientation, default crop) by the embedded vignetting correction, the gain the
+ /// pipeline applies to the render (`optics::embedded_vignette_gain`; `lens` is in the oriented, cropped frame): the
+ /// camera JPEG has it applied, so a tone fitted without it brightens the render's corners twice.
+ fn correct_vignetting(sensor: &mut Rgb32f, lens: Option<&lightcraft_develop::EmbeddedLens>, orientation: lightcraft_geom::Orientation) {
+ let Some(vignette) = lens.and_then(|l| l.vignette) else { return };
+ let (w, h) = (sensor.width as f64, sensor.height as f64);
+ let (ow, oh) = if orientation.swaps_axes() { (h, w) } else { (w, h) };
+ let width = sensor.width.max(1);
+ for (i, p) in sensor.data.iter_mut().enumerate() {
+ let (x, y) = orientation.map((i % width) as f64 + 0.5, (i / width) as f64 + 0.5, w, h);
+ let gain = lightcraft_pipeline::optics::embedded_vignette_gain(&vignette, lightcraft_geom::Point::new(x, y), ow, oh) as f32;
+ *p = p.map(|v| v * gain);
+ }
+ }
+
+ /// The embedded preview's long edge must be at least this share of the raw's (and 384 px): a DNG whose preview is a
+ /// small thumbnail is not fitted.
+ const MIN_PREVIEW_FRACTION: f64 = 0.9;
+
+ /// Lowest preview luminance and channel value a tone-only fit trains on (the other fits use 0.015 and 0.004): a dark
+ /// night scene has most of its pixels below 0.015, and a curve fitted without them overshoots its shadows.
+ const DARK_FLOOR: (f32, f32) = (0.004, 0.001);
+
+ /// Share of the proxy by which the fitted curve may clip more highlights than the plain render and the camera's
+ /// preview (chosen on the CC0 and user DNGs from the clipped share of their 1440 px renders: it refuses the fits
+ /// that clip about 1% more than the camera does and passes the ones that clip like the camera).
+ const CLIP_TOLERANCE: f64 = 0.001;
+
+ /// Log-luminance correlation (5 × 5 box blurred) the unshifted proxies must reach.
+ const MIN_STRUCTURE: f64 = 0.85;
+
+ /// Whether the sensor proxy and the camera JPEG proxy show the same picture in the same place: their log luminance
+ /// correlates by at least [`MIN_STRUCTURE`] unshifted, and no shift of more than one proxy pixel (within ±3)
+ /// correlates better.
+ fn structure_agrees(sensor: &Rgb32f, reference: &Rgb32f) -> bool {
+ let (w, h) = (sensor.width, sensor.height);
+ if (w, h) != (reference.width, reference.height) || w < 16 || h < 16 || sensor.data.len() != w * h || reference.data.len() != w * h {
+ return false;
+ }
+ let log = |img: &Rgb32f| -> Vec<f64> {
+ let l: Vec<f64> =
+ img.data.iter().map(|p| f64::from(luminance_2020(p.map(|v| if v.is_finite() { v.max(0.0) } else { 0.0 }))).max(1e-4).ln()).collect();
+ (0..w * h)
+ .map(|i| {
+ let (x, y) = (i % w, i / w);
+ let (mut sum, mut n) = (0.0, 0.0);
+ for yy in y.saturating_sub(2)..(y + 3).min(h) {
+ for xx in x.saturating_sub(2)..(x + 3).min(w) {
+ if let Some(v) = l.get(yy * w + xx) {
+ sum += v;
+ n += 1.0;
+ }
+ }
+ }
+ sum / f64::max(n, 1.0)
+ })
+ .collect()
+ };
+ let (a, b) = (log(sensor), log(reference));
+ const M: usize = 3;
+ let corr = |dx: isize, dy: isize| -> f64 {
+ let (mut n, mut sa, mut sb, mut saa, mut sbb, mut sab) = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
+ for y in M..h - M {
+ for x in M..w - M {
+ let (xx, yy) = (x.saturating_add_signed(dx), y.saturating_add_signed(dy));
+ let (Some(&u), Some(&v)) = (a.get(y * w + x), b.get(yy * w + xx)) else { continue };
+ n += 1.0;
+ sa += u;
+ sb += v;
+ saa += u * u;
+ sbb += v * v;
+ sab += u * v;
+ }
+ }
+ let var = (saa - sa * sa / n) * (sbb - sb * sb / n);
+ if n < 2.0 || var <= 1e-12 { 0.0 } else { (sab - sa * sb / n) / var.sqrt() }
+ };
+ let at_zero = corr(0, 0);
+ let (mut best, mut shift) = (at_zero, (0isize, 0isize));
+ for dy in -(M as isize)..=M as isize {
+ for dx in -(M as isize)..=M as isize {
+ let c = corr(dx, dy);
+ if c > best + 1e-9 {
+ (best, shift) = (c, (dx, dy));
+ }
+ }
+ }
+ at_zero >= MIN_STRUCTURE && shift.0.abs() <= 1 && shift.1.abs() <= 1
+ }
+
/// Same-size proxies of the sensor (white-balanced, baseline exposure, through `transform`'s
/// matrix: the generic camera ≈ sRGB model) and of the file's embedded camera JPEG (linear Rec.2020),
/// and per proxy pixel whether any sensor sample it covers is clipped.
- fn proxies(raw: &RawImage, bytes: &[u8], transform: &CameraTransform, size: usize) -> Option<(Rgb32f, Rgb32f, Vec<bool>)> {
+ fn proxies(raw: &RawImage, bytes: &[u8], transform: &CameraTransform, size: usize, min_fraction: f64) -> Option<(Rgb32f, Rgb32f, Vec<bool>)> {
let edge = (2 * size).max(384) as u32;
- let decoded = crate::files::decode_raw_preview(bytes, lightcraft_codecs::DecodeOptions { max_size: Some((edge, edge)), max_pixels: 64_000_000 })?;
+ let decoded = crate::files::decode_raw_preview(
+ bytes,
+ lightcraft_codecs::DecodeOptions { max_size: Some((edge, edge)), max_pixels: FIT_PREVIEW_MAX_PIXELS },
+ None,
+ )?;
- let mut reference = decoded.to_working();
let (a, crop) = (raw.active_area, raw.crop.clipped(raw.active_area.width, raw.active_area.height));
+ // a preview below `min_fraction` of the crop's long edge (or under 384 px) is refused; 0 = any
+ if min_fraction > 0.0 && (decoded.source_width.max(decoded.source_height) as f64) < (min_fraction * crop.width.max(crop.height) as f64).max(384.0)
+ {
+ return None;
+ }
+ let mut reference = decoded.to_working();
if crop.width == 0 || crop.height == 0 || reference.width == 0 || reference.height == 0 {
return None;
}
@@@ -691,7 -756,8 +879,8 @@@ fn collect_pairs(sensor: &Rgb32f, refer
if (0.015..=1.0).contains(&y) {
bright.push((input.map(f64::from), output.map(f64::from)));
}
- if !colour_pair(*input, *output) {
- let (ylo, clo) = if dark { DARK_FLOOR } else { (0.015, 0.004) };
- if !output.iter().all(|v| *v > clo && *v < 0.98) || !(ylo..0.85).contains(&y) {
++ let floors = if dark { DARK_FLOOR } else { (0.015, 0.004) };
++ if !colour_pair_above(*input, *output, floors) {
continue;
}
let min = output.iter().copied().fold(f32::INFINITY, f32::min);
@@@ -702,77 -768,6 +891,83 @@@
(pairs.len() >= 256 && colour >= pairs.len() / 20).then_some((pairs, bright))
}
+/// The original colour-pair eligibility, also used to preserve its held-out split in tone fitting.
+fn colour_pair(input: [f32; 3], output: [f32; 3]) -> bool {
++ colour_pair_above(input, output, (0.015, 0.004))
++}
++
++/// [`colour_pair`] with its lowest preview luminance and channel value given (`floors`: the usual 0.015 and 0.004, or
++/// [`DARK_FLOOR`] for the tone-only fit of a DNG).
++fn colour_pair_above(input: [f32; 3], output: [f32; 3], (ylo, clo): (f32, f32)) -> bool {
+ input.iter().all(|v| v.is_finite() && *v > 0.001 && *v < 1.5)
- && output.iter().all(|v| *v > 0.004 && *v < 0.98)
- && (0.015..0.85).contains(&luminance_2020(output))
++ && output.iter().all(|v| *v > clo && *v < 0.98)
++ && (ylo..0.85).contains(&luminance_2020(output))
+}
+
+/// 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
+/// keep JPEG pixels with a channel near black. Quantile medians limit the effect of noise.
+fn fit_tone_with_shadows(
+ fit: ToneFit,
+ pairs: &[([f64; 3], [f64; 3])],
+ sensor: &Rgb32f,
+ reference: &Rgb32f,
+ edge_limit: Option<f32>,
+ 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 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() {
+ if edge_limit.is_some_and(|limit| local_contrast(sensor, i) > limit || local_contrast(reference, i) > limit) {
+ continue;
+ }
+ // About one 8-bit sRGB code above black: don't learn a flat black segment from
+ // JPEG compression. The sensor proxy averages many samples before this test.
+ if !input.iter().all(|v| v.is_finite() && *v > 0.0001 && *v < 1.5)
+ || !output.iter().all(|v| v.is_finite() && *v >= 0.0)
+ || !(0.0003..0.85).contains(&luminance_2020(*output))
+ {
+ continue;
+ }
+ // A colour holdout must never enter tone training just because the wider sample set
+ // changes its index. Split the extra samples separately, keeping every colour index.
+ let index = if colour_pair(*input, *output) { &mut colour_index } else { &mut extra_index };
+ 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 {
+ eligible.push((x, y, held));
+ }
+ }
+ let dark = eligible.iter().filter(|(_, y, _)| *y < 0.015).count();
+ let held: Pairs = eligible.iter().filter(|(_, y, held)| *held && *y < 0.015).map(|(x, y, _)| ([*x; 3], [*y; 3])).collect();
+ // Sparse or uncorrelated shadow noise cannot teach a toe. Tied JPEG levels share a rank,
+ // so a single quantized grey also keeps the original curve.
+ if dark < 96 || held.len() < 32 || rank_correlation_ties(&held) < PARTIAL_MIN_RANK_CORRELATION {
+ return original;
+ }
+ let training = eligible.iter().filter(|(_, _, held)| !held).map(|(x, y, _)| (*x, *y)).collect();
+ let Some(candidate) = fit.fit(training) else { return original };
+ let error = |tone: &CameraTone| -> f64 {
+ held.iter().map(|(x, y)| (f64::from(tone.apply(x[0] as f32)).powf(1.0 / 2.2) - y[0].powf(1.0 / 2.2)).powi(2)).sum()
+ };
+ // A wider histogram must also help independent dark pixels, not just redistribute the
+ // knots. The usual held-out gates still check the completed look on the colour pairs.
+ if let Some(old) = original {
+ let (before, after) = (error(&old), error(&candidate));
+ if lightcraft_pipeline::profiling() {
+ eprintln!("[profile] camera toe: {dark} dark pairs, held-out error {before:.5} -> {after:.5}");
+ }
+ if !after.is_finite() || after >= before * 0.99 {
+ return original;
+ }
+ }
+ Some(candidate)
+}
+
/// Ridge-regularised 3×3 chromaticity matrix (luminance-normalised RGB) on the training pairs.
fn fit_matrix(pairs: &[([f64; 3], [f64; 3])]) -> Option<Mat3> {
let mut gram = [[0.0; 3]; 3];
@@@ -1506,167 -1508,6 +1704,167 @@@ fn fit_tone_conditional(mut pairs: Vec<
mod tests {
use super::*;
+ fn steep_toe_scene() -> (Rgb32f, Rgb32f) {
+ 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() {
+ // A broad scene with a steep camera toe below the old JPEG floor of 0.015.
+ // Permute brightness so the held-out pixels span the same range independently.
+ let y = 0.002 + ((i * 7919) % 6144) as f32 / 6144.0 * 0.25;
+ let rgb = [0.8 + (i % 11) as f32 * 0.04, 1.0, 0.8 + (i % 17) as f32 * 0.025];
+ *src = rgb.map(|v| v * y / luminance_2020(rgb));
+ let display = 3.0 * y * (y / (y + 0.02)).powi(2);
+ *dst = src.map(|v| v * display / y);
+ }
+ (sensor, reference)
+ }
+
+ #[test]
+ fn camera_tone_follows_the_jpegs_deep_shadows() {
+ let (sensor, reference) = steep_toe_scene();
+ let look = fit_pairs_with(&sensor, &reference, Some((Mat3::IDENTITY, None))).unwrap();
+ let dark: Vec<_> = sensor.data.iter().zip(&reference.data).filter(|(_, p)| luminance_2020(**p) < 0.015).collect();
+ assert!(dark.len() > 256);
+ let error = dark.iter().map(|(x, y)| (look.tone.apply(luminance_2020(**x)) - luminance_2020(**y)).abs()).sum::<f32>() / dark.len() as f32;
+ assert!(error < 0.001, "deep-shadow luminance error {error}");
+ assert_eq!(look.matrix, Mat3::IDENTITY);
+ }
+
+ #[test]
+ fn colour_holdouts_stay_out_of_the_wider_tone_training() {
+ let (sensor, reference) = steep_toe_scene();
+ let fit = |reference: &Rgb32f| {
- let (pairs, _) = collect_pairs(&sensor, reference, 0.005, None).unwrap();
++ let (pairs, _) = collect_pairs(&sensor, reference, 0.005, None, false).unwrap();
+ fit_tone_with_shadows(ToneFit::Quantile, &pairs, &sensor, reference, None, |x| x).unwrap()
+ };
+ let expected = fit(&reference);
+ let mut changed = reference.clone();
+ let mut index = 0;
+ for (x, y) in sensor.data.iter().zip(&mut changed.data) {
+ if colour_pair(*x, *y) {
+ if index % 3 == 0 {
+ *y = y.map(|v| v * 1.1);
+ assert!(colour_pair(*x, *y));
+ }
+ index += 1;
+ }
+ }
+ assert_eq!(fit(&changed), expected);
+ }
+
+ #[test]
+ fn camera_toe_is_robust_to_shadow_noise() {
+ let (sensor, clean) = steep_toe_scene();
+ let mut noisy = clean.clone();
+ for (i, p) in noisy.data.iter_mut().enumerate() {
+ if luminance_2020(*p) < 0.015 {
+ // Quantized JPEG levels with bounded noise and occasional compression outliers.
+ let noise = ((i * 17 % 11) as f32 - 5.0) * 0.00004;
+ *p = p.map(|v| ((v + noise).max(0.0) / 0.0003).round() * 0.0003);
+ if i % 23 == 0 {
+ *p = [0.014; 3];
+ }
+ }
+ }
+ let look = fit_pairs_with(&sensor, &noisy, Some((Mat3::IDENTITY, None))).unwrap();
+ let dark: Vec<_> = sensor.data.iter().zip(&clean.data).filter(|(_, p)| luminance_2020(**p) < 0.015).collect();
+ let error = dark.iter().map(|(x, y)| (look.tone.apply(luminance_2020(**x)) - luminance_2020(**y)).abs()).sum::<f32>() / dark.len() as f32;
+ assert!(error < 0.001, "noisy shadow luminance error {error}");
+ assert_eq!(look.tone.apply(0.0), 0.0);
+ assert_eq!(look.matrix, Mat3::IDENTITY);
+ for i in 1..1000 {
+ assert!(look.tone.apply(i as f32 / 1000.0) >= look.tone.apply((i - 1) as f32 / 1000.0));
+ }
+ }
+
+ #[test]
+ fn camera_toe_requires_lower_independent_dark_error() {
+ for holdout_matches_original in [true, false] {
+ 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 y = if i < 600 { 0.0015 + (i % 200) as f32 * 0.00001 } else { 0.02 + ((i * 7919) % 6144) as f32 / 6144.0 * 0.23 };
+ *src = [y; 3];
+ // Midtones teach the original slope of 3. Dark training teaches 0.3,
+ // but the independent dark holdout can agree with either slope.
+ let slope = if i < 600 && !(holdout_matches_original && i % 3 == 0) { 0.3 } else { 3.0 };
+ *dst = [y * slope; 3];
+ assert_eq!(colour_pair(*src, *dst), i >= 600);
+ }
- let (pairs, _) = collect_pairs(&sensor, &reference, f32::NEG_INFINITY, None).unwrap();
++ let (pairs, _) = collect_pairs(&sensor, &reference, f32::NEG_INFINITY, None, false).unwrap();
+ let original = fit_tone(pairs.iter().enumerate().filter(|(i, _)| i % 3 != 0).map(|(_, (x, y))| (luma(*x), luma(*y))).collect()).unwrap();
+ // Both sample sets start at a multiple of three, so this split matches
+ // the helper's separate colour and extra-sample indices.
+ let candidate = fit_tone(
+ sensor
+ .data
+ .iter()
+ .zip(&reference.data)
+ .enumerate()
+ .filter(|(i, _)| i % 3 != 0)
+ .map(|(_, (x, y))| (luma(x.map(f64::from)), luma(y.map(f64::from))))
+ .collect(),
+ )
+ .unwrap();
+ let held: Pairs =
+ sensor.data.iter().zip(&reference.data).take(600).step_by(3).map(|(x, y)| (x.map(f64::from), y.map(f64::from))).collect();
+ assert_eq!(held.len(), 200);
+ assert!(held.iter().all(|(_, y)| (0.0003..0.015).contains(&luma(*y))));
+ assert!(rank_correlation_ties(&held) > 0.99);
+ let error = |tone: &CameraTone| {
+ held.iter().map(|(x, y)| (f64::from(tone.apply(luma(*x) as f32)).powf(1.0 / 2.2) - luma(*y).powf(1.0 / 2.2)).powi(2)).sum::<f64>()
+ };
+ assert_ne!(candidate, original);
+ let selected = fit_tone_with_shadows(ToneFit::Quantile, &pairs, &sensor, &reference, None, |x| x).unwrap();
+ if holdout_matches_original {
+ assert!(error(&candidate) > error(&original) + 0.1);
+ assert_eq!(selected, original, "a correlated but misleading training set must keep the original curve");
+ } else {
+ assert!(error(&candidate) < error(&original) * 0.99);
+ assert_eq!(selected, candidate, "matching dark training and holdout must accept the candidate");
+ }
+ let mut previous = 0.0;
+ assert_eq!(selected.apply(0.0), 0.0);
+ for i in 0..10_000 {
+ let y = selected.apply(10f32.powf(-8.0 + i as f32 / 1000.0));
+ assert!(y.is_finite() && y >= previous && y <= 1.0);
+ previous = y;
+ }
+ assert_eq!(selected.apply(f32::NAN), 0.0);
+ assert_eq!(selected.apply(f32::INFINITY), 0.0);
+ }
+ }
+
+ #[test]
+ fn unreliable_dark_pairs_keep_the_original_tone_and_colour() {
+ let (sensor, reference) = steep_toe_scene();
+ let dark: Vec<_> = reference.data.iter().enumerate().filter(|(_, p)| luminance_2020(**p) < 0.015).map(|(i, _)| i).collect();
+ let mut excluded = sensor.clone();
+ for &i in &dark {
+ excluded.data[i] = [f32::NAN; 3];
+ }
+ let colour = Some((Mat3::IDENTITY, None));
+ let original = fit_pairs_with(&excluded, &reference, colour.clone()).unwrap();
+ for case in 0..6 {
+ let (mut x, mut y) = (sensor.clone(), reference.clone());
+ for (j, &i) in dark.iter().enumerate() {
+ match case {
+ 0 => y.data[i] = [0.0; 3], // JPEG black gives no measured shadow signal
+ 1 => x.data[i] = [0.00001; 3], // below the sensor noise floor
+ 2 if j >= 95 => y.data[i] = [0.0; 3], // too few dark pairs
+ 3 => x.data[i] = [0.003; 3], // no scene luminance spread
+ 4 => y.data[i] = [0.003; 3], // one quantized grey, not a toe
+ 5 => y.data[i] = [0.0004 + ((j * 7919) % 1000) as f32 * 0.000014; 3], // unrelated noise
+ _ => {}
+ }
+ }
+ let look = fit_pairs_with(&x, &y, colour.clone()).unwrap();
+ assert_eq!(look.tone, original.tone, "case {case}");
+ assert_eq!(look.matrix, original.matrix, "case {case}");
+ assert!(look.hue_sat.is_none());
+ }
+ }
+
#[test]
fn camera_jpeg_matches_the_sensor_proxy_size_exactly() {
// An ILCE-7RM4 ARW (9504×6336 crop) embeds a 1616×1080 preview; for the camera-profile proxy
@@@ -2184,12 -2025,10 +2382,12 @@@
let mut raw = lightcraft_raw::decode(&bytes).unwrap();
raw.opcodes.list3.retain(|op| !op.is_lens_correction());
let own = lightcraft_raw::color::has_matrix(&raw.color);
- let covered = raw.metadata.model.as_deref().and_then(|m| lightcraft_raw::spectral::find(raw.metadata.make.as_deref(), m)).is_some();
+ let (make, model) = (raw.metadata.make.as_deref(), raw.metadata.model.as_deref().unwrap_or_default());
+ let measured = lightcraft_raw::spectral::find(make, model).is_some();
+ let covered = measured || lightcraft_raw::kelvin_presets::covers(make, model);
let before = raw.color.clone();
- let (_, t, fitted) = starting_colour(&mut raw, &bytes);
+ let (_, t, fitted) = starting_colour(&mut raw, &bytes, None);
- let spectral = !own && fitted.is_none() && covered;
+ let spectral = !own && fitted.is_none() && measured;
eprintln!("{name}: own matrices {own}, JPEG fit {}, spectral {spectral}", fitted.is_some());
assert_eq!(raw.color != before, spectral, "{name}");
assert_eq!(t.matrix_is_fallback, !own && !spectral, "{name}");
@@@ -3287,19 -3002,144 +3485,160 @@@
assert!(fit_tone_only(&sorted, &reference, None).is_none(), "a flat JPEG");
}
+ /// ILCE-7RM6 full-frame: the 66.5 MP camera JPEG exceeded the old 64 MP cap, so no look was fitted.
+ #[test]
+ fn corpus_arw6_full_frame_gets_a_camera_look() {
+ const { assert!(FIT_PREVIEW_MAX_PIXELS >= 100_000_000) };
+ let path = std::env::var_os("LIGHTCRAFT_CORPUS")
+ .map(std::path::PathBuf::from)
+ .unwrap_or_else(|| std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus"))
+ .join("raw/arw6-sony-ilce7rm6-ff-land-c.arw");
+ let Ok(bytes) = std::fs::read(&path) else {
+ eprintln!("skip: {} absent", path.display());
+ return;
+ };
+ let (_, info) = crate::files::load_bytes(&bytes, 400).unwrap();
+ assert!(info.camera_tone.is_some(), "no camera look fitted");
+ }
++
+ /// A smooth synthetic scene (sensor proxy) and its camera rendering, `dx` pixels to the right.
+ fn scene_pair(w: usize, h: usize, dx: usize) -> (Rgb32f, Rgb32f) {
+ let scene = |x: f32, y: f32| -> [f32; 3] {
+ let l = 0.03 + 0.4 * (0.5 + 0.5 * (x * 0.21).sin() * (y * 0.17).cos()) + 0.2 * ((x - 30.0).hypot(y - 20.0) < 12.0) as u8 as f32;
+ [l * (0.8 + 0.3 * (y / h as f32)), l, l * (1.1 - 0.4 * (x / w as f32))]
+ };
+ let camera = |p: [f32; 3]| {
+ let y = luminance_2020(p);
+ p.map(|v| v * (1.0 - (-2.5 * y).exp()) / y)
+ };
+ let mut sensor = Rgb32f::new(w, h);
+ let mut reference = sensor.clone();
+ for y in 0..h {
+ for x in 0..w {
+ sensor.data[y * w + x] = scene(x as f32, y as f32);
+ reference.data[y * w + x] = camera(scene(x as f32 - dx as f32, y as f32));
+ }
+ }
+ (sensor, reference)
+ }
+
+ /// The structural gate accepts the same picture whatever its tone, and refuses a shifted, a broken (stripes), a
+ /// flat and a differently sized preview.
+ #[test]
+ fn dng_structure_gate_refuses_shifted_broken_and_unrelated_previews() {
+ let (sensor, reference) = scene_pair(96, 64, 0);
+ assert!(structure_agrees(&sensor, &reference));
+ assert!(structure_agrees(&sensor, &scene_pair(96, 64, 1).1), "one proxy pixel (lens correction) is tolerated");
+ assert!(!structure_agrees(&sensor, &scene_pair(96, 64, 3).1), "differently framed");
+ let mut stripes = reference.clone();
+ for (i, p) in stripes.data.iter_mut().enumerate() {
+ *p = if (i / 96) % 4 < 2 { [0.6, 0.1, 0.5] } else { [0.05, 0.4, 0.1] };
+ }
+ assert!(!structure_agrees(&sensor, &stripes), "a broken preview");
+ let mut flat = reference.clone();
+ flat.data.fill([0.2; 3]);
+ assert!(!structure_agrees(&sensor, &flat));
+ assert!(!structure_agrees(&sensor, &scene_pair(95, 64, 0).1));
+ }
+
+ /// With the file's colour given as the identity, only the tone is fitted: the colour stays and a camera that
+ /// renders mid-grey brighter than the default tone map is followed.
+ #[test]
+ fn dng_tone_fit_keeps_the_files_colour_and_follows_the_camera_tone() {
+ let (mut sensor, mut reference) = scene_pair(96, 64, 0);
+ for image in [&mut sensor, &mut reference] {
+ for p in image.data.iter_mut().step_by(5) {
+ *p = [p[0] * 1.6, p[1], p[2] * 0.5];
+ }
+ }
+ let colour = Some((Mat3::IDENTITY, None));
+ let look = fit_attempt(&sensor, &reference, &colour, Attempt { tone: ToneFit::Quantile, profile: true, away_from_edges: false, dark: true })
+ .expect("tone fitted");
+ assert_eq!(look.matrix, Mat3::IDENTITY);
+ assert!(look.hue_sat.is_none());
+ let (fitted, default) = (ToneMap::camera(&look.tone, 0.0, 0.0, 0.0), ToneMap::new(0.0, 0.0, 0.0));
+ let target = 1.0 - (-2.5f32 * 0.18).exp();
+ assert!((fitted.apply(0.18) - target).abs() < 0.03, "{} vs {target}", fitted.apply(0.18));
+ assert!(fitted.apply(0.18) > default.apply(0.18) + 0.05, "{} vs default {}", fitted.apply(0.18), default.apply(0.18));
+ assert!(!clips_more(&sensor, &reference, &fitted, &fitted), "a tone never clips more than itself");
+ }
+
+ /// The highlight gate compares the share of near-white pixels of the fitted tone with the plain render's and the
+ /// camera preview's, in one direction only: clipping what the camera clips is not refused.
+ #[test]
+ fn dng_highlight_gate_compares_near_white_shares() {
+ let mut sensor = Rgb32f::new(32, 32);
+ for (i, p) in sensor.data.iter_mut().enumerate() {
+ *p = [0.02 + 1.5 * i as f32 / 1024.0; 3];
+ }
+ let (bright, dark) = (ToneMap::new(0.0, 1.0, 0.0), ToneMap::new(0.0, -1.0, 0.0));
+ let mut white = Rgb32f::new(32, 32);
+ white.data.iter_mut().for_each(|p| *p = [1.0; 3]);
+ let mut black = Rgb32f::new(32, 32);
+ black.data.iter_mut().for_each(|p| *p = [0.1; 3]);
+ assert!(clips_more(&sensor, &black, &bright, &dark), "clips more than the plain render and a preview without white");
+ assert!(!clips_more(&sensor, &black, &dark, &bright));
+ assert!(!clips_more(&sensor, &black, &bright, &bright));
+ assert!(!clips_more(&sensor, &white, &bright, &dark), "the camera preview clips as much");
+ }
+
+ /// A dark scene's shadows below the usual training floor still reach the tone-only fit.
+ #[test]
+ fn dng_tone_fit_trains_on_dark_pixels() {
+ let mut sensor = Rgb32f::new(64, 64);
+ let mut reference = Rgb32f::new(64, 64);
+ for (i, (s, r)) in sensor.data.iter_mut().zip(reference.data.iter_mut()).enumerate() {
+ let v = 0.0005 + 0.004 * (i % 64) as f32 / 64.0 + if i % 7 == 0 { 0.2 } else { 0.0 };
+ *s = [v, v * 1.1, v * 0.9];
+ *r = [(v * 3.0).min(0.9), (v * 3.3).min(0.9), (v * 2.7).min(0.9)];
+ }
+ let usual = collect_pairs(&sensor, &reference, 0.0, None, false).map_or(0, |p| p.0.len());
+ let dark = collect_pairs(&sensor, &reference, 0.0, None, true).map_or(0, |p| p.0.len());
+ assert!(dark > usual, "{dark} vs {usual}");
+ }
+
+ /// The embedded vignetting correction reaches the sensor proxy as the pipeline applies it to the render.
+ #[test]
+ fn dng_sensor_proxy_gets_the_embedded_vignetting_correction() {
+ use lightcraft_develop::{EmbeddedLens, EmbeddedVignette};
+ let lens = EmbeddedLens {
+ vignette: Some(EmbeddedVignette { k: [1.0, 0.0, 0.0, 0.0, 0.0], center: lightcraft_geom::Point::new(0.5, 0.5), radius: 0.6 }),
+ ..Default::default()
+ };
+ let mut proxy = Rgb32f::new(96, 64);
+ proxy.data.fill([0.2; 3]);
+ correct_vignetting(&mut proxy, Some(&lens), lightcraft_geom::Orientation::Normal);
+ let (centre, corner) = (proxy.data[32 * 96 + 48][1] / 0.2, proxy.data[0][1] / 0.2);
+ assert!((centre - 1.0).abs() < 0.01 && (1.9..2.0).contains(&corner), "centre {centre}, corner {corner}");
+ let mut plain = Rgb32f::new(96, 64);
+ plain.data.fill([0.2; 3]);
+ correct_vignetting(&mut plain, None, lightcraft_geom::Orientation::Normal);
+ assert!(plain.data.iter().all(|p| *p == [0.2; 3]));
+ }
+
+ /// Which DNGs get a tone fitted to their camera JPEG: a colour matrix, a tone curve that is absent or the
+ /// identity, no look table; and no other raw format.
+ #[test]
+ fn dng_tone_gates_follow_the_files_own_tags() {
+ use lightcraft_raw::profile::{HsvTable, ToneCurve};
+ let own = Mat3([[0.6, -0.1, -0.05], [-0.4, 1.3, 0.1], [-0.1, 0.2, 0.7]]);
+ let color = lightcraft_raw::ColorData { illuminant: [21, 0], color_matrix: [Some(own), None], ..Default::default() };
+ let eligible = |format: RawFormat, color: lightcraft_raw::ColorData| {
+ let raw = raw_of(format, "Make", "Model", color);
+ let t = lightcraft_raw::color::camera_transform(&raw, lightcraft_raw::color::as_shot_white_xy(&raw));
+ dng_tone_eligible(&raw, &t)
+ };
+ assert!(eligible(RawFormat::Dng, color.clone()));
+ assert!(!eligible(RawFormat::Cr2, color.clone()), "other formats keep their own path");
+ assert!(!eligible(RawFormat::Dng, Default::default()), "no matrix, no colour to keep");
+ let mut identity = color.clone();
+ identity.profile.tone_curve = ToneCurve::from_tag(&[0.0, 0.0, 0.5, 0.5, 1.0, 1.0]);
+ assert!(identity.profile.tone_curve.is_some() && eligible(RawFormat::Dng, identity), "an identity ProfileToneCurve is no tone");
+ let mut curved = color.clone();
+ curved.profile.tone_curve = ToneCurve::from_tag(&[0.0, 0.0, 0.18, 0.4, 1.0, 1.0]);
+ assert!(curved.profile.tone_curve.is_some() && !eligible(RawFormat::Dng, curved), "the file's own tone curve is kept");
+ let mut looked = color;
+ looked.profile.look_table = HsvTable::from_tags(&[1, 2, 1], &[0.0, 1.0, 1.0, 0.0, 1.0, 1.0], 0);
+ assert!(looked.profile.look_table.is_some() && !eligible(RawFormat::Dng, looked), "a look table is the file's own rendering");
+ }
}
diff --cc crates/engine/src/media.rs
index 50983c86c,58b9712ac..6d1bfe0de
--- a/crates/engine/src/media.rs
+++ b/crates/engine/src/media.rs
@@@ -62,7 -62,7 +62,7 @@@ impl SettingsHashes
}
/// Bump when the pipeline's output changes, to invalidate cached thumbnails.
- pub const RENDER_CACHE_VERSION: u64 = 63;
-pub const RENDER_CACHE_VERSION: u64 = 62;
++pub const RENDER_CACHE_VERSION: u64 = 64;
/// Thumbnails render at one of these long edges (so window/cell size changes reuse the cache).
pub const THUMB_SIZES: [usize; 4] = [128, 256, 384, 512];
diff --cc crates/engine/src/tests_xmp.rs
index 1380e95fb,c768078ce..6921a6f8c
--- a/crates/engine/src/tests_xmp.rs
+++ b/crates/engine/src/tests_xmp.rs
@@@ -948,57 -863,6 +948,57 @@@ fn dng_export_compression_choices()
sizes.push((c, out.len()));
}
assert!(sizes[2].1 > sizes[0].1 && sizes[2].1 > sizes[1].1, "uncompressed is the biggest: {sizes:?}");
+ assert!(sizes[3].1 < sizes[0].1 / 2, "lossy is much smaller than lossless: {sizes:?}");
+}
+
+/// Issue #1745: the DNG of a raw without colour matrices of its own (a Nikon Z 9 NEF) had no `ColorMatrix1`, and
+/// other raw converters refused it. Export as DNG and Convert / Copy as DNG now write the colour the raw opens with:
+/// the measured matrices of a camera that has them (Z f), the neutral fallback of one that doesn't (Z 9, whose
+/// look is fitted to its JPEG when there is one).
+#[test]
+fn dngs_of_raws_without_colour_matrices_carry_the_colour_they_open_with() {
+ use lightcraft_raw::color::{as_shot_white_xy, camera_transform, has_matrix};
+ let dir = temp_dir("dng-colour");
+ let samples = [[0.2, 0.3, 0.1], [0.05, 0.02, 0.4], [0.6, 0.6, 0.6], [0.01, 0.5, 0.03]];
+ let render = |t: &lightcraft_raw::color::CameraTransform, c: [f64; 3]| {
+ t.matrix.apply(std::array::from_fn(|i| c[i] * t.wb[i] as f64)).map(|v| v * t.baseline_exposure.exp2())
+ };
+ for (model, measured) in [("NIKON Z f", true), ("NIKON Z 9", false)] {
+ let metadata = lightcraft_meta::Metadata { make: Some("NIKON CORPORATION".into()), model: Some(model.into()), ..Default::default() };
+ let mut raw = lightcraft_raw::decode(&synthetic_dng_with(None, metadata)).unwrap();
+ // a vendor raw's colour: a white balance, no matrices
+ raw.color = lightcraft_raw::ColorData { as_shot_neutral: Some([0.5, 1.0, 0.7]), ..Default::default() };
+ let bytes = lightcraft_raw::write_dng(&raw, &Default::default()).unwrap();
+ let path = dir.join(format!("{model}.dng"));
+ std::fs::write(&path, &bytes).unwrap();
+ let mut opened = lightcraft_raw::decode(&bytes).unwrap();
+ assert!(!has_matrix(&opened.color));
- let (_, want, look) = crate::camera_preview::starting_colour(&mut opened, &bytes);
++ let (_, want, look) = crate::camera_preview::starting_colour(&mut opened, &bytes, None);
+ assert_eq!((want.matrix_is_fallback, look.is_none()), (!measured, true), "{model}");
+
+ let mut s = Session::new().with_fs();
+ s.execute("library.import", &json!({"paths": [path.to_string_lossy()]})).unwrap();
+ let id = s.active().unwrap();
+ let o = crate::export::ExportOptions::from_json(&json!({"format": "dng"}));
+ let mut exported = Vec::new();
+ let mut write = |_: &str, b: &[u8]| {
+ exported = b.to_vec();
+ Ok(())
+ };
+ crate::export::export_batch(&mut s, &[id], &o, &crate::export::Destination { dir: "x".into(), exact: None }, &mut write, &|_| false).unwrap();
+ let converted = crate::cmd::convert::write_dng_with(None, &path.to_string_lossy(), String::new()).unwrap();
+ for (how, dng) in [("export", exported), ("convert", std::fs::read(&converted).unwrap())] {
+ let back = lightcraft_raw::decode(&dng).unwrap();
+ assert!(back.color.color_matrix[0].is_some() && back.color.illuminant[0] != 0, "{model} {how}: {:?}", back.color);
+ let got = camera_transform(&back, as_shot_white_xy(&back));
+ for c in samples {
+ let (a, b) = (render(&want, c), render(&got, c));
+ assert!(a.iter().zip(b).all(|(a, b)| (a - b).abs() < 1e-3), "{model} {how} {c:?}: {a:?} vs {b:?}");
+ }
+ }
+ std::fs::remove_file(&converted).unwrap();
+ }
+ let _ = std::fs::remove_dir_all(&dir);
}
fn write_jpeg(path: &Path, capture: Option<&str>) {