term2d/model/
image.rs

1use super::point::Point;
2use super::rgba::Rgba;
3
4#[derive(Clone)]
5pub struct Image {
6    pub pixels: Vec<Rgba>,
7    pub size: Point,
8}
9
10impl From<(u32, u32, Vec<u8>)> for Image {
11    fn from(raw_image: (u32, u32, Vec<u8>)) -> Self {
12        let (width, height, image_bytes) = raw_image;
13
14        let mut image = Image {
15            pixels: Vec::new(),
16            size: Point::from((width, height)),
17        };
18
19        for i in 0..image_bytes.len() / 4 {
20            let rgba = Rgba {
21                r: image_bytes[4 * i],
22                g: image_bytes[4 * i + 1],
23                b: image_bytes[4 * i + 2],
24                a: image_bytes[4 * i + 3],
25            };
26            image.pixels.push(rgba);
27        }
28
29        image
30    }
31}
32
33impl Image {
34    pub fn mirror_y(&self) -> Self {
35        let mut mirrored_image = Self {
36            pixels: Vec::new(),
37            size: self.size.clone(),
38        };
39
40        for y in 0..self.size.height() {
41            for x in 0..self.size.width() {
42                let index = ((self.size.width() - 1 - x) + y * self.size.width()) as usize;
43                let rgba = self.pixels[index].clone();
44                mirrored_image.pixels.push(rgba);
45            }
46        }
47
48        mirrored_image
49    }
50}