mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge branch 'bb-epic-crop-mx5bund6' of github.com:storytold/photocraft into bb-epic-crop-mx5bund6--17674-golden-spiral-smooth
This commit is contained in:
commit
aaaa210e94
2 files changed
+97
-1
No files matched your search
@@ -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<f32> {
|
||||
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<f32> = 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}");
|
||||
}
|
||||
@@ -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;
|
||||
Reference in new issue
Block a user