mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Color Lookup: Data Order and Table Order for .cube files
image.adjustments.colorLookup takes dataOrder (rgb|bgr, default rgb) and tableOrder (rgb|bgr, default bgr) and bakes them into a 3D .cube table loaded in the same call: BGR data reverses each line, RGB table order makes red the slowest index, the rule #9218 uses for a PSD's stored orders. .3dl, .look, 1D .cube and built-in looks ignore them; other values are errors. The dialog shows both radio pairs only while a .cube is the LUT, and a smart filter keeps them through its recorded params (#14423).
This commit is contained in:
1 parent
b609bc7d3c
commit
d82adfae04
6 files changed
+392
-6
No files matched your search
@@ -103,9 +103,11 @@ pub fn selective_from_params(p: &Value, base: Option<&Adjustment>) -> Result<Adj
|
||||
/// Color Lookup from params: `"lut"` (a built-in look id or `"none"`), `"file"` (a .cube / .3dl /
|
||||
/// .look path, native only) or `"data"` (file text, or base64 with `"dataEncoding":"base64"`).
|
||||
/// `"lutFormat":"cube|3dl|look"` (`"auto"`: detect) can supply the format when the file name doesn't identify it.
|
||||
/// `"interpolation":"trilinear|tetrahedral"`, `"dither":bool`. The table is embedded in the
|
||||
/// layer, as Photoshop does. Unspecified fields keep `base`'s values; a key of the wrong type is an
|
||||
/// error.
|
||||
/// `"interpolation":"trilinear|tetrahedral"`, `"dither":bool`. `"dataOrder":"rgb|bgr"` (default
|
||||
/// `rgb`) and `"tableOrder":"rgb|bgr"` (default `bgr`, the standard layout) say how a 3D `.cube`
|
||||
/// loaded in the same call is read (#14423); [`cube_in_order`] bakes them into the table. The
|
||||
/// table is embedded in the layer, as Photoshop does. Unspecified fields keep `base`'s values; a
|
||||
/// key of the wrong type is an error.
|
||||
pub fn lookup_from_params(p: &Value, base: Option<&Adjustment>) -> Result<Adjustment> {
|
||||
const CMD: &str = "colorLookup";
|
||||
let (mut name, mut lut, mut size, mut tetrahedral, mut dither, mut source) = match base {
|
||||
@@ -118,6 +120,15 @@ pub fn lookup_from_params(p: &Value, base: Option<&Adjustment>) -> Result<Adjust
|
||||
if !matches!(encoding, "text" | "base64") {
|
||||
return Err(bad(CMD, "`dataEncoding` must be \"text\" or \"base64\""));
|
||||
}
|
||||
let order = |key: &str| match opt_str(CMD, p, key)? {
|
||||
None => Ok(None),
|
||||
Some("rgb") => Ok(Some(false)),
|
||||
Some("bgr") => Ok(Some(true)),
|
||||
Some(o) => Err(bad(CMD, format!("`{key}` must be \"rgb\" or \"bgr\", not `{o}`"))),
|
||||
};
|
||||
// Data Order BGR reverses each table line; Table Order RGB (not the standard BGR) makes red the slowest index.
|
||||
let swap_out = order("dataOrder")?.unwrap_or(false);
|
||||
let swap_in = !order("tableOrder")?.unwrap_or(true);
|
||||
let format = match opt_str(CMD, p, "lutFormat")? {
|
||||
None | Some("auto") => None,
|
||||
Some("cube") => Some(craft_cms::lutfile::Format::Cube),
|
||||
@@ -169,7 +180,8 @@ pub fn lookup_from_params(p: &Value, base: Option<&Adjustment>) -> Result<Adjust
|
||||
}
|
||||
name = label;
|
||||
size = f.size as u32;
|
||||
lut = Some(std::sync::Arc::new(f.data));
|
||||
let reorder = (swap_in || swap_out) && src.as_ref().is_some_and(|s| s.format == LutFormat::Cube && is_3d_cube(&s.bytes));
|
||||
lut = Some(std::sync::Arc::new(if reorder { cube_in_order(&f.data, f.size, swap_in, swap_out) } else { f.data }));
|
||||
source = src;
|
||||
}
|
||||
match opt_str(CMD, p, "interpolation")? {
|
||||
@@ -187,6 +199,41 @@ pub fn lookup_from_params(p: &Value, base: Option<&Adjustment>) -> Result<Adjust
|
||||
Ok(Adjustment::ColorLookup { name, lut, size, tetrahedral, dither, source })
|
||||
}
|
||||
|
||||
/// 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.
|
||||
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(),
|
||||
};
|
||||
text.lines().any(|line| line.split('#').next().unwrap_or("").split_whitespace().next() == Some("LUT_3D_SIZE"))
|
||||
}
|
||||
|
||||
/// A standard `n`³ table (red index fastest, lines R G B) re-read in the reference application's other orders:
|
||||
/// `swap_in` (Table Order RGB) reads the file with the red index slowest, `swap_out` (Data Order BGR) reads each
|
||||
/// line as B G R. Both are red/blue swaps, of the input axes and of the outputs. The same rule PR #9218 applies to a
|
||||
/// PSD's stored `tableOrder` / `dataOrder`. A short table reads its missing nodes as black (the parser has
|
||||
/// already checked the length).
|
||||
fn cube_in_order(data: &[f32], n: usize, swap_in: bool, swap_out: bool) -> Vec<f32> {
|
||||
let mut out = Vec::with_capacity(data.len());
|
||||
for b in 0..n {
|
||||
for g in 0..n {
|
||||
for r in 0..n {
|
||||
let node = if swap_in { (r * n + g) * n + b } else { (b * n + g) * n + r };
|
||||
let mut c: [f32; 3] = node.checked_mul(3).and_then(|at| data.get(at..at + 3)).and_then(|s| s.try_into().ok()).unwrap_or_default();
|
||||
if swap_out {
|
||||
c.reverse();
|
||||
}
|
||||
out.extend(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// A loaded LUT file, tagged with the format [`craft_cms::lutfile::parse`] read it as.
|
||||
fn lut_source(format: craft_cms::lutfile::Format, bytes: Vec<u8>) -> LutSource {
|
||||
let format = match format {
|
||||
@@ -853,3 +900,7 @@ fn black_white_auto(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "color_lookup_order_tests.rs"]
|
||||
mod color_lookup_order_tests;
|
||||
@@ -0,0 +1,197 @@
|
||||
//! Color Lookup's Data Order and Table Order for `.cube` files (#14423), measured in the reference
|
||||
//! application with `swap2.cube` (2x2x2, out = (b, g, r)) over gray strips and six colour strips.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// The issue's strips: grays, then (200,50,50) (50,200,50) (50,50,200) (255,128,0) (30,60,90) (128,128,128).
|
||||
const STRIPS: [[u8; 3]; 10] =
|
||||
[[0, 0, 0], [64, 64, 64], [192, 192, 192], [255, 255, 255], [200, 50, 50], [50, 200, 50], [50, 50, 200], [255, 128, 0], [30, 60, 90], [128, 128, 128]];
|
||||
|
||||
const DEPTHS: [u64; 3] = [8, 16, 32];
|
||||
|
||||
/// `swap2.cube`: a 2³ table in the standard layout (red index fastest) whose output is (b, g, r).
|
||||
fn swap2() -> String {
|
||||
let mut cube = String::from("TITLE \"swap2\"\nLUT_3D_SIZE 2\n");
|
||||
for b in 0..2 {
|
||||
for g in 0..2 {
|
||||
for r in 0..2 {
|
||||
cube.push_str(&format!("{b} {g} {r}\n"));
|
||||
}
|
||||
}
|
||||
}
|
||||
cube
|
||||
}
|
||||
|
||||
/// A one-row document with one pixel per strip at `depth`.
|
||||
fn strips(depth: u64) -> Session {
|
||||
let mut s = crate::session();
|
||||
s.execute("file.new", json!({"width": STRIPS.len(), "height": 1, "depth": depth, "mode": "rgb"})).unwrap();
|
||||
s.edit("setup", |doc, active| {
|
||||
let b = doc.bounds();
|
||||
let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap();
|
||||
let fmt = surf.format();
|
||||
let mut data = Vec::new();
|
||||
for c in STRIPS {
|
||||
data.extend(photocraft_raster::from_rgba(&fmt, [0, 1, 2, 3].map(|i| c.get(i).map_or(1.0, |v| f32::from(*v) / 255.0))));
|
||||
}
|
||||
surf.write_region(b, &data);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
/// The strips as 0..255 levels.
|
||||
fn levels(s: &Session) -> Vec<[f32; 3]> {
|
||||
let d = s.active().unwrap();
|
||||
let surf = d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap();
|
||||
(0..STRIPS.len() as i32).map(|x| surf.rgba(x, 0)).map(|p| [p[0] * 255.0, p[1] * 255.0, p[2] * 255.0]).collect()
|
||||
}
|
||||
|
||||
fn swapped(c: [u8; 3]) -> [u8; 3] {
|
||||
[c[2], c[1], c[0]]
|
||||
}
|
||||
|
||||
fn assert_strips(got: &[[f32; 3]], want: impl Fn([u8; 3]) -> [u8; 3], what: &str) {
|
||||
for (strip, px) in STRIPS.iter().zip(got) {
|
||||
let w = want(*strip);
|
||||
for c in 0..3 {
|
||||
assert!((px[c] - f32::from(w[c])).abs() <= 0.5, "{what}: strip {strip:?} gave {px:?}, want {w:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The issue's truth table: the defaults apply the file as written; BGR data or RGB table order
|
||||
/// alone undo the swap; both together swap again.
|
||||
#[test]
|
||||
fn the_four_orders_give_the_measured_swap2_results() {
|
||||
let cases: [(Option<&str>, Option<&str>, bool); 5] = [
|
||||
(None, None, true),
|
||||
(Some("rgb"), Some("bgr"), true),
|
||||
(Some("bgr"), Some("bgr"), false),
|
||||
(Some("rgb"), Some("rgb"), false),
|
||||
(Some("bgr"), Some("rgb"), true),
|
||||
];
|
||||
for depth in DEPTHS {
|
||||
for (data, table, swaps) in cases {
|
||||
let mut s = strips(depth);
|
||||
let mut p = json!({"data": swap2(), "fileName": "swap2.cube", "dither": false});
|
||||
if let Some(o) = data {
|
||||
p["dataOrder"] = json!(o);
|
||||
}
|
||||
if let Some(o) = table {
|
||||
p["tableOrder"] = json!(o);
|
||||
}
|
||||
s.execute("image.adjustments.colorLookup", p).unwrap();
|
||||
let what = format!("{depth} bit, data {data:?}, table {table:?}");
|
||||
assert_strips(&levels(&s), if swaps { swapped } else { |c| c }, &what);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Table Order RGB transposes the grid (blue index fastest), so a table that is not symmetric
|
||||
/// in its axes reads different nodes: out = (r, 0.25 + 0.5g, 0.125 + 0.25b) as in #9126.
|
||||
#[test]
|
||||
fn table_order_moves_nodes_and_data_order_reverses_each_line() {
|
||||
let f = |r: f32, g: f32, b: f32| [r, 0.25 + 0.5 * g, 0.125 + 0.25 * b];
|
||||
let mut cube = String::from("LUT_3D_SIZE 2\n");
|
||||
for i in 0..8 {
|
||||
let [r, g, b] = f((i & 1) as f32, (i >> 1 & 1) as f32, (i >> 2 & 1) as f32);
|
||||
cube.push_str(&format!("{r} {g} {b}\n"));
|
||||
}
|
||||
// Input pure red (255, 0, 0) for each order, as PR #9218 reads a PSD's stored orders.
|
||||
let cases = [("rgb", "bgr", f(1.0, 0.0, 0.0)), ("bgr", "bgr", [0.125, 0.25, 1.0]), ("rgb", "rgb", f(0.0, 0.0, 1.0)), ("bgr", "rgb", [0.375, 0.25, 0.0])];
|
||||
for (data, table, want) in cases {
|
||||
let mut one = crate::session();
|
||||
one.execute("file.new", json!({"width": 1, "height": 1, "depth": 16, "mode": "rgb", "background": "backgroundColor", "backgroundColor": [1, 0, 0]}))
|
||||
.unwrap();
|
||||
one.execute("image.adjustments.colorLookup", json!({"data": cube, "fileName": "f.cube", "dataOrder": data, "tableOrder": table, "dither": false}))
|
||||
.unwrap();
|
||||
let d = one.active().unwrap();
|
||||
let px = d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().rgba(0, 0);
|
||||
for c in 0..3 {
|
||||
assert!((px[c] - want[c]).abs() < 1e-3, "data {data}, table {table}: {px:?}, want {want:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `.3dl` and `.look` files ignore both orders: Lustre tables have their own layout.
|
||||
#[test]
|
||||
fn other_formats_ignore_the_orders() {
|
||||
let mut lut = String::from("0 1023\n");
|
||||
for r in 0..2 {
|
||||
for g in 0..2 {
|
||||
for b in 0..2 {
|
||||
// Out = (b, g, r), blue fastest as `.3dl` files are.
|
||||
lut.push_str(&format!("{} {} {}\n", b * 1023, g * 1023, r * 1023));
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut plain = strips(8);
|
||||
plain.execute("image.adjustments.colorLookup", json!({"data": lut, "fileName": "swap2.3dl", "dither": false})).unwrap();
|
||||
let mut ordered = strips(8);
|
||||
ordered
|
||||
.execute("image.adjustments.colorLookup", json!({"data": lut, "fileName": "swap2.3dl", "dataOrder": "bgr", "tableOrder": "rgb", "dither": false}))
|
||||
.unwrap();
|
||||
assert_eq!(levels(&plain), levels(&ordered));
|
||||
assert_ne!(levels(&plain), levels(&strips(8)), "the .3dl applied");
|
||||
}
|
||||
|
||||
/// Not measured for 1D `.cube` files (`LUT_1D_SIZE`): they keep today's result whatever the orders.
|
||||
#[test]
|
||||
fn a_1d_cube_ignores_the_orders() {
|
||||
let cube = "LUT_1D_SIZE 2\n0 0 0\n0.5 1 0.25\n";
|
||||
let mut plain = strips(8);
|
||||
plain.execute("image.adjustments.colorLookup", json!({"data": cube, "fileName": "ramp.cube", "dither": false})).unwrap();
|
||||
let mut ordered = strips(8);
|
||||
ordered
|
||||
.execute("image.adjustments.colorLookup", json!({"data": cube, "fileName": "ramp.cube", "dataOrder": "bgr", "tableOrder": "rgb", "dither": false}))
|
||||
.unwrap();
|
||||
assert_eq!(levels(&plain), levels(&ordered));
|
||||
}
|
||||
|
||||
/// A smart filter records the orders with its params, so a re-run (here: an edit of the smart
|
||||
/// object's contents being re-rendered through the filter) gives the same pixels.
|
||||
#[test]
|
||||
fn a_smart_filter_keeps_both_orders() {
|
||||
for depth in DEPTHS {
|
||||
let mut s = strips(depth);
|
||||
s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap();
|
||||
let p = json!({"data": swap2(), "fileName": "swap2.cube", "dataOrder": "bgr", "tableOrder": "bgr", "dither": false});
|
||||
s.execute("image.adjustments.colorLookup", p).unwrap();
|
||||
let d = s.active().unwrap();
|
||||
let LayerContent::Smart(sm) = &d.doc.layer(d.active_layer.unwrap()).unwrap().content else { panic!("not a smart object") };
|
||||
let sf = sm.smart_filters.last().unwrap();
|
||||
assert_eq!(sf.params["dataOrder"], "bgr");
|
||||
assert_eq!(sf.params["tableOrder"], "bgr");
|
||||
// The filter re-runs from its recorded params: BGR data + BGR table undoes the file's swap.
|
||||
let flat = photocraft_compose::flatten(&d.doc).into_px();
|
||||
let got: Vec<[f32; 3]> = flat.iter().take(STRIPS.len()).map(|p| [p[0] * 255.0, p[1] * 255.0, p[2] * 255.0]).collect();
|
||||
assert_strips(&got, |c| c, &format!("smart filter, {depth} bit"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrong values are refused with the key named, and nothing is applied.
|
||||
#[test]
|
||||
fn bad_order_values_are_errors_not_panics() {
|
||||
for (key, value) in [
|
||||
("dataOrder", json!("grb")),
|
||||
("dataOrder", json!("")),
|
||||
("dataOrder", json!(1)),
|
||||
("dataOrder", json!(true)),
|
||||
("tableOrder", json!("RGBA")),
|
||||
("tableOrder", json!(0.5)),
|
||||
("tableOrder", json!(["rgb"])),
|
||||
("tableOrder", json!({"order": "rgb"})),
|
||||
] {
|
||||
let mut s = strips(8);
|
||||
let before = levels(&s);
|
||||
let mut p = json!({"data": swap2(), "fileName": "swap2.cube"});
|
||||
p[key] = value.clone();
|
||||
let e = s.execute("image.adjustments.colorLookup", p).unwrap_err();
|
||||
assert!(e.to_string().contains(key), "{key}={value}: {e}");
|
||||
assert_eq!(levels(&s), before, "{key}={value} changed pixels");
|
||||
// Without a LUT the value is still checked.
|
||||
assert!(s.execute("image.adjustments.colorLookup", json!({key: value})).is_err(), "{key}={value} without a LUT");
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
(
|
||||
"colorLookup",
|
||||
"Color Lookup…",
|
||||
r##"{"lut":"none|warm|cool|tealOrange|bleachBypass|fadedFilm|dayForNight|monoContrast|crossProcess"="none","file":text,"interpolation":"trilinear|tetrahedral"="trilinear","dither":bool=false,"data":text,"dataEncoding":"text|base64"="text","fileName":text,"lutFormat":"auto|cube|3dl|look"="auto"} (file: LUT path; data: file text or encoded bytes; lutFormat overrides format detection, auto detects it)"##,
|
||||
r##"{"lut":"none|warm|cool|tealOrange|bleachBypass|fadedFilm|dayForNight|monoContrast|crossProcess"="none","file":text,"dataOrder":"rgb|bgr"="rgb","tableOrder":"rgb|bgr"="bgr","interpolation":"trilinear|tetrahedral"="trilinear","dither":bool=false,"data":text,"dataEncoding":"text|base64"="text","fileName":text,"lutFormat":"auto|cube|3dl|look"="auto"} (file: LUT path; data: file text or encoded bytes; lutFormat overrides format detection, auto detects it; dataOrder/tableOrder: how a 3D .cube's lines and nodes are ordered, ignored for other files)"##,
|
||||
),
|
||||
];
|
||||
/// Composed layer options (`path`, `opacity`, `name`, `mode`, `clipToPrevious`) belong immediately
|
||||
|
||||
@@ -710,6 +710,8 @@ pub(crate) mod canvas_input;
|
||||
pub mod channel_options_ui;
|
||||
#[cfg(test)]
|
||||
mod color_lookup_no_remember_tests;
|
||||
#[cfg(test)]
|
||||
mod color_lookup_order_ui_tests;
|
||||
pub(crate) mod color_picker_preview;
|
||||
pub mod color_picker_ui;
|
||||
pub mod color_range_ui;
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
//! #14423: Image › Adjustments › Color Lookup shows Data Order and Table Order only while a `.cube`
|
||||
//! file is the LUT, and OK applies the chosen orders.
|
||||
|
||||
use craft::ui::dialog::DialogId;
|
||||
use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::dialogs::filter_dialog::open;
|
||||
|
||||
const COLOR_LOOKUP: &str = "image.adjustments.colorLookup";
|
||||
|
||||
/// `swap2.cube`: out = (b, g, r), red index fastest.
|
||||
const SWAP2: &str = "LUT_3D_SIZE 2\n0 0 0\n0 0 1\n0 1 0\n0 1 1\n1 0 0\n1 0 1\n1 1 0\n1 1 1\n";
|
||||
|
||||
fn app_with_doc() -> Box<PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 4, "height": 4, "background": "#c83232"})).unwrap();
|
||||
app
|
||||
}
|
||||
|
||||
fn fields_of(app: &PhotocraftApp, id: DialogId) -> DialogFields {
|
||||
app.framework.dialogs.get_by_id::<HostedParamDialog>(id).expect("open dialog").fields().clone()
|
||||
}
|
||||
|
||||
fn try_set(app: &mut PhotocraftApp, id: DialogId, key: &str, value: Value) -> Result<(), String> {
|
||||
let ctx = app.ctx().clone();
|
||||
app.framework.dialogs.set_json_field(&ctx, id, key, value)
|
||||
}
|
||||
|
||||
fn passes(app: &mut PhotocraftApp, n: usize) {
|
||||
let ctx = app.ctx().clone();
|
||||
for _ in 0..n {
|
||||
crate::testing::run_ui(&ctx, Default::default(), |ui| crate::dialogs::run(app, ui));
|
||||
}
|
||||
}
|
||||
|
||||
fn ok(app: &mut PhotocraftApp, id: DialogId) {
|
||||
let ctx = app.ctx().clone();
|
||||
let _ = crate::dialogs::confirm(app, &ctx, id);
|
||||
passes(app, 3);
|
||||
assert!(app.framework.dialogs.get_by_id::<HostedParamDialog>(id).is_none(), "OK closes the dialog");
|
||||
}
|
||||
|
||||
fn shown(app: &PhotocraftApp, id: DialogId) -> bool {
|
||||
fields_of(app, id).bool("lutCube") == Some(true)
|
||||
}
|
||||
|
||||
fn pixel(app: &PhotocraftApp) -> [u8; 3] {
|
||||
let px = app.session.active().unwrap().doc.layers[0].surface().unwrap().read_region(photocraft_geom::Rect::new(0, 0, 1, 1));
|
||||
[0, 1, 2].map(|c| (px[c] * 255.0).round() as u8)
|
||||
}
|
||||
|
||||
/// Hidden with no LUT, a built-in look or a `.3dl` from the LUT folder; shown for a `.cube` from the folder and
|
||||
/// for a loaded LUT; and while hidden, the rows refuse values.
|
||||
#[test]
|
||||
fn the_orders_show_only_for_a_cube() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let cube = tmp.path().join("swap2.cube");
|
||||
std::fs::write(&cube, SWAP2).unwrap();
|
||||
let threedl = tmp.path().join("ramp.3dl");
|
||||
std::fs::write(&threedl, "0 1023\n0 0 0\n0 0 1023\n0 1023 0\n0 1023 1023\n1023 0 0\n1023 0 1023\n1023 1023 0\n1023 1023 1023\n").unwrap();
|
||||
let mut app = app_with_doc();
|
||||
let id = open(&mut app, COLOR_LOOKUP).unwrap();
|
||||
passes(&mut app, 2);
|
||||
assert!(!shown(&app, id), "no LUT");
|
||||
assert!(try_set(&mut app, id, "dataOrder", json!("bgr")).is_err(), "a hidden row takes no value");
|
||||
let fields = fields_of(&app, id);
|
||||
assert_eq!((fields.text("dataOrder"), fields.text("tableOrder")), (Some("rgb"), Some("bgr")), "the defaults");
|
||||
|
||||
try_set(&mut app, id, "lut", json!("warm")).unwrap();
|
||||
passes(&mut app, 2);
|
||||
assert!(!shown(&app, id), "a built-in look");
|
||||
|
||||
try_set(&mut app, id, "file", json!(threedl.to_str().unwrap())).unwrap();
|
||||
passes(&mut app, 2);
|
||||
assert!(!shown(&app, id), "a .3dl file");
|
||||
|
||||
try_set(&mut app, id, "file", json!(cube.to_str().unwrap())).unwrap();
|
||||
passes(&mut app, 2);
|
||||
assert!(shown(&app, id), "a .cube file");
|
||||
try_set(&mut app, id, "dataOrder", json!("bgr")).unwrap();
|
||||
assert!(try_set(&mut app, id, "tableOrder", json!("grb")).is_err(), "only RGB and BGR");
|
||||
}
|
||||
|
||||
/// OK with a `.cube` and Data Order BGR (Table Order left at BGR) applies the file's table read as B G R: the
|
||||
/// swap2 table becomes the identity, as measured. With the defaults the file's swap applies.
|
||||
#[test]
|
||||
fn ok_applies_the_chosen_orders() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let cube = tmp.path().join("swap2.cube");
|
||||
std::fs::write(&cube, SWAP2).unwrap();
|
||||
for (data_order, want) in [("rgb", [50, 50, 200]), ("bgr", [200, 50, 50])] {
|
||||
let mut app = app_with_doc();
|
||||
let id = open(&mut app, COLOR_LOOKUP).unwrap();
|
||||
try_set(&mut app, id, "file", json!(cube.to_str().unwrap())).unwrap();
|
||||
passes(&mut app, 2);
|
||||
try_set(&mut app, id, "dataOrder", json!(data_order)).unwrap();
|
||||
try_set(&mut app, id, "dither", json!(false)).unwrap();
|
||||
ok(&mut app, id);
|
||||
assert_eq!(pixel(&app), want, "data order {data_order}");
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
//! blur row.
|
||||
//!
|
||||
//! These rows keep values under keys no command parameter has (`matteColor`, `sampleTool`, `resultPicked`,
|
||||
//! `lutSize`, `lutError`) and Lens Blur's `channel:N` sources and Match Color's textual
|
||||
//! `lutSize`, `lutError`, `lutCube`) and Lens Blur's `channel:N` sources and Match Color's textual
|
||||
//! `source`; the family's `command_params` (dialogs-w24) maps them back and leaves the private ones out.
|
||||
|
||||
use photocraft_engine::color_cmds::ColorExt;
|
||||
@@ -55,6 +55,8 @@ const RESULT_PICKED: &str = "resultPicked";
|
||||
/// The size and name of the LUT Load 3D LUT… embedded, and why one could not be read.
|
||||
const LUT_SIZE: &str = "lutSize";
|
||||
const LUT_ERROR: &str = "lutError";
|
||||
/// Whether the chosen LUT is a `.cube` table: Data Order and Table Order show only then (#14423).
|
||||
const LUT_CUBE: &str = "lutCube";
|
||||
|
||||
/// Builds the rows `command`'s dialog shows from the rows `params` the spec parser made.
|
||||
pub(crate) fn custom_rows(app: &mut PhotocraftApp, command: &str, params: &mut Vec<Param>) {
|
||||
@@ -295,6 +297,31 @@ fn lut_choice(fields: &DialogFields) -> LutChoice {
|
||||
photocraft_engine::adjust_cmds::LOOKS.iter().find(|(known, _)| *known == look).map_or(LutChoice::None, |(known, _)| LutChoice::Builtin(known))
|
||||
}
|
||||
|
||||
/// Whether the chosen LUT is a `.cube` file, which Data Order and Table Order apply to: a LUT folder file named
|
||||
/// `.cube`, or a loaded LUT the row holds as `.cube` text. Built-in looks are no files and have no orders.
|
||||
fn lut_is_cube(fields: &DialogFields) -> bool {
|
||||
match lut_choice(fields) {
|
||||
LutChoice::Loaded { .. } => fields.text("fileName").is_some_and(|name| name.to_ascii_lowercase().ends_with(".cube")),
|
||||
LutChoice::Folder(path) => path.extension().is_some_and(|ext| ext.eq_ignore_ascii_case("cube")),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Keeps [`LUT_CUBE`] in step with the chosen LUT; written only when it changes.
|
||||
fn sync_lut_cube(fields: &mut DialogFields) {
|
||||
let cube = lut_is_cube(fields);
|
||||
if fields.bool(LUT_CUBE) != Some(cube) {
|
||||
fields.set(LUT_CUBE, cube);
|
||||
}
|
||||
}
|
||||
|
||||
/// Data Order and Table Order (#14423): a pair of radio buttons each, shown only while a `.cube` is the LUT, as
|
||||
/// the reference application does. Not remembered, so every opening starts at its defaults (RGB, BGR).
|
||||
fn order_row(key: &str, label: Label, default: &str) -> Param {
|
||||
let options = vec![("rgb".to_owned(), Label::from(tl!("RGB"))), ("bgr".to_owned(), Label::from(tl!("BGR")))];
|
||||
Param::new(key, label, ParamKind::radio(options, Some(default.to_owned()))).visible_when(LUT_CUBE, true).not_remembered()
|
||||
}
|
||||
|
||||
/// Writes a picked choice as the command's `lut` / `file` parameters; the loaded LUT already held is no pick.
|
||||
fn set_lut_choice(fields: &mut DialogFields, choice: &LutChoice) {
|
||||
let clear = |fields: &mut DialogFields, keys: &[&str]| keys.iter().for_each(|key| drop(fields.remove(key)));
|
||||
@@ -351,6 +378,7 @@ fn color_lookup(app: &PhotocraftApp, params: &mut Vec<Param>) {
|
||||
Some(Err(error)) => fields.set(LUT_ERROR, error.0),
|
||||
None => {}
|
||||
}
|
||||
sync_lut_cube(fields);
|
||||
if let Some(error) = fields.text(LUT_ERROR) {
|
||||
ui.colored_label(ui.visuals().error_fg_color, error);
|
||||
}
|
||||
@@ -358,6 +386,11 @@ fn color_lookup(app: &PhotocraftApp, params: &mut Vec<Param>) {
|
||||
})
|
||||
.companions(["file", "data", "fileName"]);
|
||||
params.insert(at.min(params.len()), row);
|
||||
for (key, label, default) in [("dataOrder", Label::from(tl!("Data Order")), "rgb"), ("tableOrder", Label::from(tl!("Table Order")), "bgr")] {
|
||||
if let Some(p) = params.iter_mut().find(|p| p.key == key) {
|
||||
*p = order_row(key, label, default);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Match Color -----------------------------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user