diff --git a/crates/engine-core/src/cmd/adjust_cmds.rs b/crates/engine-core/src/cmd/adjust_cmds.rs index 444b1d5aa5..5c1ae2d3fc 100644 --- a/crates/engine-core/src/cmd/adjust_cmds.rs +++ b/crates/engine-core/src/cmd/adjust_cmds.rs @@ -216,17 +216,43 @@ pub fn lookup_from_params(p: &Value, base: Option<&Adjustment>) -> Result u16; + /// Whether a `.cube` file's text declares a 3D table (`LUT_3D_SIZE`, which wins over `LUT_1D_SIZE` as in the /// parser). Data Order and Table Order were measured on 3D tables only; a 1D `.cube` keeps its result. +/// +/// Only the lines before the first table row are read, decoded as they go: the Properties panel asks on every draw, +/// and a large table must not be copied for it. A file whose size keyword came after its rows would not parse to a +/// table of that size, so a layer never holds one. pub fn is_3d_cube(bytes: &[u8]) -> bool { - let utf16 = |rest: &[u8], from: fn([u8; 2]) -> u16| String::from_utf16_lossy(&rest.as_chunks::<2>().0.iter().map(|&c| from(c)).collect::>()); - let text = match bytes { - [0xFF, 0xFE, rest @ ..] => utf16(rest, u16::from_le_bytes), - [0xFE, 0xFF, rest @ ..] => utf16(rest, u16::from_be_bytes), - [0xEF, 0xBB, 0xBF, rest @ ..] => String::from_utf8_lossy(rest).into_owned(), - _ => String::from_utf8_lossy(bytes).into_owned(), + let (body, wide): (&[u8], Option) = match bytes { + [0xFF, 0xFE, rest @ ..] => (rest, Some(u16::from_le_bytes)), + [0xFE, 0xFF, rest @ ..] => (rest, Some(u16::from_be_bytes)), + [0xEF, 0xBB, 0xBF, rest @ ..] => (rest, None), + _ => (bytes, None), }; - text.lines().any(|line| line.split('#').next().unwrap_or("").split_whitespace().next() == Some("LUT_3D_SIZE")) + let chars: Box> = match wide { + Some(from) => Box::new(char::decode_utf16(body.as_chunks::<2>().0.iter().map(move |&c| from(c))).map(|c| c.unwrap_or(char::REPLACEMENT_CHARACTER))), + None => Box::new(body.iter().map(|&b| if b.is_ascii() { char::from(b) } else { char::REPLACEMENT_CHARACTER })), + }; + let mut line = String::new(); + for c in chars.chain(std::iter::once('\n')) { + if c != '\n' && c != '\r' { + line.push(c); + continue; + } + let first = line.split('#').next().unwrap_or("").split_whitespace().next().unwrap_or(""); + if first == "LUT_3D_SIZE" { + return true; + } + // The first table row ends the header: the keywords come before the data. + if first.parse::().is_ok() { + return false; + } + line.clear(); + } + false } /// A standard `n`³ table (red index fastest, lines R G B) re-read in the reference application's other orders: diff --git a/crates/engine-core/src/cmd/color_lookup_layer_order_tests.rs b/crates/engine-core/src/cmd/color_lookup_layer_order_tests.rs index cc16e26501..c087148b20 100644 --- a/crates/engine-core/src/cmd/color_lookup_layer_order_tests.rs +++ b/crates/engine-core/src/cmd/color_lookup_layer_order_tests.rs @@ -58,3 +58,21 @@ fn an_order_without_a_cube_changes_nothing() { s.execute("layer.setAdjustment", json!({"layer": layer, "dataOrder": "bgr", "tableOrder": "rgb"})).unwrap(); assert_eq!(held(&s, layer), before, "a built-in look is no .cube file"); } + +/// The panel asks whether the kept file is a 3D `.cube` on every draw, so the answer comes from its header: plain and +/// BOM-marked text, UTF-16 either way round, and a 1D file are told apart without reading the table. +#[test] +fn a_3d_cube_is_told_from_its_header() { + let utf16 = |text: &str, le: bool| { + let mut out = if le { vec![0xFF, 0xFE] } else { vec![0xFE, 0xFF] }; + out.extend(text.encode_utf16().flat_map(|u| if le { u.to_le_bytes() } else { u.to_be_bytes() })); + out + }; + let cube = swap2(); + assert!(is_3d_cube(cube.as_bytes())); + assert!(is_3d_cube(&[&[0xEF, 0xBB, 0xBF][..], cube.as_bytes()].concat())); + assert!(is_3d_cube(&utf16(&cube, true)) && is_3d_cube(&utf16(&cube, false))); + assert!(is_3d_cube(b"# a comment\r\nTITLE \"x\"\r\nLUT_3D_SIZE 2 # trailing\r\n0 0 0\r\n")); + assert!(!is_3d_cube(b"LUT_1D_SIZE 2\n0 0 0\n1 1 1\n")); + assert!(!is_3d_cube(b"0 0 0\nLUT_3D_SIZE 2\n"), "a size after the first row is no header"); +}