diff --git a/crates/raw/src/dcp.rs b/crates/raw/src/dcp.rs index 3698708fe..1775dc14b 100644 --- a/crates/raw/src/dcp.rs +++ b/crates/raw/src/dcp.rs @@ -96,8 +96,17 @@ pub fn write_dcp(p: &DcpProfile) -> Result, DcpError> { use craft_tiff::writer::srational; use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; let c = &p.color; - // ColorMatrix1 is required: do not emit a profile that our reader must reject. - if c.color_matrix[0].is_none() { + // ColorMatrix1 must remain usable after TIFF rational rounding, or our reader rejects it. + let first = c.color_matrix[0].as_ref().ok_or(DcpError::NoMatrix)?; + let serialized = first + .iter() + .flatten() + .map(|v| { + let (n, d) = srational(*v); + f64::from(n) / f64::from(d) + }) + .collect(); + if first.iter().flatten().any(|v| !v.is_finite()) || crate::dng::mat3(Some(serialized)).is_none() { return Err(DcpError::NoMatrix); } let mut ifd = IfdBuilder::new(); @@ -167,6 +176,21 @@ mod tests { assert_eq!(back.color.color_matrix, p.color.color_matrix); } + #[test] + fn writing_rejects_unusable_or_rounding_singular_first_matrices() { + let identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]; + for matrix in [ + [[0.0; 3]; 3], + [[1.0, 1.0, 0.0], [1.0, 1.0 + 1e-8, 0.0], [0.0, 0.0, 1.0]], + [[f64::NAN, 0.0, 0.0], identity[1], identity[2]], + [[f64::INFINITY, 0.0, 0.0], identity[1], identity[2]], + ] { + let mut p = DcpProfile { name: "test".into(), camera_model: "test".into(), color: ColorData::default() }; + p.color.color_matrix[0] = Some(matrix); + assert_eq!(write_dcp(&p), Err(DcpError::NoMatrix)); + } + } + #[test] fn reading_a_profile_stops_past_the_size_limit() { let dir = std::env::temp_dir().join(format!("lc-read-dcp-{}", std::process::id())); diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index aaa08e49a..238295724 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -91,7 +91,7 @@ fn permute usize>(v: &mut Vec, w: u } /// A 3×3 matrix tag: nine finite values that can be inverted. -fn mat3(v: Option>) -> Option { +pub(crate) fn mat3(v: Option>) -> Option { let v = v?; if v.len() != 9 || v.iter().any(|x| !x.is_finite()) { return None;