From 42f43b3b9a31758d8caf6ca6dc1185ff95672d4c Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 2 Dec 2026 05:19:46 +0100 Subject: [PATCH] Crop: test that a Multichannel crop adds no ink (#17680) A Multichannel document has no layers, so an expanding or turned crop leaves the new area of every ink channel empty (density 0) whatever the background colour. The new test reads the channels at 8 and 16 bit, straight and turned, with Delete Cropped Pixels on and off. Co-Authored-By: Claude Opus 5.5 --- .../src/cmd/crop_multichannel_fill_tests.rs | 69 +++++++++++++++++++ crates/engine-core/src/cmd/image_cmds.rs | 4 ++ 2 files changed, 73 insertions(+) create mode 100644 crates/engine-core/src/cmd/crop_multichannel_fill_tests.rs diff --git a/crates/engine-core/src/cmd/crop_multichannel_fill_tests.rs b/crates/engine-core/src/cmd/crop_multichannel_fill_tests.rs new file mode 100644 index 0000000000..f84f5403e3 --- /dev/null +++ b/crates/engine-core/src/cmd/crop_multichannel_fill_tests.rs @@ -0,0 +1,69 @@ +//! The area an expanding or turned crop adds to a Multichannel document, measured against the reference +//! application (#17680): no ink in every channel (255 there, density 0 here), whatever the background colour. + +use super::*; + +/// A 40 x 30 RGB document at `depth`, black all over (full ink once converted), turned into Multichannel +/// (Cyan, Magenta, Yellow), with `background` as the background colour. +fn session(depth: u32, background: [f32; 4]) -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 40, "height": 30, "mode": "rgb", "depth": depth, "background": "white"})).unwrap(); + s.edit("paint", |doc, _| { + let surface = doc.background_mut().unwrap().surface_mut().unwrap(); + let mut px = vec![0.0; surface.channels()]; + if let Some(alpha) = px.get_mut(3) { + *alpha = 1.0; + } + surface.fill_rect(Rect::new(0, 0, 40, 30), &px); + Ok(()) + }) + .unwrap(); + s.execute("image.mode.multichannel", json!({})).unwrap(); + assert_eq!(s.active().unwrap().doc.mode, ColorMode::Multichannel); + s.tools.background = background; + s +} + +const MAGENTA: [f32; 4] = [1.0, 0.0, 1.0, 1.0]; +const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0]; + +/// The channels' names and their ink density at (`x`, `y`). +fn inks(s: &Session, x: i32, y: i32) -> Vec<(String, f32)> { + s.active().unwrap().doc.channels.iter().map(|c| (c.name.clone(), c.surface.pixel(x, y)[0])).collect() +} + +fn names(s: &Session) -> Vec { + s.active().unwrap().doc.channels.iter().map(|c| c.name.clone()).collect() +} + +#[test] +fn expanding_and_turned_crops_add_no_ink_whatever_the_background_colour() { + for depth in [8, 16] { + for background in [MAGENTA, WHITE] { + for delete in [true, false] { + for angle in [None, Some(15.0)] { + let what = format!("{depth}-bit, background {background:?}, delete {delete}, angle {angle:?}"); + let mut s = session(depth, background); + let before_names = names(&s); + let before = s.active().unwrap().history.entries().len(); + let mut p = json!({"x": -6, "y": -6, "width": 52, "height": 42, "deleteCroppedPixels": delete}); + if let Some(a) = angle { + p["angle"] = json!(a); + } + s.execute("image.crop", p).unwrap(); + // The added corner holds no ink; the old image still holds its ink. + for (name, ink) in inks(&s, 0, 0) { + assert_eq!(ink, 0.0, "{name} in the added corner, {what}"); + } + for (name, ink) in inks(&s, 26, 21) { + assert!(ink > 0.5, "{name} in the old image keeps its ink ({ink}), {what}"); + } + assert_eq!(names(&s), before_names, "{what}"); + let entries = s.active().unwrap().history.entries(); + assert_eq!(entries.len(), before + 1, "{what}"); + assert_eq!(entries.last().map(String::as_str), Some("Crop"), "{what}"); + } + } + } + } +} diff --git a/crates/engine-core/src/cmd/image_cmds.rs b/crates/engine-core/src/cmd/image_cmds.rs index b6bc3eae7b..7894ccedc4 100644 --- a/crates/engine-core/src/cmd/image_cmds.rs +++ b/crates/engine-core/src/cmd/image_cmds.rs @@ -1525,3 +1525,7 @@ mod crop_delete_forced_modes_tests; #[cfg(test)] #[path = "crop_bitmap_fill_tests.rs"] mod crop_bitmap_fill_tests; + +#[cfg(test)] +#[path = "crop_multichannel_fill_tests.rs"] +mod crop_multichannel_fill_tests;