mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-17 07:25:16 +00:00
perf(cms): tabulate the first-stage curves for 8-bit exact pipelines
run_u8 on a core without a lookup table (for example sRGB to Lab) evaluated the first stage's curves with powf on every channel of every pixel. An 8-bit channel has 256 values, so the curves are tabulated once per transform with the same Curve::eval and the remaining stages run from there. Output is bit-identical. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
914de3cd21
commit
c64ca3365b
3 files changed
+55
-1
No files matched your search
@@ -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<Stage> = Vec::with_capacity(self.stages.len());
|
||||
|
||||
@@ -93,6 +93,8 @@ pub struct Transform {
|
||||
post: Option<Post>,
|
||||
/// 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)) {
|
||||
|
||||
@@ -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<u8> = (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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user