1#[derive(Clone, Debug, PartialEq)]
9pub enum Curve {
10 Identity,
11 Gamma(f64),
13 Parametric {
16 kind: u16,
17 p: [f64; 7],
18 },
19 Table(Vec<f32>),
21}
22
23pub fn srgb_trc() -> Curve {
25 Curve::Parametric {
26 kind: 3,
27 p: [
28 2.4,
29 1.0 / 1.055,
30 0.055 / 1.055,
31 1.0 / 12.92,
32 0.04045,
33 0.0,
34 0.0,
35 ],
36 }
37}
38
39impl Curve {
40 pub fn param_count(kind: u16) -> usize {
42 match kind {
43 0 => 1,
44 1 => 3,
45 2 => 4,
46 3 => 5,
47 _ => 7,
48 }
49 }
50
51 pub fn is_identity(&self) -> bool {
52 match self {
53 Curve::Identity => true,
54 Curve::Gamma(g) => (*g - 1.0).abs() < 1e-9,
55 Curve::Parametric { kind: 0, p } => (p[0] - 1.0).abs() < 1e-9,
56 Curve::Table(t) => {
57 let n = t.len();
58 n < 2
59 || t.iter()
60 .enumerate()
61 .all(|(i, v)| (*v as f64 - i as f64 / (n - 1) as f64).abs() < 1.0 / 65535.0)
62 }
63 _ => false,
64 }
65 }
66
67 pub fn eval64(&self, x: f64) -> f64 {
69 match self {
70 Curve::Identity => x,
71 Curve::Gamma(g) => mirror(x, |v| v.powf(*g)),
72 Curve::Parametric { kind, p } => para(*kind, p, x),
73 Curve::Table(t) => table(t, x as f32) as f64,
74 }
75 }
76
77 #[inline]
78 pub fn eval(&self, x: f32) -> f32 {
79 match self {
80 Curve::Identity => x,
81 Curve::Table(t) => table(t, x),
82 _ => self.eval64(x as f64) as f32,
83 }
84 }
85
86 pub fn eval_inverse64(&self, y: f64) -> f64 {
88 match self {
89 Curve::Identity => y,
90 Curve::Gamma(g) => {
91 if g.abs() < 1e-12 {
92 y
93 } else {
94 mirror(y, |v| v.powf(1.0 / g))
95 }
96 }
97 Curve::Parametric { kind, p } => para_inverse(*kind, p, y)
98 .unwrap_or_else(|| numeric_inverse(|x| para(*kind, p, x), y)),
99 Curve::Table(t) => table_inverse(t, y as f32) as f64,
100 }
101 }
102
103 #[inline]
104 pub fn eval_inverse(&self, y: f32) -> f32 {
105 match self {
106 Curve::Identity => y,
107 Curve::Table(t) => table_inverse(t, y),
108 _ => self.eval_inverse64(y as f64) as f32,
109 }
110 }
111
112 pub fn sample(&self, n: usize) -> Vec<f32> {
114 (0..n)
115 .map(|i| self.eval64(i as f64 / (n - 1) as f64) as f32)
116 .collect()
117 }
118
119 pub fn is_monotonic(&self) -> bool {
121 let s = self.sample(1024);
122 s.windows(2).all(|w| w[1] >= w[0] - 1e-6) || s.windows(2).all(|w| w[1] <= w[0] + 1e-6)
123 }
124}
125
126#[inline]
127fn mirror(x: f64, f: impl Fn(f64) -> f64) -> f64 {
128 if x < 0.0 {
129 -f(-x)
130 } else {
131 f(x)
132 }
133}
134
135fn pw(base: f64, g: f64) -> f64 {
136 if base <= 0.0 {
137 0.0
138 } else {
139 base.powf(g)
140 }
141}
142
143fn para(kind: u16, p: &[f64; 7], x: f64) -> f64 {
144 let [g, a, b, c, d, e, f] = *p;
145 match kind {
146 0 => mirror(x, |v| v.powf(g)),
147 1 => {
148 if a != 0.0 && x >= -b / a {
149 pw(a * x + b, g)
150 } else {
151 0.0
152 }
153 }
154 2 => {
155 if a != 0.0 && x >= -b / a {
156 pw(a * x + b, g) + c
157 } else {
158 c
159 }
160 }
161 3 => {
162 if x < 0.0 && b == 0.0 && d == 0.0 {
163 -para(kind, p, -x)
165 } else if x >= d {
166 pw(a * x + b, g)
167 } else {
168 c * x
169 }
170 }
171 _ => {
172 if x >= d {
173 pw(a * x + b, g) + e
174 } else {
175 c * x + f
176 }
177 }
178 }
179}
180
181fn para_inverse(kind: u16, p: &[f64; 7], y: f64) -> Option<f64> {
182 let [g, a, b, c, d, e, f] = *p;
183 if g.abs() < 1e-12 {
184 return None;
185 }
186 let root = |v: f64| if v <= 0.0 { 0.0 } else { v.powf(1.0 / g) };
187 Some(match kind {
188 0 => mirror(y, |v| v.powf(1.0 / g)),
189 1 => {
190 if a == 0.0 {
191 return None;
192 }
193 if y <= 0.0 {
194 -b / a
195 } else {
196 (root(y) - b) / a
197 }
198 }
199 2 => {
200 if a == 0.0 {
201 return None;
202 }
203 if y <= c {
204 -b / a
205 } else {
206 (root(y - c) - b) / a
207 }
208 }
209 3 => {
210 if a == 0.0 {
211 return None;
212 }
213 if y < 0.0 && b == 0.0 && d == 0.0 {
214 return para_inverse(kind, p, -y).map(|v| -v);
215 }
216 let yd = pw(a * d + b, g);
217 if y >= yd {
218 (root(y) - b) / a
219 } else if c != 0.0 {
220 y / c
221 } else {
222 0.0
223 }
224 }
225 _ => {
226 if a == 0.0 {
227 return None;
228 }
229 let yd = pw(a * d + b, g) + e;
230 if y >= yd {
231 (root(y - e) - b) / a
232 } else if c != 0.0 {
233 (y - f) / c
234 } else {
235 0.0
236 }
237 }
238 })
239}
240
241fn numeric_inverse(f: impl Fn(f64) -> f64, y: f64) -> f64 {
243 let (f0, f1) = (f(0.0), f(1.0));
244 let increasing = f1 >= f0;
245 let (mut lo, mut hi) = (0.0f64, 1.0f64);
246 for _ in 0..60 {
247 let mid = 0.5 * (lo + hi);
248 let v = f(mid);
249 if (v < y) == increasing {
250 lo = mid;
251 } else {
252 hi = mid;
253 }
254 }
255 0.5 * (lo + hi)
256}
257
258#[inline]
259fn table(t: &[f32], x: f32) -> f32 {
260 let n = t.len();
261 match n {
262 0 => x,
263 1 => t[0],
264 _ => {
265 let pos = x.clamp(0.0, 1.0) * (n - 1) as f32;
266 let i = (pos as usize).min(n - 2);
267 let f = pos - i as f32;
268 t[i] + (t[i + 1] - t[i]) * f
269 }
270 }
271}
272
273fn table_inverse(t: &[f32], y: f32) -> f32 {
275 let n = t.len();
276 if n < 2 {
277 return y;
278 }
279 let increasing = t[n - 1] >= t[0];
280 let (lo_v, hi_v) = if increasing {
281 (t[0], t[n - 1])
282 } else {
283 (t[n - 1], t[0])
284 };
285 if y <= lo_v {
286 return if increasing {
288 edge_index(t, true)
289 } else {
290 edge_index(t, false)
291 };
292 }
293 if y >= hi_v {
294 return if increasing {
295 edge_index(t, false)
296 } else {
297 edge_index(t, true)
298 };
299 }
300 let (mut lo, mut hi) = (0usize, n - 1);
301 while hi - lo > 1 {
302 let mid = (lo + hi) / 2;
303 if (t[mid] < y) == increasing {
304 lo = mid;
305 } else {
306 hi = mid;
307 }
308 }
309 let (a, b) = (t[lo], t[hi]);
310 let f = if (b - a).abs() > 1e-12 {
311 (y - a) / (b - a)
312 } else {
313 0.5
314 };
315 (lo as f32 + f) / (n - 1) as f32
316}
317
318fn edge_index(t: &[f32], start: bool) -> f32 {
321 let n = t.len();
322 if start {
323 let v = t[0];
324 let i = t
325 .iter()
326 .position(|x| (*x - v).abs() > 1e-7)
327 .map_or(n - 1, |i| i - 1);
328 i as f32 / (n - 1) as f32
329 } else {
330 let v = t[n - 1];
331 let i = t
332 .iter()
333 .rposition(|x| (*x - v).abs() > 1e-7)
334 .map_or(0, |i| i + 1);
335 i as f32 / (n - 1) as f32
336 }
337}
338
339#[cfg(test)]
340mod tests {
341 use super::*;
342
343 #[test]
344 fn srgb_matches_formula() {
345 let c = srgb_trc();
346 for i in 0..=255 {
347 let v = i as f64 / 255.0;
348 let want = if v <= 0.04045 {
349 v / 12.92
350 } else {
351 ((v + 0.055) / 1.055).powf(2.4)
352 };
353 assert!((c.eval64(v) - want).abs() < 1e-12);
354 assert!((c.eval_inverse64(want) - v).abs() < 1e-9);
355 }
356 }
357
358 #[test]
359 fn parametric_inverses() {
360 let curves = [
361 Curve::Parametric {
362 kind: 0,
363 p: [2.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
364 },
365 Curve::Parametric {
366 kind: 1,
367 p: [2.0, 0.9, 0.1, 0.0, 0.0, 0.0, 0.0],
368 },
369 Curve::Parametric {
370 kind: 2,
371 p: [2.0, 0.9, 0.1, 0.05, 0.0, 0.0, 0.0],
372 },
373 srgb_trc(),
374 Curve::Parametric {
375 kind: 4,
376 p: [2.4, 0.9, 0.05, 0.08, 0.04, 0.01, 0.002],
377 },
378 Curve::Gamma(1.8),
379 ];
380 for c in &curves {
381 for i in 1..100 {
382 let x = i as f64 / 100.0;
383 let y = c.eval64(x);
384 assert!((c.eval_inverse64(y) - x).abs() < 1e-6, "{c:?} at {x}");
385 }
386 }
387 }
388
389 #[test]
390 fn table_inverse_exact_on_knots_and_between() {
391 let t = Curve::Gamma(2.2).sample(1024);
392 let c = Curve::Table(t);
393 for i in 0..=100 {
394 let x = i as f32 / 100.0;
395 let back = c.eval_inverse(c.eval(x));
396 assert!((back - x).abs() < 1e-4, "{x} -> {back}");
397 }
398 let dec = Curve::Table(vec![1.0, 0.5, 0.0]);
399 assert!((dec.eval_inverse(0.25) - 0.75).abs() < 1e-6);
400 }
401
402 #[test]
403 fn hdr_and_negative_extension() {
404 let c = srgb_trc();
405 assert!(c.eval64(2.0) > 1.0);
406 assert!((c.eval_inverse64(c.eval64(2.0)) - 2.0).abs() < 1e-9);
407 let g = Curve::Gamma(2.2);
408 assert!((g.eval64(-0.5) + 0.5f64.powf(2.2)).abs() < 1e-12);
409 }
410}