PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge PR #7395: Auto-Blend Layers: mask and blend the whole layers, past the canvas too
aionggfe28d9d43ce9
diff --cc crates/engine/src/align_cmds.rsindex 70848448f,3cc652e57..d4cc56b2b--- a/crates/engine/src/align_cmds.rs+++ b/crates/engine/src/align_cmds.rs@@@ -456,84 -472,77 +476,155 @@@ mod tests}}+ /// #5745: the Background can't take a mask, so Auto-Blend turns it into a normal layer first.+ #[test]+ fn auto_blend_turns_the_background_into_a_layer() {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 200, "height": 60, "background": "white"})).unwrap();+ let bg = s.active().unwrap().doc.background().unwrap().id;+ s.execute("layer.new.layer", json!({"name": "right"})).unwrap();+ s.edit("r", |doc, active| {+ doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(70, 0, 200, 60), &[0.9, 0.6, 0.2, 1.0]);+ Ok(())+ })+ .unwrap();+ let right = s.active().unwrap().active_layer.unwrap();+ select(&mut s, &[bg, right]);+ s.execute("edit.autoBlendLayers", json!({"method": "panorama"})).unwrap();+ let doc = &s.active().unwrap().doc;+ assert!(!doc.is_background(bg), "the Background became a layer to take its mask");+ assert!(doc.layer(bg).unwrap().mask.is_some());+ s.undo();+ assert!(s.active().unwrap().doc.is_background(bg), "undo restores the Background");+ }++ /// Seamless Tones and Colors evens out a 10% exposure step between two panorama layers.+ #[test]+ fn auto_blend_panorama_seamless_matches_exposure() {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 200, "height": 60, "background": "transparent"})).unwrap();+ let mut ids = Vec::new();+ for (name, x0, x1, gain) in [("left", 0, 130, 1.0f32), ("right", 70, 200, 1.1)] {+ s.execute("layer.new.layer", json!({ "name": name })).unwrap();+ s.edit(name, |doc, active| {+ doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(x0, 0, x1, 60), &[0.2 * gain, 0.4 * gain, 0.5 * gain, 1.0]);+ Ok(())+ })+ .unwrap();+ ids.push(s.active().unwrap().active_layer.unwrap());+ }+ select(&mut s, &ids);+ let r = s.execute("edit.autoBlendLayers", json!({"method": "panorama"})).unwrap();+ let doc = &s.active().unwrap().doc;+ let blended = doc.layer(LayerId(r["blended"].as_u64().unwrap())).unwrap().surface().unwrap();+ let (a, b) = (blended.pixel(10, 30), blended.pixel(190, 30));+ // 0.5 vs 0.55 without gain compensation.+ assert!((a[2] - b[2]).abs() < 0.015, "{a:?} {b:?}");+ }++ /// #5745: the top layer hangs off the canvas's right edge, so cropped to the canvas it lies+ /// wholly over the bottom one. It used to get an all-black mask; now it shows on its side.+ #[test]+ fn auto_blend_panorama_of_a_layer_the_canvas_crops_to_all_overlap() {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 200, "height": 60, "background": "transparent"})).unwrap();+ s.execute("layer.new.layer", json!({"name": "left"})).unwrap();+ s.edit("l", |doc, active| {+ doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(0, 0, 200, 60), &[0.2, 0.4, 0.5, 1.0]);+ Ok(())+ })+ .unwrap();+ let left = s.active().unwrap().active_layer.unwrap();+ s.execute("layer.new.layer", json!({"name": "right"})).unwrap();+ s.edit("r", |doc, active| {+ doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(120, 0, 320, 60), &[0.2, 0.4, 0.5, 1.0]);+ Ok(())+ })+ .unwrap();+ let right = s.active().unwrap().active_layer.unwrap();+ select(&mut s, &[left, right]);+ s.execute("edit.autoBlendLayers", json!({"method": "panorama", "seamlessTones": false})).unwrap();+ let doc = &s.active().unwrap().doc;+ let ml = doc.layer(left).unwrap().mask.as_ref().unwrap();+ let mr = doc.layer(right).unwrap().mask.as_ref().unwrap();+ assert_eq!((ml.value(10, 30), mr.value(10, 30)), (1.0, 0.0));+ assert_eq!((ml.value(195, 30), mr.value(195, 30)), (0.0, 1.0));+ for x in 0..200 {+ assert_eq!(ml.value(x, 30) + mr.value(x, 30), 1.0, "x {x} shows one layer");+ }+ }++ /// #5745: a layer Auto-Align left partly off the canvas is masked and blended over all of+ /// its bounds, not cut off at the canvas edge.+ #[test]+ fn auto_blend_panorama_masks_past_the_canvas() {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 200, "height": 60, "background": "transparent"})).unwrap();+ s.execute("layer.new.layer", json!({"name": "left"})).unwrap();+ s.edit("l", |doc, active| {+ doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(0, 0, 150, 60), &[0.2, 0.4, 0.5, 1.0]);+ Ok(())+ })+ .unwrap();+ let left = s.active().unwrap().active_layer.unwrap();+ s.execute("layer.new.layer", json!({"name": "right"})).unwrap();+ s.edit("r", |doc, active| {+ doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(120, 0, 320, 60), &[0.2, 0.4, 0.5, 1.0]);+ Ok(())+ })+ .unwrap();+ let right = s.active().unwrap().active_layer.unwrap();+ select(&mut s, &[left, right]);+ let r = s.execute("edit.autoBlendLayers", json!({"method": "panorama"})).unwrap();+ let doc = &s.active().unwrap().doc;+ let ml = doc.layer(left).unwrap().mask.as_ref().unwrap();+ let mr = doc.layer(right).unwrap().mask.as_ref().unwrap();+ assert_eq!((ml.value(10, 30), mr.value(10, 30)), (1.0, 0.0));+ assert_eq!((ml.value(300, 30), mr.value(300, 30)), (0.0, 1.0), "off the canvas");+ let blended = doc.layer(LayerId(r["blended"].as_u64().unwrap())).unwrap().surface().unwrap();+ assert_eq!(blended.pixel(300, 30)[3], 1.0, "the merged layer covers the off-canvas part");+ }++ /// A sparse layer whose bounds are huge (a dot at each far corner) raises its own content+ /// estimate, so the extent past the canvas is capped independently: it keeps to the canvas.+ #[test]+ fn auto_blend_sparse_huge_bounds_keep_to_the_canvas() {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 200, "height": 200, "background": "transparent"})).unwrap();+ s.execute("layer.new.layer", json!({"name": "a"})).unwrap();+ s.edit("a", |doc, active| {+ let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap();+ surf.fill_rect(Rect::new(0, 0, 150, 200), &[0.2, 0.4, 0.5, 1.0]);+ surf.fill_rect(Rect::new(100_000, 100_000, 100_001, 100_001), &[0.2, 0.4, 0.5, 1.0]);+ Ok(())+ })+ .unwrap();+ let a = s.active().unwrap().active_layer.unwrap();+ s.execute("layer.new.layer", json!({"name": "b"})).unwrap();+ s.edit("b", |doc, active| {+ doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(50, 0, 200, 200), &[0.2, 0.4, 0.5, 1.0]);+ Ok(())+ })+ .unwrap();+ let b = s.active().unwrap().active_layer.unwrap();+ select(&mut s, &[a, b]);+ let r = s.execute("edit.autoBlendLayers", json!({"method": "panorama"})).unwrap();+ let doc = &s.active().unwrap().doc;+ let bb = doc.layer(LayerId(r["blended"].as_u64().unwrap())).unwrap().surface().unwrap().content_bounds();+ assert!(bb.x1 <= 200 && bb.y1 <= 200, "{bb:?}");+ }++ /// The budget past the canvas counts every layer's buffers, not just the work-area pixels.+ #[test]+ fn auto_blend_budget_counts_layers_and_channels() {+ // Two RGBA layers: 4 bytes × (2 layers × (4 channels + luma, alpha, weight) + merged + depth).+ assert_eq!(blend_bytes_per_pixel(2, 4), 88);+ assert!(blend_bytes_per_pixel(5, 4) > blend_bytes_per_pixel(2, 4));+ // A 2^27-pixel area would need about 11 GiB for just two RGBA layers: it keeps to the canvas.+ assert!((1u64 << 27).saturating_mul(blend_bytes_per_pixel(2, 4)) > MAX_BLEND_BYTES);+ assert_eq!(blend_bytes_per_pixel(u64::MAX, 4), u64::MAX, "saturates instead of overflowing");+ }+#[test]fn auto_blend_stack_without_seamless() {let mut s = Session::new();