Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Apply Image: write into the targeted layer mask (#6780)

David Mann031ab7d5b2ab
diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rs
index 468db2698..9c052290a 100644
--- a/crates/engine/src/channel_cmds.rs
+++ b/crates/engine/src/channel_cmds.rs
@@ -1669,14 +1669,24 @@ fn apply_image(s: &mut Session, p: &Value) -> Result<Value> {
let area = doc.bounds();
let selection = doc.selection.as_ref().map(|m| sel::mask_from_surface(Some(m), area));
let weight = |i: usize| opacity * mask.as_ref().map_or(1.0, |m| m[i]) * selection.as_ref().map_or(1.0, |m| m[i]);
- if is_channel_target(p) {
+ let mask_target = target_of(p) == Target::Mask;
+ if is_channel_target(p) || mask_target {
// A grayscale target gets the luminosity of a colour source (one plane).
let srcp =
if src.len() == 1 { src[0].clone() } else { (0..src[0].len()).map(|i| src.iter().map(|pl| pl[i]).sum::<f32>() / src.len() as f32).collect() };
- let (surf, _) = target_surface(doc, None, p)?;
+ let (surf, _) = target_surface(doc, layer, p)?;
let base = read_plane(surf, area);
let out: Vec<f32> = base.iter().enumerate().map(|(i, &b)| b + (blending.apply(b, srcp[i]) - b) * weight(i)).collect();
- *surf = plane_surface(&out, area, surf.format());
+ if mask_target {
+ // A layer mask keeps its default (reveal or hide) outside the canvas.
+ if surf.channels() != 1 {
+ return Err(EngineError::Other("the layer mask is not a grayscale surface".into()));
+ }
+ surf.write_region(area, &out);
+ surf.prune();
+ } else {
+ *surf = plane_surface(&out, area, surf.format());
+ }
return Ok(());
}
let id = layer.ok_or_else(|| EngineError::Other("no active layer".into()))?;
diff --git a/crates/engine/src/channel_cmds/tests.rs b/crates/engine/src/channel_cmds/tests.rs
index d63dc5fd0..3c2386232 100644
--- a/crates/engine/src/channel_cmds/tests.rs
+++ b/crates/engine/src/channel_cmds/tests.rs
@@ -592,6 +592,25 @@ fn apply_image_into_alpha_channel() {
assert!(chan(&s, 0, 3, 3) < 0.01);
}
+#[test]
+fn apply_image_into_targeted_layer_mask_leaves_the_pixels() {
+ let mut s = session();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ s.execute("layer.layerMask.revealAll", json!({})).unwrap();
+ s.execute("channel.target", json!({"channel": "mask"})).unwrap();
+ // The green channel of the red composite is black: it lands in the mask, not the layer.
+ s.execute("image.applyImage", json!({"source": {"channel": "green"}, "blending": "normal"})).unwrap();
+ let d = s.active().unwrap();
+ let l = d.doc.layer(d.active_layer.unwrap()).unwrap();
+ let m = &l.mask.as_ref().unwrap().surface;
+ assert!(m.sample_channel(3, 3, 0) < 0.01, "mask {}", m.sample_channel(3, 3, 0));
+ // Outside the canvas the mask keeps revealing.
+ assert!(m.sample_channel(-5, -5, 0) > 0.99, "mask outside {}", m.sample_channel(-5, -5, 0));
+ let p = px(&s, 3, 3);
+ assert!(p[0] > 0.99 && p[1] < 0.01 && p[2] < 0.01 && p[3] > 0.99, "layer pixels {p:?}");
+}
+
#[test]
fn calculations_results() {
let mut s = session();