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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#![crate_name = "blit"]
extern crate bincode;
extern crate rayon;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate quick_error;
#[cfg(feature = "image")] extern crate image;
#[cfg(feature = "aseprite")] extern crate aseprite;
use std::cmp;
use std::io::{BufWriter, Read};
use std::fs::File;
use std::path::Path;
use std::error::Error;
use bincode::{serialize_into, deserialize};
use rayon::prelude::*;
#[cfg(feature = "image")]#[doc(hidden)] pub mod image_feature;
#[cfg(feature = "aseprite")] pub mod aseprite_feature;
#[cfg(feature = "aseprite")] pub use aseprite_feature::*;
trait BlittablePrimitive {
fn blit(&mut self, color: Self, mask: Self);
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq, Default)]
pub struct Color(u32);
impl Color {
pub fn from_u32(color: u32) -> Self {
Color(color | 0xFF000000)
}
pub fn from_u8(red: u8, green: u8, blue: u8) -> Self {
Color(0xFF000000 | (u32::from(red) << 16) | (u32::from(green) << 8) | u32::from(blue))
}
pub fn u32(&self) -> u32 {
self.0
}
}
impl BlittablePrimitive for Color {
fn blit(&mut self, color: Self, mask: Self) {
self.0 = self.0 & mask.0 | color.0;
}
}
impl BlittablePrimitive for u32 {
fn blit(&mut self, color: Self, mask: Self) {
*self = *self & mask | color;
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct BlitBuffer {
width: i32,
height: i32,
data: Vec<(Color, Color)>
}
impl BlitBuffer {
pub fn blit(&self, dst: &mut [u32], dst_width: usize, offset: (i32, i32)) {
let src_size = (self.width, self.height);
let dst_size = (dst_width as i32, (dst.len() / dst_width) as i32);
if offset == (0, 0) && dst_size == src_size {
dst.par_iter_mut().zip(&self.data[..]).for_each(|(pixel, &(color, mask))| {
pixel.blit(color.u32(), mask.u32());
});
return;
}
let dst_start = (cmp::max(offset.0, 0),
cmp::max(offset.1, 0));
let dst_end = (cmp::min(offset.0 + src_size.0, dst_size.0),
cmp::min(offset.1 + src_size.1, dst_size.1));
for dst_y in dst_start.1..dst_end.1 {
let src_y = dst_y - offset.1;
let dst_y_index = dst_y * dst_size.0;
let src_y_index = src_y * src_size.0;
for dst_x in dst_start.0..dst_end.0 {
let src_x = dst_x - offset.0;
let src_index = (src_x + src_y_index) as usize;
let dst_index = (dst_x + dst_y_index) as usize;
let (color, mask) = self.data[src_index];
dst[dst_index].blit(color.u32(), mask.u32());
}
}
}
pub fn blit_rect(&self, dst: &mut [u32], dst_width: usize, offset: (i32, i32), sub_rect: (i32, i32, i32, i32)) {
let dst_size = (dst_width as i32, (dst.len() / dst_width) as i32);
let src_size = (self.width, self.height);
let dst_start = (cmp::max(offset.0, 0),
cmp::max(offset.1, 0));
let dst_end = (cmp::min(offset.0 + sub_rect.2, dst_size.0),
cmp::min(offset.1 + sub_rect.3, dst_size.1));
for dst_y in dst_start.1..dst_end.1 {
let src_y = dst_y - offset.1 + sub_rect.1;
let dst_y_index = dst_y * dst_size.0;
let src_y_index = src_y * src_size.0;
for dst_x in dst_start.0..dst_end.0 {
let src_x = dst_x - offset.0 + sub_rect.0;
let src_index = (src_x + src_y_index) as usize;
let dst_index = (dst_x + dst_y_index) as usize;
let (color, mask) = self.data[src_index];
dst[dst_index].blit(color.u32(), mask.u32());
}
}
}
pub fn from_buffer(src: &[u32], width: i32, mask_color: Color) -> Self {
let height = src.len() as i32 / width;
let pixels = (width * height) as usize;
let mut data: Vec<(Color, Color)> = vec![(Color::from_u32(0), Color::from_u32(0)); pixels];
for index in 0..src.len() {
let pixel = Color::from_u32(src[index]);
if pixel == mask_color {
data[index].1 = Color::from_u32(0xFFFFFF);
} else {
data[index].0 = pixel;
}
}
BlitBuffer { width, height, data }
}
pub fn save<P>(&self, path: P) -> Result<(), Box<Error>> where P: AsRef<Path> {
let mut file = File::create(path)?;
{
let mut writer = BufWriter::new(&mut file);
serialize_into(&mut writer, &self)?;
}
file.sync_all()?;
Ok(())
}
pub fn open<P>(path: P) -> Result<Self, Box<Error>> where P: AsRef<Path> {
let mut file = File::open(path)?;
let mut data = Vec::new();
file.read_to_end(&mut data)?;
BlitBuffer::from_memory(&data[..])
}
pub fn from_memory(buffer: &[u8]) -> Result<Self, Box<Error>> {
let buffer = deserialize(buffer)?;
Ok(buffer)
}
pub fn size(&self) -> (i32, i32) {
(self.width, self.height)
}
}
pub trait BlitExt {
fn to_blit_buffer(&self, mask_color: Color) -> BlitBuffer;
fn blit(&self, dst: &mut [u32], dst_width: usize, offset: (i32, i32), mask_color: Color);
}