From 875ec69fcee129ec2bed950b559dd6db86ad0b8b Mon Sep 17 00:00:00 2001 From: Matthias Schlich Date: Sat, 17 Oct 2026 12:28:10 +0200 Subject: [PATCH] fix(filters): Clouds size their pattern by the canvas, not the selection Since Clouds became a filter, its cloud size came from the filter's reference bounds, which are the selection's when there is one: clouds in a selection were finer than the canvas's. Before, they were sized by the document. Clouds now take the canvas as their bounds; the selection only masks them, as before (review on #6160). --- crates/algo/src/lib.rs | 6 ++++++ crates/engine/src/extra_cmds.rs | 25 +++++++++++++++++++++++++ crates/engine/src/filters.rs | 5 +++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/crates/algo/src/lib.rs b/crates/algo/src/lib.rs index ac23dace05..8d8ab4d34e 100644 --- a/crates/algo/src/lib.rs +++ b/crates/algo/src/lib.rs @@ -616,6 +616,12 @@ impl FilterParams { matches!(self, FilterParams::Fibers { .. } | FilterParams::Clouds { difference: false, .. }) } + /// Whether the filter sizes its pattern by the canvas even with a selection (Clouds): its + /// bounds are then the canvas, and the selection only masks the result. + pub fn sizes_by_canvas(&self) -> bool { + matches!(self, FilterParams::Clouds { .. }) + } + /// Human-readable name. pub fn label(&self) -> &'static str { match self { diff --git a/crates/engine/src/extra_cmds.rs b/crates/engine/src/extra_cmds.rs index 2a899858d6..d13244f7ba 100644 --- a/crates/engine/src/extra_cmds.rs +++ b/crates/engine/src/extra_cmds.rs @@ -1440,6 +1440,31 @@ mod tests { } } + /// A selection masks Clouds but doesn't change their size: inside it they are the canvas's + /// clouds, as when Clouds sized itself by the document (before it became a filter). + #[test] + fn clouds_in_a_selection_are_the_canvas_clouds() { + // Big enough that the canvas (cloud size 100 px) and the selection (16 px) size differently. + let big = |depth: u32| { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 400, "height": 300, "depth": depth})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s + }; + for depth in [8, 16, 32] { + let mut whole = big(depth); + whole.execute("filter.render.clouds", json!({"seed": 4})).unwrap(); + let mut s = big(depth); + s.execute("select.rect", json!({"x": 50, "y": 40, "width": 60, "height": 50})).unwrap(); + s.execute("filter.render.clouds", json!({"seed": 4})).unwrap(); + for (x, y) in [(50, 40), (75, 60), (100, 85), (109, 89)] { + let (a, b) = (pixel(&s, x, y), pixel(&whole, x, y)); + assert!((0..4).all(|c| (a[c] - b[c]).abs() < 1e-3), "depth {depth} at {x},{y}: {a:?} vs {b:?}"); + } + assert_eq!(pixel(&s, 300, 200)[3], 0.0, "outside the selection the layer stays empty"); + } + } + #[test] fn average_and_clouds() { let mut s = session(8); diff --git a/crates/engine/src/filters.rs b/crates/engine/src/filters.rs index e40be7b6ac..d6919ad906 100644 --- a/crates/engine/src/filters.rs +++ b/crates/engine/src/filters.rs @@ -262,9 +262,10 @@ pub(crate) fn run_filter(s: &mut Session, id: &str, p: &Value) -> Result }; let selection = doc.selection.clone(); let sel_bounds = selection.as_ref().map(photocraft_raster::Surface::content_bounds); - // Distortions centre on the selection when there is one, else the canvas. + // Distortions centre on the selection when there is one, else the canvas. Clouds size + // their pattern by the canvas either way; the selection only masks them. let doc_bounds = doc.bounds(); - let bounds = sel_bounds.filter(|b| !b.is_empty()).unwrap_or(doc_bounds); + let bounds = if fp.sizes_by_canvas() { doc_bounds } else { sel_bounds.filter(|b| !b.is_empty()).unwrap_or(doc_bounds) }; // Neighbourhood filters repeat edge pixels at the canvas edge (or past it, where the // layer has off-canvas pixels) instead of fading in transparency, as Photoshop does. if let Some(surf) = crate::channel_cmds::channel_surface_for_filter(doc, Some(layer), &p)? {