Skip to content

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

Merge PR #2006: LR-EDIT-COLOR-TEMP: Kelvin white balance for the Nikon Z5II from built-in presets

aiongg91744336d010
diff --cc crates/raw/src/kelvin_presets.rs
index 2d78151d5,f9465a94c..4db0a521e
--- a/crates/raw/src/kelvin_presets.rs
+++ b/crates/raw/src/kelvin_presets.rs
@@@ -40,51 -35,35 +40,54 @@@ const MIN_K: f64 = 2000.0
const MAX_K: f64 = 50000.0;
const MAX_TINT: f64 = 150.0;
-/// Models whose white balance is edited in Kelvin through their presets, checked to carry them (on 80–150 own
-/// photos each, or the public corpus samples for the ILCE-3500 and ILCE-7S): the Exif model as Sony writes it. A
-/// camera with measured matrices (`crate::spectral`) is read through those instead.
-const MODELS: &[&str] = &["DSC-RX10", "ILCA-77M2", "ILCA-99M2", "ILCE-3500", "ILCE-7S", "SLT-A33"];
+/// Models whose white balance is edited in Kelvin through their presets, as (brand, Exif model without the brand),
+/// each checked to carry them: the Sony bodies on 80–150 own photos each, or the public corpus samples for the
+/// ILCE-3500 and ILCE-7S; the Canon PowerShot G1 X Mark III on a reporter's nine photos, one per white-balance
+/// setting (issue #157 of a downstream fork). A camera with measured matrices (`crate::spectral`) is read through
+/// those instead.
+const MODELS: &[(&str, &str)] = &[
+ ("SONY", "DSC-RX10"),
+ ("SONY", "ILCA-77M2"),
+ ("SONY", "ILCA-99M2"),
+ ("SONY", "ILCE-3500"),
+ ("SONY", "ILCE-7S"),
+ ("SONY", "SLT-A33"),
+ ("CANON", "PowerShot G1 X Mark III"),
+];
/// Whether a raw of this Exif make and model is edited in Kelvin through its presets (when it has no colour
- /// matrices of its own or measured: see [`crate::spectral::find`], which comes first).
+ /// matrices of its own or measured: see [`crate::spectral::find`], which comes first), from the file's presets or
+ /// built-in ones ([`crate::kelvin_builtin`]).
pub fn covers(make: Option<&str>, model: &str) -> bool {
+ if crate::kelvin_builtin::covers(make, model) {
+ return true;
+ }
let model = model.trim();
- let model = model.strip_prefix("SONY ").or_else(|| model.strip_prefix("Sony ")).unwrap_or(model);
- make.is_none_or(|m| m.split_whitespace().next().is_some_and(|b| b.eq_ignore_ascii_case("SONY")))
- && MODELS.iter().any(|m| m.eq_ignore_ascii_case(model))
+ let brand = make.and_then(|m| m.split_whitespace().next());
+ MODELS.iter().any(|(b, m)| {
+ let bare = model
+ .get(..b.len())
+ .zip(model.get(b.len()..))
+ .filter(|(p, rest)| p.eq_ignore_ascii_case(b) && rest.starts_with(' '))
+ .map_or(model, |(_, rest)| rest.trim_start());
+ brand.is_none_or(|x| x.eq_ignore_ascii_case(b)) && bare.eq_ignore_ascii_case(m)
+ })
}
/// [`covers`] for make and model joined by a space, as the catalog keeps them (`"SONY ILCA-99M2"`).
pub fn covers_camera(camera: &str) -> bool {
- match camera.split_once(char::is_whitespace) {
- Some((make, model)) => covers(Some(make), model),
- None => covers(None, camera),
- }
+ let words: Vec<&str> = camera.split_whitespace().collect();
+ // the make can be several words ("NIKON CORPORATION"), so try every split, as `spectral::find_camera` does
+ (1..words.len()).any(|i| covers(Some(&words[..i].join(" ")), &words[i..].join(" "))) || covers(None, camera)
}
-/// A camera's multipliers at five stated temperatures, ascending, green = 1.
-#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
+/// Most points a curve keeps (Canon writes about seven at a stated temperature, Sony five).
+const MAX_POINTS: usize = 32;
+
+/// A camera's multipliers at stated temperatures (at least three), ascending, green = 1.
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct KelvinPresets {
- points: [(f32, [f32; 3]); 5],
+ points: Vec<(f32, [f32; 3])>,
}
fn usable(m: [f32; 3]) -> bool {
@@@ -264,51 -217,13 +267,52 @@@ mod tests
assert!(as_shot_white(&with, Some([2.0, 1.0, 1.5])).is_some());
}
+ /// Canon lists its presets by name, not by temperature, with repeats and an off-curve one among them: the
+ /// curve takes them in Kelvin order, each temperature once, and leaves out a point that would turn it back.
+ #[test]
+ fn a_curve_from_presets_in_any_order() {
+ let g = |r: f32, b: f32| [r / 1024.0, 1.0, b / 1024.0];
+ let p = KelvinPresets::from_unordered([
+ (5651.0, g(1418.0, 1357.0)),
+ (7505.0, g(1653.0, 1171.0)),
+ (6594.0, g(1533.0, 1259.0)),
+ (3174.0, g(963.0, 2016.0)),
+ (5651.0, g(1418.0, 1357.0)),
+ (8000.0, g(1400.0, 1400.0)),
+ (8529.0, g(1807.0, 1080.0)),
+ (f32::NAN, g(1000.0, 1000.0)),
+ ])
+ .unwrap();
+ assert_eq!(p.points.len(), 5);
+ assert!(p.points.windows(2).all(|w| w[0].0 < w[1].0));
+ let (t, tint) = p.temp_tint(g(1653.0, 1171.0)).unwrap();
+ assert!((t - 7505.0).abs() < 1.0 && tint.abs() < 0.1, "{t} {tint}");
+ assert_eq!(KelvinPresets::from_unordered([(5000.0, g(1400.0, 1400.0)), (6000.0, g(1500.0, 1300.0))]), None);
+ assert_eq!(KelvinPresets::from_unordered(std::iter::empty()), None);
+ // a stored curve with a repeated point (only deserializing can make one) reads without dividing by zero
+ let bad = KelvinPresets { points: vec![(5000.0, g(1400.0, 1400.0)); 3] };
+ assert_eq!(bad.temp_tint(g(1400.0, 1400.0)), None);
+ assert_eq!(KelvinPresets { points: vec![] }.temp_tint(g(1400.0, 1400.0)), None);
+ }
+
#[test]
fn covered_models() {
+ assert!(covers_camera("NIKON CORPORATION NIKON Z5_2") && covers_camera("NIKON Z5_2") && !covers_camera("NIKON CORPORATION NIKON Z6"));
- for c in ["SONY ILCA-99M2", "SONY DSC-RX10", "SONY ILCA-77M2", "SONY SLT-A33", "ILCA-99M2", "Sony ilca-99m2"] {
+ for c in [
+ "SONY ILCA-99M2",
+ "SONY DSC-RX10",
+ "SONY ILCA-77M2",
+ "SONY SLT-A33",
+ "ILCA-99M2",
+ "Sony ilca-99m2",
+ "Canon Canon PowerShot G1 X Mark III",
+ "Canon PowerShot G1 X Mark III",
+ ] {
assert!(covers_camera(c), "{c}");
}
- for c in ["SONY ILCE-7M3", "SONY DSC-RX10M4", "Canon SLT-A33", "", "SONY"] {
+ for c in
+ ["SONY ILCE-7M3", "SONY DSC-RX10M4", "Canon SLT-A33", "", "SONY", "Canon Canon PowerShot G1 X Mark II", "SONY PowerShot G1 X Mark III"]
+ {
assert!(!covers_camera(c), "{c}");
}
assert!(covers(Some("SONY"), "ILCA-99M2") && covers(None, "ILCA-99M2") && !covers(Some("NIKON CORPORATION"), "ILCA-99M2"));
diff --cc docs/parity-checklist.md
index 3ed05a34f,8fd05399c..55ed1723e
--- a/docs/parity-checklist.md
+++ b/docs/parity-checklist.md
@@@ -193,7 -193,7 +193,7 @@@ differences and missing bindings are i
| LR-EDIT-LIGHT-CURVE-TAT | Drag-on-image curve adjust | P1 | ✅ | `cmd:develop.targeted` (`target: curve`) | |
| LR-EDIT-COLOR-WB-PRESET | White-balance presets | P0 | ✅ | `cmd:develop.wb` | |
| LR-EDIT-COLOR-WB-PICKER | White-balance eyedropper | P0 | ✅ | `cmd:tool.wbPicker`, `cmd:develop.wbPick`, `crates/ui-egui/src/tests_wb_selector.rs` | the Edit panel's selector button does what W does (from Compare, Survey or the grid it opens the photo in Detail); no magnified loupe while picking |
- | LR-EDIT-COLOR-TEMP | Temperature | P0 | ✅ | `ctl:wb.temp`, `crates/pipeline/src/local.rs` (`wb_matrix_for`, `legacy_white`), `crates/raw/src/color.rs` (`rebalance`), `crates/raw/src/spectral.rs` (`find_camera`), `crates/catalog/src/model.rs` (`relative_wb_camera`), `crates/develop/src/controls.rs` (`ControlSpec::position`), `crates/raw/src/kelvin_presets.rs` | Kelvin (2000–50000 K, As Shot read from the file) on raws with colour matrices: in the file (DNG: re-developed for the chosen white through the camera's own matrices, camera-space white balance as Lightroom; the eyedropper / Auto invert the same model) or measured for the camera model (`lightcraft_raw::spectral`, 52 bodies, issue #730: the matrices turn the as-shot multipliers into Kelvin and the slider back, the look stays the file's fit, and the picture is adapted from the as-shot white, Bradford), or read from the camera's own white-balance presets (Sony ARW `SR2SubIFD`, Sony ILCA-99M2, ILCA-77M2, SLT-A33, DSC-RX10, ILCE-3500, ILCE-7S: Sony's Kelvin dial reads back within 0.3 % on 5 settings of 4 bodies; Canon CR2 `ColorData` presets, PowerShot G1 X Mark III: the camera's own As Shot temperature reads back within 5 % on its 9 white-balance settings). The Kelvin slider's track is Lightroom's: 7000 K in the middle, 2000–7000 K evenly on the left half, logarithmic up to 50000 K on the right (within 2 % of positions measured in Lightroom Classic), for drawing, clicks and drags; wheel / arrow-key nudges and Quick Develop still step a fixed 50 / 100 / 500 K; the relative slider stays linear. Settings written on the relative scale before a camera's matrices reached it keep their look (`WbScale`). Not verified against Lightroom's Kelvin: on the ILCE-7M3 the matrices read 7–10 % below Sony's dial (issue #730), and whether the matrices or the dial are off is open; raws without matrices or presets anywhere and rendered files use the relative −100..+100 scale (6500 K / 0 = as shot). |
-| LR-EDIT-COLOR-TEMP | Temperature | P0 | ✅ | `ctl:wb.temp`, `crates/pipeline/src/local.rs` (`wb_matrix_for`, `legacy_white`), `crates/raw/src/color.rs` (`rebalance`), `crates/raw/src/spectral.rs` (`find_camera`), `crates/catalog/src/model.rs` (`relative_wb_camera`), `crates/develop/src/controls.rs` (`ControlSpec::position`), `crates/raw/src/kelvin_presets.rs` | Kelvin (2000–50000 K, As Shot read from the file) on raws with colour matrices: in the file (DNG: re-developed for the chosen white through the camera's own matrices, camera-space white balance as Lightroom; the eyedropper / Auto invert the same model) or measured for the camera model (`lightcraft_raw::spectral`, 52 bodies, issue #730: the matrices turn the as-shot multipliers into Kelvin and the slider back, the look stays the file's fit, and the picture is adapted from the as-shot white, Bradford), or read from the camera's own white-balance presets (Sony ARW `SR2SubIFD`, Sony ILCA-99M2, ILCA-77M2, SLT-A33, DSC-RX10, ILCE-3500, ILCE-7S: Sony's Kelvin dial reads back within 0.3 % on 5 settings of 4 bodies; or built-in presets from the camera's own stated temperatures, `crates/raw/src/kelvin_builtin.rs`: Nikon Z5II, whose Natural Auto shot reads back at 5036 K against the 5030 K the camera wrote). The Kelvin slider's track is Lightroom's: 7000 K in the middle, 2000–7000 K evenly on the left half, logarithmic up to 50000 K on the right (within 2 % of positions measured in Lightroom Classic), for drawing, clicks and drags; wheel / arrow-key nudges and Quick Develop still step a fixed 50 / 100 / 500 K; the relative slider stays linear. Settings written on the relative scale before a camera's matrices reached it keep their look (`WbScale`). Not verified against Lightroom's Kelvin: on the ILCE-7M3 the matrices read 7–10 % below Sony's dial (issue #730), and whether the matrices or the dial are off is open; raws without matrices or presets anywhere and rendered files use the relative −100..+100 scale (6500 K / 0 = as shot). |
++| LR-EDIT-COLOR-TEMP | Temperature | P0 | ✅ | `ctl:wb.temp`, `crates/pipeline/src/local.rs` (`wb_matrix_for`, `legacy_white`), `crates/raw/src/color.rs` (`rebalance`), `crates/raw/src/spectral.rs` (`find_camera`), `crates/catalog/src/model.rs` (`relative_wb_camera`), `crates/develop/src/controls.rs` (`ControlSpec::position`), `crates/raw/src/kelvin_presets.rs` | Kelvin (2000–50000 K, As Shot read from the file) on raws with colour matrices: in the file (DNG: re-developed for the chosen white through the camera's own matrices, camera-space white balance as Lightroom; the eyedropper / Auto invert the same model) or measured for the camera model (`lightcraft_raw::spectral`, 52 bodies, issue #730: the matrices turn the as-shot multipliers into Kelvin and the slider back, the look stays the file's fit, and the picture is adapted from the as-shot white, Bradford), or read from the camera's own white-balance presets (Sony ARW `SR2SubIFD`, Sony ILCA-99M2, ILCA-77M2, SLT-A33, DSC-RX10, ILCE-3500, ILCE-7S: Sony's Kelvin dial reads back within 0.3 % on 5 settings of 4 bodies; Canon CR2 `ColorData` presets, PowerShot G1 X Mark III: the camera's own As Shot temperature reads back within 5 % on its 9 white-balance settings; or built-in presets from the camera's own stated temperatures, `crates/raw/src/kelvin_builtin.rs`: Nikon Z5II, whose Natural Auto shot reads back at 5036 K against the 5030 K the camera wrote). The Kelvin slider's track is Lightroom's: 7000 K in the middle, 2000–7000 K evenly on the left half, logarithmic up to 50000 K on the right (within 2 % of positions measured in Lightroom Classic), for drawing, clicks and drags; wheel / arrow-key nudges and Quick Develop still step a fixed 50 / 100 / 500 K; the relative slider stays linear. Settings written on the relative scale before a camera's matrices reached it keep their look (`WbScale`). Not verified against Lightroom's Kelvin: on the ILCE-7M3 the matrices read 7–10 % below Sony's dial (issue #730), and whether the matrices or the dial are off is open; raws without matrices or presets anywhere and rendered files use the relative −100..+100 scale (6500 K / 0 = as shot). |
| LR-EDIT-COLOR-TINT | Tint | P0 | ✅ | `ctl:wb.tint`, `docs/tint-direction.md` | DNG / Lightroom sign: negative (left) adds green, positive (right) magenta; XMP `crs:Tint` values render as in Lightroom (issue #188); CPU/GPU regression tests |
| LR-EDIT-COLOR-VIBRANCE | Vibrance | P0 | ✅ | `ctl:color.vibrance` | |
| LR-EDIT-COLOR-SATURATION | Saturation | P0 | ✅ | `ctl:color.saturation` | |