use clap::Parser;
use rawshift_image::formats::{
DecodeOptions, decode_standard_image, decode_standard_image_with, detect_standard_format,
};
use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(
author,
version,
about = "Detect and decode a standard image format, printing its dimensions"
)]
struct Args {
input: PathBuf,
#[arg(long)]
save_raw: Option<PathBuf>,
#[arg(long)]
explicit_backend: bool,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let Args {
input,
save_raw,
explicit_backend,
} = Args::parse();
let data = std::fs::read(&input)?;
let format = match detect_standard_format(&data) {
Some(f) => f,
None => {
eprintln!(
"Could not detect a supported standard image format in {:?}",
input
);
std::process::exit(1);
}
};
println!("Detected format: {}", format);
let image = if explicit_backend {
let backend = DecodeOptions::default_for(format)
.ok_or_else(|| format!("no decoder backend compiled in for {format}"))?;
println!("Backend: {backend:?}");
decode_standard_image_with(&data, &backend)?
} else {
decode_standard_image(&data, format)?
};
println!(
"Dimensions: {}x{} ({} pixels)",
image.width(),
image.height(),
image.width() as u64 * image.height() as u64
);
println!("Pixel data length: {} u16 values", image.data.len());
if let Some(out_path) = save_raw {
let bytes: Vec<u8> = image.data.iter().flat_map(|&v| v.to_le_bytes()).collect();
std::fs::write(&out_path, &bytes)?;
println!(
"Saved {} bytes of raw pixel data to {:?}",
bytes.len(),
out_path
);
}
Ok(())
}