Warp: keep the interpolation choice outside RGB at 8 and 16 bit (#17577)

The smooth warp kernel was measured on RGB at 8 and 16 bits only. Other
modes and 32 bits keep the chosen kernel, on pixel layers and smart
objects alike; each layer's own pixel format decides, also inside a
group.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-11-30 07:52:00 +01:00
1 parent 4e5deed971
commit 5d734ae5c8
4 files changed
+46 -19

No files matched your search

+12 -9
View File
@@ -16,7 +16,7 @@
//! 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_color::{ColorMode, PixelFormat, SampleType};
use photocraft_geom::{Rect, TileCoord};
use photocraft_raster::{Interrupt, Surface, Tile};
use std::sync::Arc;
@@ -50,14 +50,17 @@ 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 {
/// The kernel Edit › Transform › Warp samples with for an interpolation choice on a surface of
/// format `fmt`. 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). That was measured on RGB
/// at 8 and 16 bits; other modes and 32 bits keep the choice as it is.
pub fn warp_interp(choice: Interp, fmt: PixelFormat) -> Interp {
let measured = fmt.mode == ColorMode::Rgb && matches!(fmt.sample, SampleType::U8 | SampleType::U16);
match choice {
Interp::Nearest => Interp::Nearest,
Interp::Bilinear | Interp::Bicubic => Interp::Bilinear,
_ if measured => Interp::Bilinear,
other => other,
}
}
@@ -80,7 +83,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::Bilinear, &Interrupt::NONE));
return Ok(mesh(src, src_rect, &map, Fold::Stack(&rest), warp_interp(Interp::Bicubic, src.format()), &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()) {
@@ -103,7 +106,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::Bilinear, &Interrupt::NONE));
return Ok(mesh(src, src_rect, &map, Fold::Stack(&rest), warp_interp(Interp::Bicubic, src.format()), &Interrupt::NONE));
}
crate::transform::warp_surface(src, src_rect, h, Interp::Bicubic)
}
+28 -8
View File
@@ -11,15 +11,20 @@ const SMOOTH: [&str; 5] = ["bicubic", "bicubicSharper", "bicubicSmoother", "bicu
/// 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) {
grid_in(ColorMode::Rgb, depth)
}
fn grid_in(mode: ColorMode, 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]);
let mut s = Surface::new(PixelFormat::new(mode, depth, true));
let n = mode.color_channels();
let px = |v: f32| (0..=n).map(|c| if c == n { 1.0 } else { v }).collect::<Vec<f32>>();
s.fill_rect(r, &px(128.0 / 255.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]);
s.fill_rect(Rect::new(r.x0 + i * 10, r.y0, r.x0 + i * 10 + 1, r.y1), &px(0.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.fill_rect(Rect::new(r.x0, r.y0 + i * 10, r.x1, r.y0 + i * 10 + 1), &px(0.0));
}
(s, r)
}
@@ -29,7 +34,7 @@ fn arc(r: Rect) -> Warp {
}
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()
warp_mesh_surface_with(src, r, &|x, y| w.map(x, y), Fold::IN_PLACE, warp_interp(Interp::parse(choice), src.format()), &Interrupt::NONE).unwrap()
}
/// The brightest colour channel of the opaque pixels, in 8-bit levels.
@@ -39,8 +44,9 @@ fn interior_max(s: &Surface) -> f32 {
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]);
let (alpha, colour) = p.split_last().unwrap_or((&0.0, &[]));
if *alpha >= 1.0 {
max = colour.iter().fold(max, |m, &c| m.max(c));
}
}
}
@@ -110,3 +116,17 @@ fn a_smart_object_warp_does_not_ring_under_any_placement() {
let max = interior_max(&place_source_projective(&src, r, &h, Some(&w)).unwrap());
assert!(max <= 128.5, "distorted placement: interior max {max}");
}
#[test]
fn modes_and_depths_not_measured_keep_their_bicubic() {
for (mode, depth) in [(ColorMode::Grayscale, SampleType::U8), (ColorMode::Rgb, SampleType::F32), (ColorMode::Cmyk, SampleType::U16)] {
let (src, r) = grid_in(mode, depth);
let w = arc(r);
let bicubic = warp(&src, r, &w, "bicubic");
let max = interior_max(&bicubic);
assert!(max > 129.0, "{mode:?} {depth:?}: Bicubic still overshoots, interior max {max}");
let smart = place_source(&src, r, &Affine { m: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0] }, Some(&w)).unwrap();
let max = interior_max(&smart);
assert!(max > 129.0, "{mode:?} {depth:?}: a smart-object warp keeps today's bicubic, interior max {max}");
}
}
+3 -1
View File
@@ -240,6 +240,8 @@ fn warp_layer(
if locks.position || locks.all {
return Err(EngineError::Other(format!("layer \"{}\" is locked", l.name)));
}
// The layer's own pixels decide the kernel (#17577); a group passes the choice on to each child.
let interp = l.surface().map_or(interp, |s| photocraft_algo::warp::warp_interp(interp, s.format()));
let map = |x: f64, y: f64| w.map(x, y);
// A selection limits the linked mask exactly as it limits the pixels (#4175, as Free Transform
// and Perspective Warp do, #3083); a layer that moves whole takes its whole mask along.
@@ -501,7 +503,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 = photocraft_algo::warp::warp_interp(Interp::parse(p.get("interpolation").and_then(Value::as_str).unwrap_or("bicubic")));
let 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.
@@ -11,7 +11,9 @@ 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, Fold::IN_PLACE, Interp::Bicubic, &Interrupt::NONE).unwrap();
// With the kernel the layer's format gives the warp (#17577).
let interp = photocraft_algo::warp::warp_interp(Interp::Bicubic, surf.format());
let warped = warp_mesh_surface_with(&surf, rect, &map, Fold::IN_PLACE, interp, &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);