Skip to content

armgit / klad

Public Imported from GitHub
0

A drawing app built for agent/human collaboration

Rust

Flood fills feather the edges they leave in open space

Indicode BV70353acd7211
diff --git a/crates/brush/src/fill.rs b/crates/brush/src/fill.rs
index 58daae2..a7d8ad2 100644
--- a/crates/brush/src/fill.rs
+++ b/crates/brush/src/fill.rs
@@ -311,12 +311,41 @@ pub fn flood(reference: &Raster, seed: &Seed) -> Coverage {
let wide = dilate(&region, w, h, seed.expand);
and(&wide, |i| region[i] != 0 || lines[i] != 0)
} else {
- region
+ region.clone()
+ };
+ let alpha = match seed.expand > 0.0 {
+ true => feathered(&region, &grown, w, h),
+ false => grown.par_iter().map(|&v| v * 255).collect(),
};
- let alpha: Vec<u8> = grown.par_iter().map(|&v| v * 255).collect();
Coverage::from_mask(reference.width, reference.height, &alpha)
}
+fn feathered(region: &[u8], grown: &[u8], w: usize, h: usize) -> Vec<u8> {
+ const WEIGHTS: [[u32; 3]; 3] = [[1, 2, 1], [2, 4, 2], [1, 2, 1]];
+ let near = |x: usize, y: usize, of: &[u8]| -> u32 {
+ (0..3)
+ .flat_map(|dy| (0..3).map(move |dx| (dx, dy)))
+ .filter_map(|(dx, dy)| {
+ let (nx, ny) = ((x + dx).checked_sub(1)?, (y + dy).checked_sub(1)?);
+ (nx < w && ny < h && of[ny * w + nx] != 0).then_some(WEIGHTS[dy][dx])
+ })
+ .sum()
+ };
+ (0..w * h)
+ .into_par_iter()
+ .map(|i| match grown[i] != 0 {
+ true => 255,
+ false => {
+ let (x, y) = (i % w, i / w);
+ match near(x, y, region) {
+ 0 => 0,
+ _ => ((near(x, y, grown) * 255 + 8) / 16).min(127) as u8,
+ }
+ }
+ })
+ .collect()
+}
+
pub fn polygon(points: &[[f32; 2]], width: u32, height: u32) -> Coverage {
let (w, h) = (width as usize, height as usize);
if points.len() < 3 || w == 0 || h == 0 {
@@ -852,7 +881,11 @@ mod tests {
);
let first_inside = column.iter().position(|(a, _)| *a > 0).unwrap();
let last_outside = column.iter().rposition(|(_, b)| *b > 0).unwrap();
- assert!(last_outside <= first_inside + 1, "x {x}: {column:?}");
+ assert!(last_outside <= first_inside + 2, "x {x}: {column:?}");
+ assert!(
+ column[first_inside].0 < 255 && column[last_outside].1 < 255,
+ "each edge across the gap is feathered by a pixel: {column:?}"
+ );
assert!(
(40..=44).contains(&(34 + first_inside)),
"x {x}: inside starts at {}",
diff --git a/docs/DESIGN.md b/docs/DESIGN.md
index 055fc35..358c724 100644
--- a/docs/DESIGN.md
+++ b/docs/DESIGN.md
@@ -267,7 +267,7 @@ Estimate before M0: about 0.5 KB per stroke, so 10,000 strokes is about 5 MB. A
A fill is a record in a paint cel, in order with the strokes: id, colour, opacity, author, how it was made, and the coverage it produced (a crop box and 8-bit alpha, run-length coded, then zstd with the rest of the file). Replay paints the stored coverage like a stroke buffer at full flow, so a fill never depends on other layers or on the fill code: CLI and editor, macOS and Linux get the same pixels.
- **Flood fill** (`at`): pixels count as inside when every premultiplied channel is within `tolerance` (0 to 1, default 0.1) of the seed pixel in the **reference**: `this` layer as it was just before the fill (its earlier marks), a named layer (paint, vector or group, drawn alone at full opacity, even when hidden) or `all` (everything visible over the background). Four-connected.
-- **Gap closing** (`gap`, px): the lines are grown by `(gap + 1) / 2`, so openings up to `gap` px wide close and the open space splits into free regions; the fill takes the seed's region (a click near a line starts from the nearest free region). The pixels the growth took from the open space go to whichever region reaches them first along paths that stay off the lines (chamfer distance, ties to this fill), so the fill reaches into corners and sharp points, two fills that meet in a gap meet on a line across it, and a fill never escapes through a gap it closed. Islands of free space with no pixel 2 px clear of the grown lines do not count as regions.
+- **Gap closing** (`gap`, px): the lines are grown by `(gap + 1) / 2`, so openings up to `gap` px wide close and the open space splits into free regions; the fill takes the seed's region (a click near a line starts from the nearest free region). The pixels the growth took from the open space go to whichever region reaches them first along paths that stay off the lines (chamfer distance, ties to this fill), so the fill reaches into corners and sharp points, two fills that meet in a gap meet on a line across it, and a fill never escapes through a gap it closed. Islands of free space with no pixel 2 px clear of the grown lines do not count as regions. Where a fill that grows under the lines (`expand` above 0) ends in open space (across a gap, or at its tolerance), the pixels just outside it get a feathered edge from the fill's 3x3 neighbourhood (at most half cover), so a diagonal edge across a gap is smooth instead of a staircase and two fills meeting in a gap blend into each other with no seam; edges under the lines stay as they were.
- **Expand** (`expand`, px, default 2): the result grows that far into line pixels only, under the line's anti-aliased edge, so no paper shows between line and colour.
- **Lasso fill** (`polygon`): the polygon's area with 4x4 supersampled edges, nonzero winding.
- **Gaps**: `klad gaps` cuts a fill layer's reference into closed regions (as a flood fill with the layer's gap would, one pixel further from the lines) and lists those no fill of the layer covers: pockets a fill missed, or drawings not coloured yet. Regions open to the canvas edge are the outside and do not count; smaller than `--min` px (12) are ignored. The reference, gap and tolerance default to the layer's flood fills on those frames.