PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(transform): Photoshop's Bicubic for Free Transform and Warp, with a reduction prefilter (#4635)
MrCloudy29c3f96649f79
diff --git a/crates/algo/src/transform.rs b/crates/algo/src/transform.rsindex ba75a6533..8f841e745 100644--- a/crates/algo/src/transform.rs+++ b/crates/algo/src/transform.rs@@ -3,7 +3,7 @@//! A transform is described by where the four corners of a source rectangle land (a quad). Scale,//! rotate, skew and flips are affine special cases; Distort and Perspective use the full//! homography. Warping is an inverse mapping with premultiplied-alpha sampling (nearest, bilinear-//! or Catmull-Rom bicubic), processed per destination tile (in parallel on native) so memory stays+//! or Photoshop's Keys bicubic), processed per destination tile (in parallel on native) so memory stays//! bounded by the tile working set. Large reductions are pre-filtered with a proper resize first,//! so shrinking a layer doesn't alias.@@ -133,10 +133,14 @@ impl Interp {}}-fn catmull_rom(t: f64) -> [f64; 4] {- let t2 = t * t;- let t3 = t2 * t;- [-0.5 * t3 + t2 - 0.5 * t, 1.5 * t3 - 2.5 * t2 + 1.0, -1.5 * t3 + 2.0 * t2 + 0.5 * t, 0.5 * t3 - 0.5 * t2]+/// Photoshop's Bicubic: the Keys cubic at a = −0.75 (Catmull-Rom is −0.5), the weights of the+/// four source pixels around a sample `t` (0..1) past the second. Measured on Photoshop 27.11's+/// Free Transform: a 200 % step edge matches within 0.5 levels; Catmull-Rom was 6 off.+pub(crate) fn bicubic(t: f64) -> [f64; 4] {+ const A: f64 = -0.75;+ let near = |x: f64| (A + 2.0) * x * x * x - (A + 3.0) * x * x + 1.0;+ let far = |x: f64| A * (x * x * x - 5.0 * x * x + 8.0 * x - 4.0);+ [far(1.0 + t), near(t), near(1.0 - t), far(2.0 - t)]}/// Warp `src` (its content inside `src_rect`) by `h` (source → destination document space).@@ -260,8 +264,12 @@ fn warp(};let scale = h.local_scale(cx, cy);let reduced;- if interp != Interp::Nearest && scale < 0.5 && scale > 0.0 {- let f = 2f64.powf(scale.log2().ceil()).min(1.0); // remaining scale lands in [0.5, 1)+ // Photoshop reduces with its Bicubic widened by 1 / scale (as Image Size does), so the source is+ // brought down by the whole local scale first and the warp samples it at about 1:1; a plain+ // 4×4 kernel at 50 % aliased (Photoshop 27.11 Free Transform, 50 % of an impulse image).+ // Bilinear samples the source directly, as Photoshop's does (exact at 50 % without a prefilter).+ if interp == Interp::Bicubic && scale < 0.999 && scale > 0.0 {+ let f = scale.max(1.0 / 64.0);reduced = resize_surface(src, f, f, Resample::Bicubic);src = &reduced;h = h.mul(&Homography([1.0 / f, 0.0, 0.0, 0.0, 1.0 / f, 0.0, 0.0, 0.0, 1.0]));@@ -395,7 +403,7 @@ fn warp(}Interp::Bicubic => {let (ix, iy) = (u.floor() as i32, v.floor() as i32);- let (wx, wy) = (catmull_rom(u - ix as f64), catmull_rom(v - iy as f64));+ let (wx, wy) = (bicubic(u - ix as f64), bicubic(v - iy as f64));for (j, wyj) in wy.iter().enumerate() {for (i, wxi) in wx.iter().enumerate() {let k = wxi * wyj;@@ -503,6 +511,24 @@ mod tests {Surface::new(PixelFormat::RGBA8)}+ /// Photoshop 27.11's Free Transform, Bicubic, 200 % about the centre of a 64 px image whose left+ /// half is grey 64 and right half grey 192: the row across the edge, columns 26..36.+ #[test]+ fn bicubic_upscale_matches_photoshop() {+ let mut src = rgba();+ let g = |v: f32| v / 255.0;+ src.fill_rect(Rect::new(0, 0, 32, 64), &[g(64.0), g(64.0), g(64.0), 1.0]);+ src.fill_rect(Rect::new(32, 0, 64, 64), &[g(192.0), g(192.0), g(192.0), 1.0]);+ let q = [[-32.0, -32.0], [96.0, -32.0], [96.0, 96.0], [-32.0, 96.0]];+ let h = Homography::rect_to_quad([0.0, 0.0, 64.0, 64.0], q).unwrap();+ let out = warp_surface(&src, Rect::new(0, 0, 64, 64), &h, Interp::Bicubic).unwrap();+ let photoshop = [64.0, 64.0, 64.0, 60.0, 51.0, 93.0, 163.0, 206.0, 197.0, 192.0];+ for (i, want) in photoshop.iter().enumerate() {+ let got = out.pixel(26 + i as i32, 50)[0] * 255.0;+ assert!((got - want).abs() <= 1.5, "column {}: {got} vs {want}", 26 + i);+ }+ }+#[test]fn homography_maps_rect_corners_and_inverts() {let r = [10.0, 20.0, 110.0, 70.0];diff --git a/crates/algo/src/warp.rs b/crates/algo/src/warp.rsindex 2ee8b191c..01c54e764 100644--- a/crates/algo/src/warp.rs+++ b/crates/algo/src/warp.rs@@ -4,7 +4,7 @@//! The source rectangle is subdivided into a fine grid whose vertices are mapped forward; each//! cell's two triangles are rasterized in destination space with the source position//! interpolated barycentrically, then the source is sampled there (premultiplied, nearest /-//! bilinear / Catmull-Rom bicubic, like Free Transform). Cells are a few source pixels across, so+//! bilinear / Photoshop's Keys bicubic, like Free Transform). Cells are a few source pixels across, so//! the piecewise-affine inverse is within a small fraction of a pixel of 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@@ -15,13 +15,7 @@ use photocraft_color::PixelFormat;use photocraft_geom::Rect;use photocraft_raster::Surface;-use crate::transform::Interp;--fn catmull_rom(t: f64) -> [f64; 4] {- let t2 = t * t;- let t3 = t2 * t;- [-0.5 * t3 + t2 - 0.5 * t, 1.5 * t3 - 2.5 * t2 + 1.0, -1.5 * t3 + 2.0 * t2 + 0.5 * t, 0.5 * t3 - 0.5 * t2]-}+use crate::transform::{Interp, bicubic};/// Grid cell size (source px) used to approximate the map.const CELL: f64 = 4.0;@@ -290,7 +284,7 @@ pub fn warp_triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2let snap = |t: f64| if (t - t.round()).abs() < 1e-7 { t.round() } else { t };let (u, v) = (snap(u), snap(v));let (ix, iy) = (u.floor() as i32, v.floor() as i32);- let (wx, wy) = (catmull_rom(u - f64::from(ix)), catmull_rom(v - f64::from(iy)));+ let (wx, wy) = (bicubic(u - f64::from(ix)), bicubic(v - f64::from(iy)));for (j, wyj) in wy.iter().enumerate() {for (i, wxi) in wx.iter().enumerate() {let k = wxi * wyj;