diff --git a/crates/algo/src/warp.rs b/crates/algo/src/warp.rs index 87c6bde2c3..0dafc18867 100644 --- a/crates/algo/src/warp.rs +++ b/crates/algo/src/warp.rs @@ -10,8 +10,10 @@ //! the true one; affine //! maps (the identity included) are reproduced exactly. Where the warp shrinks the source, each //! pixel instead averages a grid of bilinear subsamples over its source footprint (about one per -//! source pixel), so thin lines don't alias. Where the warp folds over itself the -//! later (lower-right) cell wins. Work is split per destination tile (parallel on native). +//! source pixel), so thin lines don't alias. Where the warp folds over itself, [`Fold`] decides +//! what shows: for Edit › Transform › Warp and smart objects the part dragged furthest is on top and +//! the layers composite; for other mesh maps (lens corrections, distortions) the later +//! (lower-right) cell wins. Work is split per destination tile (parallel on native). use photocraft_color::PixelFormat; use photocraft_geom::{Rect, TileCoord}; @@ -24,6 +26,23 @@ use crate::transform::{Interp, bicubic}; const CELL: f64 = 4.0; /// Most cells along one axis (bounds memory and time on huge layers). const MAX_CELLS: usize = 512; +/// How a mesh warp stacks the picture where it folds over itself. +#[derive(Clone, Copy)] +pub enum Fold<'a> { + /// Cells in row order, the last one drawn wins (lens corrections, distortions). + Rows, + /// A warp of the picture in place (Edit › Transform › Warp, a smart object's warp): cells in + /// [`cell_order`] (a part the fold turns over on top, else the part dragged furthest), and a fold's layers composite, so transparent + /// parts show what is under them, as Warp mode's preview draws them. The map is where a + /// source point lands without the warp: the identity for a layer, a smart object's placement. + Stack(&'a (dyn Fn(f64, f64) -> (f64, f64) + Sync)), +} + +impl Fold<'_> { + /// [`Fold::Stack`] about the identity: a warp of a layer where it is. + pub const IN_PLACE: Fold<'static> = Fold::Stack(&|x: f64, y: f64| (x, y)); +} + /// Most subsamples per axis when a warp shrinks the source; also the longest source step per /// destination pixel the filter follows (bounds the footprint each tile reads). const MAX_SS: f64 = 8.0; @@ -44,7 +63,9 @@ pub fn place_source( let (u, v) = w.map(x, y); (a * u + c * v + e, b * u + d * v + f) }; - return Ok(warp_mesh_surface(src, src_rect, &map, Interp::Bicubic)); + // A fold's layers are ordered by how far the warp moved them, not the placement. + let rest = |x: f64, y: f64| (a * x + c * y + e, b * x + d * y + f); + return Ok(mesh(src, src_rect, &map, Fold::Stack(&rest), Interp::Bicubic, &Interrupt::NONE)); } let near = |x: f64, y: f64| (x - y).abs() < 1e-9; if near(a, 1.0) && near(b, 0.0) && near(c, 0.0) && near(d, 1.0) && near(e, e.round()) && near(f, f.round()) { @@ -66,7 +87,8 @@ pub fn place_source_projective( let (u, v) = w.map(x, y); h.apply(u, v) }; - return Ok(warp_mesh_surface(src, src_rect, &map, Interp::Bicubic)); + let rest = |x: f64, y: f64| h.apply(x, y); + return Ok(mesh(src, src_rect, &map, Fold::Stack(&rest), Interp::Bicubic, &Interrupt::NONE)); } crate::transform::warp_surface(src, src_rect, h, Interp::Bicubic) } @@ -75,16 +97,65 @@ pub fn place_source_projective( /// coordinates → destination document coordinates). Output has `src`'s format with alpha /// added; pixels outside the warped area are transparent. pub fn warp_mesh_surface(src: &Surface, src_rect: Rect, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), interp: Interp) -> Surface { - mesh(src, src_rect, f, interp, &Interrupt::NONE) + mesh(src, src_rect, f, Fold::Rows, interp, &Interrupt::NONE) } -/// [`warp_mesh_surface`] reporting progress per destination tile; `None` once `ctl` is cancelled. -pub fn warp_mesh_surface_with(src: &Surface, src_rect: Rect, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), interp: Interp, ctl: &Interrupt) -> Option { - let out = mesh(src, src_rect, f, interp, ctl); +/// [`warp_mesh_surface`] with folds stacked as `fold` says, reporting progress per destination +/// tile; `None` once `ctl` is cancelled. +pub fn warp_mesh_surface_with( + src: &Surface, + src_rect: Rect, + f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), + fold: Fold, + interp: Interp, + ctl: &Interrupt, +) -> Option { + let out = mesh(src, src_rect, f, fold, interp, ctl); (!ctl.cancelled()).then_some(out) } -fn mesh(src: &Surface, src_rect: Rect, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), interp: Interp, ctl: &Interrupt) -> Surface { +/// The order Warp draws the cells of a vertex grid in (`row` vertices a row; `pos` where the warp +/// put each vertex, `rest` where it would be without it). Where a warp folds the picture over +/// itself, the part turned over lies on top, like a folded-back flap: a corner or edge dragged +/// across the picture, and also the edge row that folds back when the middle is pulled out past +/// it, which leaves the pulled part under that row. Within each, the cells moved least come first, +/// so the part dragged furthest is on top. Each cell is named by its top-left vertex. Shared by +/// the render and Warp mode's preview, so they stack folds alike. +pub fn cell_order(pos: &[[f64; 2]], rest: &[[f64; 2]], row: usize) -> Vec { + if row < 2 || pos.len() != rest.len() { + return Vec::new(); + } + let rows = pos.len() / row; + // A vertex with no finite movement (sent to infinity, as near a Distort's horizon) goes under, + // not on top. + let moved = |k: usize| match (pos.get(k), rest.get(k)) { + (Some(p), Some(r)) => Some((p[0] - r[0]).hypot(p[1] - r[1])).filter(|m| m.is_finite()).unwrap_or(f64::NEG_INFINITY), + _ => f64::NEG_INFINITY, + }; + // Twice the signed area of a cell's quad: its sign is the cell's orientation (NaN when a corner + // isn't finite, which counts as not turned over). + let area = |pts: &[[f64; 2]], a: usize| { + let q = [a, a + 1, a + row + 1, a + row].map(|k| pts.get(k).copied().unwrap_or([f64::NAN; 2])); + (0..4).map(|i| q[i][0] * q[(i + 1) % 4][1] - q[(i + 1) % 4][0] * q[i][1]).sum::() + }; + let mut cells: Vec<(bool, f64, usize)> = Vec::with_capacity(rows.saturating_sub(1) * (row - 1)); + for j in 0..rows.saturating_sub(1) { + for i in 0..row - 1 { + let a = j * row + i; + let turned = area(pos, a) * area(rest, a) < 0.0; + cells.push((turned, moved(a) + moved(a + 1) + moved(a + row) + moved(a + row + 1), a)); + } + } + let by = |x: &(bool, f64, usize), y: &(bool, f64, usize)| x.0.cmp(&y.0).then(x.1.total_cmp(&y.1)); + // A stable sort, so equal cells keep row order. On this thread alone: a render asks for the + // order from a pool thread that other tiles wait on, and a sort shared with the pool could + // hand that thread one of those tiles, which would wait for the order for ever. + cells.sort_by(by); + cells.into_iter().map(|(_, _, a)| a).collect() +} + +/// Folds stack as `fold` says. +fn mesh(src: &Surface, src_rect: Rect, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), fold: Fold, interp: Interp, ctl: &Interrupt) -> Surface { if src_rect.is_empty() { let fmt = src.format(); return Surface::new(PixelFormat::new(fmt.mode, fmt.sample, true)); @@ -106,6 +177,8 @@ fn mesh(src: &Surface, src_rect: Rect, f: &(dyn Fn(f64, f64) -> (f64, f64) + Syn } } let w1 = nx + 1; + // Drawn in row order; for a fold, a tile where two parts of the picture meet is drawn again in + // [`cell_order`], each cell's two triangles ranked together. let mut tris = Vec::with_capacity(nx * ny * 2); for j in 0..ny { for i in 0..nx { @@ -114,7 +187,29 @@ fn mesh(src: &Surface, src_rect: Rect, f: &(dyn Fn(f64, f64) -> (f64, f64) + Syn tris.push([a, a + w1 + 1, a + w1]); } } - triangles(src, src_rect, &verts, &tris, interp, ctl) + let rank = || -> Vec { + let Fold::Stack(rest) = fold else { return Vec::new() }; + let pos: Vec<[f64; 2]> = verts.iter().map(|(d, _)| *d).collect(); + let at_rest: Vec<[f64; 2]> = verts + .iter() + .map(|(_, s)| { + let r = rest(s[0], s[1]); + [r.0, r.1] + }) + .collect(); + let mut rank = vec![0u32; tris.len()]; + for (r, a) in cell_order(&pos, &at_rest, w1).into_iter().enumerate() { + let c = (a / w1) * nx + a % w1; + for (t, k) in [(2 * c, 2 * r), (2 * c + 1, 2 * r + 1)] { + if let Some(slot) = rank.get_mut(t) { + *slot = u32::try_from(k).unwrap_or(u32::MAX); + } + } + } + rank + }; + let stack: Option<&(dyn Fn() -> Vec + Sync)> = matches!(fold, Fold::Stack(_)).then_some(&rank); + triangles(src, src_rect, &verts, &tris, interp, ctl, stack) } /// Probe lines per axis [`mapped_lengths`] follows through the map. @@ -159,17 +254,162 @@ fn mapped_lengths(src_rect: Rect, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync)) - /// interpolation of its source positions. Later triangles in `tris` draw over earlier ones (the /// order is the depth order). Output has `src`'s format with alpha added. pub fn warp_triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris: &[[usize; 3]], interp: Interp) -> Surface { - triangles(src, src_rect, verts, tris, interp, &Interrupt::NONE) + triangles(src, src_rect, verts, tris, interp, &Interrupt::NONE, None) } /// [`warp_triangles`], reporting progress per destination tile to `ctl`. A cancelled `ctl` skips /// the remaining tiles; the caller discards the result. /// Destination tiles one triangle render may keep bookkeeping for (a 1M-tile render is 69 Gpx). const MAX_DST_TILES: u64 = 1 << 20; +/// How close (source px) two triangles' source points under one pixel are when they are the +/// same layer of the picture meeting on a shared edge, rather than two layers of a fold. +const SAME_LAYER: f64 = 1e-3; /// Source pixels one destination tile may read densely. const MAX_FOOTPRINT_PIXELS: u64 = 1 << 25; -fn triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris: &[[usize; 3]], interp: Interp, ctl: &Interrupt) -> Surface { +/// Source points a destination tile samples, grouped into connected regions of the source, so +/// each region is read as a window of its own: a fold joins distant parts of the source in one +/// tile, and one window over both read everything between them (a corner dragged over the +/// middle of a 24 MP layer read seven times its pixels). Points are joined through the cells of a +/// coarse grid over their bounding box that their footprints touch. +struct Regions { + /// The grid's origin, cell size, columns and rows. + origin: [f64; 2], + cell: f64, + cols: usize, + rows: usize, + /// Each cell's region (`u32::MAX`: none). Empty for a single region, which every point is in. + of_cell: Vec, + /// Each region's source rectangle (the points' footprints, a 3 px border, inside `src_rect`). + rects: Vec, +} + +/// Visits points for [`Regions::new`]: each with the half extent of its footprint along x and y. +type VisitPoints<'a> = &'a dyn Fn(&mut dyn FnMut([f64; 2], [f64; 2])); + +/// Most cells per axis of a [`Regions`] grid. +const REGION_GRID: f64 = 256.0; +/// Smallest [`Regions`] cell (source px). +const REGION_CELL: f64 = 32.0; +/// A [`Regions`] bounding box this small (source px) stays one region. +const SINGLE_REGION_PIXELS: f64 = 1024.0 * 1024.0; + +impl Regions { + /// `points` visits each point with the half extent of its footprint along x and y (it is + /// called again only when the points form more than one region). + fn new(points: VisitPoints, src_rect: Rect) -> Regions { + let mut b = [f64::MAX, f64::MAX, f64::MIN, f64::MIN]; + let mut count = 0usize; + points(&mut |q, e| { + b = [b[0].min(q[0] - e[0]), b[1].min(q[1] - e[1]), b[2].max(q[0] + e[0]), b[3].max(q[1] + e[1])]; + count += 1; + }); + let empty = Regions { origin: [0.0; 2], cell: 1.0, cols: 0, rows: 0, of_cell: Vec::new(), rects: Vec::new() }; + if !(b[0] <= b[2] && b.iter().all(|v| v.is_finite())) { + return empty; + } + let rect = + |r: [f64; 4]| Rect::new(r[0].floor() as i32 - 3, r[1].floor() as i32 - 3, r[2].ceil() as i32 + 3, r[3].ceil() as i32 + 3).intersect(&src_rect); + // Most tiles sample one stretch of the source: no more than a few times the source + // pixels there are points (the footprints' own spread), or a small window, is one region. + let area = (b[2] - b[0] + 7.0) * (b[3] - b[1] + 7.0); + if area <= SINGLE_REGION_PIXELS.max(4.0 * count as f64) { + return Regions { rects: vec![rect(b)], ..empty }; + } + let cell = ((b[2] - b[0]).max(b[3] - b[1]) / REGION_GRID).max(REGION_CELL); + let (cols, rows) = (((b[2] - b[0]) / cell).floor() as usize + 1, ((b[3] - b[1]) / cell).floor() as usize + 1); + let to = |v: f64, o: f64, n: usize| (((v - o) / cell).floor().max(0.0) as usize).min(n - 1); + // Cells a footprint touches, marked (0) then labelled by region. + let mut of_cell = vec![u32::MAX; cols * rows]; + points(&mut |q, e| { + for y in to(q[1] - e[1], b[1], rows)..=to(q[1] + e[1], b[1], rows) { + for x in to(q[0] - e[0], b[0], cols)..=to(q[0] + e[0], b[0], cols) { + if let Some(c) = of_cell.get_mut(y * cols + x) { + *c = 0; + } + } + } + }); + let mut regions = 0u32; + let mut stack = Vec::new(); + for start in 0..of_cell.len() { + if of_cell.get(start) != Some(&0) { + continue; + } + regions += 1; + stack.push(start); + while let Some(c) = stack.pop() { + if of_cell.get(c) != Some(&0) { + continue; + } + of_cell[c] = regions; + let (x, y) = ((c % cols) as i64, (c / cols) as i64); + for (dx, dy) in [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)] { + let (nx, ny) = (x + dx, y + dy); + if nx >= 0 && ny >= 0 && (nx as usize) < cols && (ny as usize) < rows { + stack.push(ny as usize * cols + nx as usize); + } + } + } + } + // Labels 1.. become indices 0..; each region's rectangle covers its points' footprints. + for c in of_cell.iter_mut().filter(|c| **c != u32::MAX) { + *c -= 1; + } + let mut boxes = vec![[f64::MAX, f64::MAX, f64::MIN, f64::MIN]; regions as usize]; + let mut out = Regions { origin: [b[0], b[1]], cell, cols, rows, of_cell, rects: Vec::new() }; + points(&mut |q, e| { + if let Some(r) = out.of(q).and_then(|r| boxes.get_mut(r)) { + *r = [r[0].min(q[0] - e[0]), r[1].min(q[1] - e[1]), r[2].max(q[0] + e[0]), r[3].max(q[1] + e[1])]; + } + }); + out.rects = boxes.into_iter().map(rect).collect(); + out + } + + /// The region of a point given to [`Regions::new`]. + fn of(&self, q: [f64; 2]) -> Option { + if self.of_cell.is_empty() { + return (!self.rects.is_empty()).then_some(0); + } + if !(q[0].is_finite() && q[1].is_finite()) { + return None; + } + let x = (((q[0] - self.origin[0]) / self.cell).floor().max(0.0) as usize).min(self.cols - 1); + let y = (((q[1] - self.origin[1]) / self.cell).floor().max(0.0) as usize).min(self.rows - 1); + self.of_cell.get(y * self.cols + x).filter(|&&r| r != u32::MAX).map(|&r| r as usize) + } + + /// Source pixels all the regions' rectangles hold. + fn area(&self) -> u64 { + self.rects.iter().map(|r| r.size().area()).sum() + } +} + +/// A premultiplied window of the source that a destination tile samples. +struct Win { + px: Vec, + foot: Rect, + fw: usize, +} + +/// `stack`: the picture can fold over itself, and gives each triangle's rank in the order a fold +/// stacks them (asked once, when a tile first needs it). A tile is drawn in `tris`' order; where +/// two parts of the source meet at a pixel (a fold), the tile is drawn again in rank order, and +/// triangles drawn later composite over what they cover instead of replacing it. Without a fold +/// the order makes no difference, so a warp that doesn't fold costs no more than one that can't. +fn triangles( + src: &Surface, + src_rect: Rect, + verts: &[([f64; 2], [f64; 2])], + tris: &[[usize; 3]], + interp: Interp, + ctl: &Interrupt, + stack: Option<&(dyn Fn() -> Vec + Sync)>, +) -> Surface { + let folds = stack.is_some(); + let ranks = std::sync::OnceLock::new(); + let rank_of = |t: u32| stack.map(|f| ranks.get_or_init(f)).and_then(|r| r.get(t as usize)).copied().unwrap_or(u32::MAX); let mut fmt = src.format(); let converted; let src = if fmt.alpha { @@ -203,7 +443,8 @@ fn triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris } // Triangles: (vertex indices), binned per destination tile. let tiles: Vec = dst.tiles().map(|tc| tc.rect().intersect(&dst)).filter(|r| !r.is_empty()).collect(); - let mut bins: Vec> = vec![Vec::new(); tiles.len()]; + // Triangles by index into `tris`, in its order. + let mut bins: Vec> = vec![Vec::new(); tiles.len()]; let ts = photocraft_geom::TILE_SIZE; // Bin origin on the tile grid (the first tile is clipped to `dst`, so its x0/y0 may not be). let tx0 = tiles.iter().map(|t| t.x0).min().unwrap_or(0).div_euclid(ts) * ts; @@ -214,7 +455,8 @@ fn triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris for (k, t) in tiles.iter().enumerate() { lookup[grid_of(t)] = k; } - for &tri in tris { + for (index, &tri) in tris.iter().enumerate() { + let Ok(index) = u32::try_from(index) else { break }; let ps = tri.map(|v| verts[v].0); let bx0 = ps.iter().map(|p| p[0]).fold(f64::MAX, f64::min).floor() as i32; let by0 = ps.iter().map(|p| p[1]).fold(f64::MAX, f64::min).floor() as i32; @@ -228,7 +470,7 @@ fn triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris if let Some(&k) = lookup.get(g) && k != usize::MAX { - bins[k].push(tri); + bins[k].push(index); } } } @@ -238,14 +480,14 @@ fn triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris // Tiles finished so far, for progress reported per tile (from any worker thread). let finished = std::sync::atomic::AtomicUsize::new(0); let total = tiles.len().max(1); - let work = |(t, tris): (&Rect, &Vec<[usize; 3]>)| -> Option<(TileCoord, Arc)> { + let work = |(t, bin): (&Rect, &Vec)| -> Option<(TileCoord, Arc)> { if ctl.cancelled() { return None; } let k = finished.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; // The last few percent are putting the tiles in place. ctl.progress(0.98 * k as f32 / total as f32); - if tris.is_empty() { + if bin.is_empty() { return None; } let w = t.width() as usize; @@ -260,109 +502,206 @@ fn triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris // from. Per triangle: its source step per destination pixel along x and y (`[dx_u, dx_v, // dy_u, dy_v]`, the inverse map's Jacobian, constant over a triangle; zero when // point-sampled) and the subsamples along each. + // The tile's triangles, first in `tris`' order, then, if a fold shows here, by rank. + let mut order: Vec<[usize; 3]> = bin.iter().filter_map(|&k| tris.get(k as usize).copied()).collect(); + let mut stacked = false; let mut uv = vec![[f64::NAN; 2]; w * h]; let mut owner = vec![0u32; w * h]; - let mut jac: Vec<([f64; 4], usize, usize)> = Vec::with_capacity(tris.len()); - for (k, tri) in tris.iter().enumerate() { - let [(p0, s0), (p1, s1), (p2, s2)] = tri.map(|v| verts[v]); - let det = (p1[0] - p0[0]) * (p2[1] - p0[1]) - (p2[0] - p0[0]) * (p1[1] - p0[1]); - if det.abs() < 1e-12 { - jac.push(([0.0; 4], 1, 1)); - continue; - } - let (d1, d2) = ([s1[0] - s0[0], s1[1] - s0[1]], [s2[0] - s0[0], s2[1] - s0[1]]); - let (l1x, l1y, l2x, l2y) = ((p2[1] - p0[1]) / det, (p0[0] - p2[0]) / det, (p0[1] - p1[1]) / det, (p1[0] - p0[0]) / det); - // Capped, so a sliver triangle can't ask for an unbounded footprint. - let cap = |j: [f64; 2]| { - let l = j[0].hypot(j[1]); - if l > MAX_SS { [j[0] * MAX_SS / l, j[1] * MAX_SS / l] } else { j } - }; - let jx = cap([l1x * d1[0] + l2x * d2[0], l1x * d1[1] + l2x * d2[1]]); - let jy = cap([l1y * d1[0] + l2y * d2[0], l1y * d1[1] + l2y * d2[1]]); - let (nx, ny) = (count(jx), count(jy)); - jac.push(if nx * ny == 1 { ([0.0; 4], 1, 1) } else { ([jx[0], jx[1], jy[0], jy[1]], nx, ny) }); - let bx0 = (p0[0].min(p1[0]).min(p2[0]) - 0.5).floor().max(f64::from(t.x0)) as i32; - let by0 = (p0[1].min(p1[1]).min(p2[1]) - 0.5).floor().max(f64::from(t.y0)) as i32; - let bx1 = (p0[0].max(p1[0]).max(p2[0]) + 0.5).ceil().min(f64::from(t.x1)) as i32; - let by1 = (p0[1].max(p1[1]).max(p2[1]) + 0.5).ceil().min(f64::from(t.y1)) as i32; - let eps = -1e-9; - for y in by0..by1 { - let py = f64::from(y) + 0.5; - for x in bx0..bx1 { - let px = f64::from(x) + 0.5; - let l1 = ((px - p0[0]) * (p2[1] - p0[1]) - (p2[0] - p0[0]) * (py - p0[1])) / det; - let l2 = ((p1[0] - p0[0]) * (py - p0[1]) - (px - p0[0]) * (p1[1] - p0[1])) / det; - let l0 = 1.0 - l1 - l2; - if l0 < eps || l1 < eps || l2 < eps { - continue; + // The layers of a fold under a pixel's top one, bottom first, as a list linked through + // `under` (source position, triangle, next node or `u32::MAX`) from the node `under_at` + // holds for the pixel (`u32::MAX`: none): one arena for the tile rather than a list per + // folded pixel. A layer taken out again keeps its node, its position NaN. + let mut under: Vec<([f64; 2], u32, u32)> = Vec::new(); + let mut under_at: Vec = Vec::new(); + let mut jac: Vec<([f64; 4], usize, usize)> = Vec::with_capacity(order.len()); + 'pass: loop { + for (k, tri) in order.iter().enumerate() { + let [(p0, s0), (p1, s1), (p2, s2)] = tri.map(|v| verts[v]); + let det = (p1[0] - p0[0]) * (p2[1] - p0[1]) - (p2[0] - p0[0]) * (p1[1] - p0[1]); + if det.abs() < 1e-12 { + jac.push(([0.0; 4], 1, 1)); + continue; + } + let (d1, d2) = ([s1[0] - s0[0], s1[1] - s0[1]], [s2[0] - s0[0], s2[1] - s0[1]]); + let (l1x, l1y, l2x, l2y) = ((p2[1] - p0[1]) / det, (p0[0] - p2[0]) / det, (p0[1] - p1[1]) / det, (p1[0] - p0[0]) / det); + // Capped, so a sliver triangle can't ask for an unbounded footprint. + let cap = |j: [f64; 2]| { + let l = j[0].hypot(j[1]); + if l > MAX_SS { [j[0] * MAX_SS / l, j[1] * MAX_SS / l] } else { j } + }; + let jx = cap([l1x * d1[0] + l2x * d2[0], l1x * d1[1] + l2x * d2[1]]); + let jy = cap([l1y * d1[0] + l2y * d2[0], l1y * d1[1] + l2y * d2[1]]); + let (nx, ny) = (count(jx), count(jy)); + jac.push(if nx * ny == 1 { ([0.0; 4], 1, 1) } else { ([jx[0], jx[1], jy[0], jy[1]], nx, ny) }); + let bx0 = (p0[0].min(p1[0]).min(p2[0]) - 0.5).floor().max(f64::from(t.x0)) as i32; + let by0 = (p0[1].min(p1[1]).min(p2[1]) - 0.5).floor().max(f64::from(t.y0)) as i32; + let bx1 = (p0[0].max(p1[0]).max(p2[0]) + 0.5).ceil().min(f64::from(t.x1)) as i32; + let by1 = (p0[1].max(p1[1]).max(p2[1]) + 0.5).ceil().min(f64::from(t.y1)) as i32; + let eps = -1e-9; + for y in by0..by1 { + let py = f64::from(y) + 0.5; + for x in bx0..bx1 { + let px = f64::from(x) + 0.5; + let l1 = ((px - p0[0]) * (p2[1] - p0[1]) - (p2[0] - p0[0]) * (py - p0[1])) / det; + let l2 = ((p1[0] - p0[0]) * (py - p0[1]) - (px - p0[0]) * (p1[1] - p0[1])) / det; + let l0 = 1.0 - l1 - l2; + if l0 < eps || l1 < eps || l2 < eps { + continue; + } + let o = (y - t.y0) as usize * w + (x - t.x0) as usize; + let q = [l0 * s0[0] + l1 * s1[0] + l2 * s2[0], l0 * s0[1] + l1 * s1[1] + l2 * s2[1]]; + // Another part of the source: a fold. Triangles of one layer meeting at a pixel + // (on their shared edge) interpolate the same source point, to float noise, + // and replace each other; one drawn again after another layer comes back on + // top of it, as painting would put it. Layers of a tight fold can be well under + // a source pixel apart and still be layers. + let near = |a: [f64; 2]| (a[0] - q[0]).hypot(a[1] - q[1]) <= SAME_LAYER; + if folds && !stacked && !uv[o][0].is_nan() && !near(uv[o]) { + // A fold shows in this tile: draw it again in the fold's order. + let mut ranked: Vec<(u32, [usize; 3])> = bin.iter().filter_map(|&k| tris.get(k as usize).map(|&t| (rank_of(k), t))).collect(); + ranked.sort_by_key(|e| e.0); + order = ranked.into_iter().map(|e| e.1).collect(); + stacked = true; + uv.fill([f64::NAN; 2]); + under_at = vec![u32::MAX; w * h]; + jac.clear(); + continue 'pass; + } + if stacked + && !uv[o][0].is_nan() + && !near(uv[o]) + && let Some(head) = under_at.get_mut(o) + && let Ok(node) = u32::try_from(under.len()) + { + // This layer replaces a layer of itself already in the list; the old top + // goes on the end. + let (mut at, mut last) = (*head, u32::MAX); + *head = u32::MAX; + while let Some(&(p, _, next)) = under.get(at as usize) { + if near(p) { + if let Some(e) = under.get_mut(at as usize) { + e.0 = [f64::NAN; 2]; + } + } else { + match under.get_mut(last as usize) { + Some(prev) => prev.2 = at, + None => *head = at, + } + last = at; + } + at = next; + } + under.push((uv[o], owner[o], u32::MAX)); + match under.get_mut(last as usize) { + Some(prev) => prev.2 = node, + None => *head = node, + } + } + uv[o] = q; + owner[o] = k as u32; } - let o = (y - t.y0) as usize * w + (x - t.x0) as usize; - uv[o] = [l0 * s0[0] + l1 * s1[0] + l2 * s2[0], l0 * s0[1] + l1 * s1[1] + l2 * s2[1]]; - owner[o] = k as u32; } } + break; } - // Source footprint of the covered pixels, including the spread of their subsamples. - let (mut fx0, mut fy0, mut fx1, mut fy1) = (f64::MAX, f64::MAX, f64::MIN, f64::MIN); - for (q, j) in uv.iter().zip(&owner).filter(|(q, _)| !q[0].is_nan()).map(|(q, &k)| (q, jac[k as usize].0)) { - let (ex, ey) = (0.5 * (j[0].abs() + j[2].abs()), 0.5 * (j[1].abs() + j[3].abs())); - fx0 = fx0.min(q[0] - ex); - fy0 = fy0.min(q[1] - ey); - fx1 = fx1.max(q[0] + ex); - fy1 = fy1.max(q[1] + ey); - } - if fx0 > fx1 { - return None; - } - let foot = Rect::new(fx0.floor() as i32 - 3, fy0.floor() as i32 - 3, fx1.ceil() as i32 + 3, fy1.ceil() as i32 + 3).intersect(&src_rect); - // Sparse components far apart in the source can land in one tile: the dense window - // between them is not read (the tile stays empty). The pixel-area cap is checked first: + // Where the covered pixels sample the source, with the spread of their subsamples, in + // regions read apart ([`Regions`]): the top layer's and, apart, those of the fold layers + // under it. + let spread = |(q, k): ([f64; 2], u32)| { + let j = jac[k as usize].0; + (q, [0.5 * (j[0].abs() + j[2].abs()), 0.5 * (j[1].abs() + j[3].abs())]) + }; + // Sparse components far apart in the source can land in one region: the dense window + // between them is not read (it stays transparent). The pixel-area cap is checked first: // `has_tiles_in` walks the source tile grid, so an oversized footprint must return // without paying for that scan. - if foot.is_empty() || foot.size().area() > MAX_FOOTPRINT_PIXELS || !src.has_tiles_in(foot) { + let window = |foot: Rect| -> Option { + if foot.is_empty() || foot.size().area() > MAX_FOOTPRINT_PIXELS || !src.has_tiles_in(foot) { + return None; + } + let mut px = src.read_region(foot); + for p in px.chunks_exact_mut(n) { + let al = p[a]; + for v in &mut p[..a] { + *v *= al; + } + } + Some(Win { px, foot, fw: foot.width() as usize }) + }; + // A top layer over an empty part of the source has no window: it is transparent there, + // and the fold layers under it still show. Too much to read leaves the tile empty, as + // without folds: what is under it can't show through where it may be opaque. + let top_regions = Regions::new( + &|f| { + for (q, &k) in uv.iter().zip(&owner).filter(|(q, _)| !q[0].is_nan()) { + let (q, e) = spread((*q, k)); + f(q, e); + } + }, + src_rect, + ); + if top_regions.area() > MAX_FOOTPRINT_PIXELS { return None; } - let mut px = src.read_region(foot); - for p in px.chunks_exact_mut(n) { - let al = p[a]; - for v in &mut p[..a] { - *v *= al; - } + let top: Vec> = top_regions.rects.iter().map(|&r| window(r)).collect(); + // Fold layers whose source can't be read in windows of their own, within the one cap for + // both, are left out (the top one stays). + let below_regions = Regions::new( + &|f| { + for &(q, k, _) in under.iter().filter(|e| !e.0[0].is_nan()) { + let (q, e) = spread((q, k)); + f(q, e); + } + }, + src_rect, + ); + let room = MAX_FOOTPRINT_PIXELS.saturating_sub(top_regions.area()); + let below: Vec> = + if !under.is_empty() && below_regions.area() <= room { below_regions.rects.iter().map(|&r| window(r)).collect() } else { Vec::new() }; + if below.iter().all(Option::is_none) { + under.clear(); } - let fw = foot.width() as usize; - let at = |x: i32, y: i32, c: usize| -> f64 { + if top.iter().all(Option::is_none) && under.is_empty() { + return None; + } + // The window a point of the top layer, or of a fold layer under it, samples. + let top_win = |q: [f64; 2]| top_regions.of(q).and_then(|r| top.get(r)).and_then(Option::as_ref); + let below_win = |q: [f64; 2]| below_regions.of(q).and_then(|r| below.get(r)).and_then(Option::as_ref); + let at = |win: &Win, x: i32, y: i32, c: usize| -> f64 { + let foot = win.foot; if x < foot.x0 || y < foot.y0 || x >= foot.x1 || y >= foot.y1 { 0.0 } else { - f64::from(px[((y - foot.y0) as usize * fw + (x - foot.x0) as usize) * n + c]) + f64::from(win.px[((y - foot.y0) as usize * win.fw + (x - foot.x0) as usize) * n + c]) } }; // Offset of pixel (x, y) when the `taps` x `taps` block from it lies inside `foot`, so // the samplers can index `px` directly instead of bounds-checking every tap. - let base = |x: i32, y: i32, taps: i32| -> Option { - (x >= foot.x0 && y >= foot.y0 && x + taps <= foot.x1 && y + taps <= foot.y1).then(|| ((y - foot.y0) as usize * fw + (x - foot.x0) as usize) * n) + let base = |win: &Win, x: i32, y: i32, taps: i32| -> Option { + let foot = win.foot; + (x >= foot.x0 && y >= foot.y0 && x + taps <= foot.x1 && y + taps <= foot.y1).then(|| ((y - foot.y0) as usize * win.fw + (x - foot.x0) as usize) * n) }; let mut outp = vec![0.0f32; w * h * n]; let mut any = false; let mut acc = [0.0f64; 8]; let mut one = [0.0f64; 8]; // One premultiplied sample at source position (u, v) (pixel centres at integers). - let point = |interp: Interp, u: f64, v: f64, acc: &mut [f64; 8]| { + let point = |win: &Win, interp: Interp, u: f64, v: f64, acc: &mut [f64; 8]| { acc[..n].fill(0.0); match interp { Interp::Nearest => { let (ix, iy) = ((u + 0.5).floor() as i32, (v + 0.5).floor() as i32); for (c, s) in acc.iter_mut().enumerate().take(n) { - *s = at(ix, iy, c); + *s = at(win, ix, iy, c); } } Interp::Bilinear => { let (ix, iy) = (u.floor() as i32, v.floor() as i32); let (fx, fy) = (u - f64::from(ix), v - f64::from(iy)); - if let Some(o) = base(ix, iy, 2) { + if let Some(o) = base(win, ix, iy, 2) { for (dy, wy) in [(0, 1.0 - fy), (1, fy)] { for (dx, wx) in [(0, 1.0 - fx), (1, fx)] { - let p = &px[o + (dy * fw + dx) * n..][..n]; + let p = &win.px[o + (dy * win.fw + dx) * n..][..n]; for (s, v) in acc.iter_mut().zip(p) { *s += f64::from(*v) * wx * wy; } @@ -373,7 +712,7 @@ fn triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris for (dy, wy) in [(0, 1.0 - fy), (1, fy)] { for (dx, wx) in [(0, 1.0 - fx), (1, fx)] { for (c, s) in acc.iter_mut().enumerate().take(n) { - *s += at(ix + dx, iy + dy, c) * wx * wy; + *s += at(win, ix + dx, iy + dy, c) * wx * wy; } } } @@ -384,14 +723,14 @@ fn triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris let (u, v) = (snap(u), snap(v)); let (ix, iy) = (u.floor() as i32, v.floor() as i32); let (wx, wy) = (bicubic(u - f64::from(ix)), bicubic(v - f64::from(iy))); - if let Some(o) = base(ix - 1, iy - 1, 4) { + if let Some(o) = base(win, ix - 1, iy - 1, 4) { for (j, wyj) in wy.iter().enumerate() { for (i, wxi) in wx.iter().enumerate() { let k = wxi * wyj; if k == 0.0 { continue; } - let p = &px[o + (j * fw + i) * n..][..n]; + let p = &win.px[o + (j * win.fw + i) * n..][..n]; for (s, v) in acc.iter_mut().zip(p) { *s += f64::from(*v) * k; } @@ -406,35 +745,66 @@ fn triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris continue; } for (c, s) in acc.iter_mut().enumerate().take(n) { - *s += at(ix - 1 + i as i32, iy - 1 + j as i32, c) * k; + *s += at(win, ix - 1 + i as i32, iy - 1 + j as i32, c) * k; } } } } } }; + // A premultiplied sample of triangle `k`'s source at (u, v): one point, or the average of + // its subsamples where the warp shrinks the source. + let sample = |win: &Win, q: [f64; 2], k: u32, acc: &mut [f64; 8], one: &mut [f64; 8]| { + let (j, nx, ny) = jac[k as usize]; + if nx * ny == 1 { + point(win, interp, q[0] - 0.5, q[1] - 0.5, acc); + return; + } + acc[..n].fill(0.0); + for sy in 0..ny { + let t = (sy as f64 + 0.5) / ny as f64 - 0.5; + for sx in 0..nx { + let s = (sx as f64 + 0.5) / nx as f64 - 0.5; + point(win, Interp::Bilinear, q[0] + s * j[0] + t * j[2] - 0.5, q[1] + s * j[1] + t * j[3] - 0.5, one); + for (c, v) in acc.iter_mut().zip(one.iter()).take(n) { + *c += v; + } + } + } + let k = 1.0 / (nx * ny) as f64; + for c in acc.iter_mut().take(n) { + *c *= k; + } + }; + let mut layer = [0.0f64; 8]; for (o, (q, &k)) in uv.iter().zip(&owner).enumerate() { if q[0].is_nan() { continue; } - let (j, nx, ny) = jac[k as usize]; - if nx * ny == 1 { - point(interp, q[0] - 0.5, q[1] - 0.5, &mut acc); - } else { - acc[..n].fill(0.0); - for sy in 0..ny { - let t = (sy as f64 + 0.5) / ny as f64 - 0.5; - for sx in 0..nx { - let s = (sx as f64 + 0.5) / nx as f64 - 0.5; - point(Interp::Bilinear, q[0] + s * j[0] + t * j[2] - 0.5, q[1] + s * j[1] + t * j[3] - 0.5, &mut one); - for (c, v) in acc.iter_mut().zip(&one).take(n) { - *c += v; - } + match top_win(*q) { + Some(tw) => sample(tw, *q, k, &mut acc, &mut one), + None => acc[..n].fill(0.0), + } + // A fold: its layers under this one, bottom first, composite under the top (unless + // the top covers them). + let first = under_at.get(o).copied().unwrap_or(u32::MAX); + if first != u32::MAX && !under.is_empty() && acc[a] < 1.0 { + let mut under_top = [0.0f64; 8]; + let mut at = first; + while let Some(&(q, k, next)) = under.get(at as usize) { + at = next; + match below_win(q) { + Some(bw) => sample(bw, q, k, &mut layer, &mut one), + None => layer[..n].fill(0.0), + } + let keep = 1.0 - layer[a].clamp(0.0, 1.0); + for c in 0..n { + under_top[c] = layer[c] + under_top[c] * keep; } } - let k = 1.0 / (nx * ny) as f64; - for c in acc.iter_mut().take(n) { - *c *= k; + let keep = 1.0 - acc[a].clamp(0.0, 1.0); + for c in 0..n { + acc[c] += under_top[c] * keep; } } let al = acc[a].clamp(0.0, 1.0); @@ -482,6 +852,12 @@ fn triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris /// (layer masks, selections): the content is warped with alpha and flattened back onto the /// default. pub fn warp_mesh_gray(s: &Surface, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), interp: Interp) -> Surface { + warp_mesh_gray_with(s, f, Fold::Rows, interp) +} + +/// [`warp_mesh_gray`] with folds stacked as `fold` says: a layer's mask and its selection follow +/// the layer's pixels, the part on top of a fold included. +pub fn warp_mesh_gray_with(s: &Surface, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), fold: Fold, interp: Interp) -> Surface { let default = s.default_pixel().first().copied().unwrap_or(0.0); let fmt = s.format(); let src = s.content_bounds(); @@ -493,7 +869,7 @@ pub fn warp_mesh_gray(s: &Surface, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), let mut tmp = Surface::new(with_alpha); let v = s.read_region(src); tmp.write_region(src, &v.iter().flat_map(|g| [*g, 1.0]).collect::>()); - let w = warp_mesh_surface(&tmp, src, f, interp); + let w = mesh(&tmp, src, f, fold, interp, &Interrupt::NONE); // Clear the old region and write the warped one separately, as `warp_gray` does: one dense // `src ∪ warped` buffer grows with the square of how far a mesh moves the content (#995). // `Surface` is tile-sparse, so a distant region costs only its own tiles. @@ -511,6 +887,125 @@ pub fn warp_mesh_gray(s: &Surface, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), #[cfg(test)] mod tests { use super::*; + + /// Edit › Transform › Warp folding the picture over itself (#8927): the layers stack as the + /// preview draws them, the part dragged furthest on top, and composite, so a transparent part on top + /// shows the content folded under it, also where the top part is a stretch of the source with + /// no pixels at all (no tiles). Layers aren't composited twice, and other mesh maps keep row + /// order. + #[test] + fn a_fold_shows_what_is_under_its_transparent_parts() { + let fmt = PixelFormat::new(ColorMode::Rgb, SampleType::U8, true); + let ts = photocraft_geom::TILE_SIZE; + for width in [100, 4 * ts] { + let half = width / 2; + // The red left half stays; the right half, transparent (no pixels), folds back over it. + let mut src = Surface::new(fmt); + src.fill_rect(Rect::new(0, 0, half, 10), &[1.0, 0.0, 0.0, 1.0]); + let w = f64::from(width); + let fold = move |x: f64, y: f64| (if x <= w / 2.0 { x } else { w - x }, y); + let out = warp_mesh_surface_with(&src, Rect::new(0, 0, width, 10), &fold, Fold::IN_PLACE, Interp::Bilinear, &Interrupt::NONE).unwrap(); + let px = out.read_region(Rect::new(0, 5, half, 6)); + let alpha: Vec = px.as_chunks::<4>().0.iter().map(|p| p[3]).collect(); + let lit = alpha.iter().filter(|a| **a > 0.99).count(); + assert!(lit as i32 >= half - 4, "width {width}: the red under the transparent top shows ({lit} of {half} px)"); + // The generic mesh warp keeps row order: the last drawn (the right half, empty) wins. + let plain = warp_mesh_surface(&src, Rect::new(0, 0, width, 10), &fold, Interp::Bilinear); + assert!(plain.read_region(Rect::new(half / 2, 5, half / 2 + 1, 6))[3] < 0.01); + } + // Two half-transparent layers composite: 0.75. + let mut half = Surface::new(fmt); + half.fill_rect(Rect::new(0, 0, 100, 10), &[0.0, 0.0, 1.0, 0.5]); + let fold = |x: f64, y: f64| (if x <= 50.0 { x } else { 100.0 - x }, y); + let out = warp_mesh_surface_with(&half, Rect::new(0, 0, 100, 10), &fold, Fold::IN_PLACE, Interp::Bilinear, &Interrupt::NONE).unwrap(); + let a = out.read_region(Rect::new(20, 5, 21, 6))[3]; + assert!((a - 0.75).abs() < 0.02, "two half-transparent layers: alpha {a}"); + // No fold: every pixel of a half-transparent picture stays at 0.5 (no layer counted twice + // along the cells' edges). + let mut big = Surface::new(fmt); + big.fill_rect(Rect::new(0, 0, 200, 200), &[0.0, 0.0, 1.0, 0.5]); + let bend = |x: f64, y: f64| (x + 6.0 * (y / 30.0).sin(), y + 6.0 * (x / 40.0).cos()); + let out = warp_mesh_surface_with(&big, Rect::new(0, 0, 200, 200), &bend, Fold::IN_PLACE, Interp::Bilinear, &Interrupt::NONE).unwrap(); + let px = out.read_region(Rect::new(20, 20, 180, 180)); + let worst = px.as_chunks::<4>().0.iter().map(|p| (p[3] - 0.5).abs()).fold(0.0f32, f32::max); + assert!(worst < 0.02, "a warp without a fold keeps the alpha: off by {worst}"); + // A tight fold, magnified: the layers are under a source pixel apart and still composite. + let mut thin = Surface::new(fmt); + thin.fill_rect(Rect::new(0, 0, 20, 2), &[0.0, 0.0, 1.0, 0.5]); + let tight = |x: f64, y: f64| (50.0 * if x <= 10.0 { x } else { 20.0 - x }, y); + let out = warp_mesh_surface_with(&thin, Rect::new(0, 0, 20, 2), &tight, Fold::IN_PLACE, Interp::Bilinear, &Interrupt::NONE).unwrap(); + // 20 px from the crease: 0.4 source px into each layer, 0.8 apart. + let a = out.read_region(Rect::new(480, 1, 481, 2))[3]; + assert!((a - 0.75).abs() < 0.02, "layers 0.8 source px apart: alpha {a}"); + } + + /// A mask or selection warped with the layer follows its pixels: where the warp folds, the + /// part on top is the part the layer shows on top. + #[test] + fn a_fold_stacks_a_mask_as_it_stacks_the_layer() { + let fmt = PixelFormat::new(ColorMode::Grayscale, SampleType::F32, false); + let mut m = Surface::with_default(fmt, &[0.0]); + m.fill_rect(Rect::new(0, 0, 100, 10), &[0.25]); + m.fill_rect(Rect::new(50, 0, 100, 10), &[1.0]); + // The left half folds over the right. + let fold = |x: f64, y: f64| (if x >= 50.0 { x } else { 100.0 - x }, y); + let top = warp_mesh_gray_with(&m, &fold, Fold::IN_PLACE, Interp::Bilinear).read_region(Rect::new(80, 5, 81, 6))[0]; + assert!((top - 0.25).abs() < 1e-3, "the left part, dragged over, is on top: {top}"); + let rows = warp_mesh_gray(&m, &fold, Interp::Bilinear).read_region(Rect::new(80, 5, 81, 6))[0]; + assert!((rows - 1.0).abs() < 1e-3, "other mesh maps: the last cell wins: {rows}"); + } + + /// A vertex with no finite movement (past a Distort's horizon) goes under, not on top. + #[test] + fn cells_moved_without_bound_go_first() { + // Two cells in a row: the second's right vertices moved to infinity. + let rest = [[0.0, 0.0], [1.0, 0.0], [2.0, 0.0], [0.0, 1.0], [1.0, 1.0], [2.0, 1.0]]; + let mut pos = rest.map(|[x, y]| [x + 1.0, y]); + pos[2] = [f64::INFINITY, 0.0]; + pos[5] = [f64::NAN, 1.0]; + assert_eq!(cell_order(&pos, &rest, 3), vec![1, 0]); + } + + /// A cell the warp turns over (mirrored, as a flap folded back) goes on top, even when it moved + /// less than the cells around it; within each kind, the cell moved least goes first. + #[test] + fn a_turned_over_cell_goes_on_top() { + let rest = [[0.0, 0.0], [1.0, 0.0], [2.0, 0.0], [3.0, 0.0], [0.0, 1.0], [1.0, 1.0], [2.0, 1.0], [3.0, 1.0]]; + // Cell 0 moved far and cell 1 a little, both facing as before; cell 2 is mirrored in x and + // moved less than cell 0. + let pos = [[-9.0, 0.0], [1.0, 0.0], [2.2, 0.0], [1.6, 0.0], [-9.0, 1.0], [1.0, 1.0], [2.2, 1.0], [1.6, 1.0]]; + assert_eq!(cell_order(&pos, &rest, 4), vec![1, 0, 2]); + // A mirrored placement turns nothing over: only a change of facing counts. + let mirrored = rest.map(|[x, y]| [-x, y]); + assert_eq!(cell_order(&mirrored, &mirrored, 4), vec![0, 1, 2]); + } + + /// The middle of the picture pulled out past its right edge: the edge strip folds back over it + /// and lies on top, so the pulled middle is under that strip (by distance moved alone, the middle + /// would cover it). + #[test] + fn the_middle_pulled_past_an_edge_goes_under_the_folded_edge() { + let fmt = PixelFormat::new(ColorMode::Grayscale, SampleType::F32, false); + let mut m = Surface::with_default(fmt, &[0.0]); + m.fill_rect(Rect::new(0, 0, 75, 10), &[0.25]); + m.fill_rect(Rect::new(75, 0, 100, 10), &[1.0]); + // 0..75 stretches to 0..90; the edge strip 75..100 folds back onto 90..80; beyond, unchanged. + let pull = |x: f64, y: f64| { + ( + if x <= 75.0 { + x * 1.2 + } else if x <= 100.0 { + 90.0 - (x - 75.0) * 0.4 + } else { + x - 20.0 + }, + y, + ) + }; + let at = warp_mesh_gray_with(&m, &pull, Fold::IN_PLACE, Interp::Bilinear).read_region(Rect::new(85, 5, 86, 6))[0]; + assert!((at - 1.0).abs() < 1e-3, "the folded-back edge strip is on top: {at}"); + } + use photocraft_color::{ColorMode, SampleType}; use photocraft_geom::warp::{BezierMesh, Warp, WarpStyle}; @@ -572,13 +1067,13 @@ mod tests { *m = m.max(p); }; let never = || false; - let out = warp_mesh_surface_with(&s, r, &map, Interp::Bicubic, &Interrupt::new(&never, &progress)).expect("not cancelled"); + let out = warp_mesh_surface_with(&s, r, &map, Fold::Rows, Interp::Bicubic, &Interrupt::new(&never, &progress)).expect("not cancelled"); let want = warp_mesh_surface(&s, r, &map, Interp::Bicubic); assert_eq!(out.content_bounds(), want.content_bounds()); assert_eq!(out.read_region(want.content_bounds()), want.read_region(want.content_bounds())); assert!(*reported.lock().unwrap() > 0.9); let always = || true; - assert!(warp_mesh_surface_with(&s, r, &map, Interp::Bicubic, &Interrupt::cancel_only(&always)).is_none()); + assert!(warp_mesh_surface_with(&s, r, &map, Fold::Rows, Interp::Bicubic, &Interrupt::cancel_only(&always)).is_none()); } #[test] @@ -684,6 +1179,51 @@ mod tests { } } + /// A warp that doesn't fold is drawn in row order whether or not folds stack: the result is + /// the same to the bit, and only a tile where the picture overlaps itself is drawn again. + #[test] + fn a_warp_without_a_fold_is_the_same_either_way() { + let (w, h) = (300, 200); + let mut src = Surface::new(PixelFormat::RGBA8); + let px: Vec = (0..w * h).flat_map(|i| [(i % 7) as f32 / 6.0, (i % 11) as f32 / 10.0, 0.5, 1.0]).collect(); + let r = Rect::new(0, 0, w, h); + src.write_region(r, &px); + let bulge = |x: f64, y: f64| { + let (u, v) = (x / 300.0 - 0.5, y / 200.0 - 0.5); + let k = 1.0 - 0.6 * (u * u + v * v); + (300.0 * (0.5 + u * k) + 10.0, 200.0 * (0.5 + v * k) + 10.0) + }; + let rows = warp_mesh_surface_with(&src, r, &bulge, Fold::Rows, Interp::Bicubic, &Interrupt::NONE).unwrap(); + let stacked = warp_mesh_surface_with(&src, r, &bulge, Fold::IN_PLACE, Interp::Bicubic, &Interrupt::NONE).unwrap(); + let b = rows.content_bounds(); + assert_eq!(b, stacked.content_bounds()); + assert!(rows.read_region(b) == stacked.read_region(b), "the same pixels"); + } + + /// Points in two parts of the source far apart are two regions, each read on its own; points + /// close together are one, and a point not given has none. + #[test] + fn regions_split_distant_parts_of_the_source() { + let src = Rect::new(0, 0, 6000, 4000); + let pts: Vec<([f64; 2], [f64; 2])> = + (0..64).flat_map(|i| [([100.0 + f64::from(i), 100.0], [0.5, 0.5]), ([5000.0 + f64::from(i), 3500.0], [0.5, 0.5])]).collect(); + let r = Regions::new(&|f| pts.iter().for_each(|&(q, e)| f(q, e)), src); + assert_eq!(r.rects.len(), 2, "{:?}", r.rects); + assert!(r.area() < 20_000, "small windows: {:?}", r.rects); + assert_ne!(r.of([100.0, 100.0]), r.of([5000.0, 3500.0])); + assert_eq!(r.of([120.0, 100.0]), r.of([100.0, 100.0])); + assert_eq!(r.of([3000.0, 2000.0]), None, "nothing samples between them"); + // A compact patch stays one region; nothing at all is none. + let near: Vec<([f64; 2], [f64; 2])> = (0..400).map(|i| ([f64::from(i % 20), f64::from(i / 20)], [0.5, 0.5])).collect(); + let one = Regions::new(&|f| near.iter().for_each(|&(q, e)| f(q, e)), src); + assert_eq!(one.rects.len(), 1); + assert_eq!(one.of([5.0, 5.0]), Some(0)); + let none = Regions::new(&|_| {}, src); + assert!(none.rects.is_empty() && none.of([0.0, 0.0]).is_none()); + let bad = Regions::new(&|f| f([f64::NAN, 1.0], [0.5, 0.5]), src); + assert!(bad.rects.is_empty()); + } + #[test] fn gray_masks_keep_their_default() { let mut m = Surface::with_default(PixelFormat::GRAY8, &[1.0]); @@ -693,4 +1233,31 @@ mod tests { assert_eq!(o.pixel(12, 6), vec![0.0]); assert_eq!(o.pixel(5, 6), vec![1.0]); } + + /// Tiles are drawn on the thread pool, and the first one that meets a fold works out the + /// fold's order while the others that need it wait. That must never hand the working thread + /// another folded tile (it would wait on itself, and every tile after it for ever): a warp + /// that folds some of its tiles, with a mesh big enough to be worth sorting on several + /// threads, finishes every time. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_fold_never_stalls_the_thread_pool() { + let mut src = Surface::new(PixelFormat::RGBA8); + src.fill_rect(Rect::new(0, 0, 64, 64), &[1.0, 0.0, 0.0, 1.0]); + let r = Rect::new(0, 0, 2048, 2048); + // The left quarter folded over the next: a quarter of the tiles hold two layers. + let fold = |x: f64, y: f64| (if x < 512.0 { 1024.0 - x } else { x }, y); + let (tx, rx) = std::sync::mpsc::channel(); + // Pools of its own, so a stall can't take the other tests' pool with it. + std::thread::spawn(move || { + for threads in [8, 16] { + let Ok(pool) = rayon::ThreadPoolBuilder::new().num_threads(threads).build() else { return }; + for _ in 0..12 { + pool.install(|| warp_mesh_surface_with(&src, r, &fold, Fold::IN_PLACE, Interp::Nearest, &Interrupt::NONE)); + } + } + let _ = tx.send(()); + }); + assert!(rx.recv_timeout(std::time::Duration::from_secs(120)).is_ok(), "the warp never finished"); + } } diff --git a/crates/engine/src/cmd/warp_cmds.rs b/crates/engine/src/cmd/warp_cmds.rs index 527035f831..ae887cb7af 100644 --- a/crates/engine/src/cmd/warp_cmds.rs +++ b/crates/engine/src/cmd/warp_cmds.rs @@ -9,7 +9,7 @@ //! what the Free Transform warp UI uses) or the active smart object's stored warp (a history step). use photocraft_algo::transform::{Homography, Interp}; -use photocraft_algo::warp::{warp_mesh_gray, warp_mesh_surface_with}; +use photocraft_algo::warp::{Fold, warp_mesh_gray_with, warp_mesh_surface_with}; use photocraft_doc::{Document, Layer, LayerContent, LayerId, Locks, Rect}; use photocraft_geom::warp::{BezierMesh, Warp, WarpStyle}; use photocraft_raster::{Interrupt, Surface}; @@ -252,10 +252,10 @@ fn warp_layer( sm.warp = src_warp.filter(|w| !w.is_identity()); // Fallback appearance for sources that can't be re-rendered. if let Some(c) = &mut sm.cache { - *c = warp_mesh_surface_with(c, rect, &map, interp, ctl).ok_or(EngineError::Cancelled)?; + *c = warp_mesh_surface_with(c, rect, &map, Fold::IN_PLACE, interp, ctl).ok_or(EngineError::Cancelled)?; } if let Some(m) = &mut sm.filter_mask { - m.surface = warp_mesh_gray(&m.surface, &map, interp); + m.surface = warp_mesh_gray_with(&m.surface, &map, Fold::IN_PLACE, interp); } } _ => { @@ -263,7 +263,7 @@ fn warp_layer( *surf = match doc_sel { Some(sel) => { let (lifted, mut rest) = crate::transform_cmds::split_selected(surf, sel)?; - let moved = warp_mesh_surface_with(&lifted, rect, &map, interp, ctl).ok_or(EngineError::Cancelled)?; + let moved = warp_mesh_surface_with(&lifted, rect, &map, Fold::IN_PLACE, interp, ctl).ok_or(EngineError::Cancelled)?; cancelled(ctl)?; crate::transform_cmds::composite_over(&mut rest, &moved); rest.prune(); @@ -272,7 +272,7 @@ fn warp_layer( None => { // Content outside the warp box stays where it is. let fmt = surf.format(); - let warped = warp_mesh_surface_with(surf, rect, &map, interp, ctl).ok_or(EngineError::Cancelled)?; + let warped = warp_mesh_surface_with(surf, rect, &map, Fold::IN_PLACE, interp, ctl).ok_or(EngineError::Cancelled)?; cancelled(ctl)?; // Tiles are shared, so a surface that has alpha already costs nothing to copy. let mut rest = if fmt.alpha { surf.clone() } else { surf.convert(photocraft_color::PixelFormat::new(fmt.mode, fmt.sample, true)) }; @@ -294,12 +294,13 @@ fn warp_layer( if let Some(m) = l.mask.as_mut() && m.linked { - let whole = |m: &Surface| Ok(warp_mesh_gray(m, &map, interp)); + // The mask follows the pixels, the part on top of a fold included. + let whole = |m: &Surface| Ok(warp_mesh_gray_with(m, &map, Fold::IN_PLACE, interp)); m.surface = match mask_sel { Some(sel) => crate::transform_cmds::move_gray_selected( &m.surface, sel, - &|lifted, src| warp_mesh_surface_with(lifted, src, &map, interp, ctl).ok_or(EngineError::Cancelled), + &|lifted, src| warp_mesh_surface_with(lifted, src, &map, Fold::IN_PLACE, interp, ctl).ok_or(EngineError::Cancelled), &whole, )?, None => whole(&m.surface)?, @@ -494,7 +495,7 @@ fn warp_doc(doc: &mut Document, id: LayerId, w: &Warp, rect: Rect, interp: Inter crate::transform_cmds::refresh_text(&snapshot, l); } if let Some(sel) = &doc.selection { - doc.selection = Some(warp_mesh_gray(sel, &|x, y| w.map(x, y), Interp::Bilinear)).filter(|s| !s.content_bounds().is_empty()); + doc.selection = Some(warp_mesh_gray_with(sel, &|x, y| w.map(x, y), Fold::IN_PLACE, Interp::Bilinear)).filter(|s| !s.content_bounds().is_empty()); } Ok(()) } diff --git a/crates/engine/src/cmd/warp_cmds/tests.rs b/crates/engine/src/cmd/warp_cmds/tests.rs index 6f51abd7c3..cbbbc942ec 100644 --- a/crates/engine/src/cmd/warp_cmds/tests.rs +++ b/crates/engine/src/cmd/warp_cmds/tests.rs @@ -11,7 +11,7 @@ fn a_warp_box_holding_the_whole_layer_gives_what_compositing_over_the_cleared_la let w = Warp::preset(WarpStyle::Flag, 35.0, rect_f(rect)); // What the warp built before the shortcut: the warp composited over the cleared layer. let map = |x: f64, y: f64| w.map(x, y); - let warped = warp_mesh_surface_with(&surf, rect, &map, Interp::Bicubic, &Interrupt::NONE).unwrap(); + let warped = warp_mesh_surface_with(&surf, rect, &map, Fold::IN_PLACE, Interp::Bicubic, &Interrupt::NONE).unwrap(); let mut want = surf.clone(); crate::pixels::try_clear_surface(&mut want, rect, None).unwrap(); crate::transform_cmds::composite_over(&mut want, &warped); diff --git a/crates/ui-egui/src/tools/warp_preview.rs b/crates/ui-egui/src/tools/warp_preview.rs index ec14375793..ecd3015050 100644 --- a/crates/ui-egui/src/tools/warp_preview.rs +++ b/crates/ui-egui/src/tools/warp_preview.rs @@ -48,32 +48,22 @@ pub fn samples(mesh: &BezierMesh, zoom: f64) -> (Vec, Vec) { } /// Triangle indices for a `row`-wide vertex grid (`pos` and `rest` row by row; `rest` is where each -/// vertex sits with no warp), two triangles per cell. Cells are drawn from the most displaced to the -/// least, so the part of the picture the warp left alone is on top. +/// vertex sits with no warp), two triangles per cell, in the render's order: a part the fold turns +/// over lies on top, and otherwise the part of the picture dragged furthest. /// /// Drawing in plain row order lets the layers of a hard pull (the sheets of an S-shaped fold) cover /// each other row by row, which shows as a comb along the fold. A fold's layers differ in how far /// the warp moved them, so ordering by displacement keeps each layer whole, with the tip of the -/// pull underneath and the picture as it was on top. +/// pull on top of the picture it was dragged over. pub fn triangle_order(pos: &[[f32; 2]], rest: &[[f32; 2]], row: usize) -> Vec { if row < 2 || pos.len() != rest.len() { return Vec::new(); } - let rows = pos.len() / row; - let moved = |k: usize| match (pos.get(k), rest.get(k)) { - (Some(p), Some(r)) => (p[0] - r[0]).hypot(p[1] - r[1]), - _ => 0.0, - }; - let mut cells: Vec<(f32, usize)> = Vec::with_capacity(rows * row); - for j in 0..rows.saturating_sub(1) { - for i in 0..row - 1 { - let a = j * row + i; - cells.push(((moved(a) + moved(a + 1) + moved(a + row) + moved(a + row + 1)) / 4.0, a)); - } - } - cells.sort_by(|x, y| y.0.total_cmp(&x.0)); - let mut out = Vec::with_capacity(cells.len() * 6); - for (_, a) in cells { + // The render's own order (`photocraft_algo::warp::cell_order`), so preview and result stack + // a fold alike. + let wide = |v: &[[f32; 2]]| -> Vec<[f64; 2]> { v.iter().map(|p| [f64::from(p[0]), f64::from(p[1])]).collect() }; + let mut out = Vec::with_capacity(pos.len() * 6); + for a in photocraft_algo::warp::cell_order(&wide(pos), &wide(rest), row) { out.extend([a, a + 1, a + row + 1, a, a + row + 1, a + row].map(|k| k as u32)); } out @@ -120,17 +110,23 @@ mod tests { } #[test] - fn the_most_displaced_cells_are_drawn_first() { - // A 4 x 3 vertex grid; the middle column was pulled 30 px right, so it moved most. + fn a_turned_over_cell_is_drawn_last_then_the_most_displaced() { + // A 4 x 3 vertex grid; the middle columns were pulled 30 px right, past the last column, so + // the cells between them and it are turned over (mirrored). let rest: Vec<[f32; 2]> = (0..3).flat_map(|j| (0..4).map(move |i| [i as f32 * 10.0, j as f32 * 10.0])).collect(); let pos: Vec<[f32; 2]> = rest.iter().enumerate().map(|(k, p)| if k % 4 == 1 || k % 4 == 2 { [p[0] + 30.0, p[1]] } else { *p }).collect(); let idx = triangle_order(&pos, &rest, 4); assert_eq!(idx.len(), 2 * 2 * 3 * 3); - // Cells come in whole pairs of triangles, and displacement never rises along the list. - let moved = |k: u32| (pos[k as usize][0] - rest[k as usize][0]).abs(); - let per_cell: Vec = idx.chunks(6).map(|c| c.iter().map(|k| moved(*k)).sum::()).collect(); - assert!(per_cell.windows(2).all(|w| w[0] >= w[1]), "{per_cell:?}"); - assert!(per_cell[0] > per_cell[per_cell.len() - 1]); + // Cells come in whole pairs of triangles, named here by their top-left vertex. + let cells: Vec = idx.chunks(6).map(|c| c[0]).collect(); + let turned = |a: u32| pos[a as usize + 1][0] < pos[a as usize][0]; + let (facing, over): (Vec, Vec) = cells.iter().partition(|a| !turned(**a)); + assert_eq!(over, vec![2, 6], "the turned-over cells, last"); + assert_eq!(cells[cells.len() - over.len()..], over[..], "drawn after every other cell"); + // The others in displacement order, least first. + let moved = |a: u32| [a, a + 1, a + 4, a + 5].iter().map(|k| (pos[*k as usize][0] - rest[*k as usize][0]).abs()).sum::(); + let per_cell: Vec = facing.iter().map(|a| moved(*a)).collect(); + assert!(per_cell.windows(2).all(|w| w[0] <= w[1]), "{per_cell:?}"); assert!(triangle_order(&pos, &rest[..5], 4).is_empty()); }