fix(raw): a DNG with only DefaultCropSize is cropped from the active area's origin (#950) (#1595)

The DNG default crop was applied only when both DefaultCropOrigin and
DefaultCropSize were present, so a file carrying just the size was
developed at the whole active area. Each tag now takes its own default
from DNG 1.7.1.0: the origin (0, 0), the size the whole image. The
existing bounds check is unchanged, so an origin-only crop that no
longer fits still falls back to the active area with a warning, and a
tag with the wrong number of values is still reported as incomplete.

Fixes #950.
This commit is contained in:
Books authored and GitHub committed 2026-10-09 13:24:21 +07:00
1 parent 7c15011410
commit 0bb46df638
3 files changed
+52 -10

No files matched your search

+19 -8
View File
@@ -147,17 +147,28 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result<Sensor> {
}
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"))?;
+5
View File
@@ -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<Vec<u16>>,
/// (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())));
+28 -2
View File
@@ -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).