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
29pub struct LinearizationTable<const N: usize> {
31 table: Box<[f32; N]>,
32}
33
34impl<const N: usize> LinearizationTable<N> {
35 pub fn new() -> Self {
39 let mut table = vec![0.0f32; N].into_boxed_slice();
40 let max_value = (N - 1) as f64;
41
42 for (i, entry) in table.iter_mut().enumerate() {
43 let srgb = i as f64 / max_value;
44 *entry = srgb_to_linear_f64(srgb) as f32;
45 }
46
47 let table: Box<[f32; N]> = table.try_into().expect("size mismatch");
49
50 Self { table }
51 }
52
53 #[inline]
55 pub fn lookup(&self, index: usize) -> f32 {
56 self.table[index.min(N - 1)]
57 }
58
59 #[inline]
61 pub fn as_slice(&self) -> &[f32] {
62 &self.table[..]
63 }
64}
65
66impl<const N: usize> Default for LinearizationTable<N> {
67 fn default() -> Self {
68 Self::new()
69 }
70}
71
72pub struct EncodingTable<const N: usize> {
74 table: Box<[f32; N]>,
75}
76
77impl<const N: usize> EncodingTable<N> {
78 pub fn new() -> Self {
82 let mut table = vec![0.0f32; N].into_boxed_slice();
83 let max_value = (N - 1) as f64;
84
85 for (i, entry) in table.iter_mut().enumerate() {
86 let linear = i as f64 / max_value;
87 *entry = linear_to_srgb_f64(linear) as f32;
88 }
89
90 let table: Box<[f32; N]> = table.try_into().expect("size mismatch");
92
93 Self { table }
94 }
95
96 #[inline]
98 pub fn lookup(&self, index: usize) -> f32 {
99 self.table[index.min(N - 1)]
100 }
101
102 #[inline]
104 pub fn as_slice(&self) -> &[f32] {
105 &self.table[..]
106 }
107}
108
109impl<const N: usize> Default for EncodingTable<N> {
110 fn default() -> Self {
111 Self::new()
112 }
113}
114
115#[inline]
120pub fn lut_interp_linear_float(x: f32, table: &[f32]) -> f32 {
121 let x = x.clamp(0.0, 1.0);
122 let value = x * (table.len() - 1) as f32;
123
124 let upper = value.ceil() as usize;
125 let lower = value.floor() as usize;
126
127 let tu = table[upper.min(table.len() - 1)];
129 let tl = table[lower];
130
131 let diff = upper as f32 - value;
132
133 mlaf(neg_mlaf(tu, tu, diff), tl, diff)
137}
138
139#[inline]
143pub fn lut_interp_linear_u16(input_value: u16, table: &[u16]) -> u16 {
144 let table_len = table.len() as u32;
145 let mut value: u32 = input_value as u32 * (table_len - 1);
146
147 let upper = value.div_ceil(65535) as usize;
148 let lower = (value / 65535) as usize;
149 let interp: u32 = value % 65535;
150
151 let upper = upper.min(table.len() - 1);
153 let lower = lower.min(table.len() - 1);
154
155 value = (table[upper] as u32 * interp + table[lower] as u32 * (65535 - interp)) / 65535;
156 value as u16
157}
158
159pub type LinearTable8 = LinearizationTable<256>;
161
162pub type LinearTable10 = LinearizationTable<1024>;
164
165pub type LinearTable12 = LinearizationTable<4096>;
167
168pub type LinearTable16 = LinearizationTable<65536>;
170
171pub type EncodeTable8 = EncodingTable<256>;
173
174pub type EncodeTable12 = EncodingTable<4096>;
176
177pub type EncodeTable16 = EncodingTable<65536>;
179
180pub struct SrgbConverter;
203
204impl SrgbConverter {
205 #[inline]
209 pub const fn new() -> Self {
210 Self
211 }
212
213 #[inline]
215 pub fn srgb_u8_to_linear(&self, value: u8) -> f32 {
216 crate::const_luts::LINEAR_TABLE_8[value as usize]
217 }
218
219 #[inline]
221 pub fn linear_to_srgb(&self, linear: f32) -> f32 {
222 lut_interp_linear_float(linear, &crate::const_luts::ENCODE_TABLE_12)
223 }
224
225 #[inline]
229 pub fn linear_to_srgb_u8(&self, linear: f32) -> u8 {
230 crate::scalar::linear_to_srgb_u8(linear)
231 }
232
233 #[inline]
235 pub fn batch_srgb_to_linear(&self, input: &[u8], output: &mut [f32]) {
236 assert_eq!(input.len(), output.len());
237 for (i, o) in input.iter().zip(output.iter_mut()) {
238 *o = self.srgb_u8_to_linear(*i);
239 }
240 }
241
242 #[inline]
247 pub fn batch_linear_to_srgb(&self, input: &[f32], output: &mut [u8]) {
248 crate::simd::linear_to_srgb_u8_slice(input, output);
249 }
250}
251
252impl Default for SrgbConverter {
253 fn default() -> Self {
254 Self::new()
255 }
256}
257
258#[cfg(test)]
259mod tests {
260 use super::*;
261
262 #[cfg(not(feature = "std"))]
263 use alloc::{vec, vec::Vec};
264
265 #[test]
266 fn test_linearization_table_8bit() {
267 let table = LinearTable8::new();
268
269 assert_eq!(table.lookup(0), 0.0);
271 assert!((table.lookup(255) - 1.0).abs() < 1e-6);
272
273 let mid = table.lookup(128);
275 assert!(mid > 0.0 && mid < 1.0);
276 }
277
278 #[test]
279 fn test_encoding_table() {
280 let table = EncodeTable12::new();
281
282 assert_eq!(table.lookup(0), 0.0);
283 assert!((table.lookup(4095) - 1.0).abs() < 1e-5);
284 }
285
286 #[test]
287 fn test_lut_interpolation() {
288 let table = EncodeTable12::new();
289
290 let result = lut_interp_linear_float(0.0, table.as_slice());
292 assert!((result - 0.0).abs() < 1e-6);
293
294 let result = lut_interp_linear_float(1.0, table.as_slice());
295 assert!((result - 1.0).abs() < 1e-5);
296 }
297
298 #[test]
299 fn test_converter_roundtrip() {
300 let conv = SrgbConverter::new();
301
302 for i in 0..=255u8 {
303 let linear = conv.srgb_u8_to_linear(i);
304 let back = conv.linear_to_srgb_u8(linear);
305 assert!(
306 (i as i32 - back as i32).abs() <= 1,
307 "Roundtrip failed for {}: {} -> {} -> {}",
308 i,
309 i,
310 linear,
311 back
312 );
313 }
314 }
315
316 #[test]
317 fn test_lut_vs_direct() {
318 use crate::scalar::srgb_to_linear;
319
320 let table = LinearTable8::new();
321
322 for i in 0..=255u8 {
324 let lut_result = table.lookup(i as usize);
325 let direct_result = srgb_to_linear(i as f32 / 255.0);
326 assert!(
327 (lut_result - direct_result).abs() < 1e-5,
328 "Mismatch at {}: LUT={}, direct={}",
329 i,
330 lut_result,
331 direct_result
332 );
333 }
334 }
335
336 #[test]
337 fn test_u16_interpolation() {
338 let table: Vec<u16> = (0..=255).map(|i| (i * 257) as u16).collect();
340
341 assert_eq!(lut_interp_linear_u16(0, &table), 0);
343
344 assert_eq!(lut_interp_linear_u16(65535, &table), 65535);
346 }
347
348 #[test]
349 fn test_batch_conversion() {
350 let conv = SrgbConverter::new();
351
352 let input: Vec<u8> = (0..=255).collect();
353 let mut linear = vec![0.0f32; 256];
354 let mut back = vec![0u8; 256];
355
356 conv.batch_srgb_to_linear(&input, &mut linear);
357 conv.batch_linear_to_srgb(&linear, &mut back);
358
359 for i in 0..256 {
360 assert!(
361 (input[i] as i32 - back[i] as i32).abs() <= 1,
362 "Batch roundtrip failed at {}",
363 i
364 );
365 }
366 }
367}