Skip to content

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

Vanishing Point: perspective clone samples without allocating per pixel (#7491)

jlangee87ad39a2191
diff --git a/crates/algo/src/vanishing.rs b/crates/algo/src/vanishing.rs
index 77b7bb107..87e55f873 100644
--- a/crates/algo/src/vanishing.rs
+++ b/crates/algo/src/vanishing.rs
@@ -345,21 +345,39 @@ pub fn clone_stroke(scene: &Scene, surf: &mut Surface, source: [f64; 2], points:
let src_copy = surf.clone();
let fmt = surf.format();
let n = fmt.channels();
+ // Scratch for `sample`: the dab's source pixels, the four bilinear corners, the result.
+ let mut win = Window { area: Rect::new(0, 0, 0, 0), data: Vec::new() };
+ let (mut corners, mut s) = (vec![0.0f32; 4 * n], vec![0.0f32; n]);
let mut dabs = 0;
for p in points {
let mc = metric(&hd_inv, lud, lvd, *p);
// Image-space footprint: map a metric circle's bounding points.
- let mut bb = [f64::MAX, f64::MAX, f64::MIN, f64::MIN];
- for k in 0..16 {
- let a = k as f64 / 16.0 * std::f64::consts::TAU;
- let (u, v) = ((mc[0] + r_m * a.cos()) / lud, (mc[1] + r_m * a.sin()) / lvd);
- let (x, y) = hd.apply(u, v);
- bb = [bb[0].min(x), bb[1].min(y), bb[2].max(x), bb[3].max(y)];
- }
- let area = Rect::new(bb[0].floor() as i32, bb[1].floor() as i32, (bb[2].ceil() as i32).saturating_add(1), (bb[3].ceil() as i32).saturating_add(1));
+ let footprint = |h: &Homography, lu: f64, lv: f64, c: [f64; 2]| {
+ let mut bb = [f64::MAX, f64::MAX, f64::MIN, f64::MIN];
+ for k in 0..16 {
+ let a = k as f64 / 16.0 * std::f64::consts::TAU;
+ let (x, y) = h.apply((c[0] + r_m * a.cos()) / lu, (c[1] + r_m * a.sin()) / lv);
+ bb = [bb[0].min(x), bb[1].min(y), bb[2].max(x), bb[3].max(y)];
+ }
+ Rect::new(bb[0].floor() as i32, bb[1].floor() as i32, (bb[2].ceil() as i32).saturating_add(1), (bb[3].ceil() as i32).saturating_add(1))
+ };
+ let area = footprint(&hd, lud, lvd, mc);
if area.is_empty() || area.width() > 4096 || area.height() > 4096 {
continue;
}
+ // The source footprint, read once; `sample` reads any pixel outside it from the surface.
+ // Skipped when it dwarfs the destination (cloning from near to far in strong perspective):
+ // decoding it would cost more than the few per-pixel reads it saves.
+ win.area = footprint(&hs, lus, lvs, [mc[0] + offset[0], mc[1] + offset[1]]).inflate(2);
+ let dest_px = u64::from(area.width()) * u64::from(area.height());
+ if win.area.is_empty()
+ || win.area.width() > 4096
+ || win.area.height() > 4096
+ || u64::from(win.area.width()) * u64::from(win.area.height()) > 4 * dest_px
+ {
+ win.area = Rect::new(0, 0, 0, 0);
+ }
+ src_copy.read_region_into(win.area, &mut win.data);
let mut data = surf.read_region(area);
let w = area.width() as usize;
let mut changed = false;
@@ -376,7 +394,7 @@ pub fn clone_stroke(scene: &Scene, surf: &mut Surface, source: [f64; 2], points:
// Source pixel at the same metric offset.
let sm = [m[0] + offset[0], m[1] + offset[1]];
let (sx, sy) = hs.apply(sm[0] / lus, sm[1] / lvs);
- let s = sample(&src_copy, sx - 0.5, sy - 0.5, n);
+ sample(&src_copy, &win, sx - 0.5, sy - 0.5, &mut corners, &mut s);
for c in 0..n {
px[c] += (s[c] - px[c]) * k;
}
@@ -390,11 +408,33 @@ pub fn clone_stroke(scene: &Scene, surf: &mut Surface, source: [f64; 2], points:
dabs
}
-fn sample(s: &Surface, x: f64, y: f64, n: usize) -> Vec<f32> {
+/// Pixels of `area` read from the source surface (`read_region` layout).
+struct Window {
+ area: Rect,
+ data: Vec<f32>,
+}
+
+/// Bilinear sample of `s` at (`x`, `y`) into `out` (one pixel), using `corners` (four pixels) as
+/// scratch, so the per-pixel clone loop does not allocate. Corners inside `win` come from it.
+fn sample(s: &Surface, win: &Window, x: f64, y: f64, corners: &mut [f32], out: &mut [f32]) {
+ let n = out.len();
let (x0, y0) = (x.floor() as i32, y.floor() as i32);
let (tx, ty) = ((x - x0 as f64) as f32, (y - y0 as f64) as f32);
- let r = s.read_region(Rect::new(x0, y0, x0 + 2, y0 + 2));
- (0..n).map(|c| (r[c] * (1.0 - tx) + r[n + c] * tx) * (1.0 - ty) + (r[2 * n + c] * (1.0 - tx) + r[3 * n + c] * tx) * ty).collect()
+ let (x1, y1) = (x0.saturating_add(1), y0.saturating_add(1));
+ let w = win.area.width() as usize;
+ for (k, (px, py)) in [(x0, y0), (x1, y0), (x0, y1), (x1, y1)].into_iter().enumerate() {
+ let dst = &mut corners[k * n..(k + 1) * n];
+ if win.area.contains(px, py) {
+ let o = ((py - win.area.y0) as usize * w + (px - win.area.x0) as usize) * n;
+ dst.copy_from_slice(&win.data[o..o + n]);
+ } else {
+ s.read_pixel(px, py, dst);
+ }
+ }
+ let r = corners;
+ for (c, o) in out.iter_mut().enumerate() {
+ *o = (r[c] * (1.0 - tx) + r[n + c] * tx) * (1.0 - ty) + (r[2 * n + c] * (1.0 - tx) + r[3 * n + c] * tx) * ty;
+ }
}
#[cfg(test)]
@@ -487,4 +527,35 @@ mod tests {
assert_eq!(n, 1);
assert!(s.rgba(dx as i32, dy as i32)[0] > 0.8, "{:?}", s.rgba(dx as i32, dy as i32));
}
+
+ #[test]
+ fn sample_at_the_edge_of_i32_does_not_overflow() {
+ let s = Surface::new(PixelFormat::RGBA8);
+ let win = Window { area: Rect::new(0, 0, 0, 0), data: Vec::new() };
+ let (mut corners, mut out) = ([0.0; 16], [1.0; 4]);
+ sample(&s, &win, 3e9, 3e9, &mut corners, &mut out);
+ assert_eq!(out, [0.0; 4]);
+ }
+
+ #[test]
+ fn sample_interpolates_across_tile_edges() {
+ let mut s = Surface::new(PixelFormat::RGBA8);
+ let (a, b) = ([1.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0]);
+ // Left of x = 256 is red, right is blue; the 2×2 footprint straddles the tile edge.
+ s.fill_rect(Rect::new(0, 0, 256, 512), &a);
+ s.fill_rect(Rect::new(256, 0, 512, 512), &b);
+ let (mut corners, mut out) = ([0.0; 16], [0.0; 4]);
+ let want = [0.75, 0.0, 0.25, 1.0];
+ // From the surface, from a window holding the footprint, and from one holding half of it.
+ for area in [Rect::new(0, 0, 0, 0), Rect::new(250, 250, 260, 260), Rect::new(250, 250, 256, 260)] {
+ let mut win = Window { area, data: Vec::new() };
+ s.read_region_into(area, &mut win.data);
+ sample(&s, &win, 255.25, 255.5, &mut corners, &mut out);
+ assert!(out.iter().zip(want).all(|(o, w)| (o - w).abs() < 1e-6), "{area:?}: {out:?}");
+ }
+ // Outside every tile, the default (transparent) pixel.
+ let win = Window { area: Rect::new(0, 0, 0, 0), data: Vec::new() };
+ sample(&s, &win, -10.5, -10.5, &mut corners, &mut out);
+ assert_eq!(out, [0.0; 4]);
+ }
}