cms: share Sony SPI3D and SPIMTX readers

Port EffectCraft 492fa6de0a049c22afcc690afa36930d295261f3 readers, preserving red-fast coordinates, matrix scaling and format tolerance. Replace temporary token vectors with fixed-width parsing and bound row processing to the declared lattice.

Co-authored-by: sanyamsingh001 <314051937+sanyamsingh001@users.noreply.github.com>
Co-authored-by: Dylan M. Taylor <dylan@dylanmtaylor.com>
This commit is contained in:
bflatastic
2026-11-15 13:59:42 -05:00
committed by Dylan M. Taylor
co-authored by sanyamsingh001 Dylan M. Taylor
parent cae7c1d8a6
commit 5c0fc062b4
3 changed files with 129 additions and 0 deletions
+1
View File
@@ -31,6 +31,7 @@ pub mod math;
pub mod named;
pub mod pipeline;
pub mod profile;
pub mod sony_lut;
pub mod synth;
pub mod transform;
mod write;
+82
View File
@@ -0,0 +1,82 @@
//! Sony Imageworks SPI3D and SPIMTX text readers, extracted from EffectCraft.
//!
//! These preserve the source reader's format tolerance. SPI3D sample values are not
//! clamped or sanitized; rendering policy belongs to the caller. Duplicate coordinates
//! retain their last value, and the total row count must match the lattice volume.
/// Maximum lattice side accepted by the source reader.
pub const MAX_SIDE: usize = 129;
/// Row-major homogeneous transform with a separate normalized code-value offset.
#[derive(Clone, Debug, PartialEq)]
pub struct Matrix {
pub matrix: [f64; 16],
pub offset: [f64; 4],
}
/// A cubic RGB table, red coordinate varying fastest.
#[derive(Clone, Debug, PartialEq)]
pub struct Lattice {
pub size: usize,
pub values: Vec<[f32; 3]>,
}
/// Read exactly twelve finite SPIMTX numbers, dividing the offset column by 65535.
/// Parsing uses a fixed array, not storage proportional to an oversized input.
pub fn parse_spimtx(text: &str) -> Option<Matrix> {
let mut words = text.split_whitespace();
let mut v = [0.0f64; 12];
for value in &mut v {
*value = words.next()?.parse().ok()?;
if !value.is_finite() {
return None;
}
}
if words.next().is_some() {
return None;
}
let mut matrix = [0.0; 16];
let mut offset = [0.0; 4];
for (r, row) in v.as_chunks::<4>().0.iter().enumerate() {
matrix[r * 4..r * 4 + 3].copy_from_slice(&row[..3]);
offset[r] = row[3] / 65535.0;
}
matrix[15] = 1.0;
Some(Matrix { matrix, offset })
}
/// Read a SPI3D table with a bounded 2..=129 cubic lattice and exactly six fields per row.
/// A `SPILUT` prefix is accepted and the second header line is ignored, as in EffectCraft.
pub fn parse_spi3d(text: &str) -> Option<Lattice> {
let mut lines = text.lines().map(str::trim).filter(|l| !l.is_empty());
if !lines.next()?.starts_with("SPILUT") {
return None;
}
lines.next()?;
let mut sizes = lines.next()?.split_whitespace();
let n: usize = sizes.next()?.parse().ok()?;
let g: usize = sizes.next()?.parse().ok()?;
let b: usize = sizes.next()?.parse().ok()?;
if sizes.next().is_some() || g != n || b != n || !(2..=MAX_SIDE).contains(&n) {
return None;
}
let count = n * n * n;
let mut values = vec![[0.0f32; 3]; count];
let mut seen = 0usize;
for line in lines {
if seen == count {
return None;
}
let mut fields = line.split_whitespace();
let r: usize = fields.next()?.parse().ok()?;
let g: usize = fields.next()?.parse().ok()?;
let b: usize = fields.next()?.parse().ok()?;
let color = [fields.next()?.parse().ok()?, fields.next()?.parse().ok()?, fields.next()?.parse().ok()?];
if fields.next().is_some() || r >= n || g >= n || b >= n {
return None;
}
*values.get_mut(r + g * n + b * n * n)? = color;
seen += 1;
}
(seen == count).then_some(Lattice { size: n, values })
}
+46
View File
@@ -0,0 +1,46 @@
use craft_cms::sony_lut::{MAX_SIDE, parse_spi3d, parse_spimtx};
#[test]
fn spimtx_is_finite_exact_and_scales_offsets() {
let m = parse_spimtx("2 0 0 6553.5\n0 3 0 -65535\n0 0 4 0").unwrap();
assert_eq!(m.matrix, [2.0, 0.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0]);
assert_eq!(m.offset, [0.1, -1.0, 0.0, 0.0]);
for bad in ["", "1 0 0 0", "NaN 0 0 0 0 1 0 0 0 0 1 0", "inf 0 0 0 0 1 0 0 0 0 1 0", "1 0 0 0 0 1 0 0 0 0 1 0 9"] {
assert!(parse_spimtx(bad).is_none());
}
}
fn table() -> String {
let mut text = String::from("SPILUT 1.0\n3 3\n2 2 2\n");
// Reverse row order to prove coordinates, rather than row order, determine storage.
for i in (0..8).rev() {
let (r, g, b) = (i & 1, (i >> 1) & 1, i >> 2);
text.push_str(&format!("{r} {g} {b} {r} {g} {b}\n"));
}
text
}
#[test]
fn spi3d_red_fast_coordinates_and_limits() {
let text = table();
let lut = parse_spi3d(&text).unwrap();
assert_eq!(lut.size, 2);
for (i, row) in lut.values.iter().enumerate() {
assert_eq!(*row, [(i & 1) as f32, ((i >> 1) & 1) as f32, (i >> 2) as f32]);
}
assert!(parse_spi3d(&format!("{text}0 0 0 0 0 0\n")).is_none());
assert!(parse_spi3d("SPILUT 1.0\n3 3\n2 2 2\n0 0 0 0 0 0\n").is_none());
assert!(parse_spi3d(&format!("SPILUT 1.0\n3 3\n{0} {0} {0}\n", MAX_SIDE + 1)).is_none());
assert!(parse_spi3d(&text.replace("2 2 2", "2 2 3")).is_none());
assert!(parse_spi3d(&text.replace("0 0 0 0 0 0", "2 0 0 0 0 0")).is_none());
assert!(parse_spi3d(&text.replace("0 0 0 0 0 0", "0 0 0 0 0 0 extra")).is_none());
}
#[test]
fn inherited_tolerance_is_explicit() {
let text = "SPILUT alternative\nignored\n2 2 2\n".to_owned() + &"0 0 0 -1 2 NaN\n".repeat(8);
let lut = parse_spi3d(&text).unwrap();
assert_eq!(&lut.values[0][..2], &[-1.0, 2.0]);
assert!(lut.values[0][2].is_nan());
assert_eq!(lut.values[1], [0.0; 3]);
}