mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 19:40:21 +00:00
Color: built-in CMYK profiles carry their paper and an exact colorimetric table
The ICC profile written for a built-in CMYK working space (embedded in PDF, TIFF, JPEG and PSD exports) changes in two ways: - The media white point (wtpt) is the model's paper instead of D50, so the ICC-absolute colorimetric intent reproduces the paper. - The colorimetric Lab -> CMYK table (B2A1), which the gamut tag is also sampled from, is the inverse of the CMYK -> Lab table without black-point compensation. Before, all three Lab -> CMYK tables had compensation built in, so a dark build such as C40 M30 Y30 K60 that went through the profile and back missed by Delta E 2000 2.7 on Generic CMYK. The perceptual and saturation tables keep compensation.
This commit is contained in:
+14
-3
@@ -347,7 +347,9 @@ pub fn profiles_generation() -> u64 {
|
||||
/// Profile `name` as an ICC file to embed in an export: any RGB or CMYK profile of [`profiles`]
|
||||
/// (by its current or legacy name), or [`GRAY`]. Built-in profiles are written in code from the
|
||||
/// CMS: the RGB spaces from their primaries, the CMYK spaces as lookup tables sampled from their
|
||||
/// model (both directions, perceptual and relative colorimetric, Lab PCS); a profile the user
|
||||
/// model (both directions, perceptual and relative colorimetric, Lab PCS, with the model's paper
|
||||
/// as the media white point; the colorimetric Lab → CMYK table without black-point
|
||||
/// compensation, the perceptual and saturation ones with it); a profile the user
|
||||
/// loaded is the file it came from. Cached.
|
||||
pub fn icc_bytes(name: &str) -> Result<Arc<[u8]>, CmsError> {
|
||||
static CACHE: Mutex<Vec<(String, Arc<[u8]>)>> = Mutex::new(Vec::new());
|
||||
@@ -361,8 +363,17 @@ pub fn icc_bytes(name: &str) -> Result<Arc<[u8]>, CmsError> {
|
||||
let bytes: Arc<[u8]> = if name == GRAY {
|
||||
icc::encode(&icc::gray_profile(name))?
|
||||
} else if BUILTIN_CMYK.contains(&name) {
|
||||
let cms = Cms::new(&ColorSettings { cmyk: name.into(), ..ColorSettings::default() })?;
|
||||
icc::encode(&icc::cmyk_profile(name, |c| cms.cmyk_to_lab(c), |l| cms.lab_to_cmyk(l, Intent::RelativeColorimetric)))?
|
||||
// The perceptual and saturation tables map the shadows with black-point compensation; the
|
||||
// colorimetric table is the inverse of the CMYK → Lab table, without it.
|
||||
let mapped = Cms::new(&ColorSettings { cmyk: name.into(), bpc: true, ..ColorSettings::default() })?;
|
||||
let cms = Cms::new(&ColorSettings { cmyk: name.into(), bpc: false, ..ColorSettings::default() })?;
|
||||
let paper = match &cms.cmyk {
|
||||
CmykSpace::Generic(g) => g.paper_xyz(),
|
||||
CmykSpace::Device | CmykSpace::Icc(_) => lab::D50,
|
||||
};
|
||||
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)))?
|
||||
} else {
|
||||
icc::builtin_rgb_bytes(name)?
|
||||
}
|
||||
|
||||
@@ -133,6 +133,11 @@ pub struct GenericCmyk {
|
||||
}
|
||||
|
||||
impl GenericCmyk {
|
||||
/// Absolute XYZ (D50) of the paper.
|
||||
pub fn paper_xyz(&self) -> [f32; 3] {
|
||||
self.paper_xyz
|
||||
}
|
||||
|
||||
pub fn new(params: GenericCmykParams) -> Self {
|
||||
let paper_xyz = lab_to_xyz(params.paper);
|
||||
let mut prim = [D50; 16];
|
||||
|
||||
+26
-12
@@ -446,11 +446,20 @@ const B2A_GRID: usize = 17;
|
||||
const GAMUT_GRID: usize = 9;
|
||||
|
||||
/// A CMYK output profile sampled from a conversion: `to_lab` (CMYK → media-relative Lab) in a
|
||||
/// 9⁴ table, `from_lab` (Lab → CMYK, gamut-mapped) in a 17³ table, Lab PCS (ICC v4 encoding).
|
||||
/// 9⁴ table, and Lab → CMYK in 17³ tables, Lab PCS (ICC v4 encoding): `mapped` (gamut-mapped,
|
||||
/// with black-point compensation) for the perceptual and saturation intents, `colorimetric` (the
|
||||
/// colorimetric inverse of `to_lab`) for the colorimetric intents and the gamut tag.
|
||||
/// It carries every tag ICC.1 requires of an output profile: the tables for all three intents
|
||||
/// and the gamut tag (Windows rejects a CMYK profile without them, so Photos couldn't show a CMYK
|
||||
/// TIFF that embedded one, #2707).
|
||||
pub(super) fn cmyk_profile(name: &str, to_lab: impl Fn([f32; 4]) -> lab::Lab, from_lab: impl Fn(lab::Lab) -> [f32; 4]) -> ColorProfile {
|
||||
/// TIFF that embedded one, #2707). The media white point is `paper` (absolute XYZ, D50), which
|
||||
/// the ICC-absolute colorimetric intent reproduces.
|
||||
pub(super) fn cmyk_profile(
|
||||
name: &str,
|
||||
paper: [f32; 3],
|
||||
to_lab: impl Fn([f32; 4]) -> lab::Lab,
|
||||
mapped: impl Fn(lab::Lab) -> [f32; 4],
|
||||
colorimetric: impl Fn(lab::Lab) -> [f32; 4],
|
||||
) -> ColorProfile {
|
||||
let unit = |i: usize, n: usize| i as f32 / (n - 1) as f32;
|
||||
let q = |v: f32| (v.clamp(0.0, 1.0) * 65535.0).round() as u16;
|
||||
let mut a2b = Vec::with_capacity(A2B_GRID.pow(4) * 3);
|
||||
@@ -459,17 +468,21 @@ pub(super) fn cmyk_profile(name: &str, to_lab: impl Fn([f32; 4]) -> lab::Lab, fr
|
||||
let l = to_lab(cmyk);
|
||||
a2b.extend([l.l / 100.0, (l.a + 128.0) / 255.0, (l.b + 128.0) / 255.0].map(q));
|
||||
}
|
||||
let mut b2a = Vec::with_capacity(B2A_GRID.pow(3) * 4);
|
||||
for i in 0..B2A_GRID.pow(3) {
|
||||
let [l, a, b] = [2, 1, 0].map(|d| unit(i / B2A_GRID.pow(d) % B2A_GRID, B2A_GRID));
|
||||
b2a.extend(from_lab(lab::Lab::new(l * 100.0, a * 255.0 - 128.0, b * 255.0 - 128.0)).map(q));
|
||||
}
|
||||
let b2a_of = |from_lab: &dyn Fn(lab::Lab) -> [f32; 4]| {
|
||||
let mut b2a = Vec::with_capacity(B2A_GRID.pow(3) * 4);
|
||||
for i in 0..B2A_GRID.pow(3) {
|
||||
let [l, a, b] = [2, 1, 0].map(|d| unit(i / B2A_GRID.pow(d) % B2A_GRID, B2A_GRID));
|
||||
b2a.extend(from_lab(lab::Lab::new(l * 100.0, a * 255.0 - 128.0, b * 255.0 - 128.0)).map(q));
|
||||
}
|
||||
b2a
|
||||
};
|
||||
let (b2a_mapped, b2a) = (b2a_of(&mapped), b2a_of(&colorimetric));
|
||||
// Gamut: 0 where a colour separates and comes back within a ΔE of 3, else out of gamut.
|
||||
let mut gamut = Vec::with_capacity(GAMUT_GRID.pow(3));
|
||||
for i in 0..GAMUT_GRID.pow(3) {
|
||||
let [l, a, b] = [2, 1, 0].map(|d| unit(i / GAMUT_GRID.pow(d) % GAMUT_GRID, GAMUT_GRID));
|
||||
let want = lab::Lab::new(l * 100.0, a * 255.0 - 128.0, b * 255.0 - 128.0);
|
||||
gamut.push(if lab::delta_e2000(want, to_lab(from_lab(want))) <= 3.0 { 0 } else { u16::MAX });
|
||||
gamut.push(if lab::delta_e2000(want, to_lab(colorimetric(want))) <= 3.0 { 0 } else { u16::MAX });
|
||||
}
|
||||
let identity = |n: usize| vec![ToneReprCurve::Parametric(vec![1.0]); n];
|
||||
let table = |inputs: usize, outputs: usize, grid: usize, clut: Vec<u16>| {
|
||||
@@ -490,6 +503,7 @@ pub(super) fn cmyk_profile(name: &str, to_lab: impl Fn([f32; 4]) -> lab::Lab, fr
|
||||
})
|
||||
};
|
||||
let a2b = table(4, 3, A2B_GRID, a2b);
|
||||
let b2a_mapped = table(3, 4, B2A_GRID, b2a_mapped);
|
||||
let b2a = table(3, 4, B2A_GRID, b2a);
|
||||
let gamut = table(3, 1, GAMUT_GRID, gamut);
|
||||
let mut p = ColorProfile::default();
|
||||
@@ -498,12 +512,12 @@ pub(super) fn cmyk_profile(name: &str, to_lab: impl Fn([f32; 4]) -> lab::Lab, fr
|
||||
p.pcs = DataColorSpace::Lab;
|
||||
p.rendering_intent = RenderingIntent::RelativeColorimetric;
|
||||
p.white_point = moxcms::WHITE_POINT_D50.to_xyzd();
|
||||
p.media_white_point = Some(p.white_point);
|
||||
p.media_white_point = Some(moxcms::Xyzd::new(f64::from(paper[0]), f64::from(paper[1]), f64::from(paper[2])));
|
||||
p.lut_a_to_b_perceptual = Some(a2b.clone());
|
||||
p.lut_a_to_b_saturation = Some(a2b.clone());
|
||||
p.lut_a_to_b_colorimetric = Some(a2b);
|
||||
p.lut_b_to_a_perceptual = Some(b2a.clone());
|
||||
p.lut_b_to_a_saturation = Some(b2a.clone());
|
||||
p.lut_b_to_a_perceptual = Some(b2a_mapped.clone());
|
||||
p.lut_b_to_a_saturation = Some(b2a_mapped);
|
||||
p.lut_b_to_a_colorimetric = Some(b2a);
|
||||
p.gamut = Some(gamut);
|
||||
labelled(p, name)
|
||||
|
||||
@@ -30,11 +30,43 @@ fn cmyk_profiles_convert_like_the_cms() {
|
||||
let de = delta_e2000(lab::srgb_to_lab(want), lab::srgb_to_lab(got));
|
||||
assert!(de < 2.5, "{name} {cmyk:?}: {want:?} vs {got:?} (ΔE {de})");
|
||||
}
|
||||
// And back: an in-gamut colour separates to about the same inks.
|
||||
let srgb = cms.cmyk_to_srgb([0.1, 0.6, 0.8, 0.0], false);
|
||||
let back = p.from_srgb(srgb, Intent::RelativeColorimetric).unwrap();
|
||||
let again = cms.cmyk_to_srgb([back[0], back[1], back[2], back[3]], false);
|
||||
assert!(delta_e2000(lab::srgb_to_lab(srgb), lab::srgb_to_lab(again)) < 3.0, "{back:?}");
|
||||
// And back: in-gamut colours separate to inks that print them. The Lab → CMYK table is
|
||||
// the colorimetric inverse of the CMYK → Lab table, with no black-point compensation.
|
||||
for inks in [
|
||||
[0.1, 0.6, 0.8, 0.0],
|
||||
[0.5, 0.1, 0.3, 0.1],
|
||||
[0.3, 0.3, 0.3, 0.2],
|
||||
[0.05, 0.05, 0.4, 0.0],
|
||||
[0.4, 0.3, 0.3, 0.6],
|
||||
[0.6, 0.5, 0.5, 0.4],
|
||||
[0.2, 0.2, 0.2, 0.8],
|
||||
[0.0, 0.0, 0.0, 0.5],
|
||||
] {
|
||||
let srgb = cms.cmyk_to_srgb(inks, false);
|
||||
let back = p.from_srgb(srgb, Intent::RelativeColorimetric).unwrap();
|
||||
let again = cms.cmyk_to_srgb([back[0], back[1], back[2], back[3]], false);
|
||||
let de = delta_e2000(lab::srgb_to_lab(srgb), lab::srgb_to_lab(again));
|
||||
assert!(de < 1.5, "{name} {inks:?}: {srgb:?} -> {back:?} -> {again:?} (ΔE {de})");
|
||||
}
|
||||
// The perceptual table keeps black-point compensation: source black separates to inks
|
||||
// that print the press's black point, as the CMS separates it with compensation.
|
||||
let compensated = Cms::new(&ColorSettings { cmyk: name.into(), bpc: true, ..Default::default() }).unwrap();
|
||||
let want = compensated.srgb_to_cmyk([0.0; 3], Intent::RelativeColorimetric);
|
||||
let got = p.from_srgb([0.0; 3], Intent::Perceptual).unwrap();
|
||||
let printed = |inks: &[f32]| cms.cmyk_to_lab([inks[0], inks[1], inks[2], inks[3]]);
|
||||
let de = delta_e2000(printed(&want), printed(&got));
|
||||
assert!(de < 1.5, "{name}: perceptual black {got:?} prints {:?}, want {want:?} ({:?})", printed(&got), printed(&want));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cmyk_profiles_carry_their_paper_as_the_media_white() {
|
||||
let generic = generic::swop_like();
|
||||
let p = parse(&icc_bytes(GENERIC_CMYK).unwrap());
|
||||
let white = p.media_white_point.unwrap();
|
||||
let paper = lab::lab_to_xyz(generic.paper);
|
||||
for (got, want) in [white.x, white.y, white.z].iter().zip(paper) {
|
||||
assert!((*got - f64::from(want)).abs() < 2e-3, "{white:?} vs {paper:?}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,9 +148,10 @@ fn black_point_compensation_maps_the_darkest_ink_to_rgb_black() {
|
||||
let rich = [0.75, 0.68, 0.67, 0.9];
|
||||
let (on, off) = (icc_cmyk(true).cmyk_to_rgb(rich, Intent::RelativeColorimetric), icc_cmyk(false).cmyk_to_rgb(rich, Intent::RelativeColorimetric));
|
||||
// Without compensation the profile's black is a dark grey; with it, rich black goes most of
|
||||
// the way to RGB black (not all: it isn't quite the darkest ink the profile prints).
|
||||
// the way to RGB black (not all: it isn't quite the darkest ink the profile prints, and the
|
||||
// profile's Lab → CMYK table carries no compensation of its own).
|
||||
assert!(off.iter().all(|v| *v > 0.1), "without: {off:?}");
|
||||
assert!(on.iter().zip(off).all(|(a, b)| *a < 0.08 && *a < b * 0.5), "with: {on:?}, without: {off:?}");
|
||||
assert!(on.iter().zip(off).all(|(a, b)| *a < 0.09 && *a < b * 0.55), "with: {on:?}, without: {off:?}");
|
||||
// The screen shows what an sRGB conversion writes, compensated alike.
|
||||
let srgb = |bpc| {
|
||||
let c = icc_cmyk(bpc);
|
||||
|
||||
Reference in New Issue
Block a user