diff --git a/crates/color/src/cms.rs b/crates/color/src/cms.rs index 96640b5508..1d5c704ba1 100644 --- a/crates/color/src/cms.rs +++ b/crates/color/src/cms.rs @@ -1,12 +1,12 @@ //! Colour management: working spaces, RGB ↔ CMYK ↔ Lab conversions with rendering intents, //! gamut checks and soft-proof transforms. //! -//! * **RGB working spaces** are ICC matrix/shaper profiles via [`moxcms`] (sRGB, Wide Gamut RGB +//! * **RGB working spaces** are ICC matrix/shaper profiles via `craft-cms` (sRGB, Wide Gamut RGB //! (1998 primaries), Display P3, ProPhoto RGB) or user `.icc` files. Display output is sRGB; //! the desktop canvas converts it into the display's own profile last ([`display`]). //! * **CMYK spaces:** [`GENERIC_CMYK`] is our own documented parametric press model //! ([`generic`]); [`DEVICE_CMYK`] is the old profile-free formula (kept for exact legacy numbers); -//! any CMYK `.icc` the user loads goes through `moxcms` (LUT-based profiles). +//! any CMYK `.icc` the user loads goes through `craft-cms` (LUT-based profiles). //! * The **active settings** are process-wide: the working settings (Edit → Color Settings), with //! the profiles [`use_document_profiles`] last put in place of the working spaces; //! [`crate::Color::to_rgb`] uses them. [`with_active`] puts other settings in their place on one @@ -373,7 +373,7 @@ pub fn icc_bytes(name: &str) -> Result, CmsError> { }; let to_lab = |c| cms.cmyk_to_lab(c); let shadows = |l| mapped.lab_to_cmyk(l, Intent::RelativeColorimetric); - icc::encode(&icc::cmyk_profile(name, paper, to_lab, shadows, |l| cms.lab_to_cmyk(l, Intent::RelativeColorimetric)))? + icc::encode(&icc::cmyk_profile(name, paper, to_lab, shadows, |l| cms.lab_to_cmyk(l, Intent::RelativeColorimetric))?)? } else { icc::builtin_rgb_bytes(name)? } diff --git a/crates/color/src/cms/icc.rs b/crates/color/src/cms/icc.rs index a3b87f2fc6..c37e559b94 100644 --- a/crates/color/src/cms/icc.rs +++ b/crates/color/src/cms/icc.rs @@ -264,7 +264,7 @@ pub(super) fn cmyk_profile( to_lab: impl Fn([f32; 4]) -> lab::Lab, mapped: impl Fn(lab::Lab) -> [f32; 4], colorimetric: impl Fn(lab::Lab) -> [f32; 4], -) -> Profile { +) -> Result { let unit = |i: usize, n: usize| i as f32 / (n - 1) as f32; let mut a2b_clut = Vec::with_capacity(A2B_GRID.pow(4) * 3); for i in 0..A2B_GRID.pow(4) { @@ -287,16 +287,14 @@ pub(super) fn cmyk_profile( let want = lab::Lab::new(l * 100.0, a * 255.0 - 128.0, b * 255.0 - 128.0); gamut_clut.push(if lab::delta_e2000(want, to_lab(colorimetric(want))) <= 3.0 { 0.0 } else { 1.0 }); } - let table = |inputs: usize, outputs: usize, grid: usize, clut: Vec| Lut { - kind: LutKind::Ab, - inputs, - outputs, - stages: vec![Stage::Clut(Clut::new(vec![grid; inputs], outputs, clut).expect("valid CLUT dimensions"))], + let table = |inputs: usize, outputs: usize, grid: usize, clut: Vec| -> Result { + let clut = Clut::new(vec![grid; inputs], outputs, clut).map_err(|e| CmsError::BadProfile(format!("{e:?}")))?; + Ok(Lut { kind: LutKind::Ab, inputs, outputs, stages: vec![Stage::Clut(clut)] }) }; - let a2b = table(4, 3, A2B_GRID, a2b_clut); - let b2a_mapped = table(3, 4, B2A_GRID, b2a_mapped_clut); - let b2a = table(3, 4, B2A_GRID, b2a_clut); - let _gamut = table(3, 1, GAMUT_GRID, gamut_clut); + let a2b = table(4, 3, A2B_GRID, a2b_clut)?; + let b2a_mapped = table(3, 4, B2A_GRID, b2a_mapped_clut)?; + let b2a = table(3, 4, B2A_GRID, b2a_clut)?; + let _gamut = table(3, 1, GAMUT_GRID, gamut_clut)?; let mut p = Profile::gray([f64::from(paper[0]), f64::from(paper[1]), f64::from(paper[2])], Curve::Gamma(1.0)) .unwrap_or_else(|_| Builtin::Srgb.profile().clone()); p.class = ProfileClass::Output; @@ -309,7 +307,7 @@ pub(super) fn cmyk_profile( p.gray_trc = None; p.a2b = [Some(a2b.clone()), Some(a2b.clone()), Some(a2b)]; p.b2a = [Some(b2a_mapped.clone()), Some(b2a), Some(b2a_mapped)]; - labelled(p, name) + Ok(labelled(p, name)) } /// The grey space of greyscale exports: sRGB's tone curve, D50 white. diff --git a/crates/color/src/cms/tests.rs b/crates/color/src/cms/tests.rs index 95e465b623..414896b880 100644 --- a/crates/color/src/cms/tests.rs +++ b/crates/color/src/cms/tests.rs @@ -23,8 +23,8 @@ fn intents_parse_and_name() { } #[test] -fn srgb_via_moxcms_matches_analytic() { - // The built-in sRGB ICC profile through moxcms agrees with our analytic sRGB maths. +fn srgb_via_the_cms_matches_analytic() { + // The built-in sRGB ICC profile through craft-cms agrees with our analytic sRGB maths. let p = icc::builtin_rgb(SRGB).unwrap(); for rgb in [[0.2, 0.5, 0.8], [1.0, 0.0, 0.0], [0.5, 0.5, 0.5]] { let out = p.to_srgb(&rgb, Intent::RelativeColorimetric).unwrap(); diff --git a/crates/color/src/cms/tests_icc.rs b/crates/color/src/cms/tests_icc.rs index 2c1d96638b..2f409cebbf 100644 --- a/crates/color/src/cms/tests_icc.rs +++ b/crates/color/src/cms/tests_icc.rs @@ -1,7 +1,7 @@ use super::*; -fn parse(bytes: &[u8]) -> moxcms::ColorProfile { - moxcms::ColorProfile::new_from_slice(bytes).unwrap() +fn parse(bytes: &[u8]) -> Profile { + Profile::parse(bytes).unwrap() } #[test] @@ -19,7 +19,7 @@ fn builtin_rgb_spaces_encode_under_their_names() { fn cmyk_profiles_convert_like_the_cms() { for name in BUILTIN_CMYK { let bytes = icc_bytes(name).unwrap(); - assert_eq!(parse(&bytes).color_space, moxcms::DataColorSpace::Cmyk); + assert_eq!(parse(&bytes).color_space, ColorSpace::Cmyk); let p = IccProfile::from_bytes(None, &bytes).unwrap(); assert_eq!((p.name.as_str(), p.kind), (name, ProfileKind::Cmyk)); // The encoded profile converts without black-point compensation, so compare without it. @@ -202,10 +202,9 @@ fn builtin_profiles_align_their_sizes_and_tags() { assert_eq!(off % 4, 0, "{name}: tag {i} at {off}"); assert!(off + size <= icc.len()); } - let back = moxcms::ColorProfile::new_from_slice(&icc).unwrap(); - let orig = moxcms::ColorProfile::new_srgb(); + let back = parse(&icc); if name == super::SRGB { - assert_eq!((back.red_colorant, back.green_colorant, back.blue_colorant), (orig.red_colorant, orig.green_colorant, orig.blue_colorant)); + assert_eq!(back.matrix, Builtin::Srgb.profile().matrix); } } } @@ -227,7 +226,7 @@ fn cmyk_profiles_have_the_tags_of_an_output_profile() { } } -/// A profile whose localized-name records all point at one long string is refused before moxcms +/// A profile whose localized-name records all point at one long string is refused before the CMS /// copies each of them; a few short names pass. #[test] fn aliased_localized_names_are_refused() { @@ -256,7 +255,7 @@ fn aliased_localized_names_are_refused() { } /// A tag table that lists one tag many times, every entry pointing at the same long text, is -/// refused before moxcms decodes the text once per entry; distinct tags pass. +/// refused before the CMS decodes the text once per entry; distinct tags pass. #[test] fn repeated_tags_are_refused() { let profile = |sigs: &[&[u8; 4]], text: usize| {