From 0baeea93b08a4a9356271eca1aa3cc2c2f6e91f5 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 24 Oct 2026 11:45:20 -0600 Subject: [PATCH] Color Lookup: apply a .3dl's shaper before the table (#8982) A .3dl's first row is a shaper: Photoshop applies it as a 1-D curve to each input channel before the 3-D table. PhotoCraft skipped the row and treated every shaper as the even ramp, so a file with a bent shaper rendered as if it had none (grey 128 stayed 128 where Photoshop gives 71 for the shaper 0 114 455 1023, and 64 for a 17-point squared one). parse_3dl now keeps the shaper and, when it isn't the even ramp, bakes it into the table: each node becomes the table's trilinear value at the shaped input. Even ramps (the usual case) are left untouched. Co-Authored-By: Claude Opus 5.5 --- crates/cms/src/lutfile.rs | 119 +++++++++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 3 deletions(-) diff --git a/crates/cms/src/lutfile.rs b/crates/cms/src/lutfile.rs index d547f064ed..4db5333c24 100644 --- a/crates/cms/src/lutfile.rs +++ b/crates/cms/src/lutfile.rs @@ -229,7 +229,8 @@ pub fn parse_cube(text: &str) -> Result { /// shaper line of input values, then integer RGB rows with **blue** varying fastest. The /// `Mesh` line declares the cube edge (2^inBits + 1) and the output code range (up to /// 2^outBits − 1), and Photoshop honours both (#9073, #9078); without it the edge is the cube -/// root of the row count and the range is inferred from the largest value (#9121). +/// root of the row count and the range is inferred from the largest value (#9121). A shaper that +/// isn't the even ramp is applied before the table, as Photoshop does (#8982): see [`apply_shaper`]. pub fn parse_3dl(text: &str) -> Result { let mut mesh: Option<(u32, u32)> = None; let mut rows: Vec<[f64; 3]> = Vec::new(); @@ -239,6 +240,7 @@ pub fn parse_3dl(text: &str) -> Result { // Any text line before the rows is a header: a headerless table peaking at 255 or 1023 is // on the 12-bit scale, as Photoshop 27.11 reads it (#9121). let mut header = false; + let mut shaper: Option> = None; for line in text.lines() { let line = line.split('#').next().unwrap_or("").trim(); if line.is_empty() { @@ -275,8 +277,10 @@ pub fn parse_3dl(text: &str) -> Result { first_len.get_or_insert(v.len()); if v.len() == 3 { rows.push([v[0], v[1], v[2]]); + } else if v.len() > 3 && shaper.is_none() && rows.is_empty() { + // The input shaper: one row of values ahead of the table. + shaper = Some(v); } - // Lines with more than three numbers are the input shaper; it is assumed linear. } let (n, nodes, scale) = match mesh { Some((in_bits, out_bits)) => { @@ -314,7 +318,79 @@ pub fn parse_3dl(text: &str) -> Result { data[at + k] = (row[k] / scale) as f32; } } - LutFile { title: String::new(), size: n, data, domain_min: [0.0; 3], domain_max: [1.0; 3] }.check() + let lut = LutFile { title: String::new(), size: n, data, domain_min: [0.0; 3], domain_max: [1.0; 3] }.check()?; + Ok(match shaper { + Some(shaper) => apply_shaper(lut, &shaper), + None => lut, + }) +} + +/// Bakes a `.3dl` shaper into the table. Photoshop applies the shaper row as a 1-D curve on each +/// input channel ahead of the 3-D table (input `x` reads the shaper, sampled evenly over 0..1, +/// at `x`, interpolated linearly), so each node of the table becomes the table's own trilinear +/// value at the shaped input. An even ramp (the usual shaper) changes nothing and is skipped. +/// With as many shaper points as table nodes, as in Lustre files, the result is exact for an +/// identity table and an approximation, at the table's resolution, otherwise. +fn apply_shaper(lut: LutFile, shaper: &[f64]) -> LutFile { + let m = shaper.len(); + if m < 2 || shaper.iter().any(|v| !v.is_finite()) { + return lut; + } + // The shaper's own depth, from 10 bits up: a 0..1023 ramp is the even ramp with or without a header. + let scale = inferred_3dl_scale(shaper.iter().fold(0.0f64, |a, &b| a.max(b)), true); + let even = |k: usize| scale * k as f64 / (m - 1) as f64; + // Integer shapers round their ramp: within one code is still the even ramp. + if shaper.iter().enumerate().all(|(k, &v)| (v - even(k)).abs() <= 1.0) { + return lut; + } + let curve = |x: f64| -> f32 { + let p = x.clamp(0.0, 1.0) * (m - 1) as f64; + let i = (p.floor() as usize).min(m - 2); + let f = p - i as f64; + let (a, b) = (shaper.get(i).copied().unwrap_or(0.0), shaper.get(i + 1).copied().unwrap_or(0.0)); + ((a + (b - a) * f) / scale).clamp(0.0, 1.0) as f32 + }; + let n = lut.size; + let step = (n - 1) as f64; + let mut out = lut.clone(); + for b in 0..n { + for g in 0..n { + for r in 0..n { + let at = ((b * n + g) * n + r) * 3; + let v = sample(&lut, [curve(r as f64 / step), curve(g as f64 / step), curve(b as f64 / step)]); + if let Some(dst) = out.data.get_mut(at..at + 3) { + dst.copy_from_slice(&v); + } + } + } + } + out +} + +/// Trilinear lookup of `rgb` (0..1) in a [`LutFile`] (red fastest). +fn sample(l: &LutFile, rgb: [f32; 3]) -> [f32; 3] { + let n = l.size; + let loc = |v: f32| { + let p = v.clamp(0.0, 1.0) * (n - 1) as f32; + let i = (p as usize).min(n.saturating_sub(2)); + (i, p - i as f32) + }; + let (r0, fr) = loc(rgb[0]); + let (g0, fg) = loc(rgb[1]); + let (b0, fb) = loc(rgb[2]); + let at = |r: usize, g: usize, b: usize, k: usize| l.data.get(((b * n + g) * n + r) * 3 + k).copied().unwrap_or(0.0); + let mut o = [0.0f32; 3]; + for (k, ov) in o.iter_mut().enumerate() { + let c = |r, g, b| at(r, g, b, k); + let x00 = c(r0, g0, b0) + (c(r0 + 1, g0, b0) - c(r0, g0, b0)) * fr; + let x10 = c(r0, g0 + 1, b0) + (c(r0 + 1, g0 + 1, b0) - c(r0, g0 + 1, b0)) * fr; + let x01 = c(r0, g0, b0 + 1) + (c(r0 + 1, g0, b0 + 1) - c(r0, g0, b0 + 1)) * fr; + let x11 = c(r0, g0 + 1, b0 + 1) + (c(r0 + 1, g0 + 1, b0 + 1) - c(r0, g0 + 1, b0 + 1)) * fr; + let y0 = x00 + (x10 - x00) * fg; + let y1 = x01 + (x11 - x01) * fg; + *ov = y0 + (y1 - y0) * fb; + } + o } /// The code range a `.3dl` node table without a declared output depth was written with: the @@ -447,6 +523,43 @@ pub fn builtin(id: &str) -> Option { mod tests { use super::*; + /// An identity `.3dl` of edge `n` (12-bit nodes, blue fastest) under `shaper`. + fn identity_3dl(shaper: &[u32], n: usize) -> String { + let mut t = shaper.iter().map(u32::to_string).collect::>().join(" ") + "\n"; + let node = |i: usize| (i * 4095 / (n - 1)).to_string(); + for r in 0..n { + for g in 0..n { + for b in 0..n { + t += &format!("{} {} {}\n", node(r), node(g), node(b)); + } + } + } + t + } + + #[test] + fn three_dl_shaper_applies_before_the_table() { + // #8982: Photoshop applies a .3dl's shaper row as a curve ahead of the 3-D table. On grey + // 128 an identity table with the shaper 0 114 455 1023 gives 71 there, and a 17-point + // squared shaper 64 (reported measurements); even ramps leave grey 128 as it is. + let grey = |l: &LutFile| (sample(l, [128.0 / 255.0; 3])[0] * 255.0).round(); + let bent = parse_3dl(&identity_3dl(&[0, 114, 455, 1023], 4)).unwrap(); + assert_eq!(grey(&bent), 71.0); + let squared: Vec = (0..17).map(|i| (1023.0 * (i as f64 / 16.0).powi(2)).round() as u32).collect(); + assert_eq!(grey(&parse_3dl(&identity_3dl(&squared, 17)).unwrap()), 64.0); + let even4 = parse_3dl(&identity_3dl(&[0, 341, 682, 1023], 4)).unwrap(); + assert_eq!(grey(&even4), 128.0); + assert_eq!(even4, parse_3dl(&identity_3dl(&[0, 341, 682, 1023], 4).lines().skip(1).collect::>().join("\n")).unwrap()); + let even17: Vec = (0..17).map(|i| (1023.0 * i as f64 / 16.0).round() as u32).collect(); + assert_eq!(grey(&parse_3dl(&identity_3dl(&even17, 17)).unwrap()), 128.0); + // The ends stay put, and the shaper bends each channel on its own. + assert_eq!(sample(&bent, [0.0, 1.0, 0.0]), [0.0, 1.0, 0.0]); + let c = sample(&bent, [1.0 / 3.0, 0.0, 2.0 / 3.0]); + assert!((c[0] - 114.0 / 1023.0).abs() < 1e-4 && (c[2] - 455.0 / 1023.0).abs() < 1e-4, "{c:?}"); + // A shaper that isn't finite is ignored rather than trusted. + assert!(parse_3dl(&identity_3dl(&[0, 341, 682, 1023], 4).replacen("341", "NaN", 1)).is_ok()); + } + #[test] fn cube_title_with_hash_round_trips() { let l = LutFile::from_fn("Look #3", 2, |c| c);