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

Fibers renders from the foreground and background colours without
reading the layer, but its output area was the layer's content grown by
a zero halo: on an empty layer that is empty, so nothing was drawn (and
a following transform found nothing to transform). On a partly filled
layer only the content's bounding box was filled. It now writes the
reference bounds (the selection's, else the canvas) like the global
filters, without reading the whole layer as they do.
This commit is contained in:
Gert Pellin - GPE authored and GitHub committed 2026-10-14 01:59:00 -04:00
1 parent 2da2889546
commit 4bbcede572
3 files changed
+36 -3

No files matched your search

+11 -3
View File
@@ -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
+4
View File
@@ -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]
+21
View File
@@ -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();