use clap::Subcommand;
use gamut::dsp::av1::{forward_wht4x4, inverse_wht4x4};
use crate::error::CliError;
#[derive(Subcommand)]
pub(crate) enum DspCommand {
Wht {
#[arg(num_args = 16, allow_hyphen_values = true)]
values: Vec<i32>,
#[arg(long)]
inverse: bool,
},
}
pub(crate) fn run(cmd: &DspCommand) -> Result<(), CliError> {
match cmd {
DspCommand::Wht { values, inverse } => wht(values, *inverse),
}
}
fn wht(values: &[i32], inverse: bool) -> Result<(), CliError> {
let block: [i32; 16] = values.try_into().map_err(|_| {
CliError::Usage(format!("expected exactly 16 values, got {}", values.len()))
})?;
let (transformed, roundtrip) = if inverse {
let coeffs = inverse_wht4x4(&block);
(coeffs, forward_wht4x4(&coeffs))
} else {
let coeffs = forward_wht4x4(&block);
(coeffs, inverse_wht4x4(&coeffs))
};
let label = if inverse { "inverse" } else { "forward" };
println!("input: {block:?}");
println!("{label:>7}: {transformed:?}");
println!("round-trip: {roundtrip:?}");
println!(
"round-trip: {}",
if roundtrip == block {
"matches input"
} else {
"DIFFERS from input"
}
);
Ok(())
}