Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Absolute colorimetric refuses a zero or non-finite media white (#972) (#1125)

wstock4860cef7037e
diff --git a/crates/cms/README.md b/crates/cms/README.md
index 022d9df95..6ae6a76cf 100644
--- a/crates/cms/README.md
+++ b/crates/cms/README.md
@@ -37,7 +37,8 @@ t.eval(&[1.0, 0.0, 0.0], &mut out); // exact single colou
analytic chains (e.g. Lab ↔ RGB) are evaluated exactly. Buffers ≥ 16k pixels run on rayon
(native only).
* Rendering intents: AToB0/1/2 and BToA0/1/2 by intent with AToB0/BToA0 fallback; absolute
- colorimetric scales by media white (`wtpt`; v2 display profiles treated as D50); BPC uses
+ colorimetric scales by media white (`wtpt`; v2 display profiles treated as D50; a zero,
+ negative or non-finite white is refused with `CmsError::Invalid`); BPC uses
neutral black points (Y only) mapped linearly in XYZ.
## Built-in profiles (all CC0-1.0, generated by this crate)
diff --git a/crates/cms/src/transform.rs b/crates/cms/src/transform.rs
index 27fb12cd3..c9880bbc4 100644
--- a/crates/cms/src/transform.rs
+++ b/crates/cms/src/transform.rs
@@ -584,6 +584,13 @@ type Affine3 = ([f64; 3], [f64; 3]);
fn connection(src: &Profile, dst: &Profile, intent: Intent, bpc: bool) -> Result<Option<Affine3>, CmsError> {
if intent == Intent::AbsoluteColorimetric {
let (ws, wd) = (src.media_white(), dst.media_white());
+ // `wtpt` comes straight from the file. A zero, negative or non-finite component would
+ // make the scale NaN or infinite and poison every colour the transform produces, so a
+ // profile with such a media white is refused for this intent instead.
+ let usable = |w: &[f64; 3]| w.iter().all(|v| v.is_finite() && *v > 0.0);
+ if !usable(&ws) || !usable(&wd) {
+ return Err(CmsError::Invalid(format!("absolute colorimetric needs a positive media white point (source {ws:?}, destination {wd:?})")));
+ }
let s = [ws[0] / wd[0], ws[1] / wd[1], ws[2] / wd[2]];
if s.iter().all(|v| (v - 1.0).abs() < 1e-6) {
return Ok(None);
diff --git a/crates/cms/tests/cms.rs b/crates/cms/tests/cms.rs
index 81f9d0b67..ffd318447 100644
--- a/crates/cms/tests/cms.rs
+++ b/crates/cms/tests/cms.rs
@@ -224,6 +224,66 @@ fn intents_differ_as_expected() {
assert!(delta_e76(l, [95.0, 0.0, -2.0]) < 0.6, "absolute paper {l:?}");
}
+/// `Builtin::Srgb`'s bytes (a v4 display profile) with the three `wtpt` values replaced.
+fn srgb_with_wtpt(xyz: [i32; 3]) -> Profile {
+ let mut b = Builtin::Srgb.profile().to_bytes().to_vec();
+ let be32 = |b: &[u8], at: usize| u32::from_be_bytes([b[at], b[at + 1], b[at + 2], b[at + 3]]) as usize;
+ let count = be32(&b, 128);
+ let rec = (0..count).map(|i| 132 + 12 * i).find(|&r| &b[r..r + 4] == b"wtpt").expect("sRGB has a wtpt tag");
+ let off = be32(&b, rec + 4);
+ assert_eq!(&b[off..off + 4], b"XYZ ");
+ for (i, v) in xyz.iter().enumerate() {
+ b[off + 8 + 4 * i..off + 12 + 4 * i].copy_from_slice(&v.to_be_bytes());
+ }
+ Profile::parse(&b).expect("still a valid profile")
+}
+
+/// A malformed `wtpt` (zero or negative media white) parses, and used to turn the absolute
+/// colorimetric media-white scale into NaN, so every colour through the transform (and through
+/// a paper-simulating proof built from it) came out NaN. It is now refused when the transform
+/// is built; the other intents, which never divide by the white, still work.
+#[test]
+fn absolute_colorimetric_refuses_a_degenerate_media_white() {
+ let mut zero = srgb().clone();
+ zero.white_point = [0.0; 3];
+ assert_eq!(zero.media_white(), [0.0; 3]);
+ let parsed_zero = srgb_with_wtpt([0, 0, 0]);
+ assert_eq!(parsed_zero.media_white(), [0.0; 3], "a v4 display profile's wtpt is used verbatim");
+ let negative = srgb_with_wtpt([-0x10000, 0x10000, 0x10000]);
+ let mut nan = srgb().clone();
+ nan.white_point = [f64::NAN, 1.0, 1.0];
+ for (name, bad) in [("zero", &zero), ("parsed zero", &parsed_zero), ("negative", &negative), ("nan", &nan)] {
+ for (src, dst) in [(srgb(), bad), (bad, srgb())] {
+ match Transform::new(src, dst, Intent::AbsoluteColorimetric, false) {
+ Err(photocraft_cms::CmsError::Invalid(m)) => assert!(m.contains("media white"), "{name}: {m}"),
+ Err(e) => panic!("{name}: wrong error {e}"),
+ Ok(t) => {
+ let mut out = [0.0f32; 3];
+ t.eval(&[0.2, 0.4, 0.6], &mut out);
+ panic!("{name}: built a transform that gives {out:?}");
+ }
+ }
+ }
+ // Proof Colors with Simulate Paper Color proofs through the absolute intent.
+ assert!(Transform::proof(srgb(), bad, srgb(), Intent::AbsoluteColorimetric, false, true).is_err(), "{name}: paper proof");
+ assert!(Transform::proof(srgb(), bad, srgb(), Intent::RelativeColorimetric, true, true).is_err(), "{name}: paper proof, relative first leg");
+ // Without paper simulation the proof leg is relative colorimetric and stays finite.
+ let t = Transform::proof(srgb(), bad, srgb(), Intent::RelativeColorimetric, true, false).unwrap();
+ let mut out = [0.0f32; 3];
+ t.eval(&[0.2, 0.4, 0.6], &mut out);
+ assert!(out.iter().all(|v| v.is_finite()), "{name}: {out:?}");
+ let t = Transform::new(srgb(), bad, Intent::RelativeColorimetric, false).unwrap();
+ t.eval(&[0.2, 0.4, 0.6], &mut out);
+ assert!(out.iter().all(|v| v.is_finite()), "{name}: {out:?}");
+ }
+ // A sane patched white still builds and scales.
+ let d50 = srgb_with_wtpt([0xF6D6, 0x10000, 0xD32D]);
+ let t = Transform::new(srgb(), &d50, Intent::AbsoluteColorimetric, false).unwrap();
+ let mut out = [0.0f32; 3];
+ t.eval(&[0.2, 0.4, 0.6], &mut out);
+ assert!(out.iter().all(|v| v.is_finite()), "{out:?}");
+}
+
#[test]
fn black_point_compensation() {
let bp = photocraft_cms::black_point(cmyk(), Intent::RelativeColorimetric, false).unwrap();