diff --git a/crates/cms/src/pipeline.rs b/crates/cms/src/pipeline.rs index 1c095756ea..7fad1a107b 100644 --- a/crates/cms/src/pipeline.rs +++ b/crates/cms/src/pipeline.rs @@ -183,6 +183,17 @@ impl Pipeline { out[..self.outputs].copy_from_slice(&v[..self.outputs]); } + /// Evaluates the stages after the first one on values already passed through it. + #[inline] + pub(crate) fn eval_after_first(&self, v: &mut [f32; MAX], out: &mut [f32]) { + let mut n = self.inputs; + for s in self.stages.iter().skip(1) { + n = s.run(v, n); + } + let _ = n; + out[..self.outputs].copy_from_slice(&v[..self.outputs]); + } + /// Removes no-op stages and cancels adjacent inverse pairs (Lab↔XYZ, encode/decode). pub fn optimize(&mut self) { let mut out: Vec = Vec::with_capacity(self.stages.len()); diff --git a/crates/cms/src/transform.rs b/crates/cms/src/transform.rs index 66a4de9f30..0c8b5fa4a9 100644 --- a/crates/cms/src/transform.rs +++ b/crates/cms/src/transform.rs @@ -93,6 +93,8 @@ pub struct Transform { post: Option, /// Pre-located grid coordinates per 8-bit code: (offset contribution, fraction) per input. grid8: Vec<[(u32, f32); 256]>, + /// For an exact core: the first stage's curves at every 8-bit code. + exact8: Vec<[f32; 256]>, } impl Transform { @@ -180,7 +182,13 @@ impl Transform { .collect(), _ => Vec::new(), }; - Transform { inputs, outputs, opts, pipeline, core, post, grid8 } + let exact8 = match (&core, pipeline.stages.first()) { + (Core::Exact, Some(Stage::Curves(cs))) if cs.len() == inputs && inputs <= 4 => { + cs.iter().map(|c| std::array::from_fn(|i| c.eval(i as f32 / 255.0))).collect() + } + _ => Vec::new(), + }; + Transform { inputs, outputs, opts, pipeline, core, post, grid8, exact8 } } /// White preservation: the device-link node at the source's device white gets exactly the @@ -408,6 +416,21 @@ impl Transform { } } } + Core::Exact if !self.exact8.is_empty() => { + for (sp, dp) in s.chunks_exact(ss).zip(d.chunks_exact_mut(ds)) { + let mut v = [0.0f32; 16]; + for k in 0..self.inputs { + v[k] = self.exact8[k][sp[k] as usize]; + } + self.pipeline.eval_after_first(&mut v, &mut out); + for k in 0..self.outputs { + dp[k] = q(out[k]); + } + for e in 0..extra { + dp[self.outputs + e] = sp[self.inputs + e]; + } + } + } _ => { let mut inp = [0.0f32; 16]; for (sp, dp) in s.chunks_exact(ss).zip(d.chunks_exact_mut(ds)) { diff --git a/crates/cms/tests/it/cms.rs b/crates/cms/tests/it/cms.rs index 384075fff9..2a081d8138 100644 --- a/crates/cms/tests/it/cms.rs +++ b/crates/cms/tests/it/cms.rs @@ -856,3 +856,23 @@ fn matrix_trc_blocks_match_per_pixel_eval() { } } } + +/// 8-bit conversion through an analytic pipeline (no lookup table, here RGB to Lab) tabulates +/// the first stage's curves per code; the result must equal the exact per-pixel evaluation. +#[test] +fn exact_core_u8_matches_exact_eval() { + let t = Transform::new(srgb(), lab(), Intent::RelativeColorimetric, true).unwrap(); + let px = 256usize * 256; + let src: Vec = (0..px as u32).flat_map(|i| [(i & 255) as u8, (i >> 8) as u8, (i.wrapping_mul(2654435761) >> 13) as u8, 200]).collect(); + let mut out = vec![0u8; px * 4]; + t.convert_u8(&src, 4, &mut out, 4, true); + for (sp, dp) in src.as_chunks::<4>().0.iter().zip(out.as_chunks::<4>().0) { + let inp = [sp[0] as f32 / 255.0, sp[1] as f32 / 255.0, sp[2] as f32 / 255.0]; + let mut want = [0.0f32; 3]; + t.eval(&inp, &mut want); + for k in 0..3 { + assert_eq!(dp[k], (want[k].clamp(0.0, 1.0) * 255.0 + 0.5) as u8, "{sp:?} ch {k}"); + } + assert_eq!(dp[3], 200); + } +}