Skip to content

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

fix(masks): retain off-canvas alpha in From Transparency (#3043) (#3177)

Nigel Ihza Firdausy3188866b257a
diff --git a/crates/engine/src/layer_menu_cmds.rs b/crates/engine/src/layer_menu_cmds.rs
index 93cd438e7..a9ee18b4e 100644
--- a/crates/engine/src/layer_menu_cmds.rs
+++ b/crates/engine/src/layer_menu_cmds.rs
@@ -148,9 +148,10 @@ fn apply_mask(s: &mut Session, p: &Value) -> Result<Value> {
fn mask_from_transparency(s: &mut Session, p: &Value) -> Result<Value> {
let id = layer_param(s, p)?;
s.edit("From Transparency", |doc, _| {
- let bounds = doc.bounds();
let surf = crate::commands::paint_surface(doc, id, &Value::Null)?;
- let r = surf.content_bounds().intersect(&bounds);
+ // Masks are attached to layer coordinates, including pixels temporarily off-canvas.
+ // Clipping to document bounds loses their alpha permanently when moved back (#3043).
+ let r = surf.content_bounds();
let mut mask = LayerMask::hide_all();
if !r.is_empty() {
let mut px = read_rgba(surf, r);
diff --git a/crates/engine/src/layer_menu_cmds/tests.rs b/crates/engine/src/layer_menu_cmds/tests.rs
index 1eae5b4e2..1050e33ee 100644
--- a/crates/engine/src/layer_menu_cmds/tests.rs
+++ b/crates/engine/src/layer_menu_cmds/tests.rs
@@ -579,3 +579,21 @@ fn transfer_effects_moves_or_copies_between_layers() {
}
assert_eq!(s.active().unwrap().history.past_len(), steps);
}
+
+#[test]
+fn from_transparency_preserves_mask_coverage_outside_the_canvas() {
+ for depth in DEPTHS {
+ let mut s = session(depth, "rgb");
+ paint(&mut s, |_, _| [1.0, 0.0, 0.0, 0.5]);
+ let before = photocraft_compose::flatten(doc(&s)).px;
+ s.execute("layer.translate", json!({"dx": 20})).unwrap();
+ s.execute("layer.layerMask.fromTransparency", json!({})).unwrap();
+ let mask = active(&s).mask.as_ref().unwrap();
+ assert!((mask.value(50, 10) - 0.5).abs() < 1.0 / 255.0 + 1e-6, "off-canvas mask at depth {depth}");
+ s.execute("layer.translate", json!({"dx": -20})).unwrap();
+ let after = photocraft_compose::flatten(doc(&s)).px;
+ for (a, b) in before.iter().zip(&after) {
+ assert!((a[3] - b[3]).abs() <= 1.0 / 255.0 + 1e-6, "alpha changed at depth {depth}: {a:?} -> {b:?}");
+ }
+ }
+}