From 61f5504e35f687cfcda0c61b8c55ddaa589fbfca Mon Sep 17 00:00:00 2001 From: joachimbbp <104856283+joachimbbp@users.noreply.github.com> Date: Wed, 10 Apr 2024 10:52:55 -0400 Subject: [PATCH] sprite builder crops images --- spritefire/src/lib.rs | 28 +++++++++++++++++++--------- spritefire/src/main.rs | 3 ++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/spritefire/src/lib.rs b/spritefire/src/lib.rs index cbbdec8..87720f8 100644 --- a/spritefire/src/lib.rs +++ b/spritefire/src/lib.rs @@ -1,17 +1,19 @@ -use std::fs::{self, DirEntry}; -use std::io; +use image::{GenericImageView, ImageError}; +use std::fs::{self}; use std::path::{Path, PathBuf}; //MVP: //fn simple_resize // resizes every image in a folder to proper spritefire resolution -pub fn build_sprites(input_path: &str) -> std::io::Result<()> { - let folder = Path::new(input_path); - if folder.is_dir() { - for entry in fs::read_dir(folder)? { +pub fn build_sprites(input: &str, save: &str) -> std::io::Result<()> { + let input_folder = Path::new(input); + if input_folder.is_dir() { + for entry in fs::read_dir(input_folder)? { + print!("resizing {:?}\n", entry); let entry = entry?; let image_path = entry.path(); + let is_image = match image_path.extension() { Some(image_path) => { let ext = image_path.to_string_lossy().to_lowercase(); @@ -20,15 +22,23 @@ pub fn build_sprites(input_path: &str) -> std::io::Result<()> { None => false, }; if is_image { - simple_resize(&image_path); + print!("is image\n"); + //TODO: Open image, pass to resize + let _ = simple_resize(&image_path, &save); } } } Ok(()) } -fn simple_resize(image_path: &PathBuf) { - print!("{}", image_path.display().to_string()) +fn simple_resize(image_path: &PathBuf, save: &str) -> Result<(), ImageError> { + print!("simple resize\n"); + let img = image::open(image_path)?; + let cropped_image = img.view(50, 50, 50, 50).to_image(); + //view(x,y,width,height) + cropped_image.save(Path::new(save).join(image_path.file_name().unwrap()))?; + print!("{:?} resized", img); + Ok(()) } //Advanced: diff --git a/spritefire/src/main.rs b/spritefire/src/main.rs index 3fe4511..775c9a4 100644 --- a/spritefire/src/main.rs +++ b/spritefire/src/main.rs @@ -7,7 +7,8 @@ use spritefire::build_sprites; fn main() { let test_input = "/Users/joachimpfefferkorn/repos/spritefire/assets/sprites_512"; - _ = build_sprites(&test_input); + let test_output: &str = "/Users/joachimpfefferkorn/repos/spritefire/output"; + let _ = build_sprites(&test_input, test_output); //Ishan's web code /* let args: Vec<_> = env::args().collect();