Skip to content

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

fix(algo): keep clipped warp bounds from overflowing (#5451)

Skyfuller9cc8be43929b
diff --git a/crates/algo/src/transform.rs b/crates/algo/src/transform.rs
index 3b157b56a..050d08c32 100644
--- a/crates/algo/src/transform.rs
+++ b/crates/algo/src/transform.rs
@@ -281,7 +281,7 @@ fn warp(
// that matters is the one at the source point landing in the clip's centre.
let (cx, cy) = match clip.and_then(|c| Some((c, h.inverse()?))) {
Some((c, inv)) => inv.apply((c.x0 as f64 + c.x1 as f64) / 2.0, (c.y0 as f64 + c.y1 as f64) / 2.0),
- None => ((src_rect.x0 + src_rect.x1) as f64 / 2.0, (src_rect.y0 + src_rect.y1) as f64 / 2.0),
+ None => ((f64::from(src_rect.x0) + f64::from(src_rect.x1)) / 2.0, (f64::from(src_rect.y0) + f64::from(src_rect.y1)) / 2.0),
};
let scale = h.local_scale(cx, cy);
let reduced;
@@ -366,10 +366,10 @@ fn warp(
return Ok(None);
}
let fp: Vec<(f64, f64)> = poly.iter().map(|&(x, y)| inv.apply(x, y)).collect();
- let fx0 = fp.iter().map(|c| c.0).fold(f64::MAX, f64::min).floor() as i32 - 3;
- let fy0 = fp.iter().map(|c| c.1).fold(f64::MAX, f64::min).floor() as i32 - 3;
- let fx1 = fp.iter().map(|c| c.0).fold(f64::MIN, f64::max).ceil() as i32 + 3;
- let fy1 = fp.iter().map(|c| c.1).fold(f64::MIN, f64::max).ceil() as i32 + 3;
+ let fx0 = (fp.iter().map(|c| c.0).fold(f64::MAX, f64::min).floor() as i32).saturating_sub(3);
+ let fy0 = (fp.iter().map(|c| c.1).fold(f64::MAX, f64::min).floor() as i32).saturating_sub(3);
+ let fx1 = (fp.iter().map(|c| c.0).fold(f64::MIN, f64::max).ceil() as i32).saturating_add(3);
+ let fy1 = (fp.iter().map(|c| c.1).fold(f64::MIN, f64::max).ceil() as i32).saturating_add(3);
// Without a one-signed forward w, a tile straddling the inverse horizon has no bounded footprint.
let straddles = side.is_none() && wc.iter().any(|w| *w <= 0.0) && wc.iter().any(|w| *w >= 0.0);
let foot = if straddles { src_rect } else { Rect::new(fx0, fy0, fx1, fy1).intersect(&src_rect) };
diff --git a/crates/algo/tests/warp_clipped.rs b/crates/algo/tests/warp_clipped.rs
new file mode 100644
index 000000000..ac6497f13
--- /dev/null
+++ b/crates/algo/tests/warp_clipped.rs
@@ -0,0 +1,59 @@
+use photocraft_algo::transform::{Homography, Interp, warp_surface, warp_surface_clipped};
+use photocraft_color::{ColorMode, PixelFormat, SampleType};
+use photocraft_geom::Rect;
+use photocraft_raster::Surface;
+
+#[test]
+fn clipped_warp_matches_the_existing_kernel_at_every_depth() {
+ for sample in [SampleType::U8, SampleType::U16, SampleType::F32] {
+ let mut source = Surface::new(PixelFormat::new(ColorMode::Rgb, sample, true));
+ source.fill_rect(Rect::new(8, 12, 40, 44), &[0.8, 0.2, 0.4, 0.7]);
+ source.fill_rect(Rect::new(20, 24, 32, 36), &[0.1, 0.7, 0.3, 0.4]);
+ let clip = Rect::new(10, 10, 52, 52);
+ for interpolation in [Interp::Nearest, Interp::Bilinear, Interp::Bicubic] {
+ for h in [Homography([1.2, -0.3, 7.0, 0.3, 1.2, -5.0, 0.0, 0.0, 1.0]), Homography([0.125, 0.0, 24.0, 0.0, 0.125, 30.0, 0.0, 0.0, 1.0])] {
+ let full = warp_surface(&source, source.content_bounds(), &h, interpolation).unwrap();
+ let bounded = warp_surface_clipped(&source, source.content_bounds(), &h, interpolation, clip).unwrap();
+ assert_eq!(full.read_region(clip), bounded.read_region(clip));
+ assert!(clip.contains_rect(&bounded.content_bounds()));
+ assert!(bounded.tiles().count() <= clip.tiles().count());
+ }
+ }
+ }
+}
+
+#[test]
+fn huge_scaled_warp_allocates_only_the_destination_clip() {
+ let mut source = Surface::new(PixelFormat::RGBA8);
+ source.fill_rect(Rect::new(8, 8, 12, 12), &[0.2, 0.7, 0.4, 1.0]);
+ let clip = Rect::new(0, 0, 64, 64);
+ let h = Homography([10_000.0, 0.0, -99_968.0, 0.0, 10_000.0, -99_968.0, 0.0, 0.0, 1.0]);
+ let output = warp_surface_clipped(&source, source.content_bounds(), &h, Interp::Bicubic, clip).unwrap();
+ assert!(clip.contains_rect(&output.content_bounds()));
+ assert!(output.tiles().count() <= clip.tiles().count());
+ assert_eq!(output.pixel(32, 32), source.pixel(10, 10));
+ assert!(warp_surface_clipped(&source, source.content_bounds(), &h, Interp::Bicubic, Rect::EMPTY).unwrap().tiles().next().is_none());
+}
+
+#[test]
+fn inverse_footprint_handles_extreme_finite_affines() {
+ let mut source = Surface::new(PixelFormat::RGBA8);
+ source.fill_rect(Rect::new(0, 0, 8, 8), &[1.0, 0.0, 0.0, 1.0]);
+ // A strongly skewed inverse maps a small visible tile beyond i32 limits.
+ let h = Homography([1.0, 1e12, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]);
+ let clip = Rect::new(0, 0, 32, 32);
+ let output = warp_surface_clipped(&source, source.content_bounds(), &h, Interp::Nearest, clip).unwrap();
+ assert!(clip.contains_rect(&output.content_bounds()));
+ assert!(output.tiles().count() <= clip.tiles().count());
+}
+
+#[test]
+fn source_center_does_not_add_extreme_coordinates_in_i32() {
+ let mut source = Surface::new(PixelFormat::RGBA8);
+ let origin = 1_500_000_000;
+ let bounds = Rect::new(origin, origin, origin + 4, origin + 4);
+ source.fill_rect(bounds, &[0.2, 0.7, 0.4, 1.0]);
+ let h = Homography([1.0, 0.0, -f64::from(origin), 0.0, 1.0, -f64::from(origin), 0.0, 0.0, 1.0]);
+ let output = warp_surface_clipped(&source, bounds, &h, Interp::Nearest, Rect::new(0, 0, 8, 8)).unwrap();
+ assert_eq!(output.pixel(2, 2), source.pixel(origin + 2, origin + 2));
+}