PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Apply Image onto a transparent layer no longer turns it black (#607)
Cristian Moroaicab9a24da79917
diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rsindex dcda30211..eee83a35b 100644--- a/crates/engine/src/channel_cmds.rs+++ b/crates/engine/src/channel_cmds.rs@@ -1371,20 +1371,25 @@ fn apply_image(s: &mut Session, p: &Value) -> Result<Value> {let colors = sf.mode.color_channels();let mut v = surf.read_region(area);for (i, px) in v.chunks_exact_mut(n).enumerate() {+ let base_a = if sf.alpha { px[colors].clamp(0.0, 1.0) } else { 1.0 };let mut w = weight(i);- if preserve && sf.alpha {- w *= px[colors].clamp(0.0, 1.0);+ if preserve {+ w *= base_a;}if w <= 0.0 {continue;}+ // Painted over the layer like a brush: the blend only acts where the layer has+ // pixels (where it is transparent the source shows as is), and the alpha grows by+ // source-over, so applying to an empty layer copies the source in any mode.+ let out_a = if preserve { base_a } else { w + base_a * (1.0 - w) };for (c, b) in px[..colors].iter_mut().enumerate() {let top = src.get(c).unwrap_or(&src[0])[i];- *b += (blending.apply(*b, top) - *b) * w;+ let blended = top + (blending.apply(*b, top) - top) * base_a;+ *b = if preserve { *b + (blended - *b) * w } else { (w * blended + base_a * (1.0 - w) * *b) / out_a };}- // Painting a transparent pixel makes it opaque, as Apply Image does on layers.- if !preserve && sf.alpha {- px[colors] = px[colors].max(w);+ if sf.alpha {+ px[colors] = out_a;}}surf.write_region(area, &v);diff --git a/crates/engine/src/channel_cmds/tests.rs b/crates/engine/src/channel_cmds/tests.rsindex 7b0c28e11..9d571862a 100644--- a/crates/engine/src/channel_cmds/tests.rs+++ b/crates/engine/src/channel_cmds/tests.rs@@ -357,6 +357,29 @@ fn apply_image_hand_computed() {assert!(s.execute("image.applyImage", json!({"blending": "hue"})).is_err());}+#[test]+fn apply_image_onto_transparent_layer() {+ for depth in [8, 16, 32] {+ let mut s = session_depth(depth);+ s.execute("edit.fill", json!({"color": "#c08040"})).unwrap();+ let (r, g, b) = (192.0 / 255.0, 128.0 / 255.0, 64.0 / 255.0);+ // An empty layer has no pixels to blend with: it takes the source, whatever the mode.+ s.execute("layer.new.layer", json!({})).unwrap();+ s.execute("image.applyImage", json!({"blending": "multiply"})).unwrap();+ let p = px(&s, 3, 3);+ assert!((p[0] - r).abs() < 0.003 && (p[1] - g).abs() < 0.003 && (p[2] - b).abs() < 0.003 && p[3] > 0.999, "depth {depth}: {p:?}");+ s.undo();+ // At 50% opacity the empty layer becomes the source at 50% alpha.+ s.execute("image.applyImage", json!({"blending": "multiply", "opacity": 50})).unwrap();+ let p = px(&s, 3, 3);+ assert!((p[0] - r).abs() < 0.003 && (p[3] - 0.5).abs() < 0.003, "depth {depth}: {p:?}");+ s.undo();+ // Preserve Transparency leaves an empty layer empty.+ s.execute("image.applyImage", json!({"blending": "multiply", "preserveTransparency": true})).unwrap();+ assert!(px(&s, 3, 3)[3] < 0.001, "depth {depth}");+ }+}+#[test]fn apply_image_into_alpha_channel() {let mut s = session();