Skip to content

Commit

Permalink
feat: error handling (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
fgardt authored Mar 21, 2024
1 parent c57b05f commit d71dae1
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 218 deletions.
32 changes: 3 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spritter"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
authors = ["fgardt <me@fgardt.dev>"]
description = "Spritesheet generator for factorio"
Expand All @@ -17,11 +17,14 @@ nursery = "warn"
pedantic = "warn"
unwrap_used = "warn"
expect_used = "warn"
cast_possible_truncation = "allow"
cast_possible_wrap = "allow"
cast_lossless = "allow"
cast_sign_loss = "allow"

[dependencies]
clap = { version = "4.5", features = ["derive"] }
env_logger = "0.10"
error-stack = "0.4"
image = "0.25"
log = "0.4"
thiserror = "1.0"
19 changes: 8 additions & 11 deletions src/image_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pub enum ImgUtilError {
AllImagesEmpty,
}

type Result<T> = std::result::Result<T, ImgUtilError>;
type ImgUtilResult<T> = std::result::Result<T, ImgUtilError>;

pub fn load_from_path(path: &Path) -> Result<Vec<RgbaImage>> {
pub fn load_from_path(path: &Path) -> ImgUtilResult<Vec<RgbaImage>> {
if !path.exists() {
return Err(ImgUtilError::IOError(std::io::Error::new(
std::io::ErrorKind::NotFound,
Expand Down Expand Up @@ -61,18 +61,18 @@ pub fn load_from_path(path: &Path) -> Result<Vec<RgbaImage>> {
Ok(images)
}

fn load_image_from_file(path: &Path) -> Result<RgbaImage> {
fn load_image_from_file(path: &Path) -> ImgUtilResult<RgbaImage> {
let image = image::open(path)?.to_rgba8();
Ok(image)
}

pub fn crop_images(images: &mut Vec<RgbaImage>) -> Result<(i32, i32)> {
pub fn crop_images(images: &mut Vec<RgbaImage>) -> ImgUtilResult<(i32, i32)> {
if images.is_empty() {
return Err(ImgUtilError::NoImagesToCrop);
}

let raw_width = images.first().unwrap().width();
let raw_height = images.first().unwrap().height();
#[allow(clippy::unwrap_used)]
let (raw_width, raw_height) = images.first().unwrap().dimensions();

let mut min_x = std::u32::MAX;
let mut min_y = std::u32::MAX;
Expand Down Expand Up @@ -150,11 +150,8 @@ pub fn crop_images(images: &mut Vec<RgbaImage>) -> Result<(i32, i32)> {
}

// calculate how the center point shifted relative to the original image
let cropped_right_by = raw_width - max_x - 1;
let cropped_bottom_by = raw_height - max_y - 1;

let shift_x = i32::try_from(cropped_right_by).unwrap() - i32::try_from(min_x).unwrap();
let shift_y = i32::try_from(cropped_bottom_by).unwrap() - i32::try_from(min_y).unwrap();
let shift_x = (raw_width - cropped_width) as i32 - min_x as i32;
let shift_y = (raw_height - cropped_height) as i32 - min_y as i32;

trace!("shifted by ({shift_x}, {shift_y})");

Expand Down
Loading

0 comments on commit d71dae1

Please sign in to comment.