PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Auto-Blend Layers: blend a layer the canvas crops to all-overlap (#5745)
Higgs32584a43f8b5ece00
diff --git a/crates/algo/src/panorama.rs b/crates/algo/src/panorama.rsindex fd7ccbc59..eaf2b5eb4 100644--- a/crates/algo/src/panorama.rs+++ b/crates/algo/src/panorama.rs@@ -897,11 +897,31 @@ pub fn seam_labels(w: usize, h: usize, luma: &[Vec<f32>], cover: &[Vec<bool>], olabel[i] = img as i32;}}- if no == 0 || nn == no {+ if no == 0 {continue;}- let (ox, oy) = (sx / no as f64, sy / no as f64);let (cx, cy) = (nx / nn as f64, ny / nn as f64);+ let (ox, oy) = if nn == no {+ // Wholly inside the composite (#5745: two photos in one document, the second moved+ // by Auto-Align so the canvas crops it to the part over the first). Its side of the+ // seam is the one it lies towards from the composite's centre; an image centred on+ // the composite has no side, and the earlier images keep it.+ let (mut px, mut py, mut pn) = (0.0f64, 0.0f64, 0usize);+ for (i, l) in label.iter().enumerate() {+ if *l >= 0 {+ px += (i % w) as f64;+ py += (i / w) as f64;+ pn += 1;+ }+ }+ let (px, py) = (px / pn.max(1) as f64, py / pn.max(1) as f64);+ if (cx - px).abs() < 0.5 && (cy - py).abs() < 0.5 {+ continue;+ }+ (px, py)+ } else {+ (sx / no as f64, sy / no as f64)+ };let vertical = (cx - ox).abs() >= (cy - oy).abs();// Image on the increasing side along the seam's cross axis?let positive = if vertical { cx > ox } else { cy > oy };@@ -1430,4 +1450,28 @@ mod tests {assert!(max_jump < 0.1, "max step {max_jump}");assert!(cov.iter().all(|c| *c == 1.0));}++ #[test]+ fn an_image_wholly_inside_the_composite_still_gets_its_side() {+ // #5745: the first image covers the whole 120×40 grid, the second only its right part+ // (x ≥ 70), all of it overlap. It used to be skipped, so the first image kept everything.+ let (w, h) = (120usize, 40usize);+ let luma = vec![vec![0.5f32; w * h], vec![0.5f32; w * h]];+ let cover: Vec<Vec<bool>> = vec![vec![true; w * h], (0..w * h).map(|i| i % w >= 70).collect()];+ let labels = seam_labels(w, h, &luma, &cover, &[0, 1]);+ for y in 0..h {+ assert_eq!(labels[y * w + 5], 0, "row {y}");+ assert_eq!(labels[y * w + w - 1], 1, "row {y}");+ // The second image's region is one run at the right of its seam.+ let row = &labels[y * w..(y + 1) * w];+ let first = row.iter().position(|l| *l == 1).unwrap();+ assert!(first >= 70 && row[first..].iter().all(|l| *l == 1), "row {y}: {row:?}");+ }+ // An image centred on the composite has no side: the first image keeps the overlap.+ let centred: Vec<Vec<bool>> = vec![vec![true; w * h], (0..w * h).map(|i| (20..100).contains(&(i % w))).collect()];+ assert!(seam_labels(w, h, &luma, ¢red, &[0, 1]).iter().all(|l| *l == 0));+ // Two images covering the same cells, likewise.+ let same = vec![vec![true; w * h]; 2];+ assert!(seam_labels(w, h, &luma, &same, &[0, 1]).iter().all(|l| *l == 0));+ }}diff --git a/crates/engine/src/align_cmds.rs b/crates/engine/src/align_cmds.rsindex c7df82902..05d367e14 100644--- a/crates/engine/src/align_cmds.rs+++ b/crates/engine/src/align_cmds.rs@@ -178,13 +178,21 @@ fn auto_blend(s: &mut Session, p: &Value) -> Result<Value> {}let st = s.active().ok_or(EngineError::NoDocument)?;let doc: std::sync::Arc<Document> = st.doc.clone();- let mut area = Rect::EMPTY;+ let mut union = Rect::EMPTY;+ let mut content_px = 0u64;+ let px_of = |r: &Rect| u64::from(r.width()) * u64::from(r.height());for id in &ids {if let Some(b) = doc.layer(*id).and_then(|l| l.surface()).map(|s| s.content_bounds()) {- area = area.union(&b);+ union = union.union(&b);+ content_px = content_px.saturating_add(px_of(&b));}}- let area = area.intersect(&doc.bounds());+ // The whole layers, past the canvas too (#5745): Auto-Align leaves a panorama's layers+ // partly off the canvas, and Photoshop masks and blends all of each layer, so Reveal All+ // shows the whole panorama and the seams know where each layer really extends the picture.+ // Layers so far apart that their bounds are mostly empty keep to the canvas.+ let canvas = doc.bounds();+ let area = if px_of(&union) <= content_px.max(px_of(&canvas)).saturating_mul(2) { union } else { union.intersect(&canvas) };if area.is_empty() {return Err(EngineError::Other("the selected layers are empty".into()));}@@ -225,7 +233,9 @@ fn auto_blend(s: &mut Session, p: &Value) -> Result<Value> {let surfs: Vec<Surface> =ids.iter().map(|id| doc.layer(*id).and_then(|l| l.surface()).cloned().ok_or(EngineError::NoLayer(*id))).collect::<Result<_>>()?;let order = seam_order(&surfs, 0);- let b = seam_blend(&surfs, area, &order, seamless, false, None);+ // Seamless Tones and Colors also evens out exposure and colour between the layers (gain+ // compensation over their overlaps), as in Photomerge.+ let b = seam_blend(&surfs, area, &order, seamless, seamless, None);let weights: Vec<Vec<f32>> = b.layers.iter().map(|(_, m)| m.read_region(area)).collect();let blended = seamless.then(|| b.composite.read_region(area));(weights, blended)@@ -258,6 +268,8 @@ fn auto_blend(s: &mut Session, p: &Value) -> Result<Value> {let label = "Auto-Blend Layers";let (new_layer, depth_layer) = s.edit(label, |doc, active| {for (id, wt) in ids.iter().zip(&weights) {+ // The Background can't have a mask: it becomes a normal layer first, as in Photoshop.+ crate::extra_cmds::background_to_layer_for_mask(doc, *id);let l = doc.layer_mut(*id).ok_or(EngineError::NoLayer(*id))?;let mut mask = LayerMask::hide_all();mask.surface.write_region(area, wt);@@ -418,6 +430,57 @@ mod tests {}}+ /// #5745: two photos in one document, the second moved by Auto-Align so it hangs off the+ /// canvas's right edge, 10% brighter. The bottom (the Background) covers the whole canvas,+ /// so cropped to the canvas the top layer was all overlap: it got an all-black mask and the+ /// picture didn't change. Now the top layer shows on its side of the seam, off the canvas+ /// too, and Seamless Tones and Colors evens out the exposure.+ #[test]+ fn auto_blend_panorama_of_a_layer_hanging_off_the_canvas() {+ for depth in [8, 16, 32] {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 200, "height": 60, "depth": depth, "background": "white"})).unwrap();+ let bg = s.active().unwrap().doc.background().unwrap().id;+ // The same scene in both layers, so they agree where they overlap (x 120..200).+ let scene = |x: i32, gain: f32| [(0.2 + 0.6 * x as f32 / 320.0) * gain, 0.4 * gain, 0.5 * gain, 1.0];+ s.edit("left", |doc, _| {+ let surf = doc.layer_mut(bg).unwrap().surface_mut().unwrap();+ for x in 0..200 {+ surf.fill_rect(Rect::new(x, 0, x + 1, 60), &scene(x, 1.0));+ }+ Ok(())+ })+ .unwrap();+ s.execute("layer.new.layer", json!({"name": "right"})).unwrap();+ s.edit("right", |doc, active| {+ let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap();+ for x in 120..320 {+ surf.fill_rect(Rect::new(x, 0, x + 1, 60), &scene(x, 1.1));+ }+ Ok(())+ })+ .unwrap();+ let right = s.active().unwrap().active_layer.unwrap();+ select(&mut s, &[bg, right]);+ let r = s.execute("edit.autoBlendLayers", json!({"method": "panorama"})).unwrap();+ let doc = &s.active().unwrap().doc;+ assert!(!doc.is_background(bg), "depth {depth}: the Background became a layer to take its mask");+ let ml = doc.layer(bg).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), "depth {depth}");+ assert_eq!((ml.value(300, 30), mr.value(300, 30)), (0.0, 1.0), "depth {depth}: off the canvas");+ for x in 0..320 {+ assert_eq!(ml.value(x, 30) + mr.value(x, 30), 1.0, "depth {depth}: x {x} shows one layer");+ }+ // The merged layer covers the off-canvas part too, with the exposures matched: the+ // flat blue channel reads the same on both sides (0.5 vs 0.55 before).+ let blended = doc.layer(LayerId(r["blended"].as_u64().unwrap())).unwrap().surface().unwrap();+ let (a, b) = (blended.pixel(40, 30), blended.pixel(300, 30));+ assert_eq!(b[3], 1.0, "depth {depth}");+ assert!((a[2] - b[2]).abs() < 0.015, "depth {depth}: {a:?} {b:?}");+ }+ }+#[test]fn auto_blend_stack_without_seamless() {let mut s = Session::new();