use std::path::Path;
use super::lut_text::{
data_points, digits, f32_bytes, find_after_keyword, quoted, table_length, three_numbers,
to_uint8,
};
use crate::error::Error;
use crate::math::Vector3;
use crate::textures::{Data3DTexture, MinFilter, TextureFilter, TextureType, Wrapping};
const LOADER: &str = "LUTCubeLoader";
#[derive(Debug, Clone)]
pub struct LutCube {
pub title: Option<String>,
pub size: u32,
pub domain_min: Vector3,
pub domain_max: Vector3,
pub texture_3d: Data3DTexture,
}
#[derive(Debug, Clone)]
pub struct LutCubeLoader {
texture_type: TextureType,
}
impl Default for LutCubeLoader {
fn default() -> Self {
Self::new()
}
}
impl LutCubeLoader {
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<LutCube, 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<LutCube, Error> {
let title = find_after_keyword(input, "TITLE", quoted).map(str::to_owned);
let size = find_after_keyword(input, "LUT_3D_SIZE", digits).ok_or_else(|| Error::Lut {
loader: LOADER,
reason: "Missing LUT_3D_SIZE information".into(),
})?;
let unsigned_byte = self.texture_type == TextureType::UnsignedByte;
let (side, length) = table_length(LOADER, size, if unsigned_byte { 1 } else { 4 })?;
let mut domain_min = [0.0, 0.0, 0.0];
let mut domain_max = [1.0, 1.0, 1.0];
if let Some(min) = find_after_keyword(input, "DOMAIN_MIN", three_numbers) {
domain_min = min;
}
if let Some(max) = find_after_keyword(input, "DOMAIN_MAX", three_numbers) {
domain_max = max;
}
if domain_min[0] > domain_max[0]
|| domain_min[1] > domain_max[1]
|| domain_min[2] > domain_max[2]
{
return Err(Error::Lut {
loader: LOADER,
reason: "Invalid input domain".into(),
});
}
let scale = if unsigned_byte { 255.0 } else { 1.0 };
let values =
data_points(input).flat_map(|[r, g, b]| [r * scale, g * scale, b * scale, scale]);
let (bytes, format) = if unsigned_byte {
let mut data = vec![0u8; length];
for (slot, value) in data.iter_mut().zip(values) {
*slot = to_uint8(value);
}
(data, wgpu::TextureFormat::Rgba8Unorm)
} else {
let mut data = vec![0f32; length];
for (slot, value) in data.iter_mut().zip(values) {
*slot = value as f32;
}
(f32_bytes(&data), wgpu::TextureFormat::Rgba32Float)
};
Ok(LutCube {
title,
size: side,
domain_min: Vector3::new(domain_min[0], domain_min[1], domain_min[2]),
domain_max: Vector3::new(domain_max[0], domain_max[1], domain_max[2]),
texture_3d: lut_texture(bytes, side, format),
})
}
}
pub(super) fn lut_texture(bytes: Vec<u8>, size: u32, format: wgpu::TextureFormat) -> Data3DTexture {
let texture = Data3DTexture::new(bytes, size, size, size, format);
texture.set_min_filter(MinFilter::Linear);
texture.set_mag_filter(TextureFilter::Linear);
texture.set_wrapping(
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
);
texture
}