Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 64 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <x,y,w,h> 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 <px> target width (default 3072)
Expand Down Expand Up @@ -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];
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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));
}
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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<u32> = value
.split(',')
.map(|p| p.parse().ok())
.collect::<Option<_>>()?;
match parts[..] {
[x, y, w, h] => Some((x, y, w, h)),
_ => None,
}
}

fn load_oriented(path: &Path) -> Result<image::Rgb, String> {
// 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.
Expand Down Expand Up @@ -899,3 +947,17 @@ pub fn expand(paths: &[PathBuf]) -> Vec<PathBuf> {
}
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);
}
}
14 changes: 7 additions & 7 deletions src/pick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn session(listener: &TcpListener, files: &[PathBuf], opts: &Opts) -> Result<Out
}
exports.push_back(Export {
path: path.clone(),
handle: std::thread::spawn(move || export(&frame, &path, window, &opts)),
handle: std::thread::spawn(move || export(&frame.img, &path, window, &opts)),
});
i += 1;
let last = i >= files.len();
Expand Down Expand Up @@ -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<String, String> {
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",
Expand All @@ -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,
Expand Down
Loading