From 2d73f62b0f83fb2330751906f1f034d95e9ab7a8 Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 19 Oct 2026 13:15:47 +0200 Subject: [PATCH 1/2] Content-Aware Scale works on the Background with a selection (#7717) Photoshop treats Content-Aware Scale like Free Transform: on the Background it is unavailable without a selection and scales the selected pixels with one. background_blocks greyed it unconditionally; it now shares Free Transform's no-selection rule. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/enable_rules.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/enable_rules.rs b/crates/ui-egui/src/enable_rules.rs index e3712c1a9a..d166dbb8da 100644 --- a/crates/ui-egui/src/enable_rules.rs +++ b/crates/ui-egui/src/enable_rules.rs @@ -16,8 +16,10 @@ fn background_blocks(id: &str, has_selection: bool) -> bool { // Global Light and Hide All Effects are document-wide. return !matches!(id, "layer.layerStyle.globalLight" | "layer.layerStyle.hideAllEffects" | "layer.layerStyle.copyLayerStyle"); } - // Transforms need a selection on the Background (they then move the selected pixels). - if !has_selection && (id == "edit.freeTransform" || (id.starts_with("edit.transform.") && id != "edit.transform.again")) { + // Transforms and Content-Aware Scale need a selection on the Background (they then move the + // selected pixels). + if !has_selection && (matches!(id, "edit.freeTransform" | "edit.contentAwareScale") || (id.starts_with("edit.transform.") && id != "edit.transform.again")) + { return true; } matches!( @@ -27,7 +29,6 @@ fn background_blocks(id: &str, has_selection: bool) -> bool { | "layer.mergeDown" | "layer.lockLayers" | "layer.maskAllObjects" - | "edit.contentAwareScale" | "edit.puppetWarp" | "edit.perspectiveWarp" ) @@ -198,6 +199,24 @@ mod tests { assert!(!disabled(&app, "edit.freeTransform") && !disabled(&app, "edit.transform.flipHorizontal")); } + /// Photoshop treats Content-Aware Scale like Free Transform: on the Background it needs a + /// selection, and then scales the selected pixels. + #[test] + fn content_aware_scale_on_the_background_needs_a_selection() { + let mut s = session(); + assert!(off(&s, "edit.contentAwareScale")); + s.execute("select.all", json!({})).unwrap(); + assert!(!off(&s, "edit.contentAwareScale")); + let mut app = crate::PhotocraftApp::new(s, crate::Services::default()); + assert!(!disabled(&app, "edit.contentAwareScale")); + let r = app.session.execute("edit.contentAwareScale", json!({"scaleX": 70})).unwrap(); + assert_eq!(r["to"], json!([14, 20])); + // A normal layer takes it without a selection. + app.session.execute("select.deselect", json!({})).unwrap(); + app.session.execute("layer.new.layer", json!({})).unwrap(); + assert!(!disabled(&app, "edit.contentAwareScale")); + } + /// With several layers selected, Rasterize is judged by the whole selection, not by the layer /// that happens to be active (or was right-clicked). #[test] From 937ee1230028b803ad172d5a48f83da4a07c5e1a Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 21 Oct 2026 11:01:19 +0200 Subject: [PATCH 2/2] Address review: Content-Aware Scale on the Background vacates to the background colour Shrinking a selection's Content-Aware Scale on the Background left the vacated area transparent. It now goes through Delete's `clear_area` (background colour on the Background, transparent elsewhere), as Photoshop does; the transform box's live preview shows the same. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/engine/src/edit_menu_cmds.rs | 6 +++- crates/engine/src/edit_menu_cmds/tests.rs | 36 +++++++++++++++++++++++ crates/ui-egui/src/cas_transform.rs | 20 +++++++++++++ crates/ui-egui/src/transform_tool.rs | 18 +++++++++--- 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/edit_menu_cmds.rs b/crates/engine/src/edit_menu_cmds.rs index b620230a61..cd7aa76fd9 100644 --- a/crates/engine/src/edit_menu_cmds.rs +++ b/crates/engine/src/edit_menu_cmds.rs @@ -957,6 +957,7 @@ fn content_aware_scale(s: &mut Session, p: &Value) -> Result { let (w, h) = (region.width() as usize, region.height() as usize); let (nw, nh) = (dst.width() as usize, dst.height() as usize); let label = "Content-Aware Scale"; + let background = s.tools.background; // A background job when started with `Session::start` (#210): seam carving runs on a worker // against the document snapshot and checks for cancellation before every seam. crate::jobs::run( @@ -973,8 +974,11 @@ fn content_aware_scale(s: &mut Session, p: &Value) -> Result { }, move |s, out| { s.edit(label, |doc, _| { + // The scaled area is vacated as Delete leaves it: transparent, or on the Background, + // which can't hold transparency, the background colour (Photoshop). + writable_surface(doc, id)?; + crate::edit_cmds::clear_area(doc, id, region, None, background)?; let surf = writable_surface(doc, id)?; - crate::pixels::try_clear_surface(surf, region, None)?; surf.write_region(dst, &out); surf.prune(); Ok(()) diff --git a/crates/engine/src/edit_menu_cmds/tests.rs b/crates/engine/src/edit_menu_cmds/tests.rs index 16e98b8719..95619687b9 100644 --- a/crates/engine/src/edit_menu_cmds/tests.rs +++ b/crates/engine/src/edit_menu_cmds/tests.rs @@ -12,6 +12,10 @@ fn px(s: &Session, x: i32, y: i32) -> Vec { st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().pixel(x, y) } +fn px_rgba(s: &Session, x: i32, y: i32) -> [f32; 4] { + active(s).surface().unwrap().rgba(x, y) +} + fn active(s: &Session) -> &Layer { let st = s.active().unwrap(); st.doc.layer(st.active_layer.unwrap()).unwrap() @@ -415,6 +419,38 @@ fn content_aware_scale_keeps_subject() { } } +/// On the Background, which can't hold transparency, the area a selection's Content-Aware Scale +/// vacates takes the background colour, as Delete leaves it (Photoshop); the layer stays the +/// Background. On a normal layer it is left transparent. +#[test] +fn content_aware_scale_on_the_background_vacates_to_the_background_colour() { + for depth in [8, 16] { + let mut s = session(depth); + s.tools.background = [0.0, 0.0, 1.0, 1.0]; + let id = s.active().unwrap().active_layer.unwrap(); + s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 10})).unwrap(); + let r = s.execute("edit.contentAwareScale", json!({"width": 10})).unwrap(); + assert_eq!(r["bounds"], json!([10, 10, 10, 10]), "{depth}-bit"); + let st = s.active().unwrap(); + assert!(st.doc.is_background(id), "{depth}-bit: still the Background"); + let px = |x, y| st.doc.layer(id).unwrap().surface().unwrap().rgba(x, y); + for (x, y) in [(20, 10), (25, 15), (29, 19)] { + assert_eq!(px(x, y), [0.0, 0.0, 1.0, 1.0], "{depth}-bit: ({x}, {y}) is vacated"); + } + assert_eq!(px(12, 15), [1.0, 1.0, 1.0, 1.0], "{depth}-bit: the scaled pixels"); + assert_eq!(px(5, 5), [1.0, 1.0, 1.0, 1.0], "{depth}-bit: outside the selection"); + // A normal layer's vacated area is transparent. + let mut s = session(depth); + s.tools.background = [0.0, 0.0, 1.0, 1.0]; + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 10})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("edit.contentAwareScale", json!({"width": 10})).unwrap(); + assert_eq!(px_rgba(&s, 25, 15)[3], 0.0, "{depth}-bit: a layer's hole is transparent"); + assert_eq!(px_rgba(&s, 12, 15), [1.0, 0.0, 0.0, 1.0], "{depth}-bit"); + } +} + #[test] fn content_aware_scale_params() { let mut s = session(8); diff --git a/crates/ui-egui/src/cas_transform.rs b/crates/ui-egui/src/cas_transform.rs index d5b263d6e8..cf578a23f6 100644 --- a/crates/ui-egui/src/cas_transform.rs +++ b/crates/ui-egui/src/cas_transform.rs @@ -273,4 +273,24 @@ mod tests { let b = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds(); assert_eq!([b.x0, b.y0, b.width() as i32, b.height() as i32], [8, 8, 24, 24]); } + + /// On the Background the box's preview shows the vacated area in the background colour, as + /// the command leaves it. + #[test] + fn on_the_background_the_preview_vacates_to_the_background_colour() { + for depth in [8, 16] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + let ctx = egui::Context::default(); + app.run("file.new", json!({"width": 64, "height": 48, "depth": depth, "background": "white"})).unwrap(); + app.session.tools.background = [0.0, 0.0, 1.0, 1.0]; + app.run("select.rect", json!({"x": 8, "y": 8, "width": 40, "height": 24})).unwrap(); + app.sync_views(); + crate::menus::invoke(&mut app, &ctx, COMMAND, json!({})).unwrap(); + let pv = app.transform_preview.as_ref().unwrap(); + let id = app.session.active().unwrap().active_layer.unwrap(); + let px = |x, y| pv.doc.layer(id).unwrap().surface().unwrap().rgba(x, y); + assert_eq!(px(20, 20), [0.0, 0.0, 1.0, 1.0], "{depth}-bit: under the box"); + assert_eq!(px(4, 4), [1.0, 1.0, 1.0, 1.0], "{depth}-bit: outside it"); + } + } } diff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rs index 7ae223029d..9824d35751 100644 --- a/crates/ui-egui/src/transform_tool.rs +++ b/crates/ui-egui/src/transform_tool.rs @@ -333,11 +333,21 @@ pub(crate) fn begin_content_aware( crate::type_tool::commit(app); let rect = [region.x0 as f64, region.y0 as f64, region.x1 as f64, region.y1 as f64]; let session = app.ui.alloc_id(); - // The command clears the area before writing the result (a layer without transparency keeps it). + // The command vacates the area before writing the result, as Delete does: transparent, or on + // the Background the background colour (a layer without transparency otherwise keeps it). + let background = doc.is_background(id).then_some(app.session.tools.background); let mut pd = (*doc).clone(); - if let Some(s) = pd.layer_mut(id).and_then(|l| l.surface_mut()).filter(|s| s.format().alpha) { - let clear = vec![0.0; s.format().channels()]; - s.fill_rect(region, &clear); + if let Some(s) = pd.layer_mut(id).and_then(|l| l.surface_mut()).filter(|s| background.is_some() || s.format().alpha) { + let fmt = s.format(); + let mut fill = vec![0.0; fmt.channels()]; + if let Some([r, g, b, _]) = background { + let mut enc = [0.0f32; 8]; + let n = photocraft_raster::from_rgba_into(&fmt, [r, g, b, 1.0], &mut enc); + if let (Some(dst), Some(src)) = (fill.get_mut(..n), enc.get(..n)) { + dst.copy_from_slice(src); + } + } + s.fill_rect(region, &fill); s.prune(); } let max_side = ctx.input(|i| i.max_texture_side).max(app.gpu.as_ref().map_or(1, |g| g.max_texture_side()));