address review: lasso refuses too many edge samples; the band takes full-width rows for non-finite or huge coordinates

This commit is contained in:
Dylan M. Taylor committed 2026-11-11 10:44:12 +00:00
1 parent f2749d5242
commit c6f93cf7c1
4 files changed
+60 -9

No files matched your search

+1 -1
View File
@@ -841,7 +841,7 @@ mod wand_antialias_tests;
pub use distance::edt;
pub use inverse::inverse_surface;
pub(crate) use lasso::clip_segment;
pub use lasso::{MAX_PAINTED_TILES, polygon_edge_tiles, polygon_surface};
pub use lasso::{MAX_EDGE_SAMPLES, MAX_PAINTED_TILES, polygon_edge_samples, polygon_edge_tiles, polygon_surface};
pub use marquee::{combine_sparse, combine_surfaces, ellipse_surface};
/// Grows the selection by `r` pixels.
+13 -3
View File
@@ -96,6 +96,18 @@ struct Edge {
b: (f32, f32),
}
/// Edge samples rasterizing `points` over the rows of `clip` costs at `sub` sub-rows per row: every
/// edge is visited on every sub-row it spans, so this is the work, to compare with [`MAX_EDGE_SAMPLES`].
pub fn polygon_edge_samples(points: &[(f32, f32)], clip: Rect, sub: u64) -> u64 {
let (y0, y1) = (clip.y0 as f32, clip.y1 as f32);
points
.iter()
.zip(points.iter().cycle().skip(1))
.filter(|(a, b)| a.1 < b.1 || b.1 < a.1)
.map(|(a, b)| ((a.1.max(b.1).min(y1) - a.1.min(b.1).max(y0)).max(0.0) as u64 + 1).saturating_mul(sub))
.fold(0u64, u64::saturating_add)
}
/// The anti-aliased (even-odd) polygon through `points`, cut to `clip`, as a GRAY8 selection: the
/// bytes of [`super::polygon`] over `clip`, quantised. Edge tiles are rasterised, tiles wholly
/// inside share one solid tile. `None` when the edges cross more than [`MAX_PAINTED_TILES`] tiles.
@@ -123,9 +135,7 @@ pub fn polygon_surface(points: &[(f32, f32)], clip: Rect) -> Option<Surface> {
})
.collect();
edges.sort_by(|p, q| p.top.total_cmp(&q.top));
let (y0, y1) = (clip.y0 as f32, clip.y1 as f32);
let samples: u64 = edges.iter().map(|e| ((e.bottom.min(y1) - e.top.max(y0)).max(0.0) as u64 + 1) * SUB as u64).sum();
if samples > MAX_EDGE_SAMPLES {
if polygon_edge_samples(points, clip, SUB as u64) > MAX_EDGE_SAMPLES {
return None;
}
let crossings = |e: &[&Edge], y: f32, xs: &mut Vec<f32>| {
+19 -5
View File
@@ -680,6 +680,7 @@ pub(crate) fn polygon_selection(s: &mut Session, cmd: &str, label: &str, pts: &[
Rect::EMPTY
};
let window = feather_window(bounds, area, feather);
check_edge_samples(cmd, pts, window, p)?;
let mask = sel::polygon_window(pts, area, window, b(p, "antiAlias", true));
let mask = sel::feather(&mask, window.width() as usize, window.height() as usize, feather);
let selected = s.edit(label, |doc, _| {
@@ -694,6 +695,7 @@ pub(crate) fn polygon_selection(s: &mut Session, cmd: &str, label: &str, pts: &[
// And only the columns it spans: a narrow outline on a wide canvas must not allocate every
// row at the canvas's width. `polygon_window` keeps the whole-canvas arithmetic.
let band = polygon_window_rect(pts, area);
check_edge_samples(cmd, pts, band, p)?;
let mask = sel::polygon_window(pts, area, band, b(p, "antiAlias", true));
let m = mode(p);
let selected = s.edit(label, |doc, _| {
@@ -703,6 +705,16 @@ pub(crate) fn polygon_selection(s: &mut Session, cmd: &str, label: &str, pts: &[
Ok(json!({ "selected": selected }))
}
/// Refuses a polygon whose rasterization over `window` would visit more than [`sel::MAX_EDGE_SAMPLES`]
/// edge samples (every edge on every sub-row it spans), as [`sel::polygon_surface`] does.
fn check_edge_samples(cmd: &str, pts: &[(f32, f32)], window: Rect, p: &Value) -> Result<()> {
let sub = if b(p, "antiAlias", true) { 4 } else { 1 };
if sel::polygon_edge_samples(pts, window, sub) > sel::MAX_EDGE_SAMPLES {
return Err(EngineError::BadParams { cmd: cmd.into(), msg: "the outline has too many edges over too many rows to rasterize".into() });
}
Ok(())
}
/// The full-width rows of `area` that points `pts` span (empty when none are finite).
fn polygon_rows(pts: &[(f32, f32)], area: Rect) -> Rect {
let ys = pts.iter().map(|p| p.1).filter(|y| y.is_finite());
@@ -715,12 +727,14 @@ fn polygon_rows(pts: &[(f32, f32)], area: Rect) -> Rect {
if y0 >= y1 { Rect::EMPTY } else { Rect::new(area.x0, y0, area.x1, y1) }
}
/// [`polygon_rows`] cut to the columns the polygon spans (a pixel either side for coverage).
/// [`polygon_rows`] cut to the columns the polygon spans (two pixels either side for coverage).
fn polygon_window_rect(pts: &[(f32, f32)], area: Rect) -> Rect {
let rows = polygon_rows(pts, area);
// A non-finite x (JSON `1e40` is `inf` as f32) makes spans whose columns a narrow band would cut,
// so such a polygon takes the full-width rows, as it did before bands.
if pts.iter().any(|p| !p.0.is_finite()) {
// A non-finite coordinate (JSON `1e40` is `inf` as f32), or one so large that `f32` rounding of an
// edge's crossing could land outside the vertices' columns, makes spans a narrow band would cut:
// such a polygon takes the full-width rows, as it did before bands.
const BOUNDED: f32 = 1_048_576.0;
if pts.iter().any(|p| !(p.0.abs() <= BOUNDED && p.1.abs() <= BOUNDED)) {
return rows;
}
let xs = pts.iter().map(|p| p.0).filter(|x| x.is_finite());
@@ -729,7 +743,7 @@ fn polygon_window_rect(pts: &[(f32, f32)], area: Rect) -> Rect {
return Rect::EMPTY;
}
let clamp = |v: f32| v.clamp(area.x0 as f32, area.x1 as f32) as i32;
rows.intersect(&Rect::new(clamp(lo.floor() - 1.0), rows.y0, clamp(hi.ceil() + 1.0), rows.y1))
rows.intersect(&Rect::new(clamp(lo.floor() - 2.0), rows.y0, clamp(hi.ceil() + 2.0), rows.y1))
}
/// [`sel::combine`] of a mask covering only `band` (zero elsewhere), giving exactly what the
@@ -744,6 +744,33 @@ fn lasso_band_is_full_width_for_a_non_finite_x() {
assert_eq!((band.x0, band.x1), (0, 100));
}
/// Non-finite or huge coordinates (of either axis) take the full-width rows too: a narrow band
/// could cut coverage that an edge's `f32` crossing reaches outside the vertices' columns.
#[test]
fn lasso_band_is_full_width_for_a_non_finite_y_or_a_huge_coordinate() {
use super::polygon_window_rect;
let area = photocraft_geom::Rect::new(0, 0, 100, 100);
for pts in
[[(10.0f32, f32::INFINITY), (20.0, -1.0), (20.0, 2.0)], [(-1e10, -1e9), (600.0, 1.0), (0.0, 1.0)], [(10.0, 10.0), (20.0, f32::NAN), (10.0, 20.0)]]
{
let band = polygon_window_rect(&pts, area);
assert_eq!((band.x0, band.x1), (0, 100), "{pts:?}");
}
}
/// The lasso command refuses an outline whose rows x edges work passes the lasso limit, as the
/// Select and Mask edits do, before rasterizing anything.
#[test]
fn lasso_refuses_too_many_edge_samples() {
let mut s = Session::new();
s.execute("file.new", json!({"width": 64, "height": 1000})).unwrap();
let zigzag: Vec<[u32; 2]> = (0..80_000).map(|i| if i % 2 == 0 { [1, 0] } else { [2, 999] }).collect();
let e = s.execute("select.lasso", json!({"points": zigzag})).unwrap_err().to_string();
assert!(e.contains("too many edges"), "{e}");
assert!(s.active().unwrap().doc.selection.is_none());
assert!(s.execute("select.lasso", json!({"points": [[0, 0], [20, 0], [0, 20]]})).is_ok());
}
/// The previous per-sample padding and cropping, kept as the oracle for the row-wise ones.
fn old_pad_mask(m: &[f32], w: usize, h: usize, pad: usize, outside_empty: bool) -> Vec<f32> {
let pw = w + 2 * pad;