diff --git a/crates/engine-core/src/cmd/warp_cmds.rs b/crates/engine-core/src/cmd/warp_cmds.rs index a14ff6f0a6..f544eb75be 100644 --- a/crates/engine-core/src/cmd/warp_cmds.rs +++ b/crates/engine-core/src/cmd/warp_cmds.rs @@ -286,6 +286,11 @@ fn warp_layer( } _ => { if let Some(surf) = l.surface_mut() { + // The hard outline is measured on 8 and 16 bit RGB; 32 bit and other modes keep + // the antialiased one. + let fmt = surf.format(); + let measured = fmt.mode == photocraft_color::ColorMode::Rgb && fmt.sample != photocraft_color::SampleType::F32; + let outline = if measured { outline } else { Outline::Antialiased }; *surf = match doc_sel { Some(sel) => { let (lifted, mut rest) = crate::transform_cmds::split_selected(surf, sel)?; @@ -297,7 +302,6 @@ fn warp_layer( } None => { // Content outside the warp box stays where it is. - let fmt = surf.format(); let warped = warp_mesh_surface_outline(surf, rect, &map, Fold::IN_PLACE, interp, outline, ctl).ok_or(EngineError::Cancelled)?; cancelled(ctl)?; // Tiles are shared, so a surface that has alpha already costs nothing to copy. diff --git a/crates/engine-core/src/cmd/warp_pref_hard_edge_tests.rs b/crates/engine-core/src/cmd/warp_pref_hard_edge_tests.rs index 4d52274ab7..7ca4da5802 100644 --- a/crates/engine-core/src/cmd/warp_pref_hard_edge_tests.rs +++ b/crates/engine-core/src/cmd/warp_pref_hard_edge_tests.rs @@ -8,8 +8,13 @@ const H: i32 = 300; /// The reference test document: a grey 200x150 layer with black lines every 10 px over white. fn session(pref: &str) -> Session { + session_in(pref, "rgb", 8) +} + +/// [`session`] in another colour mode and depth. +fn session_in(pref: &str, mode: &str, depth: u32) -> Session { let mut s = crate::session(); - s.execute("file.new", json!({"width": W, "height": H})).unwrap(); + s.execute("file.new", json!({"width": W, "height": H, "mode": mode, "depth": depth})).unwrap(); s.execute("layer.new.layer", json!({})).unwrap(); s.edit("fill", |doc, active| { let surface = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); @@ -43,14 +48,30 @@ fn arc() -> Value { /// The layer's RGBA pixels over the canvas after `warp` sampled with `interp` under `pref`. fn warped(pref: &str, warp: Value, interp: &str) -> Vec<[f32; 4]> { - let mut s = session(pref); + warped_in(session(pref), warp, interp) +} + +/// [`warped`] on the document `s`; the pixels padded to four channels. +fn warped_in(mut s: Session, warp: Value, interp: &str) -> Vec<[f32; 4]> { let mut p = warp; p["rect"] = json!([100, 75, 300, 225]); p["interpolation"] = json!(interp); s.execute("edit.transform.warp", p).unwrap(); let st = s.active().unwrap(); let surface = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); - (0..H).flat_map(|y| (0..W).map(move |x| <[f32; 4]>::try_from(surface.pixel(x, y)).unwrap())).collect() + (0..H) + .flat_map(|y| { + (0..W).map(move |x| { + let px = surface.pixel(x, y); + let mut out = [0.0; 4]; + out[3] = px.last().copied().unwrap_or(0.0); + for (o, v) in out.iter_mut().zip(&px[..px.len() - 1]) { + *o = *v; + } + out + }) + }) + .collect() } /// Whether every pixel within 3 px of pixel `o` is opaque. @@ -101,3 +122,13 @@ fn free_transform_nearest_neighbor_stays_hard_whatever_the_preference() { assert_eq!(partial(&warped(pref, arc(), "nearest")), 0, "{pref}: Nearest Neighbor sampling has a hard outline"); } } + +#[test] +fn unmeasured_formats_keep_the_antialiased_outline() { + for (mode, depth) in [("rgb", 32), ("grayscale", 8)] { + let soft = warped_in(session_in("bilinear", mode, depth), arc(), "bilinear"); + let pref_nearest = warped_in(session_in("nearestNeighbor", mode, depth), arc(), "bilinear"); + assert!(partial(&soft) > 0, "{mode} {depth}: the outline is antialiased"); + assert!(pref_nearest == soft, "{mode} {depth}: the Nearest Neighbor preference changes nothing"); + } +}