use std::path::Path;
use super::lut_cube_loader::lut_texture;
use super::lut_text::{data_points, f32_bytes, js_max, lines, number, table_length, to_uint8};
use crate::error::Error;
use crate::textures::{Data3DTexture, TextureType};
const LOADER: &str = "LUT3dlLoader";
#[derive(Debug, Clone)]
pub struct Lut3dl {
pub size: u32,
pub texture_3d: Data3DTexture,
}
#[derive(Debug, Clone)]
pub struct Lut3dlLoader {
texture_type: TextureType,
}
impl Default for Lut3dlLoader {
fn default() -> Self {
Self::new()
}
}
impl Lut3dlLoader {
pub fn new() -> Self {
Self {
texture_type: TextureType::UnsignedByte,
}
}
pub fn set_type(&mut self, texture_type: TextureType) -> Result<&mut Self, Error> {
if !matches!(texture_type, TextureType::UnsignedByte | TextureType::Float) {
return Err(Error::UnsupportedTextureType {
what: "LUT",
texture_type,
});
}
self.texture_type = texture_type;
Ok(self)
}
pub fn texture_type(&self) -> TextureType {
self.texture_type
}
pub fn load<P: AsRef<Path>>(&self, path: P) -> Result<Lut3dl, Error> {
let bytes = crate::io::read(path.as_ref())?;
let text = String::from_utf8_lossy(&bytes);
self.parse(text.strip_prefix('\u{feff}').unwrap_or(&text))
}
pub fn parse(&self, input: &str) -> Result<Lut3dl, Error> {
let grid = lines(input)
.find(|line| !line.is_empty() && line.chars().all(|c| c.is_ascii_digit() || c == ' '))
.ok_or_else(|| Error::Lut {
loader: LOADER,
reason: "Missing grid information".into(),
})?;
let grid_lines: Vec<f64> = match grid.trim() {
"" => vec![0.0],
trimmed => trimmed.split_whitespace().map(number).collect(),
};
let grid_step = grid_lines
.get(1)
.map_or(f64::NAN, |second| second - grid_lines[0]);
if grid_lines
.windows(2)
.any(|pair| grid_step != pair[1] - pair[0])
{
return Err(Error::Lut {
loader: LOADER,
reason: "Inconsistent grid size".into(),
});
}
let unsigned_byte = self.texture_type == TextureType::UnsignedByte;
let (size, length) = table_length(
LOADER,
grid_lines.len() as f64,
if unsigned_byte { 1 } else { 4 },
)?;
let side = size as usize;
let side_sq = side * side;
let mut data_float = vec![0f32; length];
let mut max_value = 0.0f64;
for (index, [r, g, b]) in data_points(input).enumerate() {
max_value = js_max(js_max(js_max(max_value, r), g), b);
let b_layer = index % side;
let g_layer = (index / side) % side;
let r_layer = (index / side_sq) % side;
let d4 = (b_layer * side_sq + g_layer * side + r_layer) * 4;
data_float[d4] = r as f32;
data_float[d4 + 1] = g as f32;
data_float[d4 + 2] = b as f32;
}
let bits = max_value.log2().ceil();
let max_bit_value = 2f64.powf(bits);
let scale = if unsigned_byte { 255.0 } else { 1.0 };
let texel = |i: usize, c: usize| f64::from(data_float[i + c]) / max_bit_value * scale;
let (bytes, format) = if unsigned_byte {
let mut data = vec![0u8; length];
for i in (0..length).step_by(4) {
data[i] = to_uint8(texel(i, 0));
data[i + 1] = to_uint8(texel(i, 1));
data[i + 2] = to_uint8(texel(i, 2));
data[i + 3] = 255;
}
(data, wgpu::TextureFormat::Rgba8Unorm)
} else {
let mut data = vec![0f32; length];
for i in (0..length).step_by(4) {
data[i] = texel(i, 0) as f32;
data[i + 1] = texel(i, 1) as f32;
data[i + 2] = texel(i, 2) as f32;
data[i + 3] = 1.0;
}
(f32_bytes(&data), wgpu::TextureFormat::Rgba32Float)
};
Ok(Lut3dl {
size,
texture_3d: lut_texture(bytes, size, format),
})
}
}