From bb50794136a039f29236fb70385ac1abbdd4d1c3 Mon Sep 17 00:00:00 2001 From: Russel Arbore <russel.jma@gmail.com> Date: Fri, 14 Feb 2025 17:31:03 -0600 Subject: [PATCH] Crop the small example image to round dimensions --- juno_samples/cava/src/image_proc.rs | 37 +++++++++++++++++++---------- juno_samples/cava/src/main.rs | 13 +++++++++- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/juno_samples/cava/src/image_proc.rs b/juno_samples/cava/src/image_proc.rs index 94a7c14f..7115bac8 100644 --- a/juno_samples/cava/src/image_proc.rs +++ b/juno_samples/cava/src/image_proc.rs @@ -17,41 +17,54 @@ fn read_bin_u32(f: &mut File) -> Result<u32, Error> { Ok(u32::from_le_bytes(buffer)) } -pub fn read_raw<P: AsRef<Path>>(image: P) -> Result<RawImage, Error> { +pub fn read_raw<P: AsRef<Path>>( + image: P, + crop_rows: Option<usize>, + crop_cols: Option<usize>, +) -> Result<RawImage, Error> { let mut file = File::open(image)?; - let rows = read_bin_u32(&mut file)? as usize; - let cols = read_bin_u32(&mut file)? as usize; + let in_rows = read_bin_u32(&mut file)? as usize; + let in_cols = read_bin_u32(&mut file)? as usize; + let out_rows = crop_rows.unwrap_or(in_rows); + let out_cols = crop_cols.unwrap_or(in_cols); let chans = read_bin_u32(&mut file)? as usize; assert!( chans == CHAN, "Channel size read from the binary file doesn't match the default value" ); + assert!(in_rows >= out_rows); + assert!(in_cols >= out_cols); - let size: usize = rows * cols * CHAN; - let mut buffer: Vec<u8> = vec![0; size]; + let in_size: usize = in_rows * in_cols * CHAN; + let mut buffer: Vec<u8> = vec![0; in_size]; // The file has pixels is a 3D array with dimensions height, width, channel file.read_exact(&mut buffer)?; let chunked_channels = buffer.chunks(CHAN).collect::<Vec<_>>(); - let chunked = chunked_channels.chunks(cols).collect::<Vec<_>>(); + let chunked = chunked_channels.chunks(in_cols).collect::<Vec<_>>(); let input: &[&[&[u8]]] = chunked.as_slice(); // We need the image in a 3D array with dimensions channel, height, width - let mut pixels: Vec<u8> = vec![0; size]; - let mut pixels_columns = pixels.chunks_mut(cols).collect::<Vec<_>>(); - let mut pixels_chunked = pixels_columns.chunks_mut(rows).collect::<Vec<_>>(); + let out_size: usize = out_rows * out_cols * CHAN; + let mut pixels: Vec<u8> = vec![0; out_size]; + let mut pixels_columns = pixels.chunks_mut(out_cols).collect::<Vec<_>>(); + let mut pixels_chunked = pixels_columns.chunks_mut(out_rows).collect::<Vec<_>>(); let result: &mut [&mut [&mut [u8]]] = pixels_chunked.as_mut_slice(); - for row in 0..rows { - for col in 0..cols { + for row in 0..out_rows { + for col in 0..out_cols { for chan in 0..CHAN { result[chan][row][col] = input[row][col][chan]; } } } - Ok(RawImage { rows, cols, pixels }) + Ok(RawImage { + rows: out_rows, + cols: out_cols, + pixels, + }) } pub fn extern_image(rows: usize, cols: usize, image: &[u8]) -> RgbImage { diff --git a/juno_samples/cava/src/main.rs b/juno_samples/cava/src/main.rs index 700b74e6..142ed703 100644 --- a/juno_samples/cava/src/main.rs +++ b/juno_samples/cava/src/main.rs @@ -147,6 +147,10 @@ struct CavaInputs { #[clap(long = "output-verify", value_name = "PATH")] output_verify: Option<String>, cam_model: String, + #[clap(short, long)] + crop_rows: Option<usize>, + #[clap(short, long)] + crop_cols: Option<usize>, } fn cava_harness(args: CavaInputs) { @@ -156,8 +160,11 @@ fn cava_harness(args: CavaInputs) { verify, output_verify, cam_model, + crop_rows, + crop_cols, } = args; - let RawImage { rows, cols, pixels } = read_raw(input).expect("Error loading image"); + let RawImage { rows, cols, pixels } = + read_raw(input, crop_rows, crop_cols).expect("Error loading image"); let CamModel { tstw, num_ctrl_pts, @@ -236,6 +243,8 @@ fn cava_test_small() { verify: true, output_verify: None, cam_model: "cam_models/NikonD7000".to_string(), + crop_rows: Some(144), + crop_cols: Some(192), }); } @@ -248,5 +257,7 @@ fn cava_test_full() { verify: true, output_verify: None, cam_model: "cam_models/NikonD7000".to_string(), + crop_rows: None, + crop_cols: None, }); } -- GitLab