Warp: use one non-ringing kernel for every smooth interpolation (#17577)

Edit > Transform > Warp maps Bilinear and every Bicubic choice to the
bilinear mesh kernel (warp_interp); Nearest Neighbor stays. Smart-object
warps render with the same kernel. Free Transform without warp keeps its
kernels.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-11-30 06:17:35 +01:00
1 parent 4c8b12c5fa
commit 6d2b2fe148
3 files changed
+134 -5

No files matched your search

+21 -4
View File
@@ -4,7 +4,8 @@
//! 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 / Photoshop's Keys bicubic, like Free Transform). Cells are a few pixels across in
//! bilinear / Keys bicubic; Edit › Transform › Warp and smart objects pick theirs with
//! [`warp_interp`]). Cells are a few pixels across in
//! both the source and the destination (a thin layer stretched far gets as many cells as its
//! warped size needs), so the piecewise-affine inverse is within a small fraction of a pixel of
//! the true one; affine
@@ -49,10 +50,22 @@ const MAX_SS: f64 = 8.0;
/// Bounding-box pixels scanned per destination pixel of a tile, across all its triangles.
const MAX_SCAN_PER_PIXEL: usize = 256;
/// The kernel Edit › Transform › Warp samples with for an interpolation choice. The reference
/// application's Warp knows two: Nearest Neighbor, and one smooth kernel that does not ring next
/// to an edge, which Bilinear and every Bicubic give alike (Free Transform without warp honours
/// each choice and keeps its own kernels).
pub fn warp_interp(choice: Interp) -> Interp {
match choice {
Interp::Nearest => Interp::Nearest,
Interp::Bilinear | Interp::Bicubic => Interp::Bilinear,
}
}
/// Places a smart object's source image (`src`, its pixels in `src_rect`) in the document:
/// through `warp` (source space) and then the affine `t` (source → document) in one resampling
/// pass, an exact shift for whole-pixel translations (so conversions and re-renders are
/// lossless), bicubic otherwise. Shared by the engine's re-render and PSD export's filter cache.
/// lossless), bicubic otherwise. A warp ignores the interpolation choice and always samples with
/// the smooth kernel of [`warp_interp`], as the reference application's smart-object warps do. Shared by the engine's re-render and PSD export's filter cache.
pub fn place_source(
src: &Surface,
src_rect: Rect,
@@ -67,7 +80,7 @@ pub fn place_source(
};
// 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));
return Ok(mesh(src, src_rect, &map, Fold::Stack(&rest), Interp::Bilinear, &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()) {
@@ -90,7 +103,7 @@ pub fn place_source_projective(
h.apply(u, v)
};
let rest = |x: f64, y: f64| h.apply(x, y);
return Ok(mesh(src, src_rect, &map, Fold::Stack(&rest), Interp::Bicubic, &Interrupt::NONE));
return Ok(mesh(src, src_rect, &map, Fold::Stack(&rest), Interp::Bilinear, &Interrupt::NONE));
}
crate::transform::warp_surface(src, src_rect, h, Interp::Bicubic)
}
@@ -900,6 +913,10 @@ pub fn warp_mesh_gray_with(s: &Surface, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sy
out
}
#[cfg(test)]
#[path = "warp_smooth_kernel_tests.rs"]
mod smooth_kernel_tests;
#[cfg(test)]
mod tests {
use super::*;
+112
View File
@@ -0,0 +1,112 @@
//! Edit › Transform › Warp and smart-object warps sample with one smooth, non-ringing kernel for
//! every smooth interpolation choice (#17577).
use super::*;
use crate::transform::Homography;
use photocraft_color::{ColorMode, SampleType};
use photocraft_geom::Affine;
use photocraft_geom::warp::{Warp, WarpStyle};
const SMOOTH: [&str; 5] = ["bicubic", "bicubicSharper", "bicubicSmoother", "bicubicAutomatic", "bilinear"];
/// The item's test layer: 200 × 150 px at (100, 75), grey 128 with a 1 px black line every 10 px.
fn grid(depth: SampleType) -> (Surface, Rect) {
let r = Rect::new(100, 75, 300, 225);
let mut s = Surface::new(PixelFormat::new(ColorMode::Rgb, depth, true));
let grey = 128.0 / 255.0;
s.fill_rect(r, &[grey, grey, grey, 1.0]);
for i in 0..20 {
s.fill_rect(Rect::new(r.x0 + i * 10, r.y0, r.x0 + i * 10 + 1, r.y1), &[0.0, 0.0, 0.0, 1.0]);
}
for i in 0..15 {
s.fill_rect(Rect::new(r.x0, r.y0 + i * 10, r.x1, r.y0 + i * 10 + 1), &[0.0, 0.0, 0.0, 1.0]);
}
(s, r)
}
fn arc(r: Rect) -> Warp {
Warp::preset(WarpStyle::Arc, 50.0, [f64::from(r.x0), f64::from(r.y0), f64::from(r.x1), f64::from(r.y1)])
}
fn warp(src: &Surface, r: Rect, w: &Warp, choice: &str) -> Surface {
warp_mesh_surface_with(src, r, &|x, y| w.map(x, y), Fold::IN_PLACE, warp_interp(Interp::parse(choice)), &Interrupt::NONE).unwrap()
}
/// The brightest colour channel of the opaque pixels, in 8-bit levels.
fn interior_max(s: &Surface) -> f32 {
let b = s.content_bounds();
let mut max = 0.0f32;
for y in b.y0..b.y1 {
for x in b.x0..b.x1 {
let p = s.pixel(x, y);
if p[3] >= 1.0 {
max = max.max(p[0]).max(p[1]).max(p[2]);
}
}
}
max * 255.0
}
#[test]
fn every_smooth_choice_warps_like_bilinear_at_8_and_16_bit() {
for depth in [SampleType::U8, SampleType::U16] {
let (src, r) = grid(depth);
let w = arc(r);
let want = warp(&src, r, &w, "bilinear");
let b = want.content_bounds();
for choice in SMOOTH {
let got = warp(&src, r, &w, choice);
assert_eq!(got.content_bounds(), b, "{depth:?} {choice}");
assert!(got.read_region(b) == want.read_region(b), "{depth:?} {choice}: not the bilinear pixels");
}
}
}
#[test]
fn arc_50_on_the_grid_never_rises_above_its_grey() {
for depth in [SampleType::U8, SampleType::U16] {
let (src, r) = grid(depth);
for choice in SMOOTH {
let max = interior_max(&warp(&src, r, &arc(r), choice));
assert!(max <= 128.5, "{depth:?} {choice}: interior max {max}");
}
}
}
#[test]
fn nearest_neighbor_keeps_only_the_source_values() {
let (src, r) = grid(SampleType::U8);
let out = warp(&src, r, &arc(r), "nearestNeighbor");
let b = out.content_bounds();
assert!(b.y0 < r.y0, "the arc moved the layer: {b:?}");
for y in b.y0..b.y1 {
for x in b.x0..b.x1 {
let p = out.pixel(x, y);
if p[3] > 0.0 {
let level = (p[0] * 255.0).round();
assert!(level == 0.0 || level == 128.0, "({x}, {y}): {p:?}");
}
}
}
}
#[test]
fn a_smart_object_warp_does_not_ring_under_any_placement() {
let (src, r) = grid(SampleType::U8);
let w = arc(r);
let shifted = Affine { m: [1.0, 0.0, 0.0, 1.0, 12.0, -7.0] };
let turned = Affine { m: [0.9986, 0.0523, -0.0523, 0.9986, 8.0, -9.0] };
for t in [shifted, turned] {
let out = place_source(&src, r, &t, Some(&w)).unwrap();
assert!(!out.content_bounds().is_empty());
let max = interior_max(&out);
assert!(max <= 128.5, "{:?}: interior max {max}", t.m);
}
let h = Homography::rect_to_quad(
[f64::from(r.x0), f64::from(r.y0), f64::from(r.x1), f64::from(r.y1)],
[[100.0, 80.0], [310.0, 70.0], [300.0, 230.0], [95.0, 220.0]],
)
.unwrap();
let max = interior_max(&place_source_projective(&src, r, &h, Some(&w)).unwrap());
assert!(max <= 128.5, "distorted placement: interior max {max}");
}
+1 -1
View File
@@ -501,7 +501,7 @@ fn apply(s: &mut Session, p: &Value, cmd: &str) -> Result<Value> {
if p.get("mesh").is_none() && s.active().and_then(|d| d.doc.layer(id)).is_some_and(|l| is_stored_cylinder(l, &w)) {
return Ok(json!({"layer": id.0, "changed": false}));
}
let interp = Interp::parse(p.get("interpolation").and_then(Value::as_str).unwrap_or("bicubic"));
let interp = photocraft_algo::warp::warp_interp(Interp::parse(p.get("interpolation").and_then(Value::as_str).unwrap_or("bicubic")));
let content_knots = p.get("mesh").is_some() && p.get("warp").is_none();
// A background job when started with `Session::start` (as Free Transform, #4218): the warp of
// a large layer runs on a worker with progress, and can be cancelled.