PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Add source layer, Scale and Offset to the Apply Image dialog (#4574)
Fabio Lussi935aa35a4a5e
diff --git a/crates/engine/src/channel_cmds/tests.rs b/crates/engine/src/channel_cmds/tests.rsindex d39dda70b..499b16c32 100644--- a/crates/engine/src/channel_cmds/tests.rs+++ b/crates/engine/src/channel_cmds/tests.rs@@ -533,6 +533,18 @@ fn apply_image_hand_computed() {assert!(s.execute("image.applyImage", json!({"blending": "hue"})).is_err());}+#[test]+fn apply_image_subtract_layer_from_itself_is_mid_grey() {+ // Frequency separation: Subtract, Scale 2, Offset 128, in the flat form the dialog sends.+ let mut s = session();+ s.execute("edit.fill", json!({"color": "#c08040"})).unwrap();+ s.execute("layer.new.layer", json!({"name": "High"})).unwrap();+ s.execute("edit.fill", json!({"color": "#406080"})).unwrap();+ s.execute("image.applyImage", json!({"sourceLayer": "High", "blending": "subtract", "scale": 2.0, "offset": 128})).unwrap();+ let p = px(&s, 3, 3);+ assert!(p[..3].iter().all(|v| (v - 128.0 / 255.0).abs() < 0.003), "{p:?}");+}+#[test]fn apply_image_onto_transparent_layer() {for depth in [8, 16, 32] {diff --git a/crates/ui-egui/src/view_cmds.rs b/crates/ui-egui/src/view_cmds.rsindex c50397c49..f18c4730e 100644--- a/crates/ui-egui/src/view_cmds.rs+++ b/crates/ui-egui/src/view_cmds.rs@@ -1088,7 +1088,7 @@ pub fn form_body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {let mut s = s.clone();match choices.get(&k).and_then(Value::as_array) {Some(opts) => {- let opts: Vec<(String, String)> = opts.iter().filter_map(Value::as_str).map(|o| (o.to_string(), label_of(o))).collect();+ let opts: Vec<(String, String)> = opts.iter().filter_map(Value::as_str).map(|o| (o.to_string(), label(o))).collect();let refs: Vec<(String, &str)> = opts.iter().map(|(a, b)| (a.clone(), b.as_str())).collect();crate::widgets::dropdown(ui, &format!("form-{k}"), &mut s, &refs, 200.0);}@@ -1369,10 +1369,20 @@ fn front(app: &mut PhotocraftApp, id: &str, params: &Value) -> Option<Result<Val"image.applyImage" => {let mut masks = vec!["none".to_string()];masks.extend(load.iter().cloned());+ // Source layer: Merged, then the pixel layers top to bottom (the engine takes+ // the topmost layer of a name), shown by their own names.+ let mut layers = vec!["merged".to_string()];+ for (_, _, l) in d.walk().into_iter().rev() {+ if l.surface().is_some() && !layers.contains(&l.name) {+ layers.push(l.name.clone());+ }+ }+ let names: Map<String, Value> = layers.iter().skip(1).map(|n| (n.clone(), json!(n))).collect();+ // Scale and Offset only affect Add and Subtract.dialog(app,- json!({"sourceChannel": chans[0], "sourceInvert": false, "blending": "multiply", "opacity": 100.0, "preserveTransparency": false, "maskChannel": "none"}),- json!({"sourceChannel": load, "blending": blends, "maskChannel": masks}),+ json!({"sourceLayer": "merged", "sourceChannel": chans[0], "sourceInvert": false, "blending": "multiply", "opacity": 100.0, "scale": 1.0, "offset": 0, "preserveTransparency": false, "maskChannel": "none", "__labels": names}),+ json!({"sourceLayer": layers, "sourceChannel": load, "blending": blends, "maskChannel": masks}),)}_ => dialog(diff --git a/crates/ui-egui/src/view_cmds/tests.rs b/crates/ui-egui/src/view_cmds/tests.rsindex b930e6e26..7ef3100d9 100644--- a/crates/ui-egui/src/view_cmds/tests.rs+++ b/crates/ui-egui/src/view_cmds/tests.rs@@ -255,6 +255,45 @@ fn apply_image_and_calculations_open_dialogs_without_editing() {}}+#[test]+fn apply_image_dialog_offers_source_layer_scale_and_offset() {+ let (mut app, ctx) = app_with(0);+ app.run("file.new", json!({"width": 8, "height": 8})).unwrap();+ app.run("edit.fill", json!({"color": "#c08040"})).unwrap();+ app.run("layer.new.layer", json!({"name": "Skin High"})).unwrap();+ app.run("edit.fill", json!({"color": "#406080"})).unwrap();+ app.sync_views();+ let background = app.session.active().unwrap().doc.walk()[0].2.name.clone();+ let open = |app: &mut PhotocraftApp| menu(app, &ctx, "image.applyImage", json!({})).unwrap()["dialog"].as_u64().unwrap();+ let d = open(&mut app);+ let fields = &app.ui.dialog_mut(d).unwrap().fields;+ assert_eq!(fields["sourceLayer"], "merged");+ assert_eq!(fields["scale"], 1.0);+ assert_eq!(fields["offset"], 0);+ // Merged first, then the layers top to bottom, shown by their own names.+ assert_eq!(fields["__choices"]["sourceLayer"], json!(["merged", "Skin High", background]));+ assert_eq!(fields["__labels"]["Skin High"], "Skin High");+ // The frequency separation recipe: a layer subtracted from itself at Scale 2, Offset 128 is mid grey.+ for (k, v) in [("sourceLayer", json!("Skin High")), ("blending", json!("subtract")), ("scale", json!(2.0)), ("offset", json!(128))] {+ app.ui.dialog_mut(d).unwrap().fields.insert(k.into(), v);+ }+ crate::dialogs::confirm(&mut app, d).unwrap();+ let st = app.session.active().unwrap();+ let p = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().pixel(3, 3);+ assert!(p[..3].iter().all(|v| (v - 128.0 / 255.0).abs() < 0.003), "{p:?}");+ app.run("edit.undo", json!({})).unwrap();+ // The chosen layer is the source, not the merged image.+ let d = open(&mut app);+ for (k, v) in [("sourceLayer", json!(background)), ("blending", json!("subtract")), ("scale", json!(2.0)), ("offset", json!(128))] {+ app.ui.dialog_mut(d).unwrap().fields.insert(k.into(), v);+ }+ crate::dialogs::confirm(&mut app, d).unwrap();+ let st = app.session.active().unwrap();+ let p = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().pixel(3, 3);+ let want = (64.0 - 192.0) / 255.0 / 2.0 + 128.0 / 255.0;+ assert!((p[0] - want).abs() < 0.003, "{p:?} vs {want}");+}+#[test]fn new_guide_layout_remembers_successful_values_not_cancelled_edits() {let (mut app, ctx) = app_with(1);