1pub mod prelude {
2 pub use super::ImageData;
3}
4
5use crate::pixel::Pixel;
6
7#[derive(Clone)]
8pub struct ImageData<P>
9where
10 P: Pixel,
11{
12 pub size: (u32, u32),
13 pub data: Vec<P>,
14}
15
16impl<P> ImageData<P>
17where
18 P: Pixel,
19{
20 pub fn new(size: (u32, u32)) -> Self {
21 let pixel_count = (size.0 as usize) * (size.1 as usize);
22 let mut data = vec![P::DEFAULT; pixel_count];
23 data.shrink_to_fit();
24
25 Self { size, data }
26 }
27
28 pub fn resize(&mut self, size: (u32, u32)) {
29 self.size = size;
30
31 let pixel_count = (size.0 as usize) * (size.1 as usize);
32 self.data.resize(pixel_count, P::DEFAULT);
33 }
34
35 pub(crate) fn flip_vertically(&mut self) {
36 let (w, h) = self.size;
37 for y in 0..(h / 2) {
38 for x in 0..w {
39 let top = (x + y * w) as usize;
40 let bottom = (x + (h - y - 1) * w) as usize;
41 self.data.swap(top, bottom);
42 }
43 }
44 }
45}