Skip to content

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

Merge PR #1475 (rebased)

Brandon Thomas51a784cd95fb
diff --cc crates/engine/src/camera_preview.rs
index 84dc7ef69,92297a2cb..e01116b45
--- a/crates/engine/src/camera_preview.rs
+++ b/crates/engine/src/camera_preview.rs
@@@ -1937,9 -1703,9 +1937,10 @@@ mod tests
color: ColorData::default(),
wb_multipliers: Some([1.0; 3]),
linearized: true,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata: lightcraft_meta::Metadata::default(),
+ lens_metadata: Default::default(),
};
let proxy = sensor_proxy(&raw, 2, 384).unwrap();
assert_eq!((proxy.width, proxy.height), (32, 32));
@@@ -2326,9 -2047,9 +2327,10 @@@
color,
wb_multipliers: Some([2.0, 1.0, 1.5]),
linearized: false,
+ pixel_aspect: 1.0,
opcodes: Default::default(),
metadata: lightcraft_raw::Metadata { make: Some(make.to_owned()), model: Some(model.to_owned()), ..Default::default() },
+ lens_metadata: Default::default(),
}
}
@@@ -3540,9 -3231,9 +3542,10 @@@
color: ColorData::default(),
wb_multipliers: Some([1.0; 3]),
linearized: true,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata: lightcraft_meta::Metadata { make: Some("SONY".into()), model: Some("ILCE-7M4".into()), ..Default::default() },
+ lens_metadata: Default::default(),
};
assert!(crate::camera_profiles::get("ILCE-7M4").is_some());
// the camera's rendering: a bright curve on the scene's luminance, as a grey (or tinted) 8-bit sRGB JPEG
diff --cc crates/engine/src/tests_xmp.rs
index 11871c90f,89b871e87..c5f2b05c0
--- a/crates/engine/src/tests_xmp.rs
+++ b/crates/engine/src/tests_xmp.rs
@@@ -584,9 -584,9 +584,10 @@@ pub(crate) fn synthetic_dng_of(w: usize
},
wb_multipliers: None,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata,
+ lens_metadata: Default::default(),
};
write_dng(&raw, &DngWriteOptions { xmp: xmp.map(str::to_string), ..Default::default() }).unwrap()
}
diff --cc crates/merge/src/frame.rs
index cb74e23ed,9ce00a505..1316bfd13
--- a/crates/merge/src/frame.rs
+++ b/crates/merge/src/frame.rs
@@@ -286,125 -230,4 +286,126 @@@ mod tests
}
assert!((detect_clip(&img) - 0.9 * 0.995).abs() < 1e-6);
}
+
+ /// A 16×12 CR2-style mosaic (no colour tags, as the vendor decoders leave them) with as-shot multipliers.
+ fn mosaic(wb_multipliers: Option<[f32; 3]>) -> RawImage {
+ use lightcraft_raw::{BlackLevel, Cfa, OpcodeLists, RawData, RawFormat, Rect};
+ let (w, h) = (16usize, 12usize);
+ RawImage {
+ format: RawFormat::Cr2,
+ width: w,
+ height: h,
+ cpp: 1,
+ data: RawData::U16((0..w * h).map(|i| (100 + (i * 37) % 900) as u16).collect()),
+ cfa: Cfa::bayer("RGGB"),
+ bits: 12,
+ black: BlackLevel::uniform(64.0),
+ white: vec![1023.0],
+ active_area: Rect::new(0, 0, w, h),
+ crop: Rect::new(0, 0, w, h),
+ orientation: Orientation::Normal,
+ color: ColorData::default(),
+ wb_multipliers,
+ linearized: false,
+ opcodes: OpcodeLists::default(),
+ lens_metadata: Default::default(),
++ pixel_aspect: 1.0,
+ metadata: Metadata::default(),
+ }
+ }
+
+ fn frame_of(raw: RawImage, colour: &RawColour) -> Frame {
+ raw_frame(raw, &[], Metadata::default(), None, None, true, colour).unwrap()
+ }
+
+ /// Issue #1144: the as-shot multipliers of a CR2, NEF… (its only white balance) were dropped on loading, so
+ /// nothing downstream could tell a merged DNG what the camera had balanced to.
+ #[test]
+ fn a_raw_frame_carries_the_as_shot_multipliers() {
+ let wb = [2.05, 1.0, 1.62];
+ let f = frame_of(mosaic(Some(wb)), &|_, _| None);
+ assert!(f.raw);
+ assert_eq!(f.wb_multipliers, Some(wb));
+ // colour tags the caller works out leave them alone
+ let own = ColorData { as_shot_neutral: Some([0.5, 1.0, 0.6]), ..Default::default() };
+ let f = frame_of(mosaic(Some(wb)), &|_, _| Some(own.clone()));
+ assert_eq!(f.wb_multipliers, Some(wb));
+ assert_eq!(f.color, FrameColor::Camera(Box::new(own)));
+ // a file that gives none gives none
+ assert_eq!(frame_of(mosaic(None), &|_, _| None).wb_multipliers, None);
+ assert_eq!(Frame::from_linear_rec2020(Rgb32f::new(2, 2), None).wb_multipliers, None);
+ }
+
+ fn voter(wb_multipliers: Option<[f32; 3]>) -> Frame {
+ Frame { wb_multipliers, ..Frame::from_linear_rec2020(Rgb32f::new(2, 2), None) }
+ }
+
+ fn consensus(wb: &[Option<[f32; 3]>]) -> Option<[f32; 3]> {
+ let frames: Vec<Frame> = wb.iter().map(|m| voter(*m)).collect();
+ consensus_multipliers(&frames)
+ }
+
+ fn close(a: [f32; 3], b: [f32; 3]) -> bool {
+ a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-6)
+ }
+
+ #[test]
+ fn the_consensus_is_the_median_of_the_voters() {
+ // one voter is its own median
+ assert_eq!(consensus(&[Some([2.0, 1.0, 1.6])]), Some([2.0, 1.0, 1.6]));
+ // an odd count takes the middle one of each ratio, whichever frame it came from, whatever the order
+ let odd = [Some([2.4, 1.0, 1.4]), Some([1.8, 1.0, 1.9]), Some([2.0, 1.0, 1.6])];
+ assert_eq!(consensus(&odd), Some([2.0, 1.0, 1.6]));
+ // an outlier doesn't pull it
+ let outlier = [Some([2.0, 1.0, 1.6]), Some([2.1, 1.0, 1.5]), Some([1.9, 1.0, 1.7]), Some([5.0, 1.0, 0.7]), Some([2.0, 1.0, 1.6])];
+ assert_eq!(consensus(&outlier), Some([2.0, 1.0, 1.6]));
+ // an even count takes the mean of the middle two
+ let even = [Some([1.8, 1.0, 1.9]), Some([2.0, 1.0, 1.6]), Some([2.2, 1.0, 1.5]), Some([2.4, 1.0, 1.4])];
+ assert!(close(consensus(&even).unwrap(), [2.1, 1.0, 1.55]), "{:?}", consensus(&even));
+ let two = [Some([2.0, 1.0, 1.0]), Some([3.0, 1.0, 2.0])];
+ assert!(close(consensus(&two).unwrap(), [2.5, 1.0, 1.5]), "{:?}", consensus(&two));
+ // green is the unit even when a file's multipliers aren't normalised to it
+ assert!(close(consensus(&[Some([4.0, 2.0, 3.0])]).unwrap(), [2.0, 1.0, 1.5]));
+ }
+
+ #[test]
+ fn frames_without_usable_multipliers_do_not_vote() {
+ assert_eq!(consensus(&[]), None);
+ assert_eq!(consensus(&[None, None]), None);
+ let bad = |m: [f32; 3]| consensus(&[Some(m)]);
+ assert_eq!(bad([0.0, 1.0, 1.5]), None);
+ assert_eq!(bad([2.0, 0.0, 1.5]), None);
+ assert_eq!(bad([2.0, 1.0, -1.5]), None);
+ assert_eq!(bad([f32::NAN, 1.0, 1.5]), None);
+ assert_eq!(bad([2.0, f32::INFINITY, 1.5]), None);
+ assert_eq!(bad([2.0, 1.0, f32::NEG_INFINITY]), None);
+ // mixed: the good ones decide, the missing and the unusable ones are not counted as anything
+ let mixed = [None, Some([2.0, 1.0, 1.6]), Some([f32::NAN, 1.0, 1.0]), Some([2.2, 1.0, 1.4]), Some([0.0, 0.0, 0.0])];
+ assert!(close(consensus(&mixed).unwrap(), [2.1, 1.0, 1.5]), "{:?}", consensus(&mixed));
+ assert_eq!(consensus(&[None, Some([2.0, 1.0, 1.6]), None]), Some([2.0, 1.0, 1.6]));
+ }
+
+ #[test]
+ fn multipliers_become_the_reciprocal_as_shot_neutral() {
+ use lightcraft_color::{D50, Xy};
+ let tagged = ColorData { as_shot_neutral: Some([0.9, 1.0, 0.9]), as_shot_white_xy: Some(Xy::new(0.34, 0.35)), ..Default::default() };
+ let mut color = FrameColor::Camera(Box::new(tagged.clone()));
+ color.set_as_shot_multipliers([2.0, 1.0, 1.6]);
+ let FrameColor::Camera(c) = &color else { panic!("still camera colour") };
+ assert_eq!(c.as_shot_neutral, Some([0.5, 1.0, 1.0 / f64::from(1.6f32)]));
+ assert_eq!(c.as_shot_white_xy, None, "a stale AsShotWhiteXY would be read before the neutral");
+ assert_eq!(**c, ColorData { as_shot_neutral: c.as_shot_neutral, as_shot_white_xy: None, ..tagged.clone() }, "nothing else changes");
+ // multipliers that aren't usable change nothing
+ for bad in [[0.0, 1.0, 1.0], [2.0, f32::NAN, 1.6], [2.0, 1.0, f32::INFINITY], [-2.0, 1.0, 1.6]] {
+ let mut color = FrameColor::Camera(Box::new(ColorData { as_shot_white_xy: Some(D50), ..Default::default() }));
+ let before = color.clone();
+ color.set_as_shot_multipliers(bad);
+ assert_eq!(color, before, "{bad:?}");
+ }
+ // linear colour has no white balance to set
+ let mut linear = Frame::from_linear_rec2020(Rgb32f::new(2, 2), None).color;
+ let before = linear.clone();
+ linear.set_as_shot_multipliers([2.0, 1.0, 1.6]);
+ assert_eq!(linear, before);
+ }
}
diff --cc crates/merge/src/output.rs
index 6cb06c966,3f2bb665e..2d0a523f4
--- a/crates/merge/src/output.rs
+++ b/crates/merge/src/output.rs
@@@ -112,9 -111,9 +112,10 @@@ pub fn write_linear_dng
color: cd,
wb_multipliers: None,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists { list3: lens.to_vec(), ..Default::default() },
metadata: meta,
+ lens_metadata: Default::default(),
};
let compression = DngCompression::Deflate { tile: 256, half: samples == DngSamples::Half };
lightcraft_raw::write_dng(&raw, &DngWriteOptions { compression, ..Default::default() }).map_err(|e| MergeError::Output(e.to_string()))
diff --cc crates/raw/examples/rawinfo.rs
index 78624a0de,701b93d73..23e540b96
--- a/crates/raw/examples/rawinfo.rs
+++ b/crates/raw/examples/rawinfo.rs
@@@ -227,7 -227,7 +227,8 @@@ mod tests
linearized: false,
opcodes: OpcodeLists::default(),
metadata: Default::default(),
+ lens_metadata: Default::default(),
+ pixel_aspect: 1.0,
};
let path = dir.path("valid.dng");
std::fs::write(&path, write_dng(&raw, &DngWriteOptions::default()).expect("encode generated fixture")).expect("write fixture");
diff --cc crates/raw/src/binned.rs
index 5eb99422b,0ad9adb3d..db725a2ec
--- a/crates/raw/src/binned.rs
+++ b/crates/raw/src/binned.rs
@@@ -163,9 -163,9 +163,10 @@@ mod tests
color: ColorData::default(),
wb_multipliers: None,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata: Metadata::default(),
+ lens_metadata: Default::default(),
}
}
diff --cc crates/raw/src/color.rs
index 12c509190,88081eebc..2caafcfde
--- a/crates/raw/src/color.rs
+++ b/crates/raw/src/color.rs
@@@ -287,9 -287,9 +287,10 @@@ mod tests
color,
wb_multipliers: None,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: Default::default(),
metadata: Default::default(),
+ lens_metadata: Default::default(),
}
}
diff --cc crates/raw/src/dng.rs
index b076c8bc3,6cc43afcc..e9fc0999d
--- a/crates/raw/src/dng.rs
+++ b/crates/raw/src/dng.rs
@@@ -338,9 -338,9 +338,10 @@@ pub(crate) fn decode_as(bytes: &[u8], m
color: color_data(ifd0, raw, tiff.order),
wb_multipliers: None,
linearized,
+ pixel_aspect: default_scale_aspect(raw.f64s(t::DEFAULT_SCALE).as_deref()),
opcodes,
metadata,
+ lens_metadata: Default::default(),
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/dngwrite.rs
index 815a33790,3e46c28c5..d7ba42776
--- a/crates/raw/src/dngwrite.rs
+++ b/crates/raw/src/dngwrite.rs
@@@ -538,9 -538,9 +538,10 @@@ pub fn write_rendered_dng
color,
wb_multipliers: None,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: crate::OpcodeLists::default(),
metadata: metadata.clone(),
+ lens_metadata: Default::default(),
};
let order = ByteOrder::Little;
write_dng(&raw, &DngWriteOptions { compression: DngCompression::Deflate { tile: 256, half: true }, order, xmp })
diff --cc crates/raw/src/lib.rs
index faa28c593,0e3cfd98e..202983331
--- a/crates/raw/src/lib.rs
+++ b/crates/raw/src/lib.rs
@@@ -432,9 -430,18 +432,21 @@@ pub struct RawInfo
pub wb_multipliers: Option<[f32; 3]>,
pub opcodes: OpcodeLists,
pub metadata: Metadata,
+ /// Lens-correction tables the file records, decoded but not necessarily applied (see [`crate::lens_metadata`]).
+ #[serde(default)]
+ pub lens_metadata: LensMetadata,
+ /// See [`RawImage::pixel_aspect`].
+ #[serde(default = "square_pixels")]
+ pub pixel_aspect: f32,
+ }
+
+ fn square_pixels() -> f32 {
+ 1.0
+ }
+
+ /// Rows of a picture `height` rows tall once its pixels (taller than wide by `aspect`) are made square.
+ pub fn stretched_height(height: usize, aspect: f32) -> usize {
+ if (aspect - 1.0).abs() < 0.01 { height } else { ((height as f32 * aspect).round() as usize).max(1) }
}
impl RawInfo {
@@@ -666,12 -675,12 +680,16 @@@ pub struct RawImage
pub wb_multipliers: Option<[f32; 3]>,
/// Whether a `LinearizationTable` was applied to `data`.
pub linearized: bool,
+ /// Height of a sensor pixel over its width (1.0 for square pixels). The D1X and D1H write 2.0: their frame is
+ /// 4028 x 1324 and the picture is 3:2 only once the rows are doubled. [`Self::develop`] stretches the rows by
+ /// this factor.
+ pub pixel_aspect: f32,
pub opcodes: OpcodeLists,
pub metadata: Metadata,
+ /// Lens-correction tables the file records, decoded but not necessarily applied (see [`crate::lens_metadata`]); only
+ /// validated components are in `opcodes`.
+ #[serde(default)]
+ pub lens_metadata: LensMetadata,
}
/// Black-subtracted, white-normalised data covering the active area.
@@@ -703,7 -712,7 +721,8 @@@ impl RawImage
wb_multipliers: self.wb_multipliers,
opcodes: self.opcodes.clone(),
metadata: self.metadata.clone(),
+ lens_metadata: self.lens_metadata.clone(),
+ pixel_aspect: self.pixel_aspect,
}
}
@@@ -723,7 -732,7 +742,8 @@@
wb_multipliers: self.wb_multipliers,
opcodes: self.opcodes,
metadata: self.metadata,
+ lens_metadata: self.lens_metadata,
+ pixel_aspect: self.pixel_aspect,
}
}
@@@ -889,9 -901,9 +912,10 @@@ mod tests
color: ColorData::default(),
wb_multipliers: None,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata: Metadata::default(),
+ lens_metadata: Default::default(),
};
let direct = raw.develop(Method::Bilinear).unwrap();
let by_hand = raw.finish_demosaiced(demosaic(&raw.normalized().unwrap(), Method::Bilinear));
@@@ -932,7 -955,7 +967,8 @@@
linearized: false,
opcodes: OpcodeLists::default(),
metadata: Metadata::default(),
+ lens_metadata: Default::default(),
+ pixel_aspect: 1.0,
};
assert!(raw.validate().is_err(), "`x + width` wrapped: the area is outside the image");
assert!(raw.normalized().is_err());
diff --cc crates/raw/src/saturation.rs
index 261d5023b,76c1cd503..16768b16d
--- a/crates/raw/src/saturation.rs
+++ b/crates/raw/src/saturation.rs
@@@ -135,9 -135,9 +135,10 @@@ mod tests
color: ColorData::default(),
wb_multipliers: None,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata: Metadata::default(),
+ lens_metadata: Default::default(),
}
}
diff --cc crates/raw/src/vendor/arw.rs
index fddf47bb9,0d6fed836..a662799ae
--- a/crates/raw/src/vendor/arw.rs
+++ b/crates/raw/src/vendor/arw.rs
@@@ -810,10 -768,29 +810,11 @@@ pub(crate) fn decode(bytes: &[u8], mode
// makes a neutral surface magenta. WB edits remain relative to this as-shot RGB.
wb_multipliers: if linear_rgb { Some([1.0; 3]) } else { wb },
linearized: false,
+ pixel_aspect: 1.0,
- // The distortion table is centred on the DNG-style default crop when the file has one: on the ILCE-7RM4A
- // it starts at x = 32 where Sony's crop tags (the image crop, unchanged) start at 0, and the warp was
- // validated against Sony's exports with that centre. The opcode's centre is in active-area coordinates, so
- // it stays on the optical centre whichever crop frames the image. The ILCE-7RM6's tables follow other
- // conventions, measured against its camera JPEGs ([`super::arw_lens_rm6`]); the two allowlists are
- // disjoint, so at most one of them produces an opcode.
- opcodes: OpcodeLists {
- // DefaultCrop can retain native geometry while Sony's tags select an aspect crop.
- // Both the image framing and the table geometry must stay within the validated 3:2 scope.
- list3: if linear_rgb || crop.height == 0 || (crop.width as f64 / crop.height as f64 - 1.5).abs() > 0.005 {
- Vec::new()
- } else {
- let geometry = dng_default_crop(raw, w, h).unwrap_or(crop);
- let active = Rect::new(0, 0, w, h);
- super::arw_lens::distortion(&model, raw, active, geometry)
- .into_iter()
- .chain(super::arw_lens_rm6::lens_opcodes(raw, &model, active, geometry))
- .collect()
- },
- ..Default::default()
- },
+ // only validated components become opcodes (see `arw_lens`, and `arw_lens_rm6` for the ILCE-7RM6)
+ opcodes: OpcodeLists { list3: warp.into_iter().chain(rm6).collect(), ..Default::default() },
metadata,
+ lens_metadata: LensMetadata { sony: sony_lens },
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/vendor/cr2.rs
index f6bc1798a,6c797ec4a..3fe173e09
--- a/crates/raw/src/vendor/cr2.rs
+++ b/crates/raw/src/vendor/cr2.rs
@@@ -249,9 -249,9 +249,10 @@@ pub(crate) fn decode(bytes: &[u8], mode
color: ColorData::default(),
wb_multipliers: wb,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata,
+ lens_metadata: Default::default(),
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/vendor/cr2/sraw.rs
index e1b2988e9,f8ef18e32..6a37d627e
--- a/crates/raw/src/vendor/cr2/sraw.rs
+++ b/crates/raw/src/vendor/cr2/sraw.rs
@@@ -391,9 -391,9 +391,10 @@@ pub(super) fn decode(bytes: &[u8], tiff
// the camera's white balance is already applied, as in Sony's linear YCbCr
wb_multipliers: Some([1.0; 3]),
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata,
+ lens_metadata: Default::default(),
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/vendor/cr3.rs
index 2c37c99d3,b4c3d2d54..3df33424d
--- a/crates/raw/src/vendor/cr3.rs
+++ b/crates/raw/src/vendor/cr3.rs
@@@ -253,9 -253,9 +253,10 @@@ pub(crate) fn decode(bytes: &[u8], mode
color: ColorData::default(),
wb_multipliers: levels.wb,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata,
+ lens_metadata: Default::default(),
};
image.validate_for(mode)?;
Ok(image)
diff --cc crates/raw/src/vendor/erf.rs
index 7f98ca121,c585ad0cc..b9861ef38
--- a/crates/raw/src/vendor/erf.rs
+++ b/crates/raw/src/vendor/erf.rs
@@@ -123,7 -123,7 +123,8 @@@ pub(crate) fn decode(bytes: &[u8], mode
linearized: false,
opcodes: OpcodeLists::default(),
metadata,
+ lens_metadata: Default::default(),
+ pixel_aspect: 1.0,
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/vendor/kodak.rs
index 61c238740,def2e369d..1e972873e
--- a/crates/raw/src/vendor/kodak.rs
+++ b/crates/raw/src/vendor/kodak.rs
@@@ -115,9 -115,9 +115,10 @@@ pub(crate) fn decode(bytes: &[u8], mode
color: ColorData::default(),
wb_multipliers: wb,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata,
+ lens_metadata: Default::default(),
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/vendor/mef.rs
index a879f59f7,8298bd4f0..207bc8871
--- a/crates/raw/src/vendor/mef.rs
+++ b/crates/raw/src/vendor/mef.rs
@@@ -121,7 -121,7 +121,8 @@@ pub(crate) fn decode(bytes: &[u8], mode
linearized: false,
opcodes: OpcodeLists::default(),
metadata,
+ lens_metadata: Default::default(),
+ pixel_aspect: 1.0,
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/vendor/nef.rs
index f73183072,20a917000..5076c6df9
--- a/crates/raw/src/vendor/nef.rs
+++ b/crates/raw/src/vendor/nef.rs
@@@ -175,6 -188,31 +175,16 @@@ fn measured_body(model: &str, w: usize
MEASURED_BODIES.iter().find(|b| b.model == model.trim() && b.size == (w, h) && b.bits == bits)
}
-/// The samples of the image area `crop`, every few rows (an odd step, so both rows of the CFA are in), for measuring
-/// the white level where the picture is.
-fn area_samples(d: &[u16], w: usize, crop: Rect) -> Vec<u16> {
- let step = (crop.width.saturating_mul(crop.height) / 2_000_000).max(1) | 1;
- (crop.y..crop.y.saturating_add(crop.height))
- .step_by(step)
- .filter_map(|y| {
- let at = y.checked_mul(w)?.checked_add(crop.x)?;
- d.get(at..at.checked_add(crop.width)?)
- })
- .flatten()
- .copied()
- .collect()
-}
-
+ /// Pixel height over width from the raw IFD's resolution tags: the D1X and D1H write 600 x 300 per inch for a
+ /// 4028 x 1324 frame, rows twice as tall as the columns are wide. Only ratios from 1.5 to 4 count (a frame
+ /// squashed 3:1 or 2:1), the rest is rounding or a stray tag.
+ pub(crate) fn pixel_aspect_of(raw: &Ifd) -> f32 {
+ match (raw.f64(t::X_RESOLUTION), raw.f64(t::Y_RESOLUTION)) {
+ (Some(x), Some(y)) if x > 0.0 && y > 0.0 && (1.5..=4.0).contains(&(x / y)) => (x / y).round() as f32,
+ _ => 1.0,
+ }
+ }
+
fn raw_ifd(tiff: &Tiff) -> Option<&Ifd> {
tiff.all_ifds()
.into_iter()
@@@ -257,20 -296,23 +267,21 @@@ pub(crate) fn decode(bytes: &[u8], mode
let model = ifd0.string(t::MODEL).unwrap_or_default();
let body = measured_body(&model, w, h, bits);
let active_w = trailing_masked_columns(samples, w, bits);
- let measured = match body {
- Some(&MeasuredBody { crop: (x, y, cw, ch), .. }) if x + cw <= active_w && y + ch <= h => Some(Rect::new(x, y, cw, ch)),
- _ => None,
+ // the white level is measured from the whole image (or known for a measured body); the probe (no samples in its
+ // result) takes the nominal one
+ let white = match (body.and_then(|b| b.white), mode) {
+ (Some(white), _) => white,
+ (None, Mode::Full) => white_from_data(samples, bits),
+ (None, Mode::Header) => ((1u32 << bits.clamp(1, 16)) - 1) as f32,
};
- let crop = measured.unwrap_or_else(|| default_crop(mn.as_ref(), active_w, h));
- // the white level is measured from the whole image (only its measured area, for a body with one), or known for a
- // measured body; the probe (no samples in its result) takes the nominal one
- let white = match (body.and_then(|b| b.white), mode, measured) {
- (Some(white), _, _) => white,
- (None, Mode::Full, Some(area)) => white_from_data(&area_samples(samples, w, area), bits),
- (None, Mode::Full, None) => white_from_data(samples, bits),
- (None, Mode::Header, _) => ((1u32 << bits.clamp(1, 16)) - 1) as f32,
+ let crop = match body {
+ Some(&MeasuredBody { crop: (x, y, cw, ch), .. }) if x + cw <= active_w && y + ch <= h => Rect::new(x, y, cw, ch),
+ _ => default_crop(mn.as_ref(), active_w, h),
};
+ let pixel_aspect = pixel_aspect_of(raw);
let mut metadata = lightcraft_meta::from_tiff(&tiff);
metadata.width = Some(crop.width as u32);
- metadata.height = Some(crop.height as u32);
+ metadata.height = Some(crate::stretched_height(crop.height, pixel_aspect) as u32);
let img = RawImage {
format: RawFormat::Nef,
width: w,
diff --cc crates/raw/src/vendor/orf.rs
index 6672c8655,df8498459..92caeec26
--- a/crates/raw/src/vendor/orf.rs
+++ b/crates/raw/src/vendor/orf.rs
@@@ -334,9 -334,9 +334,10 @@@ pub(crate) fn decode(bytes: &[u8], mode
color: ColorData::default(),
wb_multipliers: wb,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata,
+ lens_metadata: Default::default(),
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/vendor/pef.rs
index de805d688,d36c92462..9b7d92bc7
--- a/crates/raw/src/vendor/pef.rs
+++ b/crates/raw/src/vendor/pef.rs
@@@ -335,9 -335,9 +335,10 @@@ pub(crate) fn decode(bytes: &[u8], mode
color: ColorData::default(),
wb_multipliers: wb,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata,
+ lens_metadata: Default::default(),
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/vendor/raf.rs
index 0ff69cd19,b45cf4902..42ed29ce8
--- a/crates/raw/src/vendor/raf.rs
+++ b/crates/raw/src/vendor/raf.rs
@@@ -212,9 -212,9 +212,10 @@@ pub(crate) fn decode(bytes: &[u8], mode
color: ColorData::default(),
wb_multipliers: wb,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists { list3: super::raf_lens::corrections(&camera_name(bytes), &ifd, active), ..Default::default() },
metadata,
+ lens_metadata: Default::default(),
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/vendor/rw2.rs
index d883bd3d1,eff18e0bc..7ebee5916
--- a/crates/raw/src/vendor/rw2.rs
+++ b/crates/raw/src/vendor/rw2.rs
@@@ -614,9 -614,9 +614,10 @@@ pub(crate) fn decode(bytes: &[u8], mode
color: ColorData::default(),
wb_multipliers: wb,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists { list3: distortion(ifd0, active).into_iter().collect(), ..Default::default() },
metadata,
+ lens_metadata: Default::default(),
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/vendor/srw.rs
index 7d5a70aaf,a48f944b4..b22faeec0
--- a/crates/raw/src/vendor/srw.rs
+++ b/crates/raw/src/vendor/srw.rs
@@@ -184,9 -184,9 +184,10 @@@ pub(crate) fn decode(bytes: &[u8], mode
color: ColorData::default(),
wb_multipliers: wb,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata,
+ lens_metadata: Default::default(),
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/tests/dng.rs
index 921e8900a,10004e0f1..dddafd5d0
--- a/crates/raw/tests/dng.rs
+++ b/crates/raw/tests/dng.rs
@@@ -59,9 -59,9 +59,10 @@@ fn synthetic(w: usize, h: usize, cfa: O
color,
wb_multipliers: None,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata,
+ lens_metadata: Default::default(),
}
}
diff --cc crates/raw/tests/lossy_dng.rs
index d2022b382,818f02c0a..3b7480ff2
--- a/crates/raw/tests/lossy_dng.rs
+++ b/crates/raw/tests/lossy_dng.rs
@@@ -37,7 -37,7 +37,8 @@@ fn mosaic(w: usize, h: usize) -> RawIma
linearized: false,
opcodes: OpcodeLists::default(),
metadata: Default::default(),
+ lens_metadata: Default::default(),
+ pixel_aspect: 1.0,
}
}
diff --cc crates/raw/tests/robust.rs
index e08c86474,a54cb0238..323149fda
--- a/crates/raw/tests/robust.rs
+++ b/crates/raw/tests/robust.rs
@@@ -24,9 -24,9 +24,10 @@@ fn sample(comp: DngCompression) -> Vec<
color: ColorData::default(),
wb_multipliers: None,
linearized: false,
+ pixel_aspect: 1.0,
opcodes: OpcodeLists::default(),
metadata: Default::default(),
+ lens_metadata: Default::default(),
};
lightcraft_raw::write_dng(&raw, &DngWriteOptions { compression: comp, ..Default::default() }).unwrap()
}
@@@ -164,7 -164,7 +165,8 @@@ fn non_finite_black_levels_and_samples_
linearized: false,
opcodes: OpcodeLists::default(),
metadata: Default::default(),
+ lens_metadata: Default::default(),
+ pixel_aspect: 1.0,
};
for img in [raw.clone(), lightcraft_raw::decode(&lightcraft_raw::write_dng(&raw, &DngWriteOptions::default()).unwrap()).unwrap()] {
let rgb = img.develop(Method::Ahd).unwrap();