PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Raw: the Nikon D4 white level follows the sample depth (#5978)
Brian Torresdb01269fea08
diff --git a/crates/raw/README.md b/crates/raw/README.mdindex bb4815a4f..e2c180fee 100644--- a/crates/raw/README.md+++ b/crates/raw/README.md@@ -149,7 +149,10 @@ Raw rendering (quarter size, medians over the frames) the measured profile is 0.luminance with a mean CIE76 ΔE of 1.8; the look fitted to the embedded JPEG would be 0.30 stop andΔE 6.4, as it follows the camera's picture style instead. The open-time re-develop(`photocraft-io`'s `import_raw_tuned`) and the tone curve its previews use (`tone_curve`) take the-same look as the default open, so changing a slider does not change the colour rendering.+same look as the default open, so changing a slider does not change the colour rendering. The D4's+saturation level was measured on 14-bit files and follows the sample depth (a 12-bit frame is the+14-bit one shifted right by two, so its level is a quarter of it): the three public 12-bit D4 files+develop within 0.02 stop of their 14-bit twin.## Toolsdiff --git a/crates/raw/src/cameras.rs b/crates/raw/src/cameras.rsindex 84204c6bc..d7ae5e84d 100644--- a/crates/raw/src/cameras.rs+++ b/crates/raw/src/cameras.rs@@ -11,7 +11,9 @@//! (2 px of optical black on the left, 50 + 8 on the right show as a purple stripe otherwise). The//! data is already black-subtracted (the darkest image pixels reach 0). Green saturates at//! 15546–15672 while red and blue reach 15687–16383, so one level of 15520 makes clipped-//! highlights white without shifting the white balance. Matrix, curve and black point copy+//! highlights white without shifting the white balance. That level is for 14-bit files: the body+//! also writes 12-bit ones, whose samples are the 14-bit values shifted right by two, so the+//! level follows the sample depth (`Profile::white_at`). Matrix, curve and black point copy//! Photoshop Camera Raw's default rendering of these files, measured black-box: 29 NEFs opened//! in Photoshop 25.4 against our own linear development of the same data (8 × 8 means, pixels//! clipped in neither). The matrix is our first ForwardMatrix (fitted to the camera's embedded@@ -92,7 +94,10 @@ struct Profile {size: (usize, usize),/// The image area: x, y, width, height.crop: (usize, usize, usize, usize),+ /// The saturation level, in codes of `white_bits`-bit samples.white: f32,+ /// The sample depth `white` was measured at.+ white_bits: u32,/// The black level is known to be 0 (the data is black-subtracted in camera).black_zero: bool,forward: Mat3,@@ -102,12 +107,25 @@ struct Profile {black_point: f32,}+impl Profile {+ /// The saturation level for `bits`-bit samples. A body that writes several depths stores the+ /// lower ones as the deepest value shifted right (a 12-bit D4 sample is the 14-bit sample+ /// divided by four), so the level scales by the same power of two. At the depth the level was+ /// measured at it is returned unchanged.+ fn white_at(&self, bits: u32) -> f32 {+ let bits = bits.clamp(1, 16);+ let measured = self.white_bits.clamp(1, 16);+ if bits >= measured { self.white * f32::from(1u16 << (bits - measured)) } else { self.white / f32::from(1u16 << (measured - bits)) }+ }+}+const PROFILES: &[Profile] = &[Profile {make: "NIKON CORPORATION",model: "NIKON D4",size: (4992, 3292),crop: (8, 6, 4928, 3280),white: 15520.0,+ white_bits: 14,black_zero: true,forward: D4_FORWARD,color: D4_COLOR,@@ -148,8 +166,9 @@ const IMAGE_AREAS: &[ImageArea] = &[];/// Applies the measured profile of the camera that made `s`, when there is one,-/// and its measured image area when the profile is not measured yet.-pub(crate) fn apply(s: &mut Sensor) {+/// and its measured image area when the profile is not measured yet. `bits` is the depth of+/// the stored samples (the profile's saturation level depends on it).+pub(crate) fn apply(s: &mut Sensor, bits: u32) {let (Some(make), Some(model)) = (s.make.as_deref(), s.model.as_deref()) else { return };let (make, model) = (make.trim(), model.trim());if let Some(a) = IMAGE_AREAS.iter().find(|a| a.make == make && a.model == model && a.size == (s.width, s.height) && s.crop == s.active) {@@ -164,7 +183,7 @@ pub(crate) fn apply(s: &mut Sensor) {}let (x, y, w, h) = p.crop;s.crop = Rect::new(x, y, w, h);- s.white = [p.white; 3];+ s.white = [p.white_at(bits); 3];if p.black_zero {s.warnings.retain(|w| !w.contains("black level not recorded"));}@@ -217,7 +236,7 @@ mod tests {#[test]fn a_d4_nef_gets_its_crop_levels_matrix_and_curve() {let mut s = nef("NIKON D4", 4992, 3292);- apply(&mut s);+ apply(&mut s, 14);assert_eq!(s.crop, Rect::new(8, 6, 4928, 3280));assert_eq!(s.white, [15520.0; 3]);assert_eq!(s.color.calibrations.len(), 1);@@ -229,6 +248,28 @@ mod tests {assert!(s.color.balanced_to_xyz_d50([0.5, 1.0, 0.7]).is_some());}+ #[test]+ fn the_d4_white_level_follows_the_sample_depth() {+ // 14 bits is the depth it was measured at: unchanged.+ let mut s = nef("NIKON D4", 4992, 3292);+ apply(&mut s, 14);+ assert_eq!(s.white, [15520.0; 3]);+ // A 12-bit sample is the 14-bit one shifted right by two.+ let mut s = nef("NIKON D4", 4992, 3292);+ apply(&mut s, 12);+ assert_eq!(s.white, [3880.0; 3]);+ // Everything else of the profile is independent of the depth.+ assert_eq!(s.crop, Rect::new(8, 6, 4928, 3280));+ assert_eq!(s.tone_curve.len(), D4_TONE.len());+ assert!(s.warnings.is_empty());+ // Other depths scale the same way and stay inside their own range; nonsense stays finite.+ for (bits, white) in [(1, 15520.0 / 8192.0), (8, 15520.0 / 64.0), (13, 7760.0), (16, 62080.0), (0, 15520.0 / 8192.0), (200, 62080.0)] {+ let mut s = nef("NIKON D4", 4992, 3292);+ apply(&mut s, bits);+ assert_eq!(s.white, [white; 3], "{bits} bits");+ }+ }+#[test]fn d5100_image_area_respects_explicit_crops_and_exact_sensor_size() {let mut s = nef("NIKON D5100", 4992, 3280);@@ -236,7 +277,7 @@ mod tests {s.active = Rect::new(0, 0, 4984, 3280);s.crop = s.active;let wb = s.camera_wb;- apply(&mut s);+ apply(&mut s, 14);assert_eq!(s.crop, Rect::new(8, 8, 4928, 3264));assert_eq!(s.active, Rect::new(0, 0, 4984, 3280));assert_eq!(s.camera_wb, wb);@@ -244,11 +285,11 @@ mod tests {assert_eq!(s.white, [16383.0; 3]);s.crop = Rect::new(16, 12, 4800, 3200);- apply(&mut s);+ apply(&mut s, 14);assert_eq!(s.crop, Rect::new(16, 12, 4800, 3200));for (model, w, h) in [("NIKON D5100", 2496, 1640), ("NIKON D5200", 4992, 3280)] {let mut other = nef(model, w, h);- apply(&mut other);+ apply(&mut other, 14);assert_eq!(other.crop, other.active);}}@@ -256,36 +297,36 @@ mod tests {#[test]fn measured_image_areas_crop_the_borders_without_touching_the_rest() {let mut s = nef("NIKON D800E", 7424, 4924);- apply(&mut s);+ apply(&mut s, 14);assert_eq!(s.crop, Rect::new(8, 6, 7360, 4912));// Only the image area is measured: levels, colour and tone stay untouched.assert!(s.color.calibrations.is_empty() && s.tone_curve.is_empty());assert_eq!(s.white, [16383.0; 3]);let mut s = sensor("SONY", "ILCE-7", 6048, 4024);- apply(&mut s);+ apply(&mut s, 14);assert_eq!(s.crop, Rect::new(12, 12, 6000, 4000));// Another size or model of the same maker is left alone.let mut s = nef("NIKON D800E", 2496, 1646);- apply(&mut s);+ apply(&mut s, 14);assert_eq!(s.crop, Rect::new(0, 0, 2496, 1646));let mut s = nef("NIKON D800", 7424, 4924);- apply(&mut s);+ apply(&mut s, 14);assert_eq!(s.crop, Rect::new(0, 0, 7424, 4924));}#[test]fn d90_image_area_matches_the_camera_jpeg() {let mut s = nef("NIKON D90", 4352, 2868);- apply(&mut s);+ apply(&mut s, 14);assert_eq!(s.crop, Rect::new(10, 10, 4288, 2848));// Only the image area is measured: levels, colour and tone stay untouched.assert!(s.color.calibrations.is_empty() && s.tone_curve.is_empty());assert_eq!(s.white, [16383.0; 3]);// An explicit crop in the file wins over the measured area.s.crop = Rect::new(0, 0, 4288, 2848);- apply(&mut s);+ apply(&mut s, 14);assert_eq!(s.crop, Rect::new(0, 0, 4288, 2848));}@@ -298,7 +339,7 @@ mod tests {calibrations: vec![Calibration { temperature: 6504.0, color_matrix: IDENTITY, forward_matrix: None, camera_calibration: IDENTITY }],..ColorInfo::default()};- apply(&mut s);+ apply(&mut s, 14);assert_eq!(s.color.calibrations.len(), 1);assert_eq!(s.color.calibrations[0].temperature, 6504.0, "the file's own calibration stands");assert!(s.tone_curve.is_empty() && s.black_point == 0.0);@@ -309,7 +350,7 @@ mod tests {fn other_cameras_and_sizes_are_left_alone() {for (model, w, h) in [("NIKON D4S", 4992, 3292), ("NIKON D4", 2496, 1646), ("NIKON D800", 7424, 4924)] {let mut s = nef(model, w, h);- apply(&mut s);+ apply(&mut s, 14);assert_eq!(s.crop, Rect::new(0, 0, w, h), "{model} {w}x{h}");assert!(s.tone_curve.is_empty() && s.color.calibrations.is_empty() && s.white == [16383.0; 3]);assert_eq!(s.warnings.len(), 1);diff --git a/crates/raw/src/tiffep.rs b/crates/raw/src/tiffep.rsindex 25befbdde..cf799de7f 100644--- a/crates/raw/src/tiffep.rs+++ b/crates/raw/src/tiffep.rs@@ -254,7 +254,7 @@ pub(crate) fn decode(t: &Tiff, format: RawFormat, limits: &Limits) -> Result<Senblack_point: 0.0,warnings,};- crate::cameras::apply(&mut sensor);+ crate::cameras::apply(&mut sensor, plane.bits);Ok(sensor)}diff --git a/crates/raw/tests/nikon_d4_depth.rs b/crates/raw/tests/nikon_d4_depth.rsnew file mode 100644index 000000000..26076c7e8--- /dev/null+++ b/crates/raw/tests/nikon_d4_depth.rs@@ -0,0 +1,44 @@+//! The Nikon D4 profile (`cameras.rs`) was measured on 14-bit NEFs. The camera also writes 12-bit+//! files (lossless, lossy and uncompressed) whose samples are the 14-bit values shifted right by+//! two, so the profile's saturation level must follow the sample depth: a 12-bit D4 frame has to+//! develop as bright as the same scene at 14 bits.++use photocraft_raw::testgen::{Val, tiff_ep};+use photocraft_raw::{Demosaic, DevelopOptions, Limits, Rect, WhiteBalance, decode, develop_sensor};++const WIDTH: usize = 4992;+const HEIGHT: usize = 3292;++/// A synthetic D4 NEF: a smooth ramp, 14-bit values (3000..7000, well below the saturation+/// level) stored at `bits`.+fn d4_nef(bits: u16) -> Vec<u8> {+ let shift = 14 - u32::from(bits);+ let data: Vec<u16> = (0..WIDTH * HEIGHT).map(|i| (3000 + ((i % WIDTH) + 2 * (i / WIDTH)) % 4000) as u16 >> shift).collect();+ tiff_ep("NIKON CORPORATION", WIDTH, HEIGHT, &data, [0, 1, 1, 2], bits, vec![(272, Val::Ascii("NIKON D4".into()))])+}++/// Mean linear luminance of the developed ProPhoto frame over a small window of the image.+fn mean_luminance(bits: u16) -> (f32, f64) {+ let mut s = decode(&d4_nef(bits), &Limits::default()).expect("a synthetic D4 NEF decodes");+ let white = s.white[1];+ // Only a window is developed: the profile is keyed on the full sensor size, the test is not.+ s.crop = Rect::new(2000, 1000, 64, 64);+ let opts = DevelopOptions { demosaic: Demosaic::Bilinear, white_balance: WhiteBalance::Multipliers([2.0, 1.0, 1.5]), ..DevelopOptions::default() };+ let d = develop_sensor(&s, &opts).expect("a synthetic D4 NEF develops");+ let lin = |v: u16| (f64::from(v) / 65535.0).powf(1.8);+ let pixels = d.rgb.as_chunks::<3>().0;+ let y: f64 = pixels.iter().map(|p| 0.2880402 * lin(p[0]) + 0.7118741 * lin(p[1]) + 0.0000857 * lin(p[2])).sum();+ (white, y / pixels.len() as f64)+}++#[test]+fn a_12_bit_d4_nef_develops_as_bright_as_the_same_scene_at_14_bit() {+ let (white14, y14) = mean_luminance(14);+ let (white12, y12) = mean_luminance(12);+ let stops = (y12 / y14).log2();+ assert!(stops.abs() < 0.2, "12-bit differs from 14-bit by {stops:.2} stops (mean Y {y12:.4} vs {y14:.4})");+ // The 14-bit level is the measured one, untouched.+ assert_eq!(white14, 15520.0);+ // The 12-bit level is the same fraction of the (4095-range) data.+ assert_eq!(white12, 3880.0);+}diff --git a/docs/file-format-parity.md b/docs/file-format-parity.mdindex 8cccdd1be..00a5f8160 100644--- a/docs/file-format-parity.md+++ b/docs/file-format-parity.md@@ -66,7 +66,7 @@ supports many more, including CR3).| DNG (incl. LJPEG, lossy JPEG and JPEG XL tiles) | yes | 2026-10-13: lossy DNG (DNG 1.4 compression 34892: 8-bit baseline JPEG tiles or strips, CFA and LinearRaw, partial edge tiles) decodes to the stored samples, which go through LinearizationTable / BlackLevel / WhiteLevel (default 255) like every other depth; `crates/raw/tests/dng.rs` `lossy_jpeg_*` (FILE-215-11, lossy DNG part; no lossy file in the raw corpus yet) || Canon CR2 | yes | || Canon CR3 | detected, unsupported | `crates/raw/src/lib.rs:219` |-| Nikon NEF/NRW (lossless, lossy) | yes; "lossy after split" no | #50; D5100 and D90 image areas measured (14-bit NEFs); [validation](nikon-nef-development.md) |+| Nikon NEF/NRW (lossless, lossy) | yes; "lossy after split" no | #50; D5100 and D90 image areas measured (14-bit NEFs); D4 profile levels follow the sample depth (12 and 14 bit); [validation](nikon-nef-development.md) || Sony ARW/SR2/SRF (incl. compressed) | yes | || Panasonic RW2, Pentax PEF | yes | || Olympus ORF | uncompressed only | |diff --git a/docs/gaps.md b/docs/gaps.mdindex 9b281d678..569e0ffd3 100644--- a/docs/gaps.md+++ b/docs/gaps.md@@ -141,6 +141,11 @@ what an agent can't finish alone.4352 × 2868 plane) is measured by aligning the decoded plane with the camera's ownJPEG, so a D90 NEF opens on the camera's frame instead of the uncropped sensor plane;`crates/raw/src/cameras.rs` (`IMAGE_AREAS`).+- **Progress (2026-10-14):** the measured Nikon D4 profile's saturation level follows the+ sample depth (15520 at 14 bits, 3880 at 12), so a 12-bit D4 NEF opens as bright as the same+ scene at 14 bits instead of 2.6 stops darker (#5978). Real CC0 pair, mean linear luminance+ against the 14-bit file: 12-bit lossy +0.02, uncompressed -0.02, lossless +0.01 stop;+ 14-bit output unchanged (`cameras.rs`, `tests/nikon_d4_depth.rs`).- **Estimate:** 120–200 h.### G15. AI and generative features@@ -296,6 +301,7 @@ what an agent can't finish alone.| 2026-10-15 | minor | G9: Animated Zoom preference takes effect (hold the Zoom tool to zoom continuously, #6210); prefs that do nothing 25 of 166; no estimate changed || 2026-10-16 | minor | G7: Content-Aware Move's live preview covers every selection the command takes (over 256 × 256: a coarse result solved scaled down, then the exact one); TOOL-213-4 done (#6680) || 2026-10-15 | minor | G9: Save in Background off saves in the foreground under a modal progress dialog (#6207); preferences that do nothing 25 → 24 of 169 |+| 2026-10-14 | minor | G14: the Nikon D4 profile's white level follows the sample depth, so 12-bit D4 NEFs no longer open 2.6 stops dark (#5978); gap remains open || 2026-10-14 | minor | G1: a PSD smart object's filter mask shows when the file opens (Photoshop stores the filtered pixels unmasked); export stores them that way too; Photoshop-oracle floor 185 → 188 of 258 || 2026-10-13 | minor | G14: D90 image area measured (4288 × 2848 at (10, 10)); gap remains open || 2026-10-14 | minor | Corpus floors raised to the measured results of the CMYK, Lab, 32-bit, Levels, Threshold, Posterize, feather and pass-through fixes landed together: Photoshop-authored 165 → 185 of 258, io 146 → 148 of 170, psd-tools 242 → 249 of 309 |