Skip to content

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

Fibers: fill an empty layer, as Photoshop does (#5241) (#5289)

Gert Pellin - GPE4bbcede5725f
diff --git a/crates/algo/src/lib.rs b/crates/algo/src/lib.rs
index 78dae7b06..49ab15871 100644
--- a/crates/algo/src/lib.rs
+++ b/crates/algo/src/lib.rs
@@ -600,6 +600,13 @@ impl FilterParams {
self.halo() == Halo::Bounds
}
+ /// Whether the filter renders new pixels without reading the layer's own (Fibers): it fills
+ /// the reference bounds even where the layer is transparent, as Photoshop's does on an
+ /// empty layer, while still reading no neighbourhood.
+ pub fn renders_from_nothing(&self) -> bool {
+ matches!(self, FilterParams::Fibers { .. })
+ }
+
/// Human-readable name.
pub fn label(&self) -> &'static str {
match self {
@@ -905,12 +912,13 @@ pub fn kernel(params: &FilterParams, src: &Image, out: Rect, ctx: &Ctx) -> Vec<f
pub const TILE: i32 = 256;
/// The area a filter writes for a layer whose pixels cover `content`:
-/// global filters write the reference bounds; others the content grown by
-/// the halo (blurs spread into transparent areas). Clipped to the
-/// selection's bounds when there is one.
+/// global filters and renders that ignore the layer's pixels (Fibers) write the reference
+/// bounds; others the content grown by the halo (blurs spread into transparent areas).
+/// Clipped to the selection's bounds when there is one.
pub fn output_area(params: &FilterParams, content: Rect, bounds: Rect, selection_bounds: Option<Rect>) -> Rect {
let mut area = match params.halo_for(bounds) {
Halo::Bounds => bounds,
+ Halo::Radius(_) if params.renders_from_nothing() => bounds,
Halo::Radius(r) => {
if content.is_empty() {
Rect::EMPTY
diff --git a/crates/algo/src/tests.rs b/crates/algo/src/tests.rs
index 9a90012df..31def5f99 100644
--- a/crates/algo/src/tests.rs
+++ b/crates/algo/src/tests.rs
@@ -446,6 +446,10 @@ fn halos_and_output_area() {
assert_eq!(output_area(&FilterParams::Twirl { angle: 1.0 }, content, R, None), R);
assert_eq!(output_area(&FilterParams::BoxBlur { radius: 1.0 }, content, R, Some(Rect::new(0, 0, 12, 12))), Rect::new(8, 8, 12, 12));
assert!(output_area(&FilterParams::Invert, Rect::EMPTY, R, None).is_empty());
+ // Fibers renders without reading the layer: an empty layer still fills the bounds (#5241).
+ let fibers = FilterParams::Fibers { variance: 16.0, strength: 4.0, seed: 0, foreground: [0.0; 4], background: [1.0; 4] };
+ assert_eq!(output_area(&fibers, Rect::EMPTY, R, None), R);
+ assert_eq!(output_area(&fibers, content, R, Some(Rect::new(0, 0, 12, 12))), Rect::new(0, 0, 12, 12));
}
#[test]
diff --git a/crates/engine/src/filters_ext/tests.rs b/crates/engine/src/filters_ext/tests.rs
index b616101f7..0f32c5367 100644
--- a/crates/engine/src/filters_ext/tests.rs
+++ b/crates/engine/src/filters_ext/tests.rs
@@ -164,6 +164,27 @@ fn colour_filters_use_and_record_tool_colours() {
}
}
+#[test]
+fn fibers_fill_an_empty_layer() {
+ // #5241: Fibers renders from the colours, not the layer's pixels, so an empty layer fills
+ // like in Photoshop (and can be transformed afterwards).
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 48, "height": 32})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.execute("filter.render.fibers", json!({"seed": 3})).unwrap();
+ let px = pixels(&s);
+ assert!(px.as_chunks::<4>().0.iter().all(|p| p[3] == 1.0), "every pixel is rendered opaque");
+ s.execute("edit.transform.flipHorizontal", json!({})).unwrap();
+
+ // With a selection only the selection fills.
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.execute("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 6})).unwrap();
+ s.execute("filter.render.fibers", json!({"seed": 3})).unwrap();
+ let d = s.active().unwrap();
+ let surf = d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap();
+ assert_eq!(surf.content_bounds(), Rect::new(4, 4, 14, 10));
+}
+
#[test]
fn displace_reads_another_document() {
let mut s = session();