Skip to content

Commit

Permalink
Merge pull request #2 from raymondytian/feature/output-dir
Browse files Browse the repository at this point in the history
Output directory user argument and better output file names
  • Loading branch information
rymdtian authored Apr 9, 2024
2 parents d782b79 + 9a0a260 commit 69848fd
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,17 @@ fn validate_args(cli: &Cli) -> Result<(), String> {
/// # Returns
///
/// A vector of split images.
fn split_image(img_path: &PathBuf, rows: Vec<u32>, cols: Vec<u32>) -> Vec<DynamicImage> {
fn split_image(img_path: &PathBuf, rows: &Vec<u32>, cols: &Vec<u32>) -> Vec<DynamicImage> {
let mut img = image::open(img_path).unwrap();
let mut split_images = Vec::new();
let (width, height) = img.dimensions();

let sum_rows: u32 = rows.iter().sum();
let sum_cols: u32 = cols.iter().sum();

let rows = rows.clone();
let cols = cols.clone();

if sum_rows > height {
eprintln!(
"splix: rows: The sum of provided rows ({}) exceeds image height ({})",
Expand Down Expand Up @@ -176,8 +179,11 @@ fn split_image(img_path: &PathBuf, rows: Vec<u32>, cols: Vec<u32>) -> Vec<Dynami
fn save_images(
split_images: &Vec<DynamicImage>,
output_directory: &PathBuf,
img_file_name: &str,
img_format: &ImageFormat,
img_format_str: &str,
num_rows: usize,
num_cols: usize,
) {
let mut output_directory = output_directory.clone();
if !output_directory.exists() {
Expand All @@ -189,20 +195,24 @@ fn save_images(
);
}
}
output_directory.push("placeholder_filename");

for i in 0..split_images.len() {
output_directory.set_file_name(format!("{}.{}", i, img_format_str));

if output_directory.exists() {
if let Err(err) = fs::remove_file(&output_directory) {
eprintln!("Failed to remove existing image {}: {}", i, err);
continue; // Skip saving this image if removing the existing one fails
output_directory.push("placeholder");

for i in 0..num_rows {
for j in 0..num_cols {
output_directory
.set_file_name(format!("{}-r{}c{}.{}", img_file_name, i, j, img_format_str));
if output_directory.exists() {
if let Err(err) = fs::remove_file(&output_directory) {
eprintln!("Failed to remove existing image {}: {}", i, err);
continue; // Skip saving this image if removing the existing one fails
}
}
}

if let Err(err) = split_images[i].save_with_format(&output_directory, *img_format) {
eprintln!("splix: Failed to save image #{}: {}", i, err);
if let Err(err) =
split_images[i * num_cols + j].save_with_format(&output_directory, *img_format)
{
eprintln!("splix: Failed to save image #{}: {}", i, err);
}
}
}
}
Expand All @@ -214,9 +224,9 @@ mod tests {
#[test]
fn test_split_image_correct_number_of_images() {
let path = &PathBuf::from("./assets/16x16.png");
assert!(split_image(path, vec![3], vec![5]).len() == 15);
assert!(split_image(path, vec![1], vec![1]).len() == 1);
assert!(split_image(path, vec![16], vec![16]).len() == 256);
assert!(split_image(path, &vec![3], &vec![5]).len() == 15);
assert!(split_image(path, &vec![1], &vec![1]).len() == 1);
assert!(split_image(path, &vec![16], &vec![16]).len() == 256);
}
}

Expand All @@ -233,13 +243,27 @@ fn main() {
let cols = cli.cols.unwrap_or(vec![1]);
let output_directory = cli.output_dir.unwrap_or(PathBuf::from("splixed-images"));

let split_images = split_image(&img_path, rows, cols);
let img_file_name = img_path.file_stem().unwrap().to_string_lossy();
let img_format = io::Reader::open(&img_path).unwrap().format().unwrap();
let img_format_str = img_format.extensions_str()[0];

let split_images = split_image(&img_path, &rows, &cols);

save_images(
&split_images,
&output_directory,
&img_file_name,
&img_format,
img_format_str,
if rows.len() > 1 {
rows.len()
} else {
rows[0] as usize
},
if cols.len() > 1 {
cols.len()
} else {
cols[0] as usize
},
);
}

0 comments on commit 69848fd

Please sign in to comment.