Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
499 changes: 499 additions & 0 deletions crates/engine/examples/raw_frame.rs

Large diffs are not rendered by default.

168 changes: 164 additions & 4 deletions crates/raw/src/vendor/nef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,88 @@ struct MeasuredBody {
/// the masked-column test keeps them and they develop as a pink band on the right. The camera's 4288 × 2848 JPEG
/// sits at (10, 10). Green saturates at 3840–3851 while red and blue run on to 4095; one level of 3830 keeps
/// clipped highlights white.
/// * **NIKON D7000** and **NIKON D5100** (and the Coolpix A, the same readout): the data is 4992 × 3280, the picture its
/// first 4948 columns, then 36 masked ones at one level (~600, with a black level of 0) and 8 of 0. The masked
/// columns sit above the dark-column trim's threshold, so without this area they show as a coloured bar on the right
/// edge (the top in portrait; issue #976). The camera's 4928 × 3264 JPEG sits at (8, 8): in the D5100's 3 CC0
/// samples, and in the D7000's one clean one, with the D5100 and Coolpix A as siblings (see below).
/// * **The other bodies** below keep masked or dark lines inside the whole data (a survey of every Nikon raw on raw.pixls.us):
/// each crop is its camera JPEG's frame as `cargo xtask raw-frame` measures it on raw.pixls.us CC0 samples (the
/// D200, D3200 and Nikon 1 V1 also on other publicly posted sample files), with at least 3 clean
/// files agreeing: of the model (bit depths pooled: both read out the same area), or, with at least
/// one of its own, together with models of the same readout (data and JPEG size) that agree on the same frame
/// (*siblings*). The D4's 12-bit mode reads out the 14-bit area measured above; the Df, the same readout, agrees on
/// (8, 6) in 6 files. Bodies whose crop holds only picture beyond the camera frame have no entry (no change).
const MEASURED_BODIES: &[MeasuredBody] = &[
MeasuredBody { model: "NIKON D4", size: (4992, 3292), bits: 14, crop: (8, 6, 4928, 3280), white: Some(15520.0) },
MeasuredBody { model: "NIKON D4", size: (4992, 3292), bits: 12, crop: (8, 6, 4928, 3280), white: None },
MeasuredBody { model: "NIKON D7100", size: (6036, 4020), bits: 14, crop: (18, 10, 6000, 4000), white: None },
MeasuredBody { model: "NIKON D7100", size: (4812, 3212), bits: 14, crop: (6, 6, 4800, 3200), white: None },
MeasuredBody { model: "NIKON D90", size: (4352, 2868), bits: 12, crop: (10, 10, 4288, 2848), white: Some(3830.0) },
MeasuredBody { model: "NIKON D7000", size: (4992, 3280), bits: 14, crop: (8, 8, 4928, 3264), white: None },
MeasuredBody { model: "NIKON D7000", size: (4992, 3280), bits: 12, crop: (8, 8, 4928, 3264), white: None },
MeasuredBody { model: "NIKON D5100", size: (4992, 3280), bits: 14, crop: (8, 8, 4928, 3264), white: None },
// siblings: D7000 (4), D5100 (3); masked: right 36
MeasuredBody { model: "COOLPIX A", size: (4992, 3280), bits: 14, crop: (8, 8, 4928, 3264), white: None },
// 6 files each; masked: left 2, right 2
MeasuredBody { model: "NIKON D3", size: (4288, 2844), bits: 12, crop: (15, 6, 4256, 2832), white: None },
MeasuredBody { model: "NIKON D3", size: (4288, 2844), bits: 14, crop: (15, 6, 4256, 2832), white: None },
MeasuredBody { model: "NIKON D3S", size: (4288, 2844), bits: 12, crop: (15, 6, 4256, 2832), white: None },
MeasuredBody { model: "NIKON D3S", size: (4288, 2844), bits: 14, crop: (15, 6, 4256, 2832), white: None },
MeasuredBody { model: "NIKON D700", size: (4288, 2844), bits: 12, crop: (15, 6, 4256, 2832), white: None },
MeasuredBody { model: "NIKON D700", size: (4288, 2844), bits: 14, crop: (15, 6, 4256, 2832), white: None },
// 6 and 4 files; masked: right up to 32
MeasuredBody { model: "NIKON D300", size: (4352, 2868), bits: 12, crop: (15, 10, 4288, 2848), white: None },
MeasuredBody { model: "NIKON D300", size: (4352, 2868), bits: 14, crop: (15, 10, 4288, 2848), white: None },
MeasuredBody { model: "NIKON D300S", size: (4352, 2868), bits: 12, crop: (15, 10, 4288, 2848), white: None },
MeasuredBody { model: "NIKON D300S", size: (4352, 2868), bits: 14, crop: (15, 10, 4288, 2848), white: None },
// D2X (1) and D2Xs (2) as siblings; dark: right 1
MeasuredBody { model: "NIKON D2Xs", size: (4320, 2868), bits: 12, crop: (16, 10, 4288, 2848), white: None },
// D3000, D40X, D60, D80: one file each, siblings; masked: right 4, bottom 3
MeasuredBody { model: "NIKON D3000", size: (3904, 2616), bits: 12, crop: (14, 9, 3872, 2592), white: None },
MeasuredBody { model: "NIKON D40X", size: (3904, 2616), bits: 12, crop: (14, 9, 3872, 2592), white: None },
MeasuredBody { model: "NIKON D60", size: (3904, 2616), bits: 12, crop: (14, 9, 3872, 2592), white: None },
MeasuredBody { model: "NIKON D80", size: (3904, 2616), bits: 12, crop: (14, 9, 3872, 2592), white: None },
// 3 files; masked: left 1, right 3 (the D80's data size, framed elsewhere)
MeasuredBody { model: "NIKON D200", size: (3904, 2616), bits: 12, crop: (16, 12, 3872, 2592), white: None },
// 22 files; masked: right 46
MeasuredBody { model: "NIKON D3200", size: (6080, 4012), bits: 12, crop: (8, 6, 6016, 4000), white: None },
// 8 clean of 19, the rest ~0.3 px between pixels on the same frame; dark: bottom 2
MeasuredBody { model: "NIKON 1 V1", size: (3904, 2606), bits: 12, crop: (12, 6, 3872, 2592), white: None },
// Best guesses, short of the evidence rule; every frame below keeps the masked lines out by several pixels.
// D3100: 3 of 4 clean files at (24, 6), the 4th at (22, 7); masked: left 6, right 22
MeasuredBody { model: "NIKON D3100", size: (4672, 3084), bits: 12, crop: (24, 6, 4608, 3072), white: None },
// D5000: 4 of 5 clean files at (11, 10), the 5th at (11, 12); the D90's sensor, whose own files also align at
// x = 11; masked: right 42 (the same filler as the D90's)
MeasuredBody { model: "NIKON D5000", size: (4352, 2868), bits: 12, crop: (11, 10, 4288, 2848), white: None },
// D40: 2 of 2 clean files at (16, 9); masked: right 1. The D50 / D70 / D70s share its data size but not its frame
MeasuredBody { model: "NIKON D40", size: (3040, 2014), bits: 12, crop: (16, 9, 3008, 2000), white: None },
// D50, D70, D70s (one readout): one clean D50 and one clean D70 file at (15, 7); the D70s's 2 files align
// between pixels next to it, at (15.4, 6.9) and (15.6, 6.4); masked: right 1
MeasuredBody { model: "NIKON D50", size: (3040, 2014), bits: 12, crop: (15, 7, 3008, 2000), white: None },
MeasuredBody { model: "NIKON D70", size: (3040, 2014), bits: 12, crop: (15, 7, 3008, 2000), white: None },
MeasuredBody { model: "NIKON D70s", size: (3040, 2014), bits: 12, crop: (15, 7, 3008, 2000), white: None },
// 3 files; dark: bottom 4
MeasuredBody { model: "NIKON D4S", size: (4936, 3288), bits: 12, crop: (4, 4, 4928, 3280), white: None },
MeasuredBody { model: "NIKON D4S", size: (4936, 3288), bits: 14, crop: (4, 4, 4928, 3280), white: None },
// 4 files; dark: bottom up to 4
MeasuredBody { model: "NIKON D5600", size: (6016, 4016), bits: 12, crop: (8, 8, 6000, 4000), white: None },
MeasuredBody { model: "NIKON D5600", size: (6016, 4016), bits: 14, crop: (8, 8, 6000, 4000), white: None },
// 4 files each; masked: right 8
MeasuredBody { model: "NIKON D600", size: (6080, 4028), bits: 12, crop: (8, 6, 6016, 4016), white: None },
MeasuredBody { model: "NIKON D600", size: (6080, 4028), bits: 14, crop: (8, 6, 6016, 4016), white: None },
MeasuredBody { model: "NIKON D610", size: (6080, 4028), bits: 12, crop: (8, 6, 6016, 4016), white: None },
MeasuredBody { model: "NIKON D610", size: (6080, 4028), bits: 14, crop: (8, 6, 6016, 4016), white: None },
// D800: 6 files; D800E: 2, with the D800 as sibling; masked: right 14 (issue #914, the top in portrait)
MeasuredBody { model: "NIKON D800", size: (7424, 4924), bits: 12, crop: (8, 6, 7360, 4912), white: None },
MeasuredBody { model: "NIKON D800", size: (7424, 4924), bits: 14, crop: (8, 6, 7360, 4912), white: None },
MeasuredBody { model: "NIKON D800E", size: (7424, 4924), bits: 14, crop: (8, 6, 7360, 4912), white: None },
// 3 files; dark: bottom 1
MeasuredBody { model: "NIKON D810", size: (7380, 4928), bits: 12, crop: (12, 8, 7360, 4912), white: None },
MeasuredBody { model: "NIKON D810", size: (7380, 4928), bits: 14, crop: (12, 8, 7360, 4912), white: None },
// 6 files; masked: left 2, right 42 (the D4's readout)
MeasuredBody { model: "NIKON Df", size: (4992, 3292), bits: 12, crop: (8, 6, 4928, 3280), white: None },
MeasuredBody { model: "NIKON Df", size: (4992, 3292), bits: 14, crop: (8, 6, 4928, 3280), white: None },
];

fn measured_body(model: &str, w: usize, h: usize, bits: u32) -> Option<&'static MeasuredBody> {
Expand Down Expand Up @@ -297,6 +374,22 @@ mod tests {
/// [`nef_with_note`] in either byte order (file and maker note).
#[allow(clippy::too_many_arguments)]
fn nef_in(order: ByteOrder, compression: u16, bits: u16, strips: Vec<Vec<u8>>, w: u32, h: u32, rps: u32, note: Option<IfdBuilder>) -> Vec<u8> {
nef_model_in(order, "NIKON TEST", compression, bits, strips, w, h, rps, note)
}

/// [`nef_in`] from camera `model`.
#[allow(clippy::too_many_arguments)]
fn nef_model_in(
order: ByteOrder,
model: &str,
compression: u16,
bits: u16,
strips: Vec<Vec<u8>>,
w: u32,
h: u32,
rps: u32,
note: Option<IfdBuilder>,
) -> Vec<u8> {
let mut raw = IfdBuilder::new();
raw.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0]));
raw.set(t::IMAGE_WIDTH, Value::Long(vec![w]));
Expand All @@ -309,7 +402,7 @@ mod tests {
raw.set_image(ImageData::Strips { rows_per_strip: rps, strips });
let mut ifd0 = IfdBuilder::new();
ifd0.set(t::MAKE, Value::Ascii("NIKON CORPORATION".into()));
ifd0.set(t::MODEL, Value::Ascii("NIKON TEST".into()));
ifd0.set(t::MODEL, Value::Ascii(model.into()));
if let Some(mn) = note {
let mut bytes = b"Nikon\0\x02\x10\0\0".to_vec();
bytes.extend(TiffWriter::new(order, false).write(&[mn]).unwrap());
Expand Down Expand Up @@ -457,9 +550,76 @@ mod tests {
assert_eq!((d4.metadata.width, d4.metadata.height), (Some(4928), Some(3280)));
let developed = d4.develop(crate::Method::Bilinear).unwrap();
assert_eq!((developed.width, developed.height), (4928, 3280));
for other in [decode_as("NIKON D5", 14), decode_as("NIKON D4", 12)] {
assert_eq!(other.crop, Rect::new(0, 0, w, h));
assert_ne!(other.white, vec![15520.0]);
// the 12-bit mode reads out the same area but keeps the white level from its data
let d4_12 = decode_as("NIKON D4", 12);
assert_eq!(d4_12.crop, Rect::new(8, 6, 4928, 3280));
assert_ne!(d4_12.white, vec![15520.0]);
let other = decode_as("NIKON D5", 14);
assert_eq!(other.crop, Rect::new(0, 0, w, h));
assert_ne!(other.white, vec![15520.0]);
}

/// Issue #976: the D7000 and D5100 record no CropArea, and their 36 masked columns at ~600 were developed as a
/// coloured bar. Their measured area applies to the measured models, data sizes and depths only.
#[test]
fn measured_d7000_and_d5100_image_area() {
let (w, h) = (4992usize, 3280usize);
let decode_as = |model: &str, bits: u16| {
let nef = nef_model_in(ByteOrder::Big, model, 1, bits, vec![vec![0x10; w * h * 2]], w as u32, h as u32, h as u32, None);
crate::decode(&nef).unwrap()
};
for (model, bits) in [("NIKON D7000", 14), ("NIKON D7000", 12), ("NIKON D5100", 14)] {
let raw = decode_as(model, bits);
assert_eq!(raw.crop, Rect::new(8, 8, 4928, 3264), "{model} {bits}-bit");
assert_eq!((raw.metadata.width, raw.metadata.height), (Some(4928), Some(3264)));
let developed = raw.develop(crate::Method::Bilinear).unwrap();
assert_eq!((developed.width, developed.height), (4928, 3264));
}
// another model, or the D5100's unmeasured 12-bit mode, keeps the whole data
for (model, bits) in [("NIKON D7100", 14), ("NIKON D5100", 12), ("NIKON D7000 ", 16)] {
assert_eq!(decode_as(model, bits).crop, Rect::new(0, 0, w, h), "{model} {bits}-bit");
}
}

/// The D200, D3200 and Nikon 1 V1 keep masked or dark lines in their whole data (the D200 shares the D80's data
/// size but frames it elsewhere), measured on public sample files beyond the CC0 ones; the D3100, D5000, D40, D50,
/// D70 and D70s get best-guess frames (the D40 shares the D50 / D70 / D70s data size but not their frame).
#[test]
fn measured_older_dx_bodies_image_area() {
let decode_as = |model: &str, (w, h): (usize, usize)| {
let nef = nef_model_in(ByteOrder::Big, model, 1, 12, vec![vec![0x10; w * h * 2]], w as u32, h as u32, h as u32, None);
crate::decode(&nef).unwrap()
};
for (model, size, crop) in [
("NIKON D200", (3904, 2616), Rect::new(16, 12, 3872, 2592)),
("NIKON D80", (3904, 2616), Rect::new(14, 9, 3872, 2592)),
("NIKON D3200", (6080, 4012), Rect::new(8, 6, 6016, 4000)),
("NIKON 1 V1", (3904, 2606), Rect::new(12, 6, 3872, 2592)),
("NIKON D3100", (4672, 3084), Rect::new(24, 6, 4608, 3072)),
("NIKON D5000", (4352, 2868), Rect::new(11, 10, 4288, 2848)),
("NIKON D40", (3040, 2014), Rect::new(16, 9, 3008, 2000)),
("NIKON D50", (3040, 2014), Rect::new(15, 7, 3008, 2000)),
("NIKON D70", (3040, 2014), Rect::new(15, 7, 3008, 2000)),
("NIKON D70s", (3040, 2014), Rect::new(15, 7, 3008, 2000)),
] {
assert_eq!(decode_as(model, size).crop, crop, "{model}");
}
// the V1's area on the V2's data size: no entry, the whole data
assert_eq!(decode_as("NIKON 1 V1", (4620, 3084)).crop, Rect::new(0, 0, 4620, 3084));
}

/// Every measured entry is a crop inside its data, once per model, data size and depth, with a white level (if
/// any) inside its bit range.
#[test]
fn measured_bodies_are_consistent() {
let mut seen = std::collections::HashSet::new();
for b in MEASURED_BODIES {
let (x, y, w, h) = b.crop;
assert!(w > 0 && h > 0 && x + w <= b.size.0 && y + h <= b.size.1, "{}: crop outside its data", b.model);
assert!(seen.insert((b.model, b.size, b.bits)), "{} {:?} {}-bit listed twice", b.model, b.size, b.bits);
assert!(matches!(b.bits, 12 | 14), "{}: {}-bit", b.model, b.bits);
assert!(b.white.is_none_or(|v| v > 0.0 && v < (1u32 << b.bits) as f32), "{}: white level", b.model);
assert_eq!(b.model.trim(), b.model, "{}: model names are matched trimmed", b.model);
}
}

Expand Down
Loading