chop/
chop.rs

1use cloudflare_soos as soos;
2use std::env;
3use std::fs;
4use std::path::Path;
5use std::path::PathBuf;
6use std::process;
7
8fn usage() -> ! {
9    eprintln!("usage: {} image.jpg", env::args().next().as_deref().unwrap_or("chop"));
10    process::exit(1)
11}
12
13fn main() {
14    let mut args = env::args_os().skip(1);
15    let input_path = PathBuf::from(args.next().unwrap_or_else(|| usage()));
16    let input_file = fs::read(&input_path).expect("The specified input file could not be opened");
17
18    let scans = soos::Scans::from_file(&input_file).expect("Scan failed");
19
20    println!("{scans:#?}");
21
22    write_up_to(&input_path, &input_file, "0-meta", scans.metadata_end);
23    write_up_to(
24        &input_path,
25        &input_file,
26        "1-render",
27        scans.frame_render_start,
28    );
29    write_up_to(
30        &input_path,
31        &input_file,
32        "2-first_scan",
33        scans.first_scan_end,
34    );
35    write_up_to(&input_path, &input_file, "3-good_scan", scans.good_scan_end);
36}
37
38fn write_up_to(src_path: &Path, data: &[u8], chunk_name: &str, len: Option<usize>) {
39    let src_ext = src_path.extension().and_then(|e| e.to_str()).unwrap_or("jpeg");
40    let src_stem = src_path.file_stem().unwrap().to_str().unwrap();
41    let dst_path = src_path.with_file_name(format!("{src_stem}-{chunk_name}.{src_ext}"));
42
43    match len {
44        Some(len) => { std::fs::write(&dst_path, &data[..len]).unwrap_or_else(|_| panic!("Can't write output file to {}", dst_path.display())) },
45        None => { let _ = std::fs::remove_file(dst_path); },
46    }
47}