mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Color Lookup: tell a 3D .cube from its header, without copying the table (#18364)
The Properties panel asks on every draw whether the kept file is a 3D .cube. is_3d_cube decoded the whole file into a new string for that; it now reads the lines before the first table row, decoding UTF-8 and UTF-16 as it goes. A file whose size keyword came after its rows does not parse to a table of that size, so a layer never holds one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
68c2325f2e
commit
201c8d529f
2 files changed
+51
-7
No files matched your search
@@ -216,17 +216,43 @@ pub fn lookup_from_params(p: &Value, base: Option<&Adjustment>) -> Result<Adjust
|
||||
Ok(Adjustment::ColorLookup { name, lut, size, tetrahedral, dither, source })
|
||||
}
|
||||
|
||||
/// Reads one UTF-16 code unit from its two bytes, little- or big-endian.
|
||||
type Utf16Unit = fn([u8; 2]) -> 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::<Vec<_>>());
|
||||
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<Utf16Unit>) = 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<dyn Iterator<Item = char>> = 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::<f32>().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:
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
Reference in new issue
Block a user