diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs new file mode 100644 index 000000000..6d8e6d958 --- /dev/null +++ b/crates/raw/src/dng_metadata.rs @@ -0,0 +1,211 @@ +//! Bounded DNG processing and camera metadata, before application rendering. +//! +//! This is the [`crate::dng_lines`] companion on the same `craft_tiff::classic` IFDs. The full +//! reader's `dng::color_data` works on `craft_tiff::Tiff` and returns the values it will render +//! with (raw IFD first, implausible or singular values dropped, `BaselineExposureOffset` added). +//! This reader returns the tags as written, IFD 0 first, and fails on a value outside the file, +//! so a damaged file does not silently lose its white balance. For a well-formed file both read the +//! same matrices and neutral (see `tests/it/dng_metadata.rs`). +use crate::dng_lines::{self, Error, PHOTOMETRIC_CFA, Result}; +use craft_tiff::classic::{Entry, Ifd, Tiff, tags}; +type M3 = [[f64; 3]; 3]; +type V3 = [f64; 3]; +/// Camera-plane colour filter pattern. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Pattern { + pub rows: usize, + pub cols: usize, + pub colors: Vec, +} +/// Parsed image processing tags. +#[derive(Clone, Debug, Default)] +pub struct Params { + pub active_area: Option<[usize; 4]>, + pub linearization: Option>, + pub black_repeat: (usize, usize), + pub black: Vec, + pub black_delta_h: Vec, + pub black_delta_v: Vec, + pub white: Vec, + pub crop_origin: Option<(f64, f64)>, + pub crop_size: Option<(f64, f64)>, + pub cfa: Option, +} +/// Parsed camera colour calibration. +#[derive(Clone, Debug, Default)] +pub struct Calibration { + pub color_matrix: [Option; 2], + pub camera_calibration: [Option; 2], + pub forward_matrix: [Option; 2], + pub illuminant: [Option; 2], + pub analog_balance: Option, + pub as_shot_neutral: Option, + pub as_shot_white_xy: Option<(f64, f64)>, + pub baseline_exposure: f64, +} +/// Metadata reader with the file's main and raw IFDs. +pub struct Metadata<'a, 'i> { + pub tiff: Tiff<'a>, + pub main: &'i Ifd, + pub raw: &'i Ifd, +} +impl Metadata<'_, '_> { + /// A raw-image tag: from the raw IFD, else IFD 0. + fn raw_tag(&self, tag: u16) -> Option<&Entry> { + self.raw.get(tag).or_else(|| self.main.get(tag)) + } + + /// A camera-wide tag: from IFD 0, else the raw IFD. + fn main_tag(&self, tag: u16) -> Option<&Entry> { + self.main.get(tag).or_else(|| self.raw.get(tag)) + } + + /// A numeric tag's values; a value outside the file (or a zero denominator) is an error, so a + /// damaged file does not silently lose its white balance. + fn floats(&self, e: Option<&Entry>) -> Result>> { + Ok(e.map(|e| self.tiff.floats(e)).transpose()?) + } + + /// A 3 × 3 matrix tag (`None` when absent or not 3 × 3, e.g. for other colour counts). + fn matrix(&self, tag: u16) -> Result> { + let Some(v) = self.floats(self.main_tag(tag))? else { return Ok(None) }; + let Ok(v) = <[f64; 9]>::try_from(v.as_slice()) else { return Ok(None) }; + Ok(Some([[v[0], v[1], v[2]], [v[3], v[4], v[5]], [v[6], v[7], v[8]]])) + } + + fn vec3(&self, tag: u16) -> Result> { + Ok(self.floats(self.main_tag(tag))?.and_then(|v| <[f64; 3]>::try_from(v.as_slice()).ok())) + } + + /// The CFA pattern in camera planes (`None` for LinearRaw). + pub fn cfa(&self) -> Result> { + let l = dng_lines::layout(&self.tiff, self.raw)?; + if l.photometric != PHOTOMETRIC_CFA { + return Ok(None); + } + let dims = self.raw_tag(tags::CFA_REPEAT_PATTERN_DIM).map(|e| self.tiff.uints(e)).transpose()?.unwrap_or_else(|| vec![2, 2]); + let (rows, cols) = match dims[..] { + [r, c, ..] => (r as usize, c as usize), + _ => return Err(Error::Malformed("CFARepeatPatternDim")), + }; + if !(1..=8).contains(&rows) || !(1..=8).contains(&cols) { + return Err(Error::Unsupported(format!("{rows} × {cols} CFA pattern"))); + } + let pattern = self.tiff.uints(self.raw_tag(tags::CFA_PATTERN).ok_or(Error::Malformed("no CFAPattern"))?)?; + if pattern.len() < rows * cols { + return Err(Error::Malformed("CFAPattern shorter than its repeat")); + } + let planes = match self.raw_tag(tags::CFA_PLANE_COLOR) { + Some(e) => self.tiff.uints(e)?, + None => vec![0, 1, 2], + }; + let mut sorted = planes.clone(); + sorted.sort_unstable(); + if sorted != [0, 1, 2] { + return Err(Error::Unsupported("CFA colours other than red, green and blue".into())); + } + if let Some(e) = self.raw_tag(tags::CFA_LAYOUT) + && self.tiff.uint(e)? != 1 + { + return Err(Error::Unsupported("a non-rectangular CFA layout".into())); + } + let colors = pattern[..rows * cols] + .iter() + .map(|&v| planes.iter().position(|&p| p == v).map(|i| i as u8).ok_or(Error::Malformed("CFAPattern colour not in CFAPlaneColor"))) + .collect::>>()?; + Ok(Some(Pattern { rows, cols, colors })) + } + + pub fn params(&self) -> Result { + let uint_pair = |tag| -> Result> { + match self.raw_tag(tag) { + Some(e) => match self.tiff.uints(e)?[..] { + [a, b, ..] => Ok(Some((a as usize, b as usize))), + _ => Err(Error::Malformed("short field")), + }, + None => Ok(None), + } + }; + let active_area = match self.raw_tag(tags::ACTIVE_AREA) { + Some(e) => match self.tiff.uints(e)?[..] { + [t, l, b, r, ..] => Some([t, l, b, r].map(|v| usize::try_from(v).unwrap_or(usize::MAX))), + _ => return Err(Error::Malformed("ActiveArea")), + }, + None => None, + }; + let linearization = match self.raw_tag(tags::LINEARIZATION_TABLE) { + Some(e) => { + // checked before `uints` allocates a u64 per entry + if e.count > 65_536 { + return Err(Error::Malformed("LinearizationTable longer than 65536")); + } + let v = self.tiff.uints(e)?; + Some(v.into_iter().map(|x| x.min(65_535) as u16).collect()) + } + None => None, + }; + let pair = |tag| -> Result> { + match self.raw_tag(tag) { + Some(e) => match self.tiff.floats(e)?[..] { + [a, b, ..] => Ok(Some((a, b))), + _ => Err(Error::Malformed("short field")), + }, + None => Ok(None), + } + }; + let list = |tag| -> Result> { Ok(self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose()?.unwrap_or_default()) }; + Ok(Params { + active_area, + linearization, + black_repeat: uint_pair(tags::BLACK_LEVEL_REPEAT_DIM)?.unwrap_or((1, 1)), + black: list(tags::BLACK_LEVEL)?, + black_delta_h: list(tags::BLACK_LEVEL_DELTA_H)?, + black_delta_v: list(tags::BLACK_LEVEL_DELTA_V)?, + white: list(tags::WHITE_LEVEL)?, + crop_origin: pair(tags::DEFAULT_CROP_ORIGIN)?, + crop_size: pair(tags::DEFAULT_CROP_SIZE)?, + cfa: self.cfa()?, + }) + } + + /// The colour tags. + pub fn calibration(&self) -> Result { + let illum = |tag| self.main_tag(tag).map(|e| self.tiff.uint(e)).transpose(); + let white_xy = self.floats(self.main_tag(tags::AS_SHOT_WHITE_XY))?.and_then(|v| match v[..] { + [x, y] => Some((x, y)), + _ => None, + }); + Ok(Calibration { + color_matrix: [self.matrix(tags::COLOR_MATRIX_1)?, self.matrix(tags::COLOR_MATRIX_2)?], + camera_calibration: [self.matrix(tags::CAMERA_CALIBRATION_1)?, self.matrix(tags::CAMERA_CALIBRATION_2)?], + forward_matrix: [self.matrix(tags::FORWARD_MATRIX_1)?, self.matrix(tags::FORWARD_MATRIX_2)?], + illuminant: [illum(tags::CALIBRATION_ILLUMINANT_1)?, illum(tags::CALIBRATION_ILLUMINANT_2)?], + analog_balance: self.vec3(tags::ANALOG_BALANCE)?, + as_shot_neutral: self.vec3(tags::AS_SHOT_NEUTRAL)?, + as_shot_white_xy: white_xy, + baseline_exposure: self.floats(self.main_tag(tags::BASELINE_EXPOSURE))?.and_then(|v| v.first().copied()).unwrap_or(0.0), + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn calibration_defaults_and_rejects_bad_rational() -> Result<()> { + let empty = Ifd::default(); + let tiff = Tiff { data: &[], big_endian: false }; + let metadata = Metadata { tiff, main: &empty, raw: &empty }; + let calibration = metadata.calibration()?; + assert_eq!(calibration.baseline_exposure, 0.0); + assert_eq!(calibration.color_matrix, [None, None]); + + // IFD with one BaselineExposure SRational pointing past the end of the file. + let data = [b'I', b'I', 42, 0, 8, 0, 0, 0, 1, 0, 42, 198, 10, 0, 1, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 0]; + let (tiff, first) = Tiff::new(&data)?; + let (ifd, _) = tiff.read_ifd(first, 0)?; + assert!(Metadata { tiff, main: &ifd, raw: &ifd }.calibration().is_err()); + Ok(()) + } +} diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index 3187ebdd2..dce9b75e1 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -96,6 +96,9 @@ mod arw_lens_rm6; /// Bounded DNG strip and tile sample reader. #[cfg(feature = "line-dng")] pub mod dng_lines; +/// Bounded DNG camera and processing metadata. +#[cfg(feature = "line-dng")] +pub mod dng_metadata; #[allow(dead_code)] mod huffman; #[cfg(feature = "jxl")] diff --git a/crates/raw/tests/it/dng.rs b/crates/raw/tests/it/dng.rs index 450b8ea96..21074a828 100644 --- a/crates/raw/tests/it/dng.rs +++ b/crates/raw/tests/it/dng.rs @@ -214,7 +214,7 @@ pub(super) fn dng(raw: IfdBuilder, order: ByteOrder) -> Vec { } /// [`dng`] with extra IFD 0 tags (where DNG writers put camera-profile tags). -fn dng_with(raw: IfdBuilder, order: ByteOrder, extra: impl FnOnce(&mut IfdBuilder)) -> Vec { +pub(super) fn dng_with(raw: IfdBuilder, order: ByteOrder, extra: impl FnOnce(&mut IfdBuilder)) -> Vec { let mut ifd0 = IfdBuilder::new(); ifd0.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![1])); ifd0.set(t::DNG_VERSION, Value::Byte(vec![1, 6, 0, 0])); diff --git a/crates/raw/tests/it/dng_metadata.rs b/crates/raw/tests/it/dng_metadata.rs new file mode 100644 index 000000000..2aa0e6d80 --- /dev/null +++ b/crates/raw/tests/it/dng_metadata.rs @@ -0,0 +1,119 @@ +//! The bounded DNG metadata reader (`dng_metadata`) on hand-built DNGs, checked against the tags +//! written and, for the colour tags, against the full reader. + +use super::dng::{base_ifd, dng, dng_with}; +use craft_raw::dng_lines::{self, Error}; +use craft_raw::dng_metadata::{Metadata, Pattern}; +use craft_tiff::classic::{Tiff, tags as ct}; +use craft_tiff::{ByteOrder, IfdBuilder, ImageData, Value}; + +fn cfa_raw(edit: impl FnOnce(&mut IfdBuilder)) -> IfdBuilder { + let mut raw = base_ifd(8, 4, 16, 1, true); + raw.set(ct::COMPRESSION, Value::Short(vec![1])); + raw.set_image(ImageData::Strips { rows_per_strip: 4, strips: vec![vec![0; 64]] }); + edit(&mut raw); + raw +} + +fn with_metadata(bytes: &[u8], f: impl FnOnce(&Metadata) -> T) -> T { + let (tiff, first) = Tiff::new(bytes).unwrap(); + let ifds = tiff.all_ifds(first).unwrap(); + let raw = dng_lines::find_raw_ifd(&tiff, &ifds).unwrap(); + f(&Metadata { tiff, main: &ifds[0], raw }) +} + +#[test] +fn processing_tags_are_read_from_the_raw_ifd() { + let raw = cfa_raw(|r| { + r.set(ct::ACTIVE_AREA, Value::Long(vec![1, 2, 3, 7])); + r.set(ct::LINEARIZATION_TABLE, Value::Short(vec![0, 10, 20])); + r.set(ct::BLACK_LEVEL_REPEAT_DIM, Value::Short(vec![2, 2])); + r.set(ct::BLACK_LEVEL, Value::Rational(vec![(64, 1), (65, 1), (66, 1), (67, 1)])); + r.set(ct::BLACK_LEVEL_DELTA_H, Value::SRational(vec![(1, 2); 8])); + r.set(ct::BLACK_LEVEL_DELTA_V, Value::SRational(vec![(-1, 2); 4])); + r.set(ct::WHITE_LEVEL, Value::Long(vec![4000])); + r.set(ct::DEFAULT_CROP_ORIGIN, Value::Rational(vec![(1, 2), (1, 1)])); + r.set(ct::DEFAULT_CROP_SIZE, Value::Long(vec![6, 2])); + }); + for order in [ByteOrder::Little, ByteOrder::Big] { + let p = with_metadata(&dng(raw.clone(), order), |m| m.params().unwrap()); + assert_eq!(p.active_area, Some([1, 2, 3, 7])); + assert_eq!(p.linearization, Some(vec![0, 10, 20])); + assert_eq!(p.black_repeat, (2, 2)); + assert_eq!(p.black, [64.0, 65.0, 66.0, 67.0]); + assert_eq!(p.black_delta_h, [0.5; 8]); + assert_eq!(p.black_delta_v, [-0.5; 4]); + assert_eq!(p.white, [4000.0]); + assert_eq!((p.crop_origin, p.crop_size), (Some((0.5, 1.0)), Some((6.0, 2.0)))); + // base_ifd writes CFAPattern 1 0 2 1 (GRBG) + assert_eq!(p.cfa, Some(Pattern { rows: 2, cols: 2, colors: vec![1, 0, 2, 1] })); + } + // a LinearizationTable longer than 65536 entries is refused + let long = cfa_raw(|r| { + r.set(ct::LINEARIZATION_TABLE, Value::Short(vec![1; 65_537])); + }); + assert!(matches!(with_metadata(&dng(long, ByteOrder::Little), |m| m.params()), Err(Error::Malformed(_)))); + // absent tags take the DNG defaults + let p = with_metadata(&dng(cfa_raw(|_| {}), ByteOrder::Little), |m| m.params().unwrap()); + assert_eq!((p.active_area, p.linearization, p.black_repeat), (None, None, (1, 1))); + assert!(p.black.is_empty() && p.white.is_empty() && p.crop_origin.is_none()); +} + +#[test] +fn cfa_patterns_map_plane_colours_and_refuse_odd_layouts() { + let pattern = |edit: fn(&mut IfdBuilder)| with_metadata(&dng(cfa_raw(edit), ByteOrder::Little), |m| m.cfa()); + // CFAPlaneColor reorders the planes: file colour 2 is plane 0 + let swapped = pattern(|r| { + r.set(ct::CFA_PLANE_COLOR, Value::Byte(vec![2, 1, 0])); + r.set(ct::CFA_LAYOUT, Value::Short(vec![1])); + }); + assert_eq!(swapped.unwrap(), Some(Pattern { rows: 2, cols: 2, colors: vec![1, 2, 0, 1] })); + let unsupported = |r: Result, Error>| matches!(r, Err(Error::Unsupported(_))); + let malformed = |r: Result, Error>| matches!(r, Err(Error::Malformed(_))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_LAYOUT, Value::Short(vec![2])); + }))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_PLANE_COLOR, Value::Byte(vec![0, 1, 3])); + }))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![9, 2])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![4, 4])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_PATTERN, Value::Byte(vec![0, 1, 1, 3])); + }))); + // LinearRaw has no pattern + let mut linear = base_ifd(4, 2, 16, 3, false); + linear.set(ct::COMPRESSION, Value::Short(vec![1])); + linear.set_image(ImageData::Strips { rows_per_strip: 2, strips: vec![vec![0; 48]] }); + assert_eq!(with_metadata(&dng(linear, ByteOrder::Big), |m| m.cfa()).unwrap(), None); +} + +#[test] +fn calibration_matches_the_full_reader() { + let bytes = dng_with(cfa_raw(|_| {}), ByteOrder::Little, |ifd0| { + ifd0.set(ct::COLOR_MATRIX_2, Value::SRational(vec![(2, 1), (0, 1), (0, 1), (0, 1), (2, 1), (0, 1), (0, 1), (0, 1), (2, 1)])); + ifd0.set(ct::CALIBRATION_ILLUMINANT_2, Value::Short(vec![17])); + ifd0.set(ct::ANALOG_BALANCE, Value::Rational(vec![(1, 1); 3])); + ifd0.set(ct::AS_SHOT_NEUTRAL, Value::Rational(vec![(1, 2), (1, 1), (3, 4)])); + ifd0.set(ct::BASELINE_EXPOSURE, Value::SRational(vec![(1, 4)])); + // not 3 x 3 (a four-colour camera): not a matrix here + ifd0.set(ct::FORWARD_MATRIX_1, Value::SRational(vec![(1, 1); 12])); + }); + let c = with_metadata(&bytes, |m| m.calibration().unwrap()); + let full = craft_raw::decode(&bytes).unwrap().color; + assert_eq!(c.color_matrix, full.color_matrix); + assert_eq!(c.illuminant, [Some(21), Some(17)]); + assert_eq!(c.as_shot_neutral, full.as_shot_neutral); + assert_eq!(c.analog_balance, Some([1.0; 3])); + assert_eq!(c.as_shot_white_xy, Some((0.3127, 0.329))); + assert_eq!(c.baseline_exposure, 0.25); + assert_eq!(c.forward_matrix, [None, None]); + assert_eq!(c.camera_calibration, [None, None]); +} diff --git a/crates/raw/tests/it/main.rs b/crates/raw/tests/it/main.rs index 2f1b1061f..5a9386eb0 100644 --- a/crates/raw/tests/it/main.rs +++ b/crates/raw/tests/it/main.rs @@ -4,6 +4,8 @@ mod dng; #[cfg(feature = "line-dng")] mod dng_lines; +#[cfg(feature = "line-dng")] +mod dng_metadata; #[cfg(feature = "dng")] mod robust; mod vendor_robust;