mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
cms: accept ICC DeviceLinks whose output isn't a 3-channel PCS (#2348)
A DeviceLink's header PCS field holds its output colour space (ICC.1:2010 §7.2.7), so a CMYK→CMYK link has a 4→4 AToB0. The LUT channel check hard-coded 3 outputs and rejected such files as invalid, so users were told a well-formed profile was corrupt and never reached the "DeviceLink profiles cannot be used" error. Check DeviceLink LUTs against the header's output space instead, and only let a Lab DeviceLink through link_stages when its tables end in 3 channels, so a Lab→CMYK link gets the unsupported error rather than building a mismatched transform. Fixes #2337 Claude-Session: https://claude.ai/code/session_01X5k8jSoLtGswanmHBipHER Co-authored-by: Leel Wasanatha <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
3 files changed
+64
-5
No files matched your search
@@ -448,7 +448,8 @@ fn parse_profile(bytes: &[u8]) -> Result<Profile, CmsError> {
|
||||
let version = (r.u8(8)?, r.u8(9)?);
|
||||
let class = ProfileClass::from_sig(r.u32(12)?);
|
||||
let color_space = ColorSpace::from_sig(r.u32(16)?);
|
||||
let pcs = match &r.u32(20)?.to_be_bytes() {
|
||||
let pcs_sig = r.u32(20)?;
|
||||
let pcs = match &pcs_sig.to_be_bytes() {
|
||||
b"Lab " => Pcs::Lab,
|
||||
_ => Pcs::Xyz,
|
||||
};
|
||||
@@ -539,15 +540,18 @@ fn parse_profile(bytes: &[u8]) -> Result<Profile, CmsError> {
|
||||
bytes: None,
|
||||
hash: Default::default(),
|
||||
};
|
||||
// Validate channel counts of LUTs against the header.
|
||||
// Validate channel counts of LUTs against the header. A DeviceLink connects two device
|
||||
// spaces: its header PCS field holds the output colour space (ICC.1:2010 §7.2.7), so a
|
||||
// CMYK→CMYK link has a 4→4 AToB0.
|
||||
let dev = p.color_space.channels();
|
||||
let conn = if p.class == ProfileClass::DeviceLink { ColorSpace::from_sig(pcs_sig).channels() } else { 3 };
|
||||
for l in p.a2b.iter().flatten() {
|
||||
if dev != 0 && l.inputs != dev || l.outputs != 3 {
|
||||
if dev != 0 && l.inputs != dev || conn != 0 && l.outputs != conn {
|
||||
return Err(CmsError::Invalid(format!("AToB tag has {}→{} channels for a {:?} profile", l.inputs, l.outputs, p.color_space)));
|
||||
}
|
||||
}
|
||||
for l in p.b2a.iter().flatten() {
|
||||
if l.inputs != 3 || dev != 0 && l.outputs != dev {
|
||||
if conn != 0 && l.inputs != conn || dev != 0 && l.outputs != dev {
|
||||
return Err(CmsError::Invalid(format!("BToA tag has {}→{} channels for a {:?} profile", l.inputs, l.outputs, p.color_space)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -549,7 +549,9 @@ fn par_inplace(buf: &mut [f32], _stride: usize, f: impl Fn(&mut [f32]) + Sync) {
|
||||
/// Stages from `src` device values to `dst` device values.
|
||||
pub(crate) fn link_stages(src: &Profile, dst: &Profile, intent: Intent, bpc: bool) -> Result<Vec<Stage>, CmsError> {
|
||||
for p in [src, dst] {
|
||||
if matches!(p.class, ProfileClass::DeviceLink | ProfileClass::NamedColor | ProfileClass::Abstract) && p.color_space != ColorSpace::Lab {
|
||||
// A Lab DeviceLink stands in for an abstract profile only when it also ends in Lab.
|
||||
let lab_to_lab = p.color_space == ColorSpace::Lab && p.a2b.iter().flatten().all(|l| l.outputs == 3) && p.b2a.iter().flatten().all(|l| l.inputs == 3);
|
||||
if matches!(p.class, ProfileClass::DeviceLink | ProfileClass::NamedColor | ProfileClass::Abstract) && !lab_to_lab {
|
||||
return Err(CmsError::Unsupported(format!("{:?} profiles cannot be used as a source or destination", p.class)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -699,3 +699,56 @@ fn windows_srgb_is_the_builtin_srgb() {
|
||||
assert_ne!(p.content_hash(), srgb().content_hash());
|
||||
assert!(p.same_colors(srgb()));
|
||||
}
|
||||
|
||||
/// A minimal DeviceLink: header plus one `A2B0` (`mft2`, 2-point grid) mapping `inputs` channels
|
||||
/// of `space` to `outputs` channels of `out_space`, which the header stores in the PCS field.
|
||||
fn device_link(space: &[u8; 4], out_space: &[u8; 4], inputs: usize, outputs: usize) -> Vec<u8> {
|
||||
let mut lut = b"mft2\0\0\0\0".to_vec();
|
||||
lut.extend_from_slice(&[inputs as u8, outputs as u8, 2, 0]);
|
||||
for k in 0..9 {
|
||||
let v: i32 = if k % 4 == 0 { 0x10000 } else { 0 };
|
||||
lut.extend_from_slice(&v.to_be_bytes()); // identity matrix
|
||||
}
|
||||
lut.extend_from_slice(&[0, 2, 0, 2]); // 2-entry input and output tables
|
||||
for _ in 0..inputs {
|
||||
lut.extend_from_slice(&[0, 0, 0xff, 0xff]);
|
||||
}
|
||||
for _ in 0..(1 << inputs) * outputs {
|
||||
lut.extend_from_slice(&[0x80, 0]);
|
||||
}
|
||||
for _ in 0..outputs {
|
||||
lut.extend_from_slice(&[0, 0, 0xff, 0xff]);
|
||||
}
|
||||
let mut icc = vec![0u8; 128];
|
||||
icc[8] = 4;
|
||||
icc[9] = 0x30;
|
||||
icc[12..16].copy_from_slice(b"link");
|
||||
icc[16..20].copy_from_slice(space);
|
||||
icc[20..24].copy_from_slice(out_space);
|
||||
icc[36..40].copy_from_slice(b"acsp");
|
||||
icc.extend_from_slice(&1u32.to_be_bytes());
|
||||
icc.extend_from_slice(b"A2B0");
|
||||
icc.extend_from_slice(&144u32.to_be_bytes());
|
||||
icc.extend_from_slice(&(lut.len() as u32).to_be_bytes());
|
||||
icc.extend_from_slice(&lut);
|
||||
let size = icc.len() as u32;
|
||||
icc[..4].copy_from_slice(&size.to_be_bytes());
|
||||
icc
|
||||
}
|
||||
|
||||
/// Regression (#2337): DeviceLinks whose output isn't a 3-channel space were rejected as
|
||||
/// invalid, because the LUT check assumed every AToB tag ends in the PCS. They parse now and
|
||||
/// get the clear "unsupported" error when used for a conversion, including a Lab→CMYK link,
|
||||
/// which must not pass for a Lab-to-Lab one.
|
||||
#[test]
|
||||
fn device_links_parse_and_are_reported_unsupported() {
|
||||
for (space, out_space, inputs, outputs) in [(b"CMYK", b"CMYK", 4, 4), (b"RGB ", b"CMYK", 3, 4), (b"Lab ", b"CMYK", 3, 4), (b"RGB ", b"RGB ", 3, 3)] {
|
||||
let p = Profile::parse(&device_link(space, out_space, inputs, outputs)).unwrap();
|
||||
assert_eq!(p.color_space.channels(), inputs);
|
||||
for err in [Transform::new(&p, srgb(), Intent::Perceptual, false).err(), Transform::new(srgb(), &p, Intent::Perceptual, false).err()] {
|
||||
assert!(matches!(err, Some(photocraft_cms::CmsError::Unsupported(_))), "{err:?}");
|
||||
}
|
||||
}
|
||||
// The output space still bounds the tag: a CMYK→CMYK link with a 4→3 table is malformed.
|
||||
assert!(matches!(Profile::parse(&device_link(b"CMYK", b"CMYK", 4, 3)), Err(photocraft_cms::CmsError::Invalid(_))));
|
||||
}
|
||||
Reference in new issue
Block a user