Skip to content

Commit

Permalink
feat: alpha crop limit (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
fgardt authored Mar 29, 2024
1 parent 5d22827 commit 6e46492
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spritter"
version = "0.5.3"
version = "0.6.0"
edition = "2021"
authors = ["fgardt <[email protected]>"]
description = "Spritesheet generator for factorio"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Options:
When set the tile resolution will be set to 64 unless specified otherwise.
--no-crop
Set when the sprites should not be cropped
-a, --crop-alpha <CROP_ALPHA>
Sets the max alpha value to consider a pixel as transparent [0-255].
Use a higher value in case your inputs have slightly transparent pixels and don't crop nicely. [default: 0]
-s, --scale <SCALE>
Set a scaling factor to rescale the used sprites by.
Values < 1.0 will shrink the sprites. Values > 1.0 will enlarge them. [default: 1]
Expand Down
18 changes: 13 additions & 5 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<(f64, f64)> {
pub fn crop_images(images: &mut Vec<RgbaImage>, limit: u8) -> ImgUtilResult<(f64, f64)> {
if images.is_empty() {
return Err(ImgUtilError::NoImagesToCrop);
}
Expand All @@ -89,13 +89,13 @@ pub fn crop_images(images: &mut Vec<RgbaImage>) -> ImgUtilResult<(f64, f64)> {

let mut x = image
.enumerate_pixels()
.filter_map(|(x, _, pxl)| if pxl[3] > 0 { Some(x) } else { None })
.filter_map(|(x, _, pxl)| if pxl[3] > limit { Some(x) } else { None })
.collect::<Vec<_>>();
x.sort_unstable();

let mut y = image
.enumerate_pixels()
.filter_map(|(_, y, pxl)| if pxl[3] > 0 { Some(y) } else { None })
.filter_map(|(_, y, pxl)| if pxl[3] > limit { Some(y) } else { None })
.collect::<Vec<_>>();
y.sort_unstable();

Expand Down Expand Up @@ -156,8 +156,16 @@ pub fn crop_images(images: &mut Vec<RgbaImage>) -> ImgUtilResult<(f64, f64)> {
}

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

if shift_x == 0.0 {
shift_x = 0.0;
}

if shift_y == 0.0 {
shift_y = 0.0;
}

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

Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ struct SpritesheetArgs {
#[clap(long, action)]
pub no_crop: bool,

/// Sets the max alpha value to consider a pixel as transparent [0-255].
/// Use a higher value in case your inputs have slightly transparent pixels and don't crop nicely.
#[clap(short = 'a', long, default_value_t = 0, verbatim_doc_comment)]
pub crop_alpha: u8,

/// Set a scaling factor to rescale the used sprites by.
/// Values < 1.0 will shrink the sprites. Values > 1.0 will enlarge them.
#[clap(short, long, default_value_t = 1.0, verbatim_doc_comment)]
Expand Down Expand Up @@ -468,7 +473,7 @@ fn generate_spritesheet(
let (shift_x, shift_y) = if args.no_crop {
(0.0, 0.0)
} else {
image_util::crop_images(&mut images)?
image_util::crop_images(&mut images, args.crop_alpha)?
};

#[allow(clippy::unwrap_used)]
Expand Down

0 comments on commit 6e46492

Please sign in to comment.