Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_graphics/src/icc/lut.rs

5.1 KiB, 1 run

created by r1870400018:68827, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Evaluation of the `mft1` and `mft2` lookup tables the reader holds as integers: the input
2//! curves, the lattice by interpolation, the output curves.
3//!
4//! Values go in and come out as fractions of full scale, which is how a table holds its encoding:
5//! a Lab colour is placed on that scale by [`crate::icc::lab::lab_enc`], and a device value is
6//! already there. The curves are interpolated linearly between their entries. The lattice is
7//! interpolated tetrahedrally on its last three axes, as LittleCMS does, and linearly on a fourth
8//! axis before them; a lattice of fewer axes uses the same rules (one is linear, two bilinear).
9//! A lattice of three axes whose input side is Lab, as in a `B2A` table, is the exception:
10//! LittleCMS reads such a table trilinearly, and a tetrahedral reading differs from it by up to
11//! 17 levels in a saturated colour, because Lab is not uniform along the cell diagonal.
12//!
13//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
14//! Anthropic Claude
15
16use crate::icc::read::Lut;
17
18// Where a coordinate lies on a lattice axis: the node below it and the fraction past that node.
19pub(crate) fn locate(x: f64, grid: usize) -> (usize, f64) {
20 let at = x.clamp(0.0, 1.0) * (grid - 1) as f64;
21 let i = (at.floor() as usize).min(grid - 2);
22 (i, at - i as f64)
23}
24
25// A table's curve at `x`, as a fraction of full scale.
26fn curve(t: &[u16], max: f64, x: f64) -> f64 {
27 let at = x.clamp(0.0, 1.0) * (t.len() - 1) as f64;
28 let i = (at.floor() as usize).min(t.len() - 2);
29 let f = at - i as f64;
30 (t[i] as f64 + (t[i + 1] as f64 - t[i] as f64) * f) / max
31}
32
33/// Tetrahedral interpolation in the cell of a 3-D lattice whose lowest node is `base`.
34///
35/// `s` is the distance in nodes between neighbours along each axis, `f` the fractions past the
36/// lowest node, and `node` gives the four values at a node. The cell is cut into the six
37/// tetrahedra that share its main diagonal, and the point is placed in the one its fractions order.
38pub fn tetra<F: Fn(usize) -> [f64; 4]>(node: F, base: usize, s: [usize; 3], f: [f64; 3]) -> [f64; 4] {
39 let mut o = [0usize, 1, 2];
40 if f[o[0]] < f[o[1]] { o.swap(0, 1); }
41 if f[o[1]] < f[o[2]] { o.swap(1, 2); }
42 if f[o[0]] < f[o[1]] { o.swap(0, 1); }
43 let p0 = base;
44 let p1 = p0 + s[o[0]];
45 let p2 = p1 + s[o[1]];
46 let p3 = p2 + s[o[2]];
47 let w = [1.0 - f[o[0]], f[o[0]] - f[o[1]], f[o[1]] - f[o[2]], f[o[2]]];
48 let (n0, n1, n2, n3) = (node(p0), node(p1), node(p2), node(p3));
49 let mut r = [0.0; 4];
50 for j in 0..4 {
51 r[j] = w[0] * n0[j] + w[1] * n1[j] + w[2] * n2[j] + w[3] * n3[j];
52 }
53 r
54}
55
56/// Trilinear interpolation in the cell of a 3-D lattice, with the arguments of [`tetra`].
57pub fn trilinear<F: Fn(usize) -> [f64; 4]>(node: F, base: usize, s: [usize; 3], f: [f64; 3]) -> [f64; 4] {
58 let mut r = [0.0; 4];
59 for c in 0..8usize {
60 let (mut w, mut at) = (1.0, base);
61 for k in 0..3 {
62 if (c >> k) & 1 == 1 { w *= f[k]; at += s[k]; } else { w *= 1.0 - f[k]; }
63 }
64 let n = node(at);
65 for j in 0..4 {
66 r[j] += w * n[j];
67 }
68 }
69 r
70}
71
72impl Lut {
73 /// The table's output for inputs `v`, each a fraction of full scale; only the first `nin`
74 /// inputs are read and only the first `nout` outputs are meaningful.
75 pub fn eval(&self, v: [f64; 4]) -> [f64; 4] {
76 let max = self.max();
77 let mut at = [(0usize, 0.0f64); 4];
78 let mut stride = [0usize; 4];
79 let mut s = self.nout;
80 for c in (0..self.nin).rev() {
81 let t = &self.ins[c * self.ine..(c + 1) * self.ine];
82 at[c] = locate(curve(t, max, v[c]), self.grid);
83 stride[c] = s;
84 s *= self.grid;
85 }
86 let mut y = self.lattice(&at[..self.nin], &stride[..self.nin], 0);
87 for j in 0..self.nout {
88 let t = &self.outs[j * self.oute..(j + 1) * self.oute];
89 y[j] = curve(t, max, y[j]);
90 }
91 y
92 }
93
94 // The node at flat offset `off`, as fractions of full scale.
95 fn at(&self, off: usize) -> [f64; 4] {
96 let max = self.max();
97 let mut r = [0.0; 4];
98 for j in 0..self.nout {
99 r[j] = self.clut[off + j] as f64 / max;
100 }
101 r
102 }
103
104 // The lattice at the axes still to interpolate, from the cell whose lowest node is `base`.
105 fn lattice(&self, axes: &[(usize, f64)], stride: &[usize], base: usize) -> [f64; 4] {
106 match axes.len() {
107 0 => self.at(base),
108 1 => {
109 let (i, f) = axes[0];
110 let (a, b) = (self.at(base + i * stride[0]), self.at(base + (i + 1) * stride[0]));
111 lerp(a, b, f)
112 },
113 2 => {
114 let (i, f) = axes[0];
115 let lo = base + i * stride[0];
116 let a = self.lattice(&axes[1..], &stride[1..], lo);
117 let b = self.lattice(&axes[1..], &stride[1..], lo + stride[0]);
118 lerp(a, b, f)
119 },
120 3 => {
121 let base = base + axes[0].0 * stride[0] + axes[1].0 * stride[1] + axes[2].0 * stride[2];
122 let (s, f) = ([stride[0], stride[1], stride[2]], [axes[0].1, axes[1].1, axes[2].1]);
123 if self.lab_in { trilinear(|n| self.at(n), base, s, f) } else { tetra(|n| self.at(n), base, s, f) }
124 },
125 _ => {
126 let (i, f) = axes[0];
127 let lo = base + i * stride[0];
128 let a = self.lattice(&axes[1..], &stride[1..], lo);
129 let b = self.lattice(&axes[1..], &stride[1..], lo + stride[0]);
130 lerp(a, b, f)
131 },
132 }
133 }
134}
135
136fn lerp(a: [f64; 4], b: [f64; 4], f: f64) -> [f64; 4] {
137 [a[0] + (b[0] - a[0]) * f, a[1] + (b[1] - a[1]) * f, a[2] + (b[2] - a[2]) * f, a[3] + (b[3] - a[3]) * f]
138}