Newer
Older
use num_traits::AsPrimitive;
use serde::{Deserialize, Serialize};
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::path::PathBuf;
#[inline(always)]
fn tile_size() -> u32 {
32
}
/// Take a spritesheet and split into individual sprites, skipping empty space
#[derive(Parser, Serialize, Deserialize, Clone, Debug)]
#[clap(author, version = "0.5.0")]
pub struct Split {
/// The path to the spritesheet file
#[serde(default)]
pub input: String,
/// The base path for all of the created sprites. Should be a directory, which will be created if it does not exist
#[serde(default)]
pub output: String,
/// The amount of horizontal space between each sprite in the image
#[clap(short = 'x', long, default_value_t = 0)]
#[serde(default)]
space_x: u32,
/// The amount of vertical space between each sprite in the image
#[clap(short = 'y', long, default_value_t = 0)]
#[serde(default)]
space_y: u32,
/// The amount of space around the edge of the image
#[clap(short, long, default_value_t = 0)]
#[serde(default)]
padding: u32,
/// The size of each tile in the spritesheet. Assumed to be square tiles
#[clap(short, long, default_value_t = 32)]
#[serde(default = "tile_size")]
tile_size: u32,
/// When set, files will be numbered consecutively, regardless of empty sprites. By default, files
/// will be numbered based on their inferred "tile id", based on position in the sheet
#[clap(short, long, default_value_t = false)]
#[serde(default)]
sequential: bool,
}
fn params_to_columns(
dimension: impl AsPrimitive<f32>,
tile_size: impl AsPrimitive<f32>,
padding: impl AsPrimitive<f32>,
spacing: impl AsPrimitive<f32>,
) -> u32 {
let dimension = dimension.as_();
let tile_size = tile_size.as_();
let padding = padding.as_();
let spacing = spacing.as_();
let base = (dimension - (2.0 * padding) - spacing) / (tile_size + spacing);
base.floor() as u32 + if spacing == 0.0 { 0 } else { 1 }
}
impl Split {
pub fn run(&self, image: &RgbaImage) -> anyhow::Result<()> {
log::info!(
"Image loaded with size {} x {}",
image.width(),
image.height()
);
let columns = params_to_columns(image.width(), self.tile_size, self.padding, self.space_x);
let rows = params_to_columns(image.height(), self.tile_size, self.padding, self.space_y);
log::info!("Inferred sheet contains {} columns", columns);
log::info!("Inferred sheet contains {} rows", rows);
std::fs::create_dir_all(&self.output)?;
let path_base = PathBuf::from(&self.output);
let mut file_count = 0;
for x in 0..columns {
for y in 0..rows {
let img_x = self.padding + (x * self.tile_size) + (x * self.space_x);
let img_y = self.padding + (y * self.tile_size) + (y * self.space_y);
// let img_y = y * self.tile_size;
let view = image.view(img_x, img_y, self.tile_size, self.tile_size);
if view
.pixels()
.any(|(_, _, pixel)| pixel.channels().get(3).map(|c| c > &0).unwrap_or(false))
{
view.to_image().save_with_format(
path_base.join(format!("{}.png", file_count)),
ImageFormat::Png,
)?;
if self.sequential {
file_count += 1;
}
}
if !self.sequential {
file_count += 1;
}
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
#[test]
fn no_pad_space() {
let columns = super::params_to_columns(160, 16, 0, 0);
let rows = super::params_to_columns(160, 16, 0, 0);
assert_eq!(columns, 10);
assert_eq!(rows, 10);
let columns = super::params_to_columns(512, 16, 0, 0);
let rows = super::params_to_columns(160, 16, 0, 0);
assert_eq!(columns, 32);
assert_eq!(rows, 10);
}
#[test]
fn some_pad() {
let columns = super::params_to_columns(168, 16, 4, 0);
let rows = super::params_to_columns(168, 16, 4, 0);
assert_eq!(columns, 10);
assert_eq!(rows, 10);
let columns = super::params_to_columns(520, 16, 4, 0);
let rows = super::params_to_columns(168, 16, 4, 0);
assert_eq!(columns, 32);
assert_eq!(rows, 10);
}
#[test]
fn some_space() {
let columns = super::params_to_columns(168, 16, 0, 0);
let rows = super::params_to_columns(168, 16, 0, 0);
assert_eq!(columns, 10);
assert_eq!(rows, 10);
let columns = super::params_to_columns(520, 16, 0, 0);
let rows = super::params_to_columns(168, 16, 0, 0);
assert_eq!(columns, 32);
assert_eq!(rows, 10);
}
#[test]
fn different_spacing() {
let space_x = 3;
let space_y = 2;
let columns = super::params_to_columns(189, 13, 0, space_x);
assert_eq!(columns, 12);
assert_eq!(rows, 9);
}
#[test]
fn correct_conversion() {
let columns = super::params_to_columns(434, 32, 3, 1);
let rows = super::params_to_columns(302, 32, 6, 1);
assert_eq!(columns, 13);
assert_eq!(rows, 9);
}
}