#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, vec};
use crate::mlaf::{mlaf, neg_mlaf};
use crate::scalar::{linear_to_srgb_f64, srgb_to_linear_f64};
#[allow(unused_imports)]
use num_traits::Float;
pub struct LinearizationTable<const N: usize> {
table: Box<[f32; N]>,
}
impl<const N: usize> LinearizationTable<N> {
pub fn new() -> Self {
let mut table = vec![0.0f32; N].into_boxed_slice();
let max_value = (N - 1) as f64;
for (i, entry) in table.iter_mut().enumerate() {
let srgb = i as f64 / max_value;
*entry = srgb_to_linear_f64(srgb) as f32;
}
let table: Box<[f32; N]> = table.try_into().expect("size mismatch");
Self { table }
}
#[inline]
pub fn lookup(&self, index: usize) -> f32 {
self.table[index.min(N - 1)]
}
#[inline]
pub fn as_slice(&self) -> &[f32] {
&self.table[..]
}
}
impl<const N: usize> Default for LinearizationTable<N> {
fn default() -> Self {
Self::new()
}
}
pub struct EncodingTable<const N: usize> {
table: Box<[f32; N]>,
}
impl<const N: usize> EncodingTable<N> {
pub fn new() -> Self {
let mut table = vec![0.0f32; N].into_boxed_slice();
let max_value = (N - 1) as f64;
for (i, entry) in table.iter_mut().enumerate() {
let linear = i as f64 / max_value;
*entry = linear_to_srgb_f64(linear) as f32;
}
let table: Box<[f32; N]> = table.try_into().expect("size mismatch");
Self { table }
}
#[inline]
pub fn lookup(&self, index: usize) -> f32 {
self.table[index.min(N - 1)]
}
#[inline]
pub fn as_slice(&self) -> &[f32] {
&self.table[..]
}
}
impl<const N: usize> Default for EncodingTable<N> {
fn default() -> Self {
Self::new()
}
}
#[inline]
pub fn lut_interp_linear_float(x: f32, table: &[f32]) -> f32 {
let x = x.clamp(0.0, 1.0);
let value = x * (table.len() - 1) as f32;
let upper = value.ceil() as usize;
let lower = value.floor() as usize;
let tu = table[upper.min(table.len() - 1)];
let tl = table[lower];
let diff = upper as f32 - value;
mlaf(neg_mlaf(tu, tu, diff), tl, diff)
}
#[inline]
pub fn lut_interp_linear_u16(input_value: u16, table: &[u16]) -> u16 {
let table_len = table.len() as u32;
let mut value: u32 = input_value as u32 * (table_len - 1);
let upper = value.div_ceil(65535) as usize;
let lower = (value / 65535) as usize;
let interp: u32 = value % 65535;
let upper = upper.min(table.len() - 1);
let lower = lower.min(table.len() - 1);
value = (table[upper] as u32 * interp + table[lower] as u32 * (65535 - interp)) / 65535;
value as u16
}
pub type LinearTable8 = LinearizationTable<256>;
pub type LinearTable10 = LinearizationTable<1024>;
pub type LinearTable12 = LinearizationTable<4096>;
#[deprecated(
since = "0.7.0",
note = "use default::srgb_u16_to_linear instead (faster, shared LUT)"
)]
pub type LinearTable16 = LinearizationTable<65536>;
pub type EncodeTable8 = EncodingTable<256>;
pub type EncodeTable12 = EncodingTable<4096>;
#[deprecated(
since = "0.7.0",
note = "use default::linear_to_srgb_u16 or linear_to_srgb_u16_fast instead"
)]
pub type EncodeTable16 = EncodingTable<65536>;
pub struct SrgbConverter;
impl SrgbConverter {
#[inline]
pub const fn new() -> Self {
Self
}
#[inline]
pub fn srgb_u8_to_linear(&self, value: u8) -> f32 {
crate::const_luts::linear_table_8()[value as usize]
}
#[inline]
pub fn linear_to_srgb(&self, linear: f32) -> f32 {
lut_interp_linear_float(linear, crate::const_luts::encode_table_12())
}
#[inline]
pub fn linear_to_srgb_u8(&self, linear: f32) -> u8 {
crate::scalar::linear_to_srgb_u8(linear)
}
#[inline]
pub fn batch_srgb_to_linear(&self, input: &[u8], output: &mut [f32]) {
assert_eq!(input.len(), output.len());
for (i, o) in input.iter().zip(output.iter_mut()) {
*o = self.srgb_u8_to_linear(*i);
}
}
#[inline]
pub fn batch_linear_to_srgb(&self, input: &[f32], output: &mut [u8]) {
crate::simd::linear_to_srgb_u8_slice(input, output);
}
}
impl Default for SrgbConverter {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};
#[test]
fn test_linearization_table_8bit() {
let table = LinearTable8::new();
assert_eq!(table.lookup(0), 0.0);
assert!((table.lookup(255) - 1.0).abs() < 1e-6);
let mid = table.lookup(128);
assert!(mid > 0.0 && mid < 1.0);
}
#[test]
fn test_encoding_table() {
let table = EncodeTable12::new();
assert_eq!(table.lookup(0), 0.0);
assert!((table.lookup(4095) - 1.0).abs() < 1e-5);
}
#[test]
fn test_lut_interpolation() {
let table = EncodeTable12::new();
let result = lut_interp_linear_float(0.0, table.as_slice());
assert!((result - 0.0).abs() < 1e-6);
let result = lut_interp_linear_float(1.0, table.as_slice());
assert!((result - 1.0).abs() < 1e-5);
}
#[test]
fn test_converter_roundtrip() {
let conv = SrgbConverter::new();
for i in 0..=255u8 {
let linear = conv.srgb_u8_to_linear(i);
let back = conv.linear_to_srgb_u8(linear);
assert!(
(i as i32 - back as i32).abs() <= 1,
"Roundtrip failed for {}: {} -> {} -> {}",
i,
i,
linear,
back
);
}
}
#[test]
fn test_lut_vs_direct() {
use crate::scalar::srgb_to_linear;
let table = LinearTable8::new();
for i in 0..=255u8 {
let lut_result = table.lookup(i as usize);
let direct_result = srgb_to_linear(i as f32 / 255.0);
assert!(
(lut_result - direct_result).abs() < 1e-5,
"Mismatch at {}: LUT={}, direct={}",
i,
lut_result,
direct_result
);
}
}
#[test]
fn test_u16_interpolation() {
let table: Vec<u16> = (0..=255).map(|i| (i * 257) as u16).collect();
assert_eq!(lut_interp_linear_u16(0, &table), 0);
assert_eq!(lut_interp_linear_u16(65535, &table), 65535);
}
#[test]
fn test_batch_conversion() {
let conv = SrgbConverter::new();
let input: Vec<u8> = (0..=255).collect();
let mut linear = vec![0.0f32; 256];
let mut back = vec![0u8; 256];
conv.batch_srgb_to_linear(&input, &mut linear);
conv.batch_linear_to_srgb(&linear, &mut back);
for i in 0..256 {
assert!(
(input[i] as i32 - back[i] as i32).abs() <= 1,
"Batch roundtrip failed at {}",
i
);
}
}
}