Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(filters): Clouds size their pattern by the canvas, not the selection

Matthias Schlich875ec69fcee1
diff --git a/crates/algo/src/lib.rs b/crates/algo/src/lib.rs
index ac23dace0..8d8ab4d34 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 2a899858d..d13244f7b 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 e40be7b6a..d6919ad90 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<Value>
};
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)? {