Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Batch: Format "Same" saves camera raws as PSD instead of failing (#5799)

Higgs325842750809257fb
diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs
index 43828b76b..e43723f0a 100644
--- a/crates/engine/src/file_cmds.rs
+++ b/crates/engine/src/file_cmds.rs
@@ -1042,13 +1042,21 @@ pub(crate) fn process_files(inputs: &[String], output: &str, format: &str, save:
/// under `output` (created as needed), so `a/x.png` and `b/x.png` don't collide.
pub(crate) fn process_tree(inputs: &[BatchInput], output: &str, format: &str, save: SaveOpts, suffix: &str, f: &dyn Fn(&mut Session) -> Result<()>) -> Value {
let outputs = inputs.iter().map(|BatchInput { path, rel_dir }| {
- 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" { same_format(path) } else { format.trim_start_matches('.').to_ascii_lowercase() };
(path.as_str(), join(&join(output, rel_dir), &format!("{}{suffix}.{ext}", stem(path))))
});
process_outputs(outputs, save, f)
}
+/// The extension a batch output takes with Format "Same": the input's own, except for formats
+/// PhotoCraft opens but can't write, camera raws above all (#5799: a folder of ARWs failed on
+/// every file). Those are saved as PSD, the format Photoshop's Save As offers for a raw.
+fn same_format(path: &str) -> String {
+ const IMPORT_ONLY: &[&str] = &["pdn", "kra", "krz", "af", "afdesign", "afphoto", "afpub"];
+ let ext = path.rsplit('.').next().unwrap_or("png").to_ascii_lowercase();
+ if photocraft_io::raw::EXTENSIONS.contains(&ext.as_str()) || IMPORT_ONLY.contains(&ext.as_str()) { "psd".into() } else { ext }
+}
+
fn process_outputs<'a>(outputs: impl IntoIterator<Item = (&'a str, String)>, save: SaveOpts, f: &dyn Fn(&mut Session) -> Result<()>) -> Value {
let mut files = Vec::new();
let mut errors = Vec::new();
diff --git a/crates/engine/src/file_cmds/tests.rs b/crates/engine/src/file_cmds/tests.rs
index 3cc72c30c..ece0858c8 100644
--- a/crates/engine/src/file_cmds/tests.rs
+++ b/crates/engine/src/file_cmds/tests.rs
@@ -65,6 +65,27 @@ fn camera_raw_opens_as_16_bit_with_notes() {
assert!(open_bytes_as(&mut s, "bad.dng", &bad, None, None).is_err());
}
+/// #5799: Batch with Format "Same" over a folder of camera raws saved nothing (every output was
+/// a raw PhotoCraft can't write). Raws are saved as PSD; other inputs keep their format.
+#[test]
+fn batch_same_format_saves_camera_raws_as_psd() {
+ use photocraft_raw::testgen::{DngSpec, mosaic, scene};
+ let dir = tmp("batch-same-raw");
+ 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));
+}
+
#[test]
fn revert_reloads_as_one_undoable_step() {
let dir = tmp("revert");
diff --git a/crates/io/src/raw.rs b/crates/io/src/raw.rs
index 008173839..aba70c21e 100644
--- a/crates/io/src/raw.rs
+++ b/crates/io/src/raw.rs
@@ -15,7 +15,7 @@ use std::sync::Arc;
use photocraft_cms::{Builtin, Intent, Profile, Transform};
use photocraft_codecs::{self as codecs, ChannelLayout, Format, Image, SampleType};
use photocraft_raw::{CameraLook, DevelopOptions, Limits, PreviewColorSpace, RawError, Reference, Sensor, WhiteBalance};
-pub use photocraft_raw::{Tone, retone};
+pub use photocraft_raw::{EXTENSIONS, Tone, retone};
use crate::flat::image_to_document;
use crate::{ImportResult, IoError};