diff --git a/crates/cms/src/sony_lut.rs b/crates/cms/src/sony_lut.rs index cc3f8fb00..38936c817 100644 --- a/crates/cms/src/sony_lut.rs +++ b/crates/cms/src/sony_lut.rs @@ -61,22 +61,74 @@ pub fn parse_spi3d(text: &str) -> Option { return None; } let count = n * n * n; - let mut values = vec![[0.0f32; 3]; count]; + // Validate every row and the row count before reserving the lattice, so a short or + // malformed file never allocates the table its header claims. The rows are then read + // again in file order, which keeps the last value of a duplicate coordinate. let mut seen = 0usize; - for line in lines { + for line in lines.clone() { 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; + spi3d_row(line, n)?; seen += 1; } - (seen == count).then_some(Lattice { size: n, values }) + if seen != count { + return None; + } + #[cfg(test)] + tests::LATTICES_RESERVED.with(|r| r.set(r.get() + 1)); + let mut values = vec![[0.0f32; 3]; count]; + for line in lines { + let (index, color) = spi3d_row(line, n)?; + *values.get_mut(index)? = color; + } + Some(Lattice { size: n, values }) +} + +/// One `r g b R G B` row of a side-`n` lattice: its red-fastest index and its colour. +fn spi3d_row(line: &str, n: usize) -> Option<(usize, [f32; 3])> { + 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; + } + Some((r + g * n + b * n * n, color)) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] +mod tests { + use super::*; + + thread_local! { + /// How many lattices [`parse_spi3d`] has reserved on this thread. + pub(super) static LATTICES_RESERVED: std::cell::Cell = const { std::cell::Cell::new(0) }; + } + + fn reserved() -> usize { + LATTICES_RESERVED.with(std::cell::Cell::get) + } + + #[test] + fn spi3d_reserves_the_lattice_only_once_its_rows_are_valid() { + // A header-only maximal claim, a short table and a full count of malformed rows. + let max = format!("SPILUT 1.0\n3 3\n{MAX_SIDE} {MAX_SIDE} {MAX_SIDE}\n"); + let short = "SPILUT 1.0\n3 3\n2 2 2\n0 0 0 1 1 1\n".to_owned(); + let junk = "SPILUT 1.0\n3 3\n2 2 2\n".to_owned() + &"x\n".repeat(8); + for bad in [max, short, junk] { + let before = reserved(); + assert!(parse_spi3d(&bad).is_none()); + assert_eq!(reserved(), before, "{bad}"); + } + let mut ok = "SPILUT 1.0\n3 3\n2 2 2\n".to_owned(); + for i in 0..8 { + ok += &format!("{} {} {} 0 0 0\n", i & 1, (i >> 1) & 1, i >> 2); + } + let before = reserved(); + assert!(parse_spi3d(&ok).is_some()); + assert_eq!(reserved(), before + 1); + } } diff --git a/crates/cms/tests/sony_lut.rs b/crates/cms/tests/sony_lut.rs index 065792a27..3add8f263 100644 --- a/crates/cms/tests/sony_lut.rs +++ b/crates/cms/tests/sony_lut.rs @@ -44,3 +44,19 @@ fn inherited_tolerance_is_explicit() { assert!(lut.values[0][2].is_nan()); assert_eq!(lut.values[1], [0.0; 3]); } + +#[test] +fn spi3d_duplicate_coordinates_keep_the_last_value() { + // Forward order: the last row (1 1 1) is replaced by a second 0 0 0, which wins, and 1 1 1 + // keeps the lattice's zero. + let mut text = String::from("SPILUT 1.0\n3 3\n2 2 2\n"); + for i in 0..7 { + let (r, g, b) = (i & 1, (i >> 1) & 1, i >> 2); + text.push_str(&format!("{r} {g} {b} 1 1 1\n")); + } + text.push_str("0 0 0 5 5 5\n"); + let lut = parse_spi3d(&text).unwrap(); + assert_eq!(lut.values[0], [5.0; 3]); + assert_eq!(lut.values[7], [0.0; 3]); + assert_eq!(lut.values[1], [1.0; 3]); +}