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
#![crate_name = "blit"]
extern crate image;
extern crate bincode;
#[macro_use]
extern crate serde_derive;
use std::cmp;
use std::io::{BufWriter, Read};
use std::fs::File;
use std::path::Path;
use std::error::Error;
use image::*;
use bincode::{serialize_into, deserialize, Infinite};
#[derive(Serialize, Deserialize)]
pub struct BlitBuffer {
width: usize,
height: usize,
color: Vec<u32>,
mask: Vec<u32>
}
impl BlitBuffer {
pub fn blit(&self, buffer: &mut Vec<u32>, buffer_size: (usize, usize), pos: (i32, i32)) {
if pos == (0, 0) && buffer_size.0 == self.width && buffer_size.1 == self.height {
for (pixel, (color, mask)) in buffer.iter_mut().zip(self.color.iter().zip(&self.mask)) {
*pixel = *pixel & *mask | *color;
}
return;
}
let min_x = cmp::max(-pos.0, 0);
let min_y = cmp::max(-pos.1, 0);
let max_x = cmp::min(buffer_size.0 as i32 - pos.0, self.width as i32);
let max_y = cmp::min(buffer_size.1 as i32 - pos.1, self.height as i32);
let mut y_index = min_y as usize;
for y in min_y..max_y {
let buffer_y = (y + pos.1) as usize * buffer_size.0;
for x in min_x..max_x {
let buffer_x = (x + pos.0) as usize;
let pixel = &mut buffer[buffer_x + buffer_y];
let index = x as usize + y_index;
*pixel = *pixel & self.mask[index] | self.color[index];
}
y_index += self.width;
}
}
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, Infinite)?;
}
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::load_from_memory(&data[..])
}
pub fn load_from_memory(buffer: &[u8]) -> Result<Self, Box<Error>> {
let buffer = deserialize(buffer)?;
Ok(buffer)
}
pub fn size(&self) -> (usize, usize) {
(self.width, self.height)
}
}
pub trait BlitExt {
fn as_blit_buffer(&self, mask_color: u32) -> BlitBuffer;
fn blit_with_mask_color(&self, buffer: &mut Vec<u32>, buffer_size: (usize, usize), pos: (i32, i32), mask_color: u32);
}
impl BlitExt for RgbImage {
fn as_blit_buffer(&self, mask_color: u32) -> BlitBuffer {
let (width, height) = self.dimensions();
let pixels = (width * height) as usize;
let mut color: Vec<u32> = vec![0; pixels];
let mut mask: Vec<u32> = vec![0; pixels];
let mask_correct = mask_color | 0xFF000000;
let mut index = 0;
for y in 0..height {
for x in 0..width {
let pixel = self.get_pixel(x, y).data;
let raw = 0xFF000000 | ((pixel[0] as u32) << 16) | ((pixel[1] as u32) << 8) | (pixel[2] as u32);
if raw == mask_correct {
mask[index] = 0xFFFFFF;
} else {
color[index] = raw;
}
index += 1;
}
}
BlitBuffer {
width: width as usize,
height: height as usize,
color,
mask
}
}
fn blit_with_mask_color(&self, buffer: &mut Vec<u32>, buffer_size: (usize, usize), pos: (i32, i32), mask_color: u32) {
let (width, height) = self.dimensions();
let mask_correct = mask_color | 0xFF000000;
let min_x = cmp::max(-pos.0, 0);
let min_y = cmp::max(-pos.1, 0);
let max_x = cmp::min(buffer_size.0 as i32 - pos.0, width as i32);
let max_y = cmp::min(buffer_size.1 as i32 - pos.1, height as i32);
for y in min_y..max_y {
for x in min_x..max_x {
let pixel = self.get_pixel(x as u32, y as u32).data;
let raw = 0xFF000000 | ((pixel[0] as u32) << 16) | ((pixel[1] as u32) << 8) | (pixel[2] as u32);
if raw != mask_correct {
let buffer_x = (x + pos.0) as usize;
let buffer_y = (y + pos.1) as usize;
let index = buffer_x + buffer_y * buffer_size.0;
buffer[index] = raw;
}
}
}
}
}