From 878a2a5f58bbba6c67b43fd023e660207968d986 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Sat, 14 Nov 2026 04:47:12 +0530 Subject: [PATCH 1/2] Photomerge: name the result Untitled_Panorama-N (#14093) The name was "Untitled_Panorama" plus the number of open documents plus one, with no hyphen. Use the existing untitled_name helper instead: the first "Untitled_Panorama-N" no open document has, so "-1" when no panorama is open. --- crates/engine/src/cmd/photo_cmds.rs | 5 +- .../cmd/photo_cmds/photomerge_name_tests.rs | 96 +++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 crates/engine/src/cmd/photo_cmds/photomerge_name_tests.rs diff --git a/crates/engine/src/cmd/photo_cmds.rs b/crates/engine/src/cmd/photo_cmds.rs index e9a98c6184..462a042e25 100644 --- a/crates/engine/src/cmd/photo_cmds.rs +++ b/crates/engine/src/cmd/photo_cmds.rs @@ -881,7 +881,7 @@ fn photomerge(s: &mut Session, p: &Value) -> Result { return Err(bad(cmd, "Photomerge needs two or more images")); } let focal_param = p.get("focalLength").and_then(Value::as_f64).filter(|f| *f > 0.0); - let doc_name = format!("Untitled_Panorama{}", s.documents().len() + 1); + let doc_name = crate::file_cmds::untitled_name("Untitled_Panorama", |n| s.documents().iter().any(|d| d.doc.name == n) || s.held_open_named(n)); // A background job when started with `Session::start` (#210): alignment, warping and // blending run on a worker, checking for cancellation between stages and images. crate::jobs::run( @@ -1405,3 +1405,6 @@ pub fn specs() -> Vec { #[cfg(test)] mod tests; + +#[cfg(test)] +mod photomerge_name_tests; diff --git a/crates/engine/src/cmd/photo_cmds/photomerge_name_tests.rs b/crates/engine/src/cmd/photo_cmds/photomerge_name_tests.rs new file mode 100644 index 0000000000..01ed51c4d4 --- /dev/null +++ b/crates/engine/src/cmd/photo_cmds/photomerge_name_tests.rs @@ -0,0 +1,96 @@ +//! A Photomerge result is named "Untitled_Panorama-N", N being the first number no open +//! document uses, so "Untitled_Panorama-1" when no panorama is open (#14093). + +#![cfg(not(target_arch = "wasm32"))] + +use super::*; + +/// An RGB document `name` of `w`x`h` cut at `x0` from a `sw`-wide noise image. +fn tile(name: &str, sc: &[[f32; 4]], sw: usize, x0: usize, w: usize, h: usize) -> Document { + let mut d = Document::new(name, Size::new(w as u32, h as u32), ColorMode::Rgb, SampleType::U8); + let fmt = d.pixel_format(); + let n = fmt.channels(); + let mut data = vec![0.0f32; w * h * n]; + for (i, o) in data.chunks_exact_mut(n).enumerate() { + photocraft_raster::from_rgba_into(&fmt, sc[(i / w) * sw + x0 + i % w], o); + } + let mut surf = Surface::new(fmt); + surf.write_region(Rect::new(0, 0, w as i32, h as i32), &data); + d.layers.push(Layer::new("Background", LayerContent::Raster(surf))); + d +} + +/// A deterministic textured RGB image: a gradient with scattered squares. +fn scene(w: usize, h: usize) -> Vec<[f32; 4]> { + let mut z = 7u64; + let mut rnd = || { + z = z.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); + (z >> 33) as f64 / (1u64 << 31) as f64 + }; + let mut px: Vec<[f32; 4]> = (0..w * h).map(|i| [0.3 + 0.2 * (i % w) as f32 / w as f32, 0.35, 0.4, 1.0]).collect(); + for _ in 0..(w * h / 600) { + let (cx, cy, r) = ((rnd() * w as f64) as usize, (rnd() * h as f64) as usize, 2 + (rnd() * 8.0) as usize); + let col = [rnd() as f32, rnd() as f32, rnd() as f32, 1.0]; + for y in cy.saturating_sub(r)..(cy + r).min(h) { + for x in cx.saturating_sub(r)..(cx + r).min(w) { + px[y * w + x] = col; + } + } + } + px +} + +/// Two 300x300 tiles cut at x 0 and 200 from one 500x300 image, written as PNG files; returns +/// their paths. +fn source_files(name: &str) -> Vec { + let (sw, h) = (500usize, 300usize); + let sc = scene(sw, h); + let dir = std::env::temp_dir().join(format!("photocraft-photomerge-name-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + [("a.png", 0usize), ("b.png", 200)] + .into_iter() + .map(|(file, x0)| { + let png = photocraft_io::export(&tile(file, &sc, sw, x0, 300, h), file, &Default::default()).unwrap().bytes; + let path = dir.join(file); + std::fs::write(&path, &png).unwrap(); + path.to_string_lossy().into_owned() + }) + .collect() +} + +/// Runs Photomerge on `paths` and returns the result's name. +fn merge(s: &mut Session, paths: &[String]) -> String { + let r = s.execute("file.automate.photomerge", json!({"paths": paths, "layout": "reposition"})).unwrap(); + assert_eq!(r["placed"].as_array().unwrap().len(), 2, "{r}"); + s.active().unwrap().doc.name.clone() +} + +fn blank(name: &str) -> Document { + Document::new(name, Size::new(10, 10), ColorMode::Rgb, SampleType::U8) +} + +#[test] +fn panorama_is_untitled_panorama_1_whatever_else_is_open() { + let paths = source_files("first"); + for open in [0, 1, 3] { + let mut s = Session::new(); + for k in 0..open { + s.add_document(blank(&format!("doc{k}")), None); + } + assert_eq!(merge(&mut s, &paths), "Untitled_Panorama-1", "{open} other documents open"); + } +} + +#[test] +fn a_second_panorama_gets_another_number() { + let paths = source_files("second"); + let mut s = Session::new(); + assert_eq!(merge(&mut s, &paths), "Untitled_Panorama-1"); + // The first panorama is still open, so the next one takes the next free number. + assert_eq!(merge(&mut s, &paths), "Untitled_Panorama-2"); + // A free number below the taken ones is reused. + let mut s = Session::new(); + s.add_document(blank("Untitled_Panorama-2"), None); + assert_eq!(merge(&mut s, &paths), "Untitled_Panorama-1"); +} From 33cc8796912c949dee66f03c5d0b7f3dc751fcb4 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Sat, 14 Nov 2026 04:59:21 +0530 Subject: [PATCH 2/2] Photomerge name tests: write the source PNGs to a TempDir that cleans up --- .../cmd/photo_cmds/photomerge_name_tests.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/photo_cmds/photomerge_name_tests.rs b/crates/engine/src/cmd/photo_cmds/photomerge_name_tests.rs index 01ed51c4d4..b3bf560b43 100644 --- a/crates/engine/src/cmd/photo_cmds/photomerge_name_tests.rs +++ b/crates/engine/src/cmd/photo_cmds/photomerge_name_tests.rs @@ -41,22 +41,21 @@ fn scene(w: usize, h: usize) -> Vec<[f32; 4]> { } /// Two 300x300 tiles cut at x 0 and 200 from one 500x300 image, written as PNG files; returns -/// their paths. -fn source_files(name: &str) -> Vec { +/// their paths and the directory guard that removes them when dropped. +fn source_files() -> (tempfile::TempDir, Vec) { let (sw, h) = (500usize, 300usize); let sc = scene(sw, h); - let dir = std::env::temp_dir().join(format!("photocraft-photomerge-name-{}-{name}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).unwrap(); - [("a.png", 0usize), ("b.png", 200)] + let dir = tempfile::tempdir().unwrap(); + let paths = [("a.png", 0usize), ("b.png", 200)] .into_iter() .map(|(file, x0)| { let png = photocraft_io::export(&tile(file, &sc, sw, x0, 300, h), file, &Default::default()).unwrap().bytes; - let path = dir.join(file); + let path = dir.path().join(file); std::fs::write(&path, &png).unwrap(); path.to_string_lossy().into_owned() }) - .collect() + .collect(); + (dir, paths) } /// Runs Photomerge on `paths` and returns the result's name. @@ -72,7 +71,7 @@ fn blank(name: &str) -> Document { #[test] fn panorama_is_untitled_panorama_1_whatever_else_is_open() { - let paths = source_files("first"); + let (_dir, paths) = source_files(); for open in [0, 1, 3] { let mut s = Session::new(); for k in 0..open { @@ -84,7 +83,7 @@ fn panorama_is_untitled_panorama_1_whatever_else_is_open() { #[test] fn a_second_panorama_gets_another_number() { - let paths = source_files("second"); + let (_dir, paths) = source_files(); let mut s = Session::new(); assert_eq!(merge(&mut s, &paths), "Untitled_Panorama-1"); // The first panorama is still open, so the next one takes the next free number.