Color Lookup: a .3dl shaper keeps its knots over any table size

Review by @bommerts on #9170: baking at max(table nodes, shaper points) only
kept the shaper's breakpoints when the two grids lined up. Shaper
0 0 512 1023 over a 5³ table read 0.0834 at 1/3, where the shaper is 0
(about 21 8-bit levels off). The bake grid now holds both the table's nodes
and the shaper's knots (lcm of their intervals, up to MAX_SIZE), so an
identity table carries the curve exactly. New test:
three_dl_shaper_knots_survive_a_nonaligned_table (0.0834 without the fix).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Hector VillarrealandClaude Opus 5.5 committed 2026-10-30 12:57:09 -06:00
1 parent 216b53301f
commit 42ddf561cc
1 file changed
+31 -5
+31 -5
View File
@@ -329,8 +329,9 @@ pub fn parse_3dl(text: &str) -> Result<LutFile, LutError> {
/// 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.
/// The table is baked with at least as many nodes as the shaper has points (up to [`MAX_SIZE`]):
/// exact for an identity table, an approximation at that resolution otherwise.
/// The table is baked on a grid holding both its own nodes and the shaper's knots (up to
/// [`MAX_SIZE`] nodes; past that, the finer of the two): exact for an identity table, an
/// approximation at that resolution otherwise.
fn apply_shaper(lut: LutFile, shaper: &[f64]) -> Result<LutFile, LutError> {
let m = shaper.len();
if m < 2 || shaper.iter().any(|v| !v.is_finite()) {
@@ -354,9 +355,19 @@ fn apply_shaper(lut: LutFile, shaper: &[f64]) -> Result<LutFile, LutError> {
let (a, b) = (shaped.get(i).copied().unwrap_or(0.0), shaped.get(i + 1).copied().unwrap_or(0.0));
(a + (b - a) * f).clamp(0.0, 1.0) as f32
};
// A shaper finer than the table is baked at the shaper's resolution (up to MAX_SIZE), so its
// curve survives a small table: a 2-node table would otherwise read it only at its two ends.
let n = lut.size.max(m.min(MAX_SIZE));
// The bake grid holds both the table's nodes and the shaper's knots (lcm of their intervals),
// so the curve keeps its breakpoints whatever the table's size: exact on an identity table. A
// grid that would pass MAX_SIZE falls back to the finer of the two, an approximation between
// the knots.
let gcd = |mut a: usize, mut b: usize| {
while b != 0 {
(a, b) = (b, a % b);
}
a
};
let (a, k) = (lut.size - 1, m - 1);
let aligned = (a / gcd(a, k)).checked_mul(k).map(|l| l + 1).filter(|&l| l <= MAX_SIZE);
let n = aligned.unwrap_or_else(|| lut.size.max(m.min(MAX_SIZE)));
let step = (n - 1) as f64;
let mut out = LutFile { size: n, data: vec![0.0; n * n * n * 3], ..lut.clone() };
for b in 0..n {
@@ -603,6 +614,21 @@ mod tests {
}
}
/// #9170 review (bommerts): the shaper's breakpoints survive a table whose grid doesn't line
/// up with them. The bake grid holds both the table's nodes and the shaper's knots, so on an
/// identity table the curve is exact: `0 0 512 1023` is 0 at ⅓ over 5³ and 17³ tables.
#[test]
fn three_dl_shaper_knots_survive_a_nonaligned_table() {
for n in [5, 17] {
let l = parse_3dl(&identity_3dl(&[0, 0, 512, 1023], n)).unwrap();
let at = |x: f32| sample(&l, [x, 0.0, 0.0])[0];
assert!(at(1.0 / 3.0).abs() < 1e-6, "{n}³: {}", at(1.0 / 3.0));
// The helper's 12-bit nodes are truncated, so the table itself is within a 12-bit step.
assert!((at(2.0 / 3.0) - 512.0 / 1023.0).abs() < 1.0 / 4095.0, "{n}³: {}", at(2.0 / 3.0));
assert!((at(0.5) - 256.0 / 1023.0).abs() < 1.0 / 4095.0, "{n}³: halfway between the knots");
}
}
#[test]
fn cube_title_with_hash_round_trips() {
let l = LutFile::from_fn("Look #3", 2, |c| c);