Skip to content

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

Merge remote-tracking branch 'origin/main' into claude/run-out-refuses-input-7437

Khawar Zamman Wani0ccd1f2642ee
diff --cc apps/photocraft-cli/src/lib.rs
index 15660f65b,c2497e1bf..5d66941b9
--- a/apps/photocraft-cli/src/lib.rs
+++ b/apps/photocraft-cli/src/lib.rs
@@@ -26,13 -27,15 +27,16 @@@ USAGE
--dpx-bits sets a DPX's bits per sample. Without it: 8 for an 8-bit document, else 16.
--dds-compression sets a DDS texture's compression: none (uncompressed, the default), bc1
(DXT1: alpha on or off), bc2 (DXT3) or bc3 (DXT5). --dds-mipmaps also writes the full mip chain.
+ --jpeg-format picks a JPEG's scans, as JPEG Options does: baseline (the default), optimized
+ (Huffman tables fitted to the image; RGB is written 4:2:0 and keeps the standard tables, so
+ only Gray and CMYK shrink) or progressive.
photocraft-cli info <file> [--compact]
Print the document as JSON (size, mode, depth, layer tree).
- photocraft-cli run (<file> | --new <json>) --cmd <id> [--params <json>] [--cmd …] [--out <file>] [--format <ext>] [--quality <1-100>] [--in-place] [--tiff-layers] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>] [--dds-compression <none|bc1|bc2|bc3>] [--dds-mipmaps]
- photocraft-cli run (<file> | --new <json>) --cmd <id> [--params <json>] [--cmd …] [--out <file>] [--format <ext>] [--quality <1-100>] [--tiff-layers] [--tiff-compression <none|lzw|zip>] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>] [--dds-compression <none|bc1|bc2|bc3>] [--dds-mipmaps] [--jpeg-format <baseline|optimized|progressive>]
++ photocraft-cli run (<file> | --new <json>) --cmd <id> [--params <json>] [--cmd …] [--out <file>] [--format <ext>] [--quality <1-100>] [--in-place] [--tiff-layers] [--tiff-compression <none|lzw|zip>] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>] [--dds-compression <none|bc1|bc2|bc3>] [--dds-mipmaps] [--jpeg-format <baseline|optimized|progressive>]
Open a file, run engine commands in order, save the result. Each --params
- applies to the preceding --cmd. Prints each command's JSON result.
+ applies to the preceding --cmd. Prints each command's JSON result. An --out that is the
+ <file> is refused, as the result would replace the original; --in-place allows it.
- photocraft-cli batch --actions <actions.json|set.atn> [--action <name>] --in <dir> --out <dir> [--format <ext>] [--quality <1-100>] [--in-place] [--tiff-layers] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>] [--dds-compression <none|bc1|bc2|bc3>] [--dds-mipmaps]
+ photocraft-cli batch --actions <actions.json|set.atn> [--action <name>] --in <dir> --out <dir> [--format <ext>] [--quality <1-100>] [--in-place] [--tiff-layers] [--tiff-compression <none|lzw|zip>] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>] [--dds-compression <none|bc1|bc2|bc3>] [--dds-mipmaps] [--jpeg-format <baseline|optimized|progressive>]
Apply an action list to every image in a directory. A Photoshop Actions file (.atn) is a
set: its action --action names plays, as below. JSON steps are [id, params] pairs,
{\"command\": id, \"params\": {…}} objects or bare ids, as a recorded action or droplet stores them
@@@ -83,8 -86,20 +87,20 @@@ const SUBCOMMANDS: &[Subcommand] = &
Subcommand { name: "info", values: &[], bare: &["--compact"], run: |a, out, _| info(a, out) },
Subcommand {
name: "run",
- values: &["--new", "--cmd", "--params", "--out", "--format", "--quality", "--tga-bits", "--dpx-bits", "--dds-compression"],
+ values: &[
+ "--new",
+ "--cmd",
+ "--params",
+ "--out",
+ "--format",
+ "--quality",
+ "--tga-bits",
+ "--dpx-bits",
+ "--dds-compression",
+ "--tiff-compression",
+ "--jpeg-format",
+ ],
- bare: &["--tiff-layers", "--dds-mipmaps"],
+ bare: &["--in-place", "--tiff-layers", "--dds-mipmaps"],
run: run_cmds,
},
Subcommand {
diff --cc apps/photocraft-cli/tests/cli.rs
index ce8539996,c31239d14..95e46a7b4
--- a/apps/photocraft-cli/tests/cli.rs
+++ b/apps/photocraft-cli/tests/cli.rs
@@@ -563,46 -563,33 +563,73 @@@ fn tga_bits_choose_the_targa_depth()
}
}
+/// #7437: `run <file> --out` to that same file, however `--out` is spelt, is refused before the
+/// file is read or any command runs, unless `--in-place` asks for it (as `batch` does, #492).
+#[test]
+fn run_refuses_to_write_over_its_input() {
+ let d = tmp("run-in-place");
+ write_png(&d.join("in.png"), 8, 4, 7);
+ let before = std::fs::read(d.join("in.png")).unwrap();
+ let run = |out: &Path, extra: &[&str]| {
+ bin().current_dir(&d).args(["run", "in.png", "--cmd", "image.adjustments.invert", "--out"]).arg(out).args(extra).output().unwrap()
+ };
+ let mut outs = vec![PathBuf::from("in.png"), PathBuf::from("./in.png"), d.join("in.png"), d.join(".").join("in.png")];
+ std::fs::hard_link(d.join("in.png"), d.join("hard.png")).unwrap();
+ outs.push(PathBuf::from("hard.png"));
+ #[cfg(unix)]
+ {
+ std::os::unix::fs::symlink(d.join("in.png"), d.join("soft.png")).unwrap();
+ outs.push(PathBuf::from("soft.png"));
+ }
+ for out in &outs {
+ for extra in [&[][..], &["--format", "png"][..]] {
+ let o = run(out, extra);
+ let (stdout, err) = (String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr));
+ assert_eq!(o.status.code(), Some(1), "--out {} {extra:?}: {err}", out.display());
+ assert!(err.contains("is the input file") && err.contains("--in-place"), "{err}");
+ assert!(stdout.is_empty(), "--out {} {extra:?} ran commands: {stdout}", out.display());
+ assert_eq!(std::fs::read(d.join("in.png")).unwrap(), before, "--out {} {extra:?} changed the original", out.display());
+ }
+ }
+ // Another file is written as before, leaving the original alone.
+ ok(bin().current_dir(&d).args(["run", "in.png", "--cmd", "image.adjustments.invert", "--out", "out.png"]));
+ assert!(d.join("out.png").is_file());
+ assert_eq!(std::fs::read(d.join("in.png")).unwrap(), before);
+ // `--in-place` opts in: the inverted result replaces the original, which still opens.
+ ok(bin().current_dir(&d).args(["run", "in.png", "--cmd", "image.adjustments.invert", "--out", "in.png", "--in-place"]));
+ assert_ne!(std::fs::read(d.join("in.png")).unwrap(), before, "--in-place left the original unchanged");
+ let (out, _) = ok(bin().current_dir(&d).args(["info", "in.png", "--compact"]));
+ let info: Value = serde_json::from_str(&out).unwrap();
+ assert_eq!((info["width"].as_u64(), info["height"].as_u64()), (Some(8), Some(4)), "{info}");
+}
+
+ /// `--jpeg-format` picks the JPEG's scans: progressive writes a progressive frame (SOF2). One it
+ /// doesn't name is refused and nothing is written.
+ #[test]
+ fn jpeg_format_chooses_the_scans() {
+ let d = tmp("jpeg-format");
+ let a = d.join("a.png");
+ write_png(&a, 16, 8, 3);
+ let marker = |b: &[u8], m: u8| b.windows(2).any(|w| w == [0xFF, m]);
+ for (format, progressive) in [(None, false), (Some("baseline"), false), (Some("optimized"), false), (Some("progressive"), true)] {
+ let out = d.join("a.jpg");
+ let mut convert = bin();
+ convert.arg("convert").arg(&a).arg(&out);
+ if let Some(f) = format {
+ convert.args(["--jpeg-format", f]);
+ }
+ ok(&mut convert);
+ assert_eq!(marker(&std::fs::read(&out).unwrap(), 0xC2), progressive, "--jpeg-format {format:?}");
+ }
+ for bad in ["Progressive", "1", ""] {
+ let out = d.join("bad.jpg");
+ let o = bin().arg("convert").arg(&a).arg(&out).args(["--jpeg-format", bad]).output().unwrap();
+ assert_eq!(o.status.code(), Some(1), "--jpeg-format {bad}");
+ assert!(String::from_utf8_lossy(&o.stderr).contains(&format!("bad --jpeg-format `{bad}`")), "--jpeg-format {bad}");
+ assert!(!out.exists(), "--jpeg-format {bad} wrote a file");
+ }
+ }
+
/// #492: an `--out` folder that is the `--in` folder, however it is spelt, is refused before
/// anything is written, unless `--in-place` asks for it.
#[test]