Skip to content
Snippets Groups Projects
Commit bb507941 authored by Russel Arbore's avatar Russel Arbore
Browse files

Crop the small example image to round dimensions

parent 9397f55d
No related branches found
No related tags found
1 merge request!178Some Cava optimization
Pipeline #201659 passed
...@@ -17,41 +17,54 @@ fn read_bin_u32(f: &mut File) -> Result<u32, Error> { ...@@ -17,41 +17,54 @@ fn read_bin_u32(f: &mut File) -> Result<u32, Error> {
Ok(u32::from_le_bytes(buffer)) 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 mut file = File::open(image)?;
let rows = read_bin_u32(&mut file)? as usize; let in_rows = read_bin_u32(&mut file)? as usize;
let cols = 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; let chans = read_bin_u32(&mut file)? as usize;
assert!( assert!(
chans == CHAN, chans == CHAN,
"Channel size read from the binary file doesn't match the default value" "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 in_size: usize = in_rows * in_cols * CHAN;
let mut buffer: Vec<u8> = vec![0; size]; let mut buffer: Vec<u8> = vec![0; in_size];
// The file has pixels is a 3D array with dimensions height, width, channel // The file has pixels is a 3D array with dimensions height, width, channel
file.read_exact(&mut buffer)?; file.read_exact(&mut buffer)?;
let chunked_channels = buffer.chunks(CHAN).collect::<Vec<_>>(); 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(); let input: &[&[&[u8]]] = chunked.as_slice();
// We need the image in a 3D array with dimensions channel, height, width // We need the image in a 3D array with dimensions channel, height, width
let mut pixels: Vec<u8> = vec![0; size]; let out_size: usize = out_rows * out_cols * CHAN;
let mut pixels_columns = pixels.chunks_mut(cols).collect::<Vec<_>>(); let mut pixels: Vec<u8> = vec![0; out_size];
let mut pixels_chunked = pixels_columns.chunks_mut(rows).collect::<Vec<_>>(); 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(); let result: &mut [&mut [&mut [u8]]] = pixels_chunked.as_mut_slice();
for row in 0..rows { for row in 0..out_rows {
for col in 0..cols { for col in 0..out_cols {
for chan in 0..CHAN { for chan in 0..CHAN {
result[chan][row][col] = input[row][col][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 { pub fn extern_image(rows: usize, cols: usize, image: &[u8]) -> RgbImage {
......
...@@ -147,6 +147,10 @@ struct CavaInputs { ...@@ -147,6 +147,10 @@ struct CavaInputs {
#[clap(long = "output-verify", value_name = "PATH")] #[clap(long = "output-verify", value_name = "PATH")]
output_verify: Option<String>, output_verify: Option<String>,
cam_model: String, cam_model: String,
#[clap(short, long)]
crop_rows: Option<usize>,
#[clap(short, long)]
crop_cols: Option<usize>,
} }
fn cava_harness(args: CavaInputs) { fn cava_harness(args: CavaInputs) {
...@@ -156,8 +160,11 @@ fn cava_harness(args: CavaInputs) { ...@@ -156,8 +160,11 @@ fn cava_harness(args: CavaInputs) {
verify, verify,
output_verify, output_verify,
cam_model, cam_model,
crop_rows,
crop_cols,
} = args; } = 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 { let CamModel {
tstw, tstw,
num_ctrl_pts, num_ctrl_pts,
...@@ -236,6 +243,8 @@ fn cava_test_small() { ...@@ -236,6 +243,8 @@ fn cava_test_small() {
verify: true, verify: true,
output_verify: None, output_verify: None,
cam_model: "cam_models/NikonD7000".to_string(), cam_model: "cam_models/NikonD7000".to_string(),
crop_rows: Some(144),
crop_cols: Some(192),
}); });
} }
...@@ -248,5 +257,7 @@ fn cava_test_full() { ...@@ -248,5 +257,7 @@ fn cava_test_full() {
verify: true, verify: true,
output_verify: None, output_verify: None,
cam_model: "cam_models/NikonD7000".to_string(), cam_model: "cam_models/NikonD7000".to_string(),
crop_rows: None,
crop_cols: None,
}); });
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment