1
2
3
4
5
6
7
8
9
10
11
12
13
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
#![crate_name = "sprite_gen"]
extern crate rand;
use rand::{Rng, XorShiftRng};
#[derive(Copy, Clone)]
pub struct Options {
pub mirror_x: bool,
pub mirror_y: bool,
pub colored: bool,
pub edge_brightness: f64,
pub color_variations: f64,
pub brightness_noise: f64,
pub saturation: f64
}
impl Default for Options {
fn default() -> Self {
Options {
mirror_x: false,
mirror_y: false,
colored: true,
edge_brightness: 0.3,
color_variations: 0.2,
brightness_noise: 0.3,
saturation: 0.5
}
}
}
pub fn gen_sprite(mask_buffer: &[i8], mask_width: usize, options: Options) -> Vec<u32> {
let mask_height = mask_buffer.len() / mask_width;
let mut mask: Vec<i8> = mask_buffer.iter().cloned().collect();
let mut rng: XorShiftRng = rand::thread_rng().gen();
for val in mask.iter_mut() {
if *val == 1 {
*val = rng.next_f32().round() as i8;
} else if *val == 2 {
*val = (rng.next_f32().round() as i8) * 2 - 1;
}
}
for y in 0..mask_height {
for x in 0..mask_width {
let index = x + y * mask_width;
if mask[index] <= 0 {
continue;
}
if y > 0 && mask[index - mask_width] == 0 {
mask[index - mask_width] = -1;
}
if y < mask_height - 1 && mask[index + mask_width] == 0 {
mask[index + mask_width] = -1;
}
if x > 0 && mask[index - 1] == 0 {
mask[index - 1] = -1;
}
if x < mask_width - 1 && mask[index + 1] == 0 {
mask[index + 1] = -1;
}
}
}
if !options.mirror_x && !options.mirror_y {
return mask.iter().map(|&v| match v {
-1 => 0,
_ => 0xFFFFFFFF
}).collect();
} else if options.mirror_x && !options.mirror_y {
let width = mask_width * 2;
let mut result = vec![0; width * mask_height];
for y in 0..mask_height {
for x in 0..mask_width {
let value = match mask[x + y * mask_width] {
-1 => 0,
_ => 0xFFFFFFFF
};
let index = x + y * width;
result[index] = value;
let index = (width - x - 1) + y * width;
result[index] = value;
}
}
return result;
} else if options.mirror_y && !options.mirror_x {
let height = mask_height * 2;
let mut result = vec![0; mask_width * height];
for y in 0..mask_height {
for x in 0..mask_width {
let value = match mask[x + y * mask_width] {
-1 => 0,
_ => 0xFFFFFFFF
};
let index = x + y * mask_width;
result[index] = value;
let index = x + (height - y - 1) * mask_width;
result[index] = value;
}
}
return result;
} else {
let width = mask_width * 2;
let height = mask_height * 2;
let mut result = vec![0; width * height];
for y in 0..mask_height {
for x in 0..mask_width {
let value = match mask[x + y * mask_width] {
-1 => 0,
_ => 0xFFFFFFFF
};
let index = x + y * width;
result[index] = value;
let index = (width - x - 1) + y * width;
result[index] = value;
let index = x + (height - y - 1) * width;
result[index] = value;
let index = (width - x - 1) + (height - y - 1) * width;
result[index] = value;
}
}
return result;
}
}