PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Integration: naming templates and more read-only formats use same_format, for #5857
aiongg6265d67de9e6
diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rsindex 5909d4073..b563b302c 100644--- a/crates/engine/src/file_cmds.rs+++ b/crates/engine/src/file_cmds.rs@@ -1149,7 +1149,8 @@ pub(crate) fn process_tree(inputs: &[BatchInput], output: &str, format: &str, sa/// every file). Those are saved as PSD, the format Photoshop's Save As offers for a raw. An input/// whose file name has no extension (a dot in a folder name doesn't count) is saved as PNG.fn same_format(path: &str) -> String {- const IMPORT_ONLY: &[&str] = &["pdn", "kra", "krz", "af", "afdesign", "afphoto", "afpub"];+ const IMPORT_ONLY: &[&str] =+ &["pdn", "kra", "krz", "af", "afdesign", "afphoto", "afpub", "heic", "heif", "hif", "svg", "svgz", "cin", "pcx", "xpm"];let ext = extension(path).unwrap_or_else(|| "png".into());if photocraft_io::raw::EXTENSIONS.contains(&ext.as_str()) || IMPORT_ONLY.contains(&ext.as_str()) { "psd".into() } else { ext }}@@ -1160,6 +1161,9 @@ fn same_format_takes_the_file_names_extension() {assert_eq!(same_format("/in/a.PNG"), "png");assert_eq!(same_format("/in/IMG_0001.ARW"), "psd");assert_eq!(same_format("/in/a.kra"), "psd");+ assert_eq!(same_format("/in/IMG_0001.HEIC"), "psd");+ assert_eq!(same_format("/in/logo.svg"), "psd");+ assert_eq!(same_format("/in/scan.cin"), "psd");// No extension in the file name: PNG, not the path after a dot in a folder name.assert_eq!(same_format("/in/v1.2/scan"), "png");assert_eq!(same_format("C:\\in.d\\scan"), "png");diff --git a/crates/engine/src/file_cmds/batch_naming.rs b/crates/engine/src/file_cmds/batch_naming.rsindex e5dcc4a6e..8e14bcf42 100644--- a/crates/engine/src/file_cmds/batch_naming.rs+++ b/crates/engine/src/file_cmds/batch_naming.rs@@ -87,8 +87,7 @@ pub(super) fn plan(inputs: &[BatchInput], output: &str, format: &str, params: &Vif base.is_empty() || base.ends_with(['.', ' ']) {return Err(bad("naming must produce a nonempty filename stem without a trailing dot or space"));}- let ext =- if format == "same" { path.rsplit('.').next().unwrap_or("png").to_ascii_lowercase() } else { format.trim_start_matches('.').to_ascii_lowercase() };+ let ext = if format == "same" { super::same_format(path) } else { format.trim_start_matches('.').to_ascii_lowercase() };let name = format!("{base}.{ext}");if name.len() > 255 || name.chars().any(|c| c.is_control() || "/\\<>:\"|?*".contains(c)) {return Err(bad("naming must produce a filename of at most 255 bytes without path separators or reserved characters"));diff --git a/crates/engine/src/file_cmds/tests.rs b/crates/engine/src/file_cmds/tests.rsindex 6ef1ecce6..9b5ec43a7 100644--- a/crates/engine/src/file_cmds/tests.rs+++ b/crates/engine/src/file_cmds/tests.rs@@ -74,16 +74,22 @@ fn batch_same_format_saves_camera_raws_as_psd() {let (w, h) = (24, 16);std::fs::write(join(&dir, "IMG_0001.dng"), DngSpec::cfa(w, h, mosaic(&scene(w, h), w, [0, 1, 1, 2], 0, 65535)).build()).unwrap();png(&dir, "b.png", 8, 8, "#ff0000");- let out = join(&dir, "out");- let mut s = Session::new();- let r = s.execute("file.automate.batch", json!({"steps": [["image.adjustments.invert", {}]], "input": dir, "output": out, "format": "same"})).unwrap();- assert!(r["errors"].as_array().unwrap().is_empty(), "{r}");- let mut files: Vec<String> = r["files"].as_array().unwrap().iter().map(|f| f.as_str().unwrap().replace('\\', "/")).collect();- files.sort();- assert_eq!(files.len(), 2, "{r}");- assert!(files[0].ends_with("/IMG_0001.psd") && files[1].ends_with("/b.png"), "{files:?}");- let psd = photocraft_io::import("IMG_0001.psd", &std::fs::read(&files[0]).unwrap()).unwrap().document;- assert_eq!((psd.size.width, psd.size.height), (w as u32, h as u32));+ // Also with a file naming template, which picks the output extension separately.+ for (out, naming) in [("out", None), ("named", Some("{name}_{serial}"))] {+ let out = join(&dir, out);+ let mut p = json!({"steps": [["image.adjustments.invert", {}]], "input": dir, "output": out, "format": "same"});+ if let Some(naming) = naming {+ p["naming"] = json!(naming);+ }+ let r = Session::new().execute("file.automate.batch", p).unwrap();+ assert!(r["errors"].as_array().unwrap().is_empty(), "{naming:?}: {r}");+ let mut files: Vec<String> = r["files"].as_array().unwrap().iter().map(|f| f.as_str().unwrap().replace('\\', "/")).collect();+ files.sort();+ assert_eq!(files.len(), 2, "{r}");+ assert!(files[0].contains("/IMG_0001") && files[0].ends_with(".psd") && files[1].contains("/b") && files[1].ends_with(".png"), "{files:?}");+ let psd = photocraft_io::import("IMG_0001.psd", &std::fs::read(&files[0]).unwrap()).unwrap().document;+ assert_eq!((psd.size.width, psd.size.height), (w as u32, h as u32));+ }}#[test]