From 141a83579b44f55c177cff6951c8debc2d59ff69 Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 2 Dec 2026 04:37:04 +0100 Subject: [PATCH 1/2] WIP: Crop: fill a Bitmap document's border with black or white (#17679) From d22dc459bef7ab23cfd3b39db6bcd9b8cb1d8e29 Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 2 Dec 2026 04:42:02 +0100 Subject: [PATCH 2/2] Crop: fill a Bitmap document's border with black or white (#17679) An expanding crop on a Bitmap document filled the new border with the background colour's grey, a value a Bitmap image cannot hold. The grey is now thresholded at 50 %, as in the reference application: magenta fills black, white fills white. Grayscale keeps the grey. Co-Authored-By: Claude Opus 5.5 --- .../src/cmd/crop_bitmap_fill_tests.rs | 87 +++++++++++++++++++ crates/engine-core/src/cmd/image_cmds.rs | 11 ++- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 crates/engine-core/src/cmd/crop_bitmap_fill_tests.rs diff --git a/crates/engine-core/src/cmd/crop_bitmap_fill_tests.rs b/crates/engine-core/src/cmd/crop_bitmap_fill_tests.rs new file mode 100644 index 0000000000..25e9c31ef8 --- /dev/null +++ b/crates/engine-core/src/cmd/crop_bitmap_fill_tests.rs @@ -0,0 +1,87 @@ +//! The border an expanding crop adds to a Bitmap document, measured against the reference application (#17679). +//! +//! A Bitmap image holds only black and white: the background colour's grey is thresholded at 50 %, so magenta +//! fills black and white fills white. Grayscale documents keep the grey itself. + +use super::*; + +/// A 40 x 30 white document with a black block on its Background, converted to Bitmap (50 % threshold) unless +/// `bitmap` is false, with `background` as the background colour. +fn session(bitmap: bool, background: [f32; 4]) -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 40, "height": 30, "mode": "grayscale", "depth": 8, "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(1) { + *alpha = 1.0; + } + surface.fill_rect(Rect::new(5, 5, 20, 15), &px); + Ok(()) + }) + .unwrap(); + if bitmap { + s.execute("image.mode.bitmap", json!({"method": "threshold"})).unwrap(); + assert_eq!(s.active().unwrap().doc.mode, ColorMode::Bitmap); + } + 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]; + +/// Crops 6 px past the left and top edges and 4 px past the right and bottom ones. +fn expand(s: &mut Session, delete: bool) { + s.execute("image.crop", json!({"x": -6, "y": -6, "width": 50, "height": 40, "deleteCroppedPixels": delete})).unwrap(); +} + +/// The grey of every pixel of the Background. +fn greys(s: &Session) -> Vec { + let doc = &s.active().unwrap().doc; + let surface = doc.background().unwrap().surface().unwrap(); + (0..doc.size.height as i32).flat_map(|y| (0..doc.size.width as i32).map(move |x| surface.rgba(x, y)[0])).collect() +} + +/// The grey of a pixel in the added border. +fn border(s: &Session) -> f32 { + s.active().unwrap().doc.background().unwrap().surface().unwrap().rgba(1, 1)[0] +} + +#[test] +fn bitmap_border_is_black_for_a_dark_background_colour_and_white_for_a_light_one() { + for delete in [true, false] { + let mut s = session(true, MAGENTA); + expand(&mut s, delete); + assert_eq!(border(&s), 0.0, "magenta, delete {delete}"); + let mut s = session(true, WHITE); + expand(&mut s, delete); + assert_eq!(border(&s), 1.0, "white, delete {delete}"); + } + // Either side of the threshold. + for (grey, want) in [(0.45, 0.0), (0.55, 1.0)] { + let mut s = session(true, [grey, grey, grey, 1.0]); + expand(&mut s, true); + assert_eq!(border(&s), want, "grey {grey}"); + } +} + +#[test] +fn bitmap_crop_leaves_only_black_and_white_in_one_step() { + let mut s = session(true, MAGENTA); + let before = s.active().unwrap().history.entries().len(); + expand(&mut s, false); + let stray: Vec = greys(&s).into_iter().filter(|g| *g != 0.0 && *g != 1.0).collect(); + assert!(stray.is_empty(), "values other than black and white: {stray:?}"); + let entries = s.active().unwrap().history.entries(); + assert_eq!(entries.len(), before + 1); + assert_eq!(entries.last().map(String::as_str), Some("Crop")); +} + +#[test] +fn grayscale_border_keeps_the_grey() { + let mut s = session(false, MAGENTA); + expand(&mut s, true); + let grey = border(&s); + assert!(grey > 0.0 && grey < 1.0, "a grey, not black or white: {grey}"); +} diff --git a/crates/engine-core/src/cmd/image_cmds.rs b/crates/engine-core/src/cmd/image_cmds.rs index f3ecd7ea99..b6bc3eae7b 100644 --- a/crates/engine-core/src/cmd/image_cmds.rs +++ b/crates/engine-core/src/cmd/image_cmds.rs @@ -598,12 +598,17 @@ pub(crate) fn crop_doc(doc: &mut Document, cmd: &str, r: Rect, delete_pixels: bo doc.size = Size::new(r.width(), r.height()); // A Background cannot hold transparent pixels: like an opaque Canvas Size extension, the area // an extending Crop adds is filled with the extension (background) colour. + let bitmap = doc.mode == ColorMode::Bitmap; if extends && let Some(bg) = doc.background_mut() && let LayerContent::Raster(surf) = &mut bg.content { let old = old_canvas.translate(dx, dy); - let px = photocraft_raster::from_rgba(&surf.format(), [extension[0], extension[1], extension[2], 1.0]); + let mut px = photocraft_raster::from_rgba(&surf.format(), [extension[0], extension[1], extension[2], 1.0]); + // A Bitmap image holds only black and white: the colour's grey is thresholded at 50 % (#17679). + if bitmap && let Some(grey) = px.first_mut() { + *grey = if *grey < 0.5 { 0.0 } else { 1.0 }; + } fill_background_extension(surf, Rect::from_xywh(0, 0, r.width(), r.height()), old, &px); } if extends { @@ -1516,3 +1521,7 @@ mod canvas_size_gray_tests; #[cfg(test)] #[path = "crop_delete_forced_modes_tests.rs"] mod crop_delete_forced_modes_tests; + +#[cfg(test)] +#[path = "crop_bitmap_fill_tests.rs"] +mod crop_bitmap_fill_tests;