PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Sample every visible layer in Content-Aware Fill by default. (#4133)
Vikram Bhaskaran964f9fb9d09c
diff --git a/crates/engine/src/edit_menu_cmds.rs b/crates/engine/src/edit_menu_cmds.rsindex 734f7a1b5..f798b6e4a 100644--- a/crates/engine/src/edit_menu_cmds.rs+++ b/crates/engine/src/edit_menu_cmds.rs@@ -391,6 +391,13 @@ fn content_aware_fill_as(s: &mut Session, p: &Value, cmd: &'static str, label: &}let surf = doc.layer(id).and_then(|l| l.surface()).ok_or(EngineError::NoLayer(id))?;let fmt = surf.format();+ // Photoshop's Sample All Layers checkbox is off unless the caller asks. When the active+ // layer has no pixels in the window there is nothing to sample, so that case uses the+ // visible composite (#4026). An explicit false still reads only this layer.+ let sample_all = match p.get("sampleAllLayers").and_then(Value::as_bool) {+ Some(v) => v,+ None => surf.content_bounds().intersect(&window).is_empty(),+ };let n = fmt.channels();// Refuse a window too large to read before allocating for it, whatever the sampling (#963).let (w, h) = crate::fill_cmds::window_size(window)?;@@ -407,7 +414,18 @@ fn content_aware_fill_as(s: &mut Session, p: &Value, cmd: &'static str, label: &ctx.progress(0.0, label);let cancel = || ctx.cancelled();let surf = doc.layer(id).and_then(|l| l.surface()).ok_or(EngineError::NoLayer(id))?;- let img = read_window(surf, window, &cancel).ok_or(EngineError::Cancelled)?;+ let img = if sample_all {+ if cancel() {+ return Err(EngineError::Cancelled);+ }+ let region = crate::retouch_cmds::composite_region(&doc, None, crate::retouch_cmds::SampleLayers::All, window, fmt);+ if cancel() {+ return Err(EngineError::Cancelled);+ }+ region.data+ } else {+ read_window(surf, window, &cancel).ok_or(EngineError::Cancelled)?+ };let hole = read_window_cover(&sel, window, &cancel).ok_or(EngineError::Cancelled)?;let mut source = match (sampling.as_str(), custom_rect, custom_mask.as_ref()) {("custom", Some(r), _) => (0..w * h).into_par_iter().map(|i| r.contains(window.x0 + (i % w) as i32, window.y0 + (i / w) as i32)).collect(),@@ -1139,7 +1157,7 @@ pub fn specs() -> Vec<CommandSpec> {"Content-Aware Fill…",["Edit"],None,- r##"{"sampling":"auto|rectangular|custom","margin":px?,"area":[x,y,w,h]?,"channel":index|name?,"colorAdaptation":"default|none|high|veryHigh","rotationAdaptation":"none|low|medium|high|full","scale":bool=false,"mirror":bool=false,"output":"current|new|duplicate","seed":u64=1}"##,+ r##"{"sampling":"auto|rectangular|custom","margin":px?,"area":[x,y,w,h]?,"channel":index|name?,"colorAdaptation":"default|none|high|veryHigh","rotationAdaptation":"none|low|medium|high|full","scale":bool=false,"mirror":bool=false,"sampleAllLayers":bool=false,"output":"current|new|duplicate","seed":u64=1}"##,can_caf,content_aware_fill),diff --git a/crates/engine/src/edit_menu_cmds/tests.rs b/crates/engine/src/edit_menu_cmds/tests.rsindex 1bede8725..624a23620 100644--- a/crates/engine/src/edit_menu_cmds/tests.rs+++ b/crates/engine/src/edit_menu_cmds/tests.rs@@ -218,6 +218,38 @@ fn delete_and_fill_selection_removes_the_object_in_one_step() {assert!(s.execute("edit.deleteAndFillSelection", json!({})).is_err());}+#[test]+fn content_aware_fill_on_an_empty_layer_samples_the_composite() {+ let mut s = blob_session(8);+ s.execute("layer.new.layer", json!({})).unwrap();+ let empty = s.active().unwrap().active_layer.unwrap();+ assert_eq!(px(&s, 30, 20)[3], 0.0, "the new layer has no paint");+ let r = s.execute("edit.contentAwareFill", json!({"colorAdaptation": "none"})).unwrap();+ assert!(r["filled"].as_u64().unwrap() > 0);+ assert_eq!(s.active().unwrap().active_layer, Some(empty));+ assert!(px(&s, 30, 20)[3] > 0.5, "the hole is filled from the layers underneath");+ s.undo();+ let missed = s.execute("edit.contentAwareFill", json!({"sampleAllLayers": false, "colorAdaptation": "none"})).unwrap();+ assert!(missed["filled"].as_u64().unwrap() > 0);+ assert!(px(&s, 30, 20)[3] < 0.05, "sampling only the empty layer leaves it empty");+}++#[test]+fn content_aware_fill_on_an_empty_cmyk_layer_uses_the_layer_format() {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 48, "height": 36, "mode": "cmyk", "background": "white"})).unwrap();+ s.execute("select.rect", json!({"x": 16, "y": 12, "width": 12, "height": 10})).unwrap();+ s.execute("layer.new.layer", json!({})).unwrap();+ let before = px(&s, 20, 16);+ assert!(before.iter().all(|c| *c < 0.05), "the new CMYK layer is empty: {before:?}");+ s.execute("edit.contentAwareFill", json!({"colorAdaptation": "none"})).unwrap();+ let v = px(&s, 20, 16);+ assert_eq!(v.len(), before.len());+ // White in the layer's CMYK is near zero ink with a covered alpha, not the compositor's RGBA.+ assert!(v[..4].iter().all(|c| *c < 0.15), "CMYK ink stayed low: {v:?}");+ assert!(v.last().copied().unwrap_or(0.0) > 0.5, "the hole is covered: {v:?}");+}+#[test]fn content_aware_fill_samples_photoshops_window() {// A 50 px selection samples the 200 px square centred on it (Photoshop 25.4's overlay);diff --git a/crates/engine/src/retouch_cmds.rs b/crates/engine/src/retouch_cmds.rsindex 315a8d392..c25608bb2 100644--- a/crates/engine/src/retouch_cmds.rs+++ b/crates/engine/src/retouch_cmds.rs@@ -146,7 +146,7 @@ fn damage_json(r: Rect) -> Value {/// Which pixels Clone Stamp / Healing Brush sample.#[derive(Clone, Copy, Debug, PartialEq, Eq)]-enum SampleLayers {+pub(crate) enum SampleLayers {Current,CurrentAndBelow,All,@@ -163,7 +163,7 @@ fn sample_layers(p: &Value, cmd: &str) -> Result<SampleLayers> {/// Composite pixels of `rect` in `fmt` (native channels): the whole document, or the target layer/// and everything below it.-fn composite_region(pre: &Document, id: Option<LayerId>, which: SampleLayers, rect: Rect, fmt: PixelFormat) -> Region {+pub(crate) fn composite_region(pre: &Document, id: Option<LayerId>, which: SampleLayers, rect: Rect, fmt: PixelFormat) -> Region {let mut doc = pre.clone();if which == SampleLayers::CurrentAndBelow&& let Some(id) = id