mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-27 17:27:51 +00:00
Merge pull request #13913 from qvest-ssels/fix/fit-image-drops-selection-13856
Fit Image and Image Size drop the selection when they resample
This commit is contained in:
@@ -318,6 +318,12 @@ fn image_size(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Method::Automatic if nw > ow || nh > oh => Resample::PreserveDetails,
|
||||
Method::Automatic => Resample::BicubicSharper,
|
||||
};
|
||||
// Resampling to a new pixel size drops the selection rather than scaling it, as Photoshop
|
||||
// does (#13856); undo brings it back with the old size. Taken before the resample so it
|
||||
// isn't scaled for nothing. A resolution-only change (returned above) keeps it.
|
||||
if (nw, nh) != (ow, oh) {
|
||||
doc.selection = None;
|
||||
}
|
||||
crate::canvas_geom::keeping_count_points(doc, |doc| resample_doc_with_styles(doc, "image.imageSize", nw, nh, filter, scale_styles))
|
||||
})?;
|
||||
// The size the document has now: without resampling `width`/`height` are ignored (#1815).
|
||||
@@ -1293,3 +1299,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "image_size_drops_selection_tests.rs"]
|
||||
mod image_size_drops_selection_tests;
|
||||
|
||||
@@ -205,8 +205,20 @@ fn image_size_keeps_canvas_edges_opaque() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 30, "height": 20, "depth": depth, "background": "#336699"})).unwrap();
|
||||
s.execute("select.all", json!({})).unwrap();
|
||||
// The Crop tool's W x H x Resolution mode still resamples the selection with the
|
||||
// pixels: a full selection keeps reaching the canvas edges.
|
||||
let mut cropped = (*doc(&s)).clone();
|
||||
let ch = (20 * w + 15) / 30;
|
||||
resample_doc(&mut cropped, "test", w, ch, Resample::Bicubic).unwrap();
|
||||
let sel = cropped.selection.as_ref().unwrap();
|
||||
let (x1, y1) = (cropped.size.width as i32 - 1, cropped.size.height as i32 - 1);
|
||||
for (x, y) in [(0, 0), (x1, 0), (0, y1), (x1, y1)] {
|
||||
assert!(sel.pixel(x, y)[0] > 0.99, "{depth} {resample}: selection at ({x},{y})");
|
||||
}
|
||||
s.execute("image.imageSize", json!({"width": w, "resample": resample})).unwrap();
|
||||
let d = doc(&s);
|
||||
// Image Size drops the selection when it resamples (#13856).
|
||||
assert!(d.selection.is_none(), "{depth} {resample}: Image Size dropped the selection");
|
||||
let flat = photocraft_compose::flatten(d);
|
||||
let (w, h) = (flat.rect.width() as usize, flat.rect.height() as usize);
|
||||
let mid = flat.px[(h / 2) * w + w / 2];
|
||||
@@ -215,11 +227,6 @@ fn image_size_keeps_canvas_edges_opaque() {
|
||||
let off = p.iter().zip(mid).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max);
|
||||
assert!(off < 0.01, "{depth} {resample} {w}: {p:?} vs {mid:?}");
|
||||
}
|
||||
let sel = d.selection.as_ref().unwrap();
|
||||
let (x1, y1) = (d.size.width as i32 - 1, d.size.height as i32 - 1);
|
||||
for (x, y) in [(0, 0), (x1, 0), (0, y1), (x1, y1)] {
|
||||
assert!(sel.pixel(x, y)[0] > 0.99, "{depth} {resample}: selection at ({x},{y})");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
//! #13856: Fit Image and Image Size drop the active selection when they resample to a new pixel
|
||||
//! size, as the reference application does; undo brings back the old size and selection in one
|
||||
//! step. A Fit Image that changes nothing and a resolution-only Image Size keep the selection.
|
||||
|
||||
use photocraft_geom::Rect;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// The issue's document: 400 x 300 px with a 10,10 50 x 50 selection.
|
||||
fn doc() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||||
s.execute("select.rect", json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn size(s: &Session) -> (u32, u32) {
|
||||
let d = &s.active().unwrap().doc;
|
||||
(d.size.width, d.size.height)
|
||||
}
|
||||
|
||||
fn selection(s: &Session) -> Option<Rect> {
|
||||
s.active().unwrap().doc.selection.as_ref().map(|sel| sel.content_bounds())
|
||||
}
|
||||
|
||||
fn steps(s: &Session) -> usize {
|
||||
s.active().unwrap().history.past_len()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fit_image_that_resizes_drops_the_selection_and_undo_restores_it() {
|
||||
let mut s = doc();
|
||||
let before = selection(&s);
|
||||
assert_eq!(before, Some(Rect::from_xywh(10, 10, 50, 50)));
|
||||
let n = steps(&s);
|
||||
let r = s.execute("file.automate.fitImage", json!({"width": 200, "height": 200})).unwrap();
|
||||
assert_eq!(r["changed"], Value::Bool(true));
|
||||
assert_eq!(size(&s), (200, 150));
|
||||
assert_eq!(selection(&s), None, "no selection after Fit Image");
|
||||
assert_eq!(steps(&s), n + 1, "one history step");
|
||||
s.undo();
|
||||
assert_eq!(size(&s), (400, 300), "undo restores the size");
|
||||
assert_eq!(selection(&s), before, "and the selection, in the same step");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_size_with_a_new_pixel_size_drops_the_selection_and_undo_restores_it() {
|
||||
let mut s = doc();
|
||||
let before = selection(&s);
|
||||
let n = steps(&s);
|
||||
s.execute("image.imageSize", json!({"width": 200, "height": 150, "resample": "bicubic"})).unwrap();
|
||||
assert_eq!(size(&s), (200, 150));
|
||||
assert_eq!(selection(&s), None, "no selection after Image Size");
|
||||
assert_eq!(steps(&s), n + 1, "one history step");
|
||||
s.undo();
|
||||
assert_eq!(size(&s), (400, 300));
|
||||
assert_eq!(selection(&s), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fit_image_that_changes_nothing_keeps_the_selection_and_adds_no_step() {
|
||||
for p in [json!({"width": 400, "height": 300}), json!({"width": 800, "height": 800, "dontEnlarge": true})] {
|
||||
let mut s = doc();
|
||||
let before = selection(&s);
|
||||
let n = steps(&s);
|
||||
let r = s.execute("file.automate.fitImage", p.clone()).unwrap();
|
||||
assert_eq!(r["changed"], Value::Bool(false), "{p}");
|
||||
assert_eq!(size(&s), (400, 300), "{p}");
|
||||
assert_eq!(selection(&s), before, "{p}: selection kept");
|
||||
assert_eq!(steps(&s), n, "{p}: no history step");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_size_with_only_a_new_resolution_keeps_the_selection() {
|
||||
let mut s = doc();
|
||||
let before = selection(&s);
|
||||
s.execute("image.imageSize", json!({"resolution": 300, "resample": "none"})).unwrap();
|
||||
assert_eq!(size(&s), (400, 300));
|
||||
assert_eq!(s.active().unwrap().doc.resolution_dpi, 300.0);
|
||||
assert_eq!(selection(&s), before, "a resolution-only change keeps the selection");
|
||||
}
|
||||
Reference in New Issue
Block a user