mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Color Lookup Tables export: the CUBE file's comment layout and, for 8- and 16-bit documents, values on 1/32768 steps (#13750)
bake_cube_layers now takes the document's sample type, writes the reference comment layout around the header, and rounds 8- and 16-bit values to multiples of 1/32768. 32-bit values stay unquantised. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Model-Effort: Opus 5.5 drove; Opus 5.5 briefed; Sonnet 5.5 wrote at medium
This commit is contained in:
1 parent
0b15ad2401
commit
d62728aeae
3 files changed
+110
-5
No files matched your search
@@ -436,6 +436,10 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
#[path = "file_cmds/tests.rs"]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "file_cmds/export_clut_cube_header_tests.rs"]
|
||||
mod export_clut_cube_header_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "file_cmds/place_job_tests.rs"]
|
||||
mod place_job_tests;
|
||||
|
||||
@@ -148,7 +148,7 @@ fn lut_layers<'a>(doc: &'a Document, ids: Option<&[LayerId]>) -> Result<Vec<&'a
|
||||
|
||||
/// Bake the chosen adjustment stack against an identity RGB lattice.
|
||||
/// Source order always follows the document stack, even if an explicit list is reversed.
|
||||
fn bake_cube_layers(size: usize, title: &str, layers: &[&Layer]) -> String {
|
||||
fn bake_cube_layers(size: usize, title: &str, layers: &[&Layer], depth: photocraft_color::SampleType) -> String {
|
||||
let n = size;
|
||||
let (w, h) = ((n * n) as u32, n as u32);
|
||||
let mut lattice = Document::new("lut", photocraft_doc::Size::new(w, h), ColorMode::Rgb, photocraft_color::SampleType::F32);
|
||||
@@ -176,14 +176,24 @@ fn bake_cube_layers(size: usize, title: &str, layers: &[&Layer]) -> String {
|
||||
lattice.layers.push(a);
|
||||
}
|
||||
let out = photocraft_compose::flatten(&lattice);
|
||||
let mut s = format!("TITLE \"{}\"\n# Created by PhotoCraft\nLUT_3D_SIZE {n}\nDOMAIN_MIN 0.0 0.0 0.0\nDOMAIN_MAX 1.0 1.0 1.0\n", title.replace('"', "'"));
|
||||
// The reference application's comment layout. There is no Copyright input yet (#13748), so the
|
||||
// `#Copyright:` line is never written.
|
||||
let mut s = format!(
|
||||
"#Created by: PhotoCraft\nTITLE \"{}\"\n\n#LUT size\nLUT_3D_SIZE {n}\n\n#data domain\nDOMAIN_MIN 0.0 0.0 0.0\nDOMAIN_MAX 1.0 1.0 1.0\n\n#LUT data points\n",
|
||||
title.replace('"', "'")
|
||||
);
|
||||
// 8- and 16-bit documents write values on 1/32768 steps; 32-bit stays unquantised.
|
||||
let quantise = |v: f32| -> f32 {
|
||||
let v = v.clamp(0.0, 1.0);
|
||||
if matches!(depth, photocraft_color::SampleType::F32) { v } else { (v * 32768.0).round() / 32768.0 }
|
||||
};
|
||||
// .cube order: red changes fastest, then green, then blue.
|
||||
for b in 0..n {
|
||||
for g in 0..n {
|
||||
for r in 0..n {
|
||||
let (x, y) = (b * n + r, g);
|
||||
let px = out.px[y * w as usize + x];
|
||||
s.push_str(&format!("{:.6} {:.6} {:.6}\n", px[0].clamp(0.0, 1.0), px[1].clamp(0.0, 1.0), px[2].clamp(0.0, 1.0)));
|
||||
s.push_str(&format!("{:.6} {:.6} {:.6}\n", quantise(px[0]), quantise(px[1]), quantise(px[2])));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -193,7 +203,7 @@ fn bake_cube_layers(size: usize, title: &str, layers: &[&Layer]) -> String {
|
||||
/// Backwards-compatible helper for exporting all visible top-level adjustments.
|
||||
pub fn bake_cube(doc: &Document, size: usize, title: &str) -> String {
|
||||
let layers: Vec<&Layer> = doc.layers.iter().filter(|l| l.visible && matches!(l.content, LayerContent::Adjustment(_))).collect();
|
||||
bake_cube_layers(size.clamp(2, photocraft_cms::lutfile::MAX_SIZE), title, &layers)
|
||||
bake_cube_layers(size.clamp(2, photocraft_cms::lutfile::MAX_SIZE), title, &layers, doc.depth)
|
||||
}
|
||||
|
||||
/// Export either the whole visible stack (backward-compatible default), the Layers panel's
|
||||
@@ -235,7 +245,7 @@ pub(super) fn color_lookup_tables(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let layers = lut_layers(&d.doc, explicit.as_deref())?;
|
||||
let count = layers.len();
|
||||
let title = p.get("title").and_then(Value::as_str).map(str::to_string).unwrap_or_else(|| stem(&d.doc.name));
|
||||
let cube = bake_cube_layers(size as usize, &title, &layers);
|
||||
let cube = bake_cube_layers(size as usize, &title, &layers, d.doc.depth);
|
||||
match p.get("path").and_then(Value::as_str) {
|
||||
Some(path) => {
|
||||
write_file(path, cube.as_bytes())?;
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
//! The exported CUBE file's comment layout and 1/32768 steps (#13750).
|
||||
|
||||
use super::*;
|
||||
|
||||
fn session(depth: u32) -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 8, "height": 8, "depth": depth})).unwrap();
|
||||
s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn cube(s: &mut Session, size: u32) -> String {
|
||||
s.execute("file.export.colorLookupTables", json!({"size": size, "title": "My Look"})).unwrap()["cube"].as_str().unwrap().to_string()
|
||||
}
|
||||
|
||||
fn data_lines(cube: &str) -> Vec<&str> {
|
||||
cube.lines().filter(|l| l.chars().next().is_some_and(|c| c.is_ascii_digit())).collect()
|
||||
}
|
||||
|
||||
fn values(cube: &str) -> Vec<String> {
|
||||
data_lines(cube).iter().flat_map(|l| l.split(' ').map(str::to_string)).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn header_follows_the_reference_comment_layout() {
|
||||
let mut s = session(8);
|
||||
let text = cube(&mut s, 3);
|
||||
let head: Vec<&str> = text.lines().take(12).collect();
|
||||
assert_eq!(
|
||||
head,
|
||||
[
|
||||
"#Created by: PhotoCraft",
|
||||
"TITLE \"My Look\"",
|
||||
"",
|
||||
"#LUT size",
|
||||
"LUT_3D_SIZE 3",
|
||||
"",
|
||||
"#data domain",
|
||||
"DOMAIN_MIN 0.0 0.0 0.0",
|
||||
"DOMAIN_MAX 1.0 1.0 1.0",
|
||||
"",
|
||||
"#LUT data points",
|
||||
data_lines(&text)[0],
|
||||
]
|
||||
);
|
||||
assert_eq!(text.lines().count(), 11 + 27);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eight_and_sixteen_bit_values_sit_on_1_32768_steps() {
|
||||
for depth in [8, 16] {
|
||||
let mut s = session(depth);
|
||||
// Inverted size-7 lattice: 1 - k/6 gives 5/6 and 1/6.
|
||||
let v = values(&cube(&mut s, 7));
|
||||
assert!(v.contains(&"0.833344".to_string()), "depth {depth}");
|
||||
assert!(v.contains(&"0.166656".to_string()), "depth {depth}");
|
||||
assert!(!v.contains(&"0.833333".to_string()), "depth {depth}");
|
||||
// Inverted size-52 lattice: 1 - 19/51 = 32/51 = 160/255.
|
||||
let v = values(&cube(&mut s, 52));
|
||||
assert!(v.contains(&"0.627441".to_string()), "depth {depth}");
|
||||
for x in v {
|
||||
let f: f64 = x.parse().unwrap();
|
||||
assert!((f * 32768.0 - (f * 32768.0).round()).abs() < 32768.0 * 1e-6 + 1e-9, "{x}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thirty_two_bit_values_are_not_quantised() {
|
||||
let mut s = session(32);
|
||||
let v = values(&cube(&mut s, 7));
|
||||
assert!(v.contains(&"0.833333".to_string()));
|
||||
assert!(v.contains(&"0.166667".to_string()));
|
||||
assert!(!v.contains(&"0.833344".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exported_text_parses_back_with_the_same_values() {
|
||||
for depth in [8, 32] {
|
||||
let mut s = session(depth);
|
||||
let text = cube(&mut s, 5);
|
||||
let lut = photocraft_cms::lutfile::parse_cube(&text).unwrap();
|
||||
assert_eq!(lut.size, 5);
|
||||
assert_eq!(lut.title, "My Look");
|
||||
let written: Vec<f32> = values(&text).iter().map(|x| x.parse().unwrap()).collect();
|
||||
assert_eq!(lut.data.len(), written.len());
|
||||
for (a, b) in lut.data.iter().zip(&written) {
|
||||
assert!((a - b).abs() < 1e-6, "{a} vs {b}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user