PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge PR #5816: Auto-Blend Layers: blend a layer the canvas crops to all-overlap (#5745)
aiongg18bc4ab87692
diff --cc crates/engine/src/align_cmds.rsindex 0a508cc91,10fc2e7f9..70848448f--- a/crates/engine/src/align_cmds.rs+++ b/crates/engine/src/align_cmds.rs@@@ -456,52 -452,38 +456,84 @@@ 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");+ }+ }+#[test]fn auto_blend_stack_without_seamless() {let mut s = Session::new();