diff --git a/README.md b/README.md index 44968a7..7276841 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,14 @@ itself after the last export or a quit; a browser that refuses says so on the page instead. Leave the framing untouched in the app; any adjustment there reintroduces the resample. +A window decided elsewhere, such as one from a contact sheet of candidate +crops, skips the browser. `--window` takes it in oriented source pixels and +exports one photograph through the same path: + +```sh +ig-prep --window 0,811,7728,4046 -o ig XT500965.HIF # 1.91:1, 3072x1608 +``` + ## Lend an export its RAW's detail A photograph smaller than the delivery width, such as an edit exported at diff --git a/src/main.rs b/src/main.rs index 1c73b61..c6a31b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,9 @@ FIT at a time. The export is a whole-pixel, in-band window at the target width, so the app has nothing to resample. The fit flags are ignored; -o, -w, -q and chroma apply. + --window export one photograph through the window --pick + would post: whole oriented pixels, inside the band, at + the target width. Repeats a choice without the browser. OPTIONS -w, --width target width (default 3072) @@ -171,6 +174,7 @@ fn main() { let mut dither = false; let mut variants = false; let mut pick = false; + let mut window = None; let mut match_tables = false; let mut out_dir = PathBuf::from("ig"); let mut pad = [255u8, 255, 255]; @@ -191,6 +195,13 @@ fn main() { "--dither" => dither = true, "--variants" => variants = true, "--pick" => pick = true, + "--window" => match args.next().as_deref().and_then(parse_window) { + Some(w) => window = Some(w), + None => { + eprintln!("ig-prep: --window needs x,y,width,height in oriented source pixels"); + std::process::exit(2); + } + }, "--match" => match_tables = true, "--gravity" => { gravity = match args.next().as_deref() { @@ -264,9 +275,9 @@ fn main() { eprintln!("ig-prep: {MATCH_CONFLICT}"); std::process::exit(2); } - if !raw_dirs.is_empty() && (no_raw || pick || variants || check) { + if !raw_dirs.is_empty() && (no_raw || pick || variants || check || window.is_some()) { eprintln!( - "ig-prep: --raw applies to an ordinary conversion; it cannot combine with --no-raw, --pick, --variants or --check" + "ig-prep: --raw applies to an ordinary conversion; it cannot combine with --no-raw, --pick, --window, --variants or --check" ); std::process::exit(2); } @@ -276,11 +287,24 @@ fn main() { ); std::process::exit(2); } + if window.is_some() && (pick || check || dry || variants) { + eprintln!( + "ig-prep: --window frames one photograph; it cannot combine with --pick, --check, --dry-run or --variants" + ); + std::process::exit(2); + } let files = expand(&paths); if files.is_empty() { eprintln!("ig-prep: no readable images"); std::process::exit(1); } + if window.is_some() && files.len() != 1 { + eprintln!( + "ig-prep: --window frames one photograph; {} were given", + files.len() + ); + std::process::exit(2); + } if check { std::process::exit(run_check(&files)); } @@ -313,6 +337,17 @@ fn main() { if pick { std::process::exit(pick::run(&files, &opts)); } + if let Some(window) = window { + match load_oriented(&files[0]).and_then(|img| pick::export(&img, &files[0], window, &opts)) + { + Ok(line) => println!("{line}"), + Err(e) => { + eprintln!("ig-prep: {}: {e}", files[0].display()); + std::process::exit(1); + } + } + return; + } if variants { match comparison::generate(&files, &opts) { Ok(path) => println!("Comparison written to {}", path.display()), @@ -811,6 +846,19 @@ fn lend( (Some(Lent { img: r, look, plan }), Some(note)) } +/// `--window`, as `x,y,w,h` in whole oriented pixels. The frame and the band +/// are checked later by `geometry::plan_window`, once the image is decoded. +fn parse_window(value: &str) -> Option<(u32, u32, u32, u32)> { + let parts: Vec = value + .split(',') + .map(|p| p.parse().ok()) + .collect::>()?; + match parts[..] { + [x, y, w, h] => Some((x, y, w, h)), + _ => None, + } +} + fn load_oriented(path: &Path) -> Result { // Orientation comes from the metadata reader rather than from the decoder, // because a HEIF's rotation lives in its EXIF and sips does not apply it. @@ -899,3 +947,17 @@ pub fn expand(paths: &[PathBuf]) -> Vec { } out } + +#[cfg(test)] +mod tests { + use super::parse_window; + + #[test] + fn a_window_is_four_whole_numbers() { + assert_eq!(parse_window("0,541,5152,6869"), Some((0, 541, 5152, 6869))); + assert_eq!(parse_window("0,541,5152"), None); + assert_eq!(parse_window("0,541,5152,6869,1"), None); + assert_eq!(parse_window("0,541.5,5152,6869"), None); + assert_eq!(parse_window("-1,541,5152,6869"), None); + } +} diff --git a/src/pick.rs b/src/pick.rs index e726fcb..8f535a1 100644 --- a/src/pick.rs +++ b/src/pick.rs @@ -146,7 +146,7 @@ fn session(listener: &TcpListener, files: &[PathBuf], opts: &Opts) -> Result= files.len(); @@ -305,14 +305,14 @@ fn parse_window(body: &[u8]) -> Result<(u32, u32, u32, u32), String> { Ok((n("x")?, n("y")?, n("w")?, n("h")?)) } -fn export( - cur: &Current, +pub fn export( + img: &image::Rgb, path: &Path, window: (u32, u32, u32, u32), opts: &Opts, ) -> Result { - let plan = geometry::plan_window(cur.img.w, cur.img.h, window, opts.width)?; - let final_img = crate::render(&cur.img, &plan, opts.pad)?; + let plan = geometry::plan_window(img.w, img.h, window, opts.width)?; + let final_img = crate::render(img, &plan, opts.pad)?; let bytes = crate::encode_with(opts, &final_img)?; let dest = opts.out_dir.join(format!( "{}.jpg", @@ -322,8 +322,8 @@ fn export( Ok(format!( "{}: {}x{} -> {}x{} (window {}x{} at {},{}) {} {} KB", path.file_name().unwrap_or_default().to_string_lossy(), - cur.img.w, - cur.img.h, + img.w, + img.h, plan.scale.0, plan.scale.1, window.2,