From 6f1705a32bdb91467eba1d88df2ccb66100b0302 Mon Sep 17 00:00:00 2001 From: DuBearte <130190089+DuBearte@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:17:39 -0400 Subject: [PATCH] fix(cms): headerless .3dl tables read on at least the 12-bit scale (#9121) (#9215) A table with no header peaking at 255 or 1023 was scaled to full range, so white came out 255 where Photoshop gives 16 or 64. Without a header, the inferred output depth now starts at 12 bits. Co-authored-by: Claude Opus 5.5 --- crates/cms/src/lutfile.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/cms/src/lutfile.rs b/crates/cms/src/lutfile.rs index 11e3fb24caa..349bd792bea 100644 --- a/crates/cms/src/lutfile.rs +++ b/crates/cms/src/lutfile.rs @@ -179,12 +179,15 @@ pub fn parse_cube(text: &str) -> Result { } /// Lustre `.3dl`: an optional shaper line of input values, then integer RGB rows with **blue** -/// varying fastest; the output bit depth is inferred from the largest value. +/// varying fastest; the output bit depth is inferred from the largest value, and is at least 12 +/// bits in a file without a header. pub fn parse_3dl(text: &str) -> Result { let mut rows: Vec<[f64; 3]> = Vec::new(); + let mut header = false; for line in text.lines() { let line = line.split('#').next().unwrap_or("").trim(); if line.is_empty() || line.starts_with(|c: char| c.is_ascii_alphabetic()) { + header |= !line.is_empty(); continue; } let v: Vec = line.split_whitespace().filter_map(|s| s.parse().ok()).collect(); @@ -198,7 +201,9 @@ pub fn parse_3dl(text: &str) -> Result { return err(format!("{} rows is not a cube", rows.len())); } let max = rows.iter().flatten().fold(0.0f64, |a, &b| a.max(b)); - let scale = if max <= 1.0 { 1.0 } else { [1023.0, 4095.0, 16383.0, 65535.0].into_iter().find(|&s| max <= s).unwrap_or(max) }; + // Photoshop 27.11 reads a headerless table peaking at 255 or 1023 on the 12-bit scale (#9121). + let depths: &[f64] = if header { &[1023.0, 4095.0, 16383.0, 65535.0] } else { &[4095.0, 16383.0, 65535.0] }; + let scale = if max <= 1.0 { 1.0 } else { depths.iter().copied().find(|&s| max <= s).unwrap_or(max) }; let mut data = vec![0.0f32; n * n * n * 3]; for (i, row) in rows.iter().enumerate() { let (r, g, b) = (i / (n * n), (i / n) % n, i % n); @@ -449,6 +454,25 @@ DOMAIN_MAX 0.5 1 1 assert!(parse_cube(&text).is_err()); } + #[test] + fn three_dl_without_a_header_reads_low_nodes_as_12_bit() { + let table = |peak: u32| -> String { + let mut text = String::from("0 1023\n"); + for i in 0..8 { + let on = |bit: u32| if i & bit != 0 { peak } else { 0 }; + text.push_str(&format!("{} {} {}\n", on(4), on(2), on(1))); + } + text + }; + for peak in [255, 1023, 4095] { + let white = parse_3dl(&table(peak)).unwrap().data[21..24].to_vec(); + assert_eq!(white, [peak as f32 / 4095.0; 3], "peak {peak}"); + } + // A header keeps the inferred 10-bit depth. + let white = parse_3dl(&format!("3DMESH\nMesh 1 10\n{}", table(1023))).unwrap().data[21..24].to_vec(); + assert_eq!(white, [1.0; 3]); + } + #[test] fn three_dl_blue_fastest_and_bit_depth() { let mut text = String::from("0 1023\n");