Merge pull request #7719 from davidmann4/fix/cas-background-selection

Content-Aware Scale works on the Background with a selection (#7717)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-06 17:28:41 +01:00
commit af99f69602
5 files changed
+97 -8

No files matched your search

@@ -112,6 +112,7 @@ pub(super) fn content_aware_scale(s: &mut Session, p: &Value) -> Result<Value> {
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(
@@ -128,8 +129,11 @@ pub(super) fn content_aware_scale(s: &mut Session, p: &Value) -> Result<Value> {
},
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(())
@@ -14,6 +14,10 @@ fn px(s: &Session, x: i32, y: i32) -> Vec<f32> {
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()
@@ -545,6 +549,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);
+22 -3
View File
@@ -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"
)
@@ -218,6 +219,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]
+20
View File
@@ -295,4 +295,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");
}
}
}
@@ -315,11 +315,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()));