-
Notifications
You must be signed in to change notification settings - Fork 25
/
image.rs
192 lines (147 loc) · 5.29 KB
/
image.rs
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
//! Simple containers to track images and perform operations on them.
use std::io::{Read, Write};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ImageFormat {
Rgba8,
}
impl ImageFormat {
fn stride(&self) -> u32 {
match self {
ImageFormat::Rgba8 => 4,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Pixel {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Pixel {
pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
}
#[derive(Debug, Clone)]
pub(crate) struct Image {
size: (u32, u32),
data: Vec<u8>,
format: ImageFormat,
}
impl Image {
pub fn new_rgba8<D: Into<Vec<u8>>>(size: (u32, u32), data: D) -> Self {
let data = data.into();
let format = ImageFormat::Rgba8;
assert!(data.len() == (size.0 * size.1 * format.stride()) as usize);
Self { size, data, format }
}
pub fn new_empty_rgba8(size: (u32, u32)) -> Self {
let data = vec![0; (size.0 * size.1 * ImageFormat::Rgba8.stride()) as usize];
Self::new_rgba8(size, data)
}
pub fn decode_png<R: Read>(input: R) -> Result<Self, png::DecodingError> {
let decoder = png::Decoder::new(input);
// Get the metadata we need from the image and read its data into a
// buffer for processing by the sprite packing algorithm
let (info, mut reader) = decoder.read_info()?;
// TODO: Transcode images to RGBA if possible
if info.color_type != png::ColorType::RGBA {
return Err(png::DecodingError::Other(
format!(
"Color format {:?} is not supported by Tarmac.",
info.color_type
)
.into(),
));
}
let mut data = vec![0; info.buffer_size()];
reader.next_frame(&mut data)?;
let size = (info.width, info.height);
Ok(Self::new_rgba8(size, data))
}
pub fn encode_png<W: Write>(&self, output: W) -> Result<(), png::EncodingError> {
let mut encoder = png::Encoder::new(output, self.size.0, self.size.1);
match self.format {
ImageFormat::Rgba8 => {
encoder.set_color(png::ColorType::RGBA);
encoder.set_depth(png::BitDepth::Eight);
}
}
let mut output_writer = encoder.write_header()?;
output_writer.write_image_data(&self.data)?;
// On drop, output_writer will write the last chunk of the PNG file.
Ok(())
}
pub fn size(&self) -> (u32, u32) {
self.size
}
pub fn blit(&mut self, other: &Image, pos: (u32, u32)) {
assert!(self.format == ImageFormat::Rgba8 && other.format == ImageFormat::Rgba8);
let stride = self.format.stride();
let other_width_bytes = other.size.0 * stride;
let other_rows = other.data.chunks_exact((other_width_bytes) as usize);
for (other_y, other_row) in other_rows.enumerate() {
let self_y = pos.1 + other_y as u32;
let start_px = pos.0 + self.size.0 * self_y;
let start_in_bytes = (stride * start_px) as usize;
let end_in_bytes = start_in_bytes + other_row.len();
let self_row = &mut self.data[start_in_bytes..end_in_bytes];
self_row.copy_from_slice(other_row);
}
}
pub fn get_pixel(&self, pos: (u32, u32)) -> Pixel {
assert!(pos.0 < self.size.0);
assert!(pos.1 < self.size.1);
let stride = self.format.stride() as usize;
let start = stride * (pos.0 + pos.1 * self.size.0) as usize;
Pixel {
r: self.data[start],
g: self.data[start + 1],
b: self.data[start + 2],
a: self.data[start + 3],
}
}
pub fn set_pixel(&mut self, pos: (u32, u32), pixel: Pixel) {
assert!(pos.0 < self.size.0);
assert!(pos.1 < self.size.1);
let stride = self.format.stride() as usize;
let start = stride * (pos.0 + pos.1 * self.size.0) as usize;
self.data[start] = pixel.r;
self.data[start + 1] = pixel.g;
self.data[start + 2] = pixel.b;
self.data[start + 3] = pixel.a;
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn blit_zero() {
let source = Image::new_empty_rgba8((17, 20));
let mut target = Image::new_empty_rgba8((17, 20));
target.blit(&source, (0, 0));
}
#[test]
fn blit_corner() {
let source = Image::new_empty_rgba8((4, 4));
let mut target = Image::new_empty_rgba8((8, 8));
target.blit(&source, (4, 4));
}
#[test]
fn get_pixel() {
let source = Image::new_empty_rgba8((4, 4));
assert_eq!(source.get_pixel((0, 0)), Pixel::new(0, 0, 0, 0));
assert_eq!(source.get_pixel((3, 3)), Pixel::new(0, 0, 0, 0));
}
#[test]
fn set_pixel() {
let mut source = Image::new_empty_rgba8((3, 3));
source.set_pixel((0, 0), Pixel::new(1, 2, 3, 4));
assert_eq!(source.get_pixel((0, 0)), Pixel::new(1, 2, 3, 4));
source.set_pixel((2, 2), Pixel::new(5, 6, 7, 8));
assert_eq!(source.get_pixel((2, 2)), Pixel::new(5, 6, 7, 8));
assert_eq!(&source.data[0..4], &[1, 2, 3, 4]);
assert_eq!(&source.data[(source.data.len() - 4)..], &[5, 6, 7, 8]);
}
}