From ab033734d22dbb7730e263dc4849441b4f6222ed Mon Sep 17 00:00:00 2001 From: SBaksa <18648812+SBaksa@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:09:45 +1100 Subject: [PATCH] Photomerge: merging the open documents no longer scans the dialog's folder (#13580) With Use open documents ticked the dialog still sends its prefilled folder, and the engine listed and loaded it on top of the open documents, so an unreadable or huge folder failed or exhausted memory. A folder string is now ignored when useOpenDocuments is true (explicit file lists still add). luma_alpha also tolerates a short row read instead of indexing past it. --- crates/engine/src/cmd/photo_cmds.rs | 10 ++++++--- crates/engine/src/cmd/photo_cmds/tests.rs | 26 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/photo_cmds.rs b/crates/engine/src/cmd/photo_cmds.rs index e9a98c6184..a721db97d9 100644 --- a/crates/engine/src/cmd/photo_cmds.rs +++ b/crates/engine/src/cmd/photo_cmds.rs @@ -303,12 +303,15 @@ impl Sources { fn load_sources(s: &Session, p: &Value, cmd: &str, budget: Option) -> Result { let mut src = Sources::default(); let mut load = SourceLoad::new(budget); - if p.get("useOpenDocuments").and_then(Value::as_bool).unwrap_or(false) { + // "Use open documents" merges exactly the open documents; the dialog also sends its prefilled + // folder (a string), which is not scanned then (#13580). Explicit file lists still add on top. + let open = p.get("useOpenDocuments").and_then(Value::as_bool).unwrap_or(false); + if open { for d in s.documents() { src.add(&mut load, d.doc.name.clone(), &d.doc, 0, cmd)?; } } - let paths: Vec = match p.get("paths").or_else(|| p.get("input")) { + let paths: Vec = match p.get("paths").or_else(|| p.get("input")).filter(|v| !(open && v.is_string())) { Some(Value::Array(a)) => a.iter().filter_map(Value::as_str).map(str::to_string).collect(), Some(Value::String(dir)) => list_images(dir)?, _ => Vec::new(), @@ -335,7 +338,8 @@ pub(crate) fn luma_alpha(surf: &Surface, area: Rect, k: usize) -> (usize, usize, for y in 0..h { let row = surf.read_region(Rect::new(area.x0, area.y0 + y as i32, area.x1, area.y0 + y as i32 + 1)); for x in 0..w { - let p = to_rgba(&fmt, &row[x * n..(x + 1) * n]); + let Some(px) = row.get(x * n..(x + 1) * n) else { break }; + let p = to_rgba(&fmt, px); let i = (y / k) * sw + x / k; l[i] += (0.2126 * p[0] + 0.7152 * p[1] + 0.0722 * p[2]) * p[3] + (1.0 - p[3]) * 0.0; a[i] += p[3]; diff --git a/crates/engine/src/cmd/photo_cmds/tests.rs b/crates/engine/src/cmd/photo_cmds/tests.rs index a161099d46..d00399d6fe 100644 --- a/crates/engine/src/cmd/photo_cmds/tests.rs +++ b/crates/engine/src/cmd/photo_cmds/tests.rs @@ -628,3 +628,29 @@ fn merge_to_hdr_pro_accepts_sources_without_the_photomerge_budget() { assert_eq!(srcs.len(), 2); assert_eq!(fmt, PixelFormat::RGBA8); } + +/// Ticking "Use open documents" merges the open documents only: the dialog also sends its +/// prefilled folder, which must not be scanned (#13580). +#[test] +fn photomerge_open_documents_ignores_the_folder() { + let (sw, sh) = (520, 220); + let sc = scene(sw, sh, 5); + let open_two = |sc: &[[f32; 4]]| { + let mut s = Session::new(); + for (k, x0) in [0usize, 180].into_iter().enumerate() { + s.add_document(doc_from(&format!("o{k}"), &crop(sc, sw, x0, 10, 260, 200, 1.0), 260, 200, SampleType::U8), None); + } + s + }; + for paths in [json!("."), json!("/no/such/folder"), json!("")] { + let mut s = open_two(&sc); + let r = s.execute("file.automate.photomerge", json!({"useOpenDocuments": true, "layout": "reposition", "paths": paths})); + let r = r.unwrap_or_else(|e| panic!("{paths}: {e}")); + assert_eq!(r["placed"].as_array().map(Vec::len), Some(2), "{paths}: {r}"); + } + // Hostile shapes are errors or successes, never panics. + let mut s = open_two(&sc); + for paths in [json!(7), json!(null), json!([1, 2])] { + let _ = s.execute("file.automate.photomerge", json!({"useOpenDocuments": true, "layout": "reposition", "paths": paths})); + } +}