PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(layers): merging layers keeps pixels that lie outside the canvas (#5067) (#5124)
MarekSpodBiedryd12dc6b71882
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex a954eca7e..38a1b4d7e 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -1550,7 +1550,7 @@ fn merge_group(s: &mut Session, id: LayerId) -> Result<Value> {solo.clipped = false;let mut tmp = doc.clone();tmp.layers = vec![solo];- let mut merged = Layer::new(group.name.clone(), LayerContent::Raster(pixels::composite_layers(&tmp, fmt, None)));+ let mut merged = Layer::new(group.name.clone(), LayerContent::Raster(pixels::composite_layers_uncropped(&tmp, fmt)));merged.id = group.id;merged.opacity = group.opacity;merged.blend = if group.blend == photocraft_doc::BlendMode::PassThrough { photocraft_doc::BlendMode::Normal } else { group.blend };diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rsindex 80da3bdd8..2accc4ea8 100644--- a/crates/engine/src/layer_multi_cmds.rs+++ b/crates/engine/src/layer_multi_cmds.rs@@ -1270,7 +1270,7 @@ fn merge_layers(s: &mut Session) -> Result<Value> {let fmt = PixelFormat::new(fmt.mode, fmt.sample, true);let mut merged = Layer::raster(top.name.clone(), fmt);merged.locks = top.locks;- *crate::pixels_mut(&mut merged)? = crate::pixels::composite_layers(&solo, fmt, None);+ *crate::pixels_mut(&mut merged)? = crate::pixels::composite_layers_uncropped(&solo, fmt);let mid = merged.id;// Hidden selected layers are discarded, as in Photoshop.for id in &ids[..ids.len() - 1] {@@ -2051,6 +2051,37 @@ mod tests {assert!(!s.is_enabled("layer.distribute.leftEdges"));}+ #[test]+ fn merge_keeps_pixels_outside_the_canvas() {+ // #5067: the merged layer was cropped to the 100x80 canvas.+ let mut s = session(8);+ let a = rect_layer(&mut s, Rect::new(-30, -20, 10, 10));+ let b = rect_layer(&mut s, Rect::new(90, 70, 140, 120));+ select_all(&mut s, &[a, b]);+ let m = LayerId(s.execute("layer.mergeLayers", json!({})).unwrap()["layer"].as_u64().unwrap());+ let ml = doc(&s).layer(m).unwrap();+ assert_eq!(layer_bounds(ml).unwrap(), Rect::new(-30, -20, 140, 120));+ assert_eq!(ml.surface().unwrap().pixel(-10, -5)[3], 1.0);+ assert_eq!(ml.surface().unwrap().pixel(130, 110)[3], 1.0);+ }++ #[test]+ fn merge_down_and_merge_group_keep_pixels_outside_the_canvas() {+ let mut s = session(8);+ let a = rect_layer(&mut s, Rect::new(-30, -20, 10, 10));+ let b = rect_layer(&mut s, Rect::new(90, 70, 140, 120));+ s.execute("layer.mergeDown", json!({"layer": b.0})).unwrap();+ assert_eq!(layer_bounds(doc(&s).layer(a).unwrap()).unwrap(), Rect::new(-30, -20, 140, 120));++ let mut s = session(8);+ let a = rect_layer(&mut s, Rect::new(-30, -20, 10, 10));+ let b = rect_layer(&mut s, Rect::new(90, 70, 140, 120));+ select_all(&mut s, &[a, b]);+ let g = LayerId(s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap());+ s.execute("layer.mergeGroup", json!({"layer": g.0})).unwrap();+ assert_eq!(layer_bounds(doc(&s).layer(g).unwrap()).unwrap(), Rect::new(-30, -20, 140, 120));+ }+#[test]fn merge_selected_into_top_most() {for depth in [8, 16, 32] {diff --git a/crates/engine/src/pixels.rs b/crates/engine/src/pixels.rsindex cde7fa41d..b2641a5c1 100644--- a/crates/engine/src/pixels.rs+++ b/crates/engine/src/pixels.rs@@ -28,6 +28,36 @@ pub fn composite_layers(solo: &Document, format: photocraft_color::PixelFormat,photocraft_compose::composite_to_surface(solo, area, format, background)}+/// [`composite_layers`] without cropping to the canvas: pixels of the layers that lie outside it are kept+/// (merging layers must not lose them, #5067).+pub fn composite_layers_uncropped(solo: &Document, format: photocraft_color::PixelFormat) -> Surface {+ let canvas = solo.bounds();+ // The canvas grown to every visible layer's content and effects, so no layer is clipped by `composite_bounds`.+ let reach = solo+ .layers+ .iter()+ .filter(|layer| layer.visible)+ .map(|layer| {+ let margin = if photocraft_compose::effects::has_effects(layer) { photocraft_compose::effects::margin(layer) } else { 0 };+ let bounds = photocraft_compose::layer_bounds(layer, canvas);+ if bounds.is_empty() { bounds } else { bounds.inflate(margin) }+ })+ .filter(|bounds| !bounds.is_empty())+ .fold(canvas, |a, b| a.union(&b));+ let area = solo+ .layers+ .iter()+ .filter(|layer| layer.visible)+ .map(|layer| photocraft_compose::composite_bounds(layer, reach).unwrap_or(canvas))+ .filter(|bounds| !bounds.is_empty())+ .reduce(|a, b| a.union(&b))+ .unwrap_or(Rect::EMPTY);+ if area.is_empty() {+ return Surface::new(format);+ }+ photocraft_compose::composite_to_surface(solo, area, format, None)+}+/// Apply an adjustment destructively to a surface, weighted by an optional selection./// Applies `adj` to a surface (any colour model and depth, via straight RGBA) through the/// selection; `mode` is the document's, for the tone transfer (e.g. Exposure in Grayscale).@@ -339,7 +369,8 @@ pub fn merge_down(doc: &Document, lower: &Layer, upper: &Layer) -> Layer {// The pair is read (and a CMYK one separated again) through the document's own profile.tmp.icc_profile = doc.icc_profile.clone();tmp.layers = stack;- let s = composite_layers(&tmp, format, None);+ // A Background stays canvas-sized; any other layer keeps the pixels outside the canvas (#5067).+ let s = if doc.is_background(lower.id) { composite_layers(&tmp, format, None) } else { composite_layers_uncropped(&tmp, format) };let mut merged = Layer::new(lower.name.clone(), LayerContent::Raster(s));merged.id = lower.id;merged.blend = lower.blend;