1#[cfg(not(feature = "std"))]
24use alloc::{boxed::Box, vec};
25
26use crate::mlaf::{mlaf, neg_mlaf};
27use crate::scalar::{linear_to_srgb_f64, srgb_to_linear_f64};
28#[allow(unused_imports)]
29use num_traits::Float; pub struct LinearizationTable<const N: usize> {
33 table: Box<[f32; N]>,
34}
35
36impl<const N: usize> LinearizationTable<N> {
37 pub fn new() -> Self {
41 let mut table = vec![0.0f32; N].into_boxed_slice();
42 let max_value = (N - 1) as f64;
43
44 for (i, entry) in table.iter_mut().enumerate() {
45 let srgb = i as f64 / max_value;
46 *entry = srgb_to_linear_f64(srgb) as f32;
47 }
48
49 let table: Box<[f32; N]> = table.try_into().expect("size mismatch");
51
52 Self { table }
53 }
54
55 #[inline]
57 pub fn lookup(&self, index: usize) -> f32 {
58 self.table[index.min(N - 1)]
59 }
60
61 #[inline]
63 pub fn as_slice(&self) -> &[f32] {
64 &self.table[..]
65 }
66}
67
68impl<const N: usize> Default for LinearizationTable<N> {
69 fn default() -> Self {
70 Self::new()
71 }
72}
73
74pub struct EncodingTable<const N: usize> {
76 table: Box<[f32; N]>,
77}
78
79impl<const N: usize> EncodingTable<N> {
80 pub fn new() -> Self {
84 let mut table = vec![0.0f32; N].into_boxed_slice();
85 let max_value = (N - 1) as f64;
86
87 for (i, entry) in table.iter_mut().enumerate() {
88 let linear = i as f64 / max_value;
89 *entry = linear_to_srgb_f64(linear) as f32;
90 }
91
92 let table: Box<[f32; N]> = table.try_into().expect("size mismatch");
94
95 Self { table }
96 }
97
98 #[inline]
100 pub fn lookup(&self, index: usize) -> f32 {
101 self.table[index.min(N - 1)]
102 }
103
104 #[inline]
106 pub fn as_slice(&self) -> &[f32] {
107 &self.table[..]
108 }
109}
110
111impl<const N: usize> Default for EncodingTable<N> {
112 fn default() -> Self {
113 Self::new()
114 }
115}
116
117#[inline]
122pub fn lut_interp_linear_float(x: f32, table: &[f32]) -> f32 {
123 let x = x.clamp(0.0, 1.0);
124 let value = x * (table.len() - 1) as f32;
125
126 let upper = value.ceil() as usize;
127 let lower = value.floor() as usize;
128
129 let tu = table[upper.min(table.len() - 1)];
131 let tl = table[lower];
132
133 let diff = upper as f32 - value;
134
135 mlaf(neg_mlaf(tu, tu, diff), tl, diff)
139}
140
141#[inline]
145pub fn lut_interp_linear_u16(input_value: u16, table: &[u16]) -> u16 {
146 let table_len = table.len() as u32;
147 let mut value: u32 = input_value as u32 * (table_len - 1);
148
149 let upper = value.div_ceil(65535) as usize;
150 let lower = (value / 65535) as usize;
151 let interp: u32 = value % 65535;
152
153 let upper = upper.min(table.len() - 1);
155 let lower = lower.min(table.len() - 1);
156
157 value = (table[upper] as u32 * interp + table[lower] as u32 * (65535 - interp)) / 65535;
158 value as u16
159}
160
161pub type LinearTable8 = LinearizationTable<256>;
163
164pub type LinearTable10 = LinearizationTable<1024>;
166
167pub type LinearTable12 = LinearizationTable<4096>;
169
170pub type LinearTable16 = LinearizationTable<65536>;
172
173pub type EncodeTable8 = EncodingTable<256>;
175
176pub type EncodeTable12 = EncodingTable<4096>;
178
179pub type EncodeTable16 = EncodingTable<65536>;
181
182pub struct SrgbConverter;
205
206impl SrgbConverter {
207 #[inline]
211 pub const fn new() -> Self {
212 Self
213 }
214
215 #[inline]
217 pub fn srgb_u8_to_linear(&self, value: u8) -> f32 {
218 crate::const_luts::LINEAR_TABLE_8[value as usize]
219 }
220
221 #[inline]
223 pub fn linear_to_srgb(&self, linear: f32) -> f32 {
224 lut_interp_linear_float(linear, &crate::const_luts::ENCODE_TABLE_12)
225 }
226
227 #[inline]
231 pub fn linear_to_srgb_u8(&self, linear: f32) -> u8 {
232 crate::scalar::linear_to_srgb_u8(linear)
233 }
234
235 #[inline]
237 pub fn batch_srgb_to_linear(&self, input: &[u8], output: &mut [f32]) {
238 assert_eq!(input.len(), output.len());
239 for (i, o) in input.iter().zip(output.iter_mut()) {
240 *o = self.srgb_u8_to_linear(*i);
241 }
242 }
243
244 #[inline]
249 pub fn batch_linear_to_srgb(&self, input: &[f32], output: &mut [u8]) {
250 crate::simd::linear_to_srgb_u8_slice(input, output);
251 }
252}
253
254impl Default for SrgbConverter {
255 fn default() -> Self {
256 Self::new()
257 }
258}
259
260#[cfg(test)]
261mod tests {
262 use super::*;
263
264 #[cfg(not(feature = "std"))]
265 use alloc::{vec, vec::Vec};
266
267 #[test]
268 fn test_linearization_table_8bit() {
269 let table = LinearTable8::new();
270
271 assert_eq!(table.lookup(0), 0.0);
273 assert!((table.lookup(255) - 1.0).abs() < 1e-6);
274
275 let mid = table.lookup(128);
277 assert!(mid > 0.0 && mid < 1.0);
278 }
279
280 #[test]
281 fn test_encoding_table() {
282 let table = EncodeTable12::new();
283
284 assert_eq!(table.lookup(0), 0.0);
285 assert!((table.lookup(4095) - 1.0).abs() < 1e-5);
286 }
287
288 #[test]
289 fn test_lut_interpolation() {
290 let table = EncodeTable12::new();
291
292 let result = lut_interp_linear_float(0.0, table.as_slice());
294 assert!((result - 0.0).abs() < 1e-6);
295
296 let result = lut_interp_linear_float(1.0, table.as_slice());
297 assert!((result - 1.0).abs() < 1e-5);
298 }
299
300 #[test]
301 fn test_converter_roundtrip() {
302 let conv = SrgbConverter::new();
303
304 for i in 0..=255u8 {
305 let linear = conv.srgb_u8_to_linear(i);
306 let back = conv.linear_to_srgb_u8(linear);
307 assert!(
308 (i as i32 - back as i32).abs() <= 1,
309 "Roundtrip failed for {}: {} -> {} -> {}",
310 i,
311 i,
312 linear,
313 back
314 );
315 }
316 }
317
318 #[test]
319 fn test_lut_vs_direct() {
320 use crate::scalar::srgb_to_linear;
321
322 let table = LinearTable8::new();
323
324 for i in 0..=255u8 {
326 let lut_result = table.lookup(i as usize);
327 let direct_result = srgb_to_linear(i as f32 / 255.0);
328 assert!(
329 (lut_result - direct_result).abs() < 1e-5,
330 "Mismatch at {}: LUT={}, direct={}",
331 i,
332 lut_result,
333 direct_result
334 );
335 }
336 }
337
338 #[test]
339 fn test_u16_interpolation() {
340 let table: Vec<u16> = (0..=255).map(|i| (i * 257) as u16).collect();
342
343 assert_eq!(lut_interp_linear_u16(0, &table), 0);
345
346 assert_eq!(lut_interp_linear_u16(65535, &table), 65535);
348 }
349
350 #[test]
351 fn test_batch_conversion() {
352 let conv = SrgbConverter::new();
353
354 let input: Vec<u8> = (0..=255).collect();
355 let mut linear = vec![0.0f32; 256];
356 let mut back = vec![0u8; 256];
357
358 conv.batch_srgb_to_linear(&input, &mut linear);
359 conv.batch_linear_to_srgb(&linear, &mut back);
360
361 for i in 0..256 {
362 assert!(
363 (input[i] as i32 - back[i] as i32).abs() <= 1,
364 "Batch roundtrip failed at {}",
365 i
366 );
367 }
368 }
369}