From 9fd926dc93be7d4723e6eb4f484c7233d91d3e94 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 15 Nov 2026 13:43:03 -0500 Subject: [PATCH] cms: share EffectCraft SPI1D table parsing Port the SPI1D reader introduced by EffectCraft 492fa6de0a049c22afcc690afa36930d295261f3. Validate the declared table before storing samples and accumulate RGB entries directly to bound retention while preserving accepted format syntax. Co-authored-by: Dylan M. Taylor --- crates/cms/src/lib.rs | 1 + crates/cms/src/spi1d.rs | 59 +++++++++++++++++++++++++++++++++++++++ crates/cms/tests/spi1d.rs | 32 +++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 crates/cms/src/spi1d.rs create mode 100644 crates/cms/tests/spi1d.rs diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs index 5e8a1e268..6302b042e 100644 --- a/crates/cms/src/lib.rs +++ b/crates/cms/src/lib.rs @@ -31,6 +31,7 @@ pub mod math; pub mod named; pub mod pipeline; pub mod profile; +pub mod spi1d; pub mod synth; pub mod transform; mod write; diff --git a/crates/cms/src/spi1d.rs b/crates/cms/src/spi1d.rs new file mode 100644 index 000000000..ea77bdc88 --- /dev/null +++ b/crates/cms/src/spi1d.rs @@ -0,0 +1,59 @@ +//! Sony Imageworks SPI1D tables, preserving EffectCraft's permissive header syntax. +//! +//! Unknown header lines and trailing text after the closing brace are ignored. Table +//! samples are not clamped or sanitized: callers own their numerical rendering policy. +//! The declared length is validated before storing samples; surplus samples are refused. + +/// Largest accepted table (the existing EffectCraft interpolation limit). +pub const MAX_ENTRIES: usize = 1 << 24; + +/// A one-dimensional RGB lookup table and its common input domain. +#[derive(Clone, Debug, PartialEq)] +pub struct Table { + pub values: Vec<[f32; 3]>, + pub domain: [f32; 2], +} + +/// Parse a SPI1D table with one or three components, expanding monochrome samples to RGB. +/// No allocation is based only on the claimed length. At most `MAX_ENTRIES` RGB entries +/// are retained. Missing final braces remain accepted, as in the source reader. +pub fn parse(text: &str) -> Option { + let (mut from, mut len, mut comps) = ([0.0f32, 1.0], 0usize, 1usize); + let mut values = Vec::new(); + let mut body = false; + let mut sample = [0.0; 3]; + let mut channel = 0; + for line in text.lines().map(str::trim).filter(|l| !l.is_empty()) { + if body { + if line.starts_with('}') { + break; + } + for t in line.split_whitespace() { + if values.len() == len { + return None; + } + sample[channel] = t.parse().ok()?; + channel += 1; + if channel == comps { + values.push(if comps == 1 { [sample[0]; 3] } else { sample }); + channel = 0; + } + } + continue; + } + let mut t = line.split_whitespace(); + match t.next()? { + "{" => { + if !(2..=MAX_ENTRIES).contains(&len) || !matches!(comps, 1 | 3) || from[1] <= from[0] || from.iter().any(|v| !v.is_finite()) { + return None; + } + body = true; + } + "From" => from = [t.next()?.parse().ok()?, t.next()?.parse().ok()?], + "Length" => len = t.next()?.parse().ok()?, + "Components" => comps = t.next()?.parse().ok()?, + _ => {} + } + } + (body && values.len() == len && channel == 0).then_some(Table { values, domain: from }) +} diff --git a/crates/cms/tests/spi1d.rs b/crates/cms/tests/spi1d.rs new file mode 100644 index 000000000..f4e27d051 --- /dev/null +++ b/crates/cms/tests/spi1d.rs @@ -0,0 +1,32 @@ +use craft_cms::spi1d::{MAX_ENTRIES, Table, parse}; + +#[test] +fn monochrome_and_three_component_samples_preserve_domain() { + assert_eq!( + parse("Version 1\nFrom -1 2\nLength 2\nComponents 1\n{\n-2\n3\n}"), + Some(Table { values: vec![[-2.0; 3], [3.0; 3]], domain: [-1.0, 2.0] }) + ); + assert_eq!( + parse("Length 2\nComponents 3\n{\n0 1\n2 3 4 5\n}"), + Some(Table { values: vec![[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]], domain: [0.0, 1.0] }) + ); +} + +#[test] +fn legacy_header_and_eof_tolerance_is_preserved() { + let table = parse("Ignored arbitrary header\nLength 2 extra\n{\n0 1"); + assert!(table.is_some()); + assert_eq!(table, parse("Length 2\n{\n0 1\n} ignored\ninvalid trailing text")); + assert!(parse("Length 2\n{\nNaN inf\n}").is_some(), "sample sanitization belongs to the caller"); +} + +#[test] +fn invalid_headers_partial_and_surplus_tables_are_refused() { + for header in ["Length 1", "Length 2\nComponents 2", "Length 2\nFrom 2 1", "Length 2\nFrom NaN 1"] { + assert!(parse(&format!("{header}\n{{\n0 1\n}}")).is_none()); + } + assert!(parse(&format!("Length {}\n{{\n0 1", MAX_ENTRIES + 1)).is_none()); + assert!(parse("Length 2\nComponents 3\n{\n0 1 2 3 4").is_none()); + assert!(parse("Length 2\n{\n0 1 2").is_none()); + assert!(parse("Length 2\n{\n").is_none()); +}