Skip to content

Commit

Permalink
fix: shift calc
Browse files Browse the repository at this point in the history
  • Loading branch information
fgardt committed Mar 22, 2024
1 parent a11567b commit 716ba73
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/image_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn load_image_from_file(path: &Path) -> ImgUtilResult<RgbaImage> {
Ok(image)
}

pub fn crop_images(images: &mut Vec<RgbaImage>) -> ImgUtilResult<(i32, i32)> {
pub fn crop_images(images: &mut Vec<RgbaImage>) -> ImgUtilResult<(f64, f64)> {
if images.is_empty() {
return Err(ImgUtilError::NoImagesToCrop);
}
Expand Down Expand Up @@ -134,7 +134,7 @@ pub fn crop_images(images: &mut Vec<RgbaImage>) -> ImgUtilResult<(i32, i32)> {
// do we need to crop?
if min_x == 0 && min_y == 0 && max_x == (raw_width - 1) && max_y == (raw_height - 1) {
// no cropping needed
return Ok((0, 0));
return Ok((0.0, 0.0));
}

let cropped_width = max_x - min_x + 1;
Expand All @@ -151,8 +151,8 @@ pub fn crop_images(images: &mut Vec<RgbaImage>) -> ImgUtilResult<(i32, i32)> {
}

// calculate how the center point shifted relative to the original image
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;
let shift_x = (f64::from(raw_width - cropped_width) / 2.0) - f64::from(min_x);
let shift_y = (f64::from(raw_height - cropped_height) / 2.0) - f64::from(min_y);

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

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ fn generate_spritesheet(
}

let (shift_x, shift_y) = if args.no_crop {
(0, 0)
(0.0, 0.0)
} else {
image_util::crop_images(&mut images)?
};
Expand Down

0 comments on commit 716ba73

Please sign in to comment.