Skip to content

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

LR-RAW-NEF: only take the NRW block's white balance where the as-shot kelvin is mirrored (P7000); P7100 gets none

radialmonster4f51f051e7b3
diff --git a/crates/raw/src/vendor/nef.rs b/crates/raw/src/vendor/nef.rs
index b9d6c9cc4..39dfe6fd5 100644
--- a/crates/raw/src/vendor/nef.rs
+++ b/crates/raw/src/vendor/nef.rs
@@ -47,13 +47,16 @@ fn nrw_black(mn: Option<&makernote::MakerNote>, bits: u32) -> Option<f32> {
/// As-shot white balance from the NRW data block, for bodies whose maker note has no `0x000c`: the first entry of the
/// block's colour-temperature table (byte `0x34`) is five `u32`s, kelvin, R, G, G, B, in the same 1/256 units as
-/// `0x000c` (the same R and B values in the P950, B700, P1000 and P1100 files, which carry both).
+/// `0x000c` (the same R and B values in the P950, B700, P1000 and P1100 files, which carry both). Only used when the
+/// table's fourth entry, a kelvin-only slot, repeats the first entry's kelvin (the P7000); the P7100 keeps its
+/// as-shot kelvin there (3195) while entry 0 is the 3000 K preset, so that body gets no white balance from here.
fn nrw_wb(mn: Option<&makernote::MakerNote>) -> Option<[f32; 3]> {
let (b, order) = nrw_data(mn)?;
let at = |i: usize| -> Option<u32> { Some(order.u32(b.get(0x34 + 4 * i..0x38 + 4 * i)?.try_into().ok()?)) };
let (kelvin, r, g1, g2, bl) = (at(0)?, at(1)?, at(2)?, at(3)?, at(4)?);
let sane = |v: u32| (64..=2560).contains(&v);
- ((2000..=12000).contains(&kelvin) && g1 == 512 && g2.abs_diff(512) <= 8 && sane(r) && sane(bl)).then(|| [r as f32 / 256.0, 1.0, bl as f32 / 256.0])
+ let mirrored = at(15)? == kelvin;
+ (mirrored && (2000..=12000).contains(&kelvin) && g1 == 512 && g2.abs_diff(512) <= 8 && sane(r) && sane(bl)).then(|| [r as f32 / 256.0, 1.0, bl as f32 / 256.0])
}
/// Nikon CropArea is `[left, top, width, height]` in sensor pixels (maker-note tag documentation).
@@ -601,22 +604,26 @@ mod tests {
#[test]
fn nrw_block_supplies_the_as_shot_white_balance_when_the_maker_note_has_none() {
- let note = |kelvin: u32, r: u32, g1: u32, g2: u32, b: u32| {
+ let note = |kelvin: u32, r: u32, g1: u32, g2: u32, b: u32, mirror: Option<u32>| {
let mut block = b"NRW 0101".to_vec();
block.resize(0x34, 0);
for v in [kelvin, r, g1, g2, b] {
block.extend_from_slice(&v.to_le_bytes());
}
+ block.resize(0x34 + 60, 0);
+ block.extend_from_slice(&mirror.unwrap_or(kelvin).to_le_bytes());
block.resize(0x80, 0);
let mut mn = IfdBuilder::new();
mn.set(NRW_DATA, Value::Undefined(block));
mn
};
let wb = |mn| crate::decode(&nef_in(ByteOrder::Little, 1, 12, vec![vec![0; 144]], 16, 6, 6, Some(mn))).unwrap().wb_multipliers;
- assert_eq!(wb(note(5236, 430, 512, 510, 567)), Some([430.0 / 256.0, 1.0, 567.0 / 256.0]));
+ assert_eq!(wb(note(5236, 430, 512, 510, 567, None)), Some([430.0 / 256.0, 1.0, 567.0 / 256.0]));
// an implausible entry is not a white balance
- assert_eq!(wb(note(0, 430, 512, 510, 567)), None);
- assert_eq!(wb(note(5236, 430, 300, 510, 567)), None);
+ assert_eq!(wb(note(0, 430, 512, 510, 567, None)), None);
+ assert_eq!(wb(note(5236, 430, 300, 510, 567, None)), None);
+ // the P7100 layout: entry 0 is a preset, the as-shot kelvin sits in the fourth entry
+ assert_eq!(wb(note(3000, 278, 512, 510, 868, Some(3195))), None);
}
#[test]