diff --git a/crates/algo/src/liquify.rs b/crates/algo/src/liquify.rs index 1788873ccd..5b3e39ae0e 100644 --- a/crates/algo/src/liquify.rs +++ b/crates/algo/src/liquify.rs @@ -653,9 +653,9 @@ impl LiquifyField { } let Some((i0, j0, i1, j1)) = self.node_span(c, r) else { return Rect::EMPTY }; // Snapshot of the affected nodes plus a margin (sampling reads around them). - let m = ((delta[0].abs().max(delta[1].abs()) + r * 0.2) / self.cell).ceil() as usize + 2; + let m = (((delta[0].abs().max(delta[1].abs()) + r * 0.2) / self.cell).ceil() as usize).saturating_add(2); let (si0, sj0) = (i0.saturating_sub(m), j0.saturating_sub(m)); - let (si1, sj1) = ((i1 + m).min(self.w - 1), (j1 + m).min(self.h - 1)); + let (si1, sj1) = (i1.saturating_add(m).min(self.w - 1), j1.saturating_add(m).min(self.h - 1)); let sw = si1 - si0 + 1; let sh = sj1 - sj0 + 1; let mut snap = Vec::with_capacity(sw * sh); @@ -680,7 +680,9 @@ impl LiquifyField { let p = self.node_pos(i, j); let (dx, dy) = (p[0] - c[0], p[1] - c[1]); let t2 = (dx * dx + dy * dy) / (r * r); - if t2 >= 1.0 { + // NaN is skipped too: it is what a far-off centre with a huge radius gives + // (infinity over infinity): a NaN weight would spread NaN displacements. + if t2.is_nan() || t2 >= 1.0 { continue; } let fall = falloff(t2, density); @@ -749,8 +751,10 @@ impl LiquifyField { self.d[idx] = d; self.freeze[idx] = f; } - let pad = r.ceil() as i32 + 2; - Rect::new(c[0].floor() as i32 - pad, c[1].floor() as i32 - pad, c[0].ceil() as i32 + pad, c[1].ceil() as i32 + pad).intersect(&self.bounds) + // Each edge is worked out in f64 and only then converted (saturating), so a far-off centre + // with a huge radius still covers the nodes it changed. + let pad = r.ceil() + 2.0; + Rect::new((c[0].floor() - pad) as i32, (c[1].floor() - pad) as i32, (c[0].ceil() + pad) as i32, (c[1].ceil() + pad) as i32).intersect(&self.bounds) } /// Replays strokes on a fresh field. diff --git a/crates/algo/tests/it/liquify_extreme_stroke.rs b/crates/algo/tests/it/liquify_extreme_stroke.rs new file mode 100644 index 0000000000..d7686c9681 --- /dev/null +++ b/crates/algo/tests/it/liquify_extreme_stroke.rs @@ -0,0 +1,61 @@ +//! A project file stores Liquify strokes as plain numbers, so a stroke may name any `f64` for its +//! brush size or its points. Replaying one must never overflow, however absurd the values. + +use photocraft_algo::liquify::{LiquifyField, LiquifyStroke, LiquifyTool}; +use photocraft_geom::Rect; + +const BRUSH_TOOLS: [LiquifyTool; 10] = [ + LiquifyTool::ForwardWarp, + LiquifyTool::Reconstruct, + LiquifyTool::Smooth, + LiquifyTool::TwirlCw, + LiquifyTool::TwirlCcw, + LiquifyTool::Pucker, + LiquifyTool::Bloat, + LiquifyTool::PushLeft, + LiquifyTool::Freeze, + LiquifyTool::Thaw, +]; + +const EXTREME: [f64; 9] = [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, f64::MAX, f64::MIN, 1e300, 3.0e9, -1.0, 0.0]; + +/// A stroke of `tool` with brush `size` through `points`, at middling density, pressure and rate. +fn stroke(tool: LiquifyTool, size: f64, points: Vec>) -> LiquifyStroke { + LiquifyStroke { tool, size, density: 50.0, pressure: 50.0, rate: 50.0, points, amount: None } +} + +/// Every brush tool, with every absurd size and position, replays without a panic. +#[test] +fn a_brush_with_an_absurd_size_or_position_replays_without_overflow() { + let bounds = Rect::new(0, 0, 64, 64); + for tool in BRUSH_TOOLS { + for size in EXTREME { + for p in [0.0, 1e300, f64::NAN, 3.0e9, -3.0e9] { + let points = vec![vec![p, 10.0], vec![20.0, p], vec![p, p, f64::MAX]]; + let _ = LiquifyField::from_strokes(bounds, 8.0, &[stroke(tool, size, points)]); + } + } + } +} + +/// Review on #11013: a dab whose centre and size are both huge gives infinity over infinity for its +/// distance; it must not write NaN displacements. +#[test] +fn a_huge_far_off_dab_leaves_the_field_finite() { + let bounds = Rect::new(0, 0, 64, 64); + for tool in BRUSH_TOOLS { + let f = LiquifyField::from_strokes(bounds, 8.0, &[stroke(tool, 4e200, vec![vec![1e200, 10.0]])]); + assert!(f.max_displacement().is_finite(), "{tool:?}"); + assert!(f.sample(10.0, 10.0).iter().all(|v| v.is_finite()), "{tool:?}"); + } +} + +/// Review on #11013: a Freeze dab centred far to the left with a radius reaching the image changes +/// nodes inside it, so the area it reports must not be empty. +#[test] +fn a_far_off_dab_that_reaches_the_image_reports_what_it_changed() { + let mut f = LiquifyField::new(Rect::new(0, 0, 64, 64), 8.0); + let changed = f.stroke_begin(&stroke(LiquifyTool::Freeze, 1.2e10, vec![]), [-3e9, 10.0, 1.0]); + assert!(f.freeze_at(10.0, 10.0) > 0.0, "the dab reaches the image, so it freezes nodes there"); + assert!(!changed.is_empty(), "the freeze changed nodes but reported {changed:?}"); +} diff --git a/crates/algo/tests/it/main.rs b/crates/algo/tests/it/main.rs index 342eeb2501..4537dc0790 100644 --- a/crates/algo/tests/it/main.rs +++ b/crates/algo/tests/it/main.rs @@ -1,6 +1,7 @@ //! The crate's integration tests, linked into one test binary. mod gallery_reach_extreme; +mod liquify_extreme_stroke; mod liquify_freeze_formats; mod liquify_source_footprint; mod retouch_blur_exact;