Skip to main content

linear_srgb/
lut.rs

1//! Lookup table (LUT) based sRGB conversions.
2//!
3//! This module provides two types of lookup tables:
4//!
5//! ## Build-time Tables (Recommended)
6//!
7//! `SrgbConverter` uses pre-computed const tables embedded in the binary:
8//! - **Zero initialization cost** - tables exist at compile time
9//! - **8-bit linearization** (256 entries, 1KB): Direct lookup for sRGB u8 → linear f32
10//! - **12-bit encoding** (4096 entries, 16KB): Interpolated lookup for linear f32 → sRGB f32
11//!
12//! ## Runtime Tables (For Custom Bit Depths)
13//!
14//! `LinearizationTable` and `EncodingTable` generate tables at runtime:
15//! - Use when you need non-standard bit depths (10-bit, 16-bit, etc.)
16//! - Incur one-time allocation and computation cost
17//! - Table sizes:
18//!   - 8-bit: 256 entries (1KB for f32)
19//!   - 10-bit: 1024 entries (4KB for f32)
20//!   - 12-bit: 4096 entries (16KB for f32)
21//!   - 16-bit: 65536 entries (256KB for f32)
22
23#[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; // provides ceil/floor via libm in no_std
30
31/// Pre-computed linearization table for sRGB to linear conversion.
32pub struct LinearizationTable<const N: usize> {
33    table: Box<[f32; N]>,
34}
35
36impl<const N: usize> LinearizationTable<N> {
37    /// Create a linearization table for the given bit depth.
38    ///
39    /// N must equal 2^BIT_DEPTH (e.g., N=256 for 8-bit, N=65536 for 16-bit).
40    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        // Convert boxed slice to boxed array (safe - size is guaranteed by const N)
50        let table: Box<[f32; N]> = table.try_into().expect("size mismatch");
51
52        Self { table }
53    }
54
55    /// Direct lookup (no interpolation) - for exact bit-depth matches.
56    #[inline]
57    pub fn lookup(&self, index: usize) -> f32 {
58        self.table[index.min(N - 1)]
59    }
60
61    /// Get the raw table for custom interpolation.
62    #[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
74/// Pre-computed encoding table for linear to sRGB conversion.
75pub struct EncodingTable<const N: usize> {
76    table: Box<[f32; N]>,
77}
78
79impl<const N: usize> EncodingTable<N> {
80    /// Create an encoding table for the given resolution.
81    ///
82    /// Higher N means finer granularity for interpolation.
83    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        // Convert boxed slice to boxed array (safe - size is guaranteed by const N)
93        let table: Box<[f32; N]> = table.try_into().expect("size mismatch");
94
95        Self { table }
96    }
97
98    /// Direct lookup (no interpolation).
99    #[inline]
100    pub fn lookup(&self, index: usize) -> f32 {
101        self.table[index.min(N - 1)]
102    }
103
104    /// Get the raw table for custom interpolation.
105    #[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/// Linear interpolation in a float LUT.
118///
119/// `x` should be in [0, 1]. Values outside are clamped.
120/// Uses FMA for the interpolation calculation.
121#[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    // Safety: upper and lower are bounded by table.len() - 1 due to clamping
130    let tu = table[upper.min(table.len() - 1)];
131    let tl = table[lower];
132
133    let diff = upper as f32 - value;
134
135    // result = tl * diff + tu * (1 - diff)
136    // Using FMA: neg_mlaf(tu, tu, diff) = tu - tu*diff = tu*(1-diff)
137    // Then: mlaf(tu*(1-diff), tl, diff) = tu*(1-diff) + tl*diff
138    mlaf(neg_mlaf(tu, tu, diff), tl, diff)
139}
140
141/// Linear interpolation in a u16 LUT using fixed-point arithmetic.
142///
143/// Avoids floating-point entirely for integer-only pipelines.
144#[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    // Safety: upper and lower are bounded
154    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
161/// Pre-computed 8-bit linearization table (256 entries).
162pub type LinearTable8 = LinearizationTable<256>;
163
164/// Pre-computed 10-bit linearization table (1024 entries).
165pub type LinearTable10 = LinearizationTable<1024>;
166
167/// Pre-computed 12-bit linearization table (4096 entries).
168pub type LinearTable12 = LinearizationTable<4096>;
169
170/// Pre-computed 16-bit linearization table (65536 entries).
171pub type LinearTable16 = LinearizationTable<65536>;
172
173/// Pre-computed 8-bit encoding table (256 entries).
174pub type EncodeTable8 = EncodingTable<256>;
175
176/// Pre-computed 12-bit encoding table (4096 entries).
177pub type EncodeTable12 = EncodingTable<4096>;
178
179/// Pre-computed 16-bit encoding table (65536 entries).
180pub type EncodeTable16 = EncodingTable<65536>;
181
182/// Converter using pre-computed const LUTs for fast batch conversion.
183///
184/// This is a **zero-sized type** - the tables are embedded in the binary at
185/// compile time with no runtime initialization cost. This is the recommended
186/// approach for standard 8-bit sRGB workflows.
187///
188/// # Example
189///
190/// ```rust
191/// use linear_srgb::lut::SrgbConverter;
192///
193/// let conv = SrgbConverter::new();  // No allocation, just a marker type
194///
195/// // Fast 8-bit lookups
196/// let linear = conv.srgb_u8_to_linear(128);
197/// let srgb = conv.linear_to_srgb_u8(linear);
198/// ```
199///
200/// # Implementation Details
201///
202/// - Uses a 256-entry const table for sRGB u8 → linear f32 (direct lookup)
203/// - Uses a 4096-entry const table for linear f32 → sRGB f32 (linear interpolation)
204pub struct SrgbConverter;
205
206impl SrgbConverter {
207    /// Create a new converter.
208    ///
209    /// This is a no-op since tables are const and embedded in binary.
210    #[inline]
211    pub const fn new() -> Self {
212        Self
213    }
214
215    /// Convert 8-bit sRGB to linear using direct table lookup.
216    #[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    /// Convert linear to sRGB using table interpolation.
222    #[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    /// Convert linear to 8-bit sRGB using direct LUT lookup.
228    ///
229    /// Uses a 4096-entry const u8 table — no interpolation or transcendental math.
230    #[inline]
231    pub fn linear_to_srgb_u8(&self, linear: f32) -> u8 {
232        crate::scalar::linear_to_srgb_u8(linear)
233    }
234
235    /// Batch convert sRGB u8 values to linear f32.
236    #[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    /// Batch convert linear f32 values to sRGB u8.
245    ///
246    /// Uses [`crate::default::linear_to_srgb_u8_slice`] for SIMD-accelerated
247    /// index computation with LUT lookup.
248    #[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        // Check boundaries
272        assert_eq!(table.lookup(0), 0.0);
273        assert!((table.lookup(255) - 1.0).abs() < 1e-6);
274
275        // Check middle value
276        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        // Test interpolation at exact points
293        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        // Compare LUT to direct computation
325        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        // Create a simple linear ramp table
341        let table: Vec<u16> = (0..=255).map(|i| (i * 257) as u16).collect();
342
343        // Input 0 should give 0
344        assert_eq!(lut_interp_linear_u16(0, &table), 0);
345
346        // Input max should give max
347        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}