diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index 2ffa1a6dc61..96931227a5c 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -147,17 +147,28 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result { } let cfa = if is_cfa { Some(cfa(t, &raw, &active)?) } else { None }; - // Default crop, relative to the active area. + // Default crop, relative to the active area. Each tag has its own default (DNG 1.7.1.0, + // DefaultCropOrigin and DefaultCropSize): the origin (0, 0), the size the whole image. let mut crop = active; - let origin = t.tag_floats(&raw, tag::DEFAULT_CROP_ORIGIN); - let size = t.tag_floats(&raw, tag::DEFAULT_CROP_SIZE); if raw.has(tag::DEFAULT_CROP_ORIGIN) || raw.has(tag::DEFAULT_CROP_SIZE) { - if let ([ox, oy], [cw, ch]) = (origin.as_slice(), size.as_slice()) { + // A present tag must hold two values; an absent one takes its default. + let pair = |tg: u16, default: [f64; 2]| -> Option<[f64; 2]> { + if !raw.has(tg) { + return Some(default); + } + match t.tag_floats(&raw, tg).as_slice() { + [a, b] => Some([*a, *b]), + _ => None, + } + }; + let origin = pair(tag::DEFAULT_CROP_ORIGIN, [0.0, 0.0]); + let size = pair(tag::DEFAULT_CROP_SIZE, [plane.width as f64, plane.height as f64]); + if let (Some([ox, oy]), Some([cw, ch])) = (origin, size) { let (ox, oy, cw, ch) = ( - crop_value(*ox, "DefaultCropOrigin"), - crop_value(*oy, "DefaultCropOrigin"), - crop_value(*cw, "DefaultCropSize"), - crop_value(*ch, "DefaultCropSize"), + crop_value(ox, "DefaultCropOrigin"), + crop_value(oy, "DefaultCropOrigin"), + crop_value(cw, "DefaultCropSize"), + crop_value(ch, "DefaultCropSize"), ); let (ox, oy, cw, ch) = (ox?, oy?, cw?, ch?); let active_right = active.x.checked_add(active.width).ok_or_else(|| RawError::malformed("DNG active-area right edge overflow"))?; diff --git a/crates/raw/src/testgen.rs b/crates/raw/src/testgen.rs index 32051e3c817..7a784b6e072 100644 --- a/crates/raw/src/testgen.rs +++ b/crates/raw/src/testgen.rs @@ -396,6 +396,8 @@ pub struct DngSpec { pub default_crop: Option<([u32; 2], [u32; 2])>, /// Double-precision origin override for malformed-metadata regression tests. pub default_crop_origin_double: Option<[f64; 2]>, + /// A DefaultCropSize with no DefaultCropOrigin (`default_crop` must be `None`). + pub default_crop_size_only: Option<[u32; 2]>, pub linearization: Option>, /// (illuminant code, ColorMatrix row-major) pub color_matrix1: Option<(u16, [f64; 9])>, @@ -429,6 +431,7 @@ impl DngSpec { active_area: None, default_crop: None, default_crop_origin_double: None, + default_crop_size_only: None, linearization: None, color_matrix1: None, color_matrix2: None, @@ -564,6 +567,8 @@ impl DngSpec { raw.push((50720, Val::Long(s.to_vec()))); } else if let Some(origin) = self.default_crop_origin_double { raw.push((50719, Val::Double(origin.to_vec()))); + } else if let Some(size) = self.default_crop_size_only { + raw.push((50720, Val::Long(size.to_vec()))); } if let Some(o) = &self.opcode_list2 { raw.push((51009, Val::Undefined(o.clone()))); diff --git a/crates/raw/tests/dng.rs b/crates/raw/tests/dng.rs index 8b2e4014431..32224b54fdf 100644 --- a/crates/raw/tests/dng.rs +++ b/crates/raw/tests/dng.rs @@ -128,8 +128,9 @@ fn out_of_range_default_crop_falls_back_to_active_area_with_warning() { assert!(s.warnings.iter().any(|warning| warning.contains("out-of-range") && warning.contains("default crop")), "{:?}", s.warnings); } +/// DefaultCropSize defaults to the whole image, which can't fit once the origin moves in. #[test] -fn incomplete_default_crop_falls_back_to_active_area_with_warning() { +fn default_crop_origin_without_size_falls_back_to_active_area_with_warning() { let (w, h) = (16, 12); let mut spec = DngSpec::cfa(w, h, vec![1000; w * h]); spec.active_area = Some([2, 3, 10, 14]); @@ -137,7 +138,32 @@ fn incomplete_default_crop_falls_back_to_active_area_with_warning() { let s = sensor(&spec.build()); assert_eq!(s.crop, s.active); - assert!(s.warnings.iter().any(|warning| warning.contains("incomplete") && warning.contains("default crop")), "{:?}", s.warnings); + assert!(s.warnings.iter().any(|warning| warning.contains("out-of-range") && warning.contains("default crop")), "{:?}", s.warnings); +} + +/// #950: DefaultCropOrigin defaults to (0, 0), so a size-only crop starts at the active area's +/// top-left corner. +#[test] +fn default_crop_size_without_origin_crops_from_the_active_area_origin() { + let (w, h) = (8, 8); + let mut spec = DngSpec::cfa(w, h, (0..w * h).map(|i| (i as u16) * 37).collect()); + spec.active_area = Some([0, 0, 8, 8]); + spec.default_crop_size_only = Some([4, 4]); + let bytes = spec.build(); + + let s = sensor(&bytes); + assert_eq!(s.crop, Rect::new(0, 0, 4, 4)); + assert!(s.warnings.iter().all(|warning| !warning.contains("default crop")), "{:?}", s.warnings); + let d = develop(&bytes, &DevelopOptions::default()).unwrap(); + assert_eq!((d.width, d.height), (4, 4)); + + // Inside an offset active area the crop starts at that area's corner. + let (w, h) = (16, 12); + let mut spec = DngSpec::cfa(w, h, vec![1000; w * h]); + spec.active_area = Some([2, 3, 10, 14]); + spec.default_crop_size_only = Some([6, 4]); + let s = sensor(&spec.build()); + assert_eq!(s.crop, Rect::new(3, 2, 6, 4)); } /// Linear sRGB (D65) → XYZ, from the sRGB primaries (IEC 61966-2-1).