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
/// Image with pixel data
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Image {
    pub(crate) width: u32,
    pub(crate) height: u32,
    pub(crate) data: Vec<u8>,
}

impl Image {
    pub fn dimensions(&self) -> (u32, u32) {
        (self.width, self.height)
    }

    pub fn width(&self) -> u32 {
        self.width
    }

    pub fn height(&self) -> u32 {
        self.height
    }

    /// Flat slice of uncompressed SRGBA image data
    ///
    /// Length is `width * height * 4`
    ///
    /// Pattern is: `RGBARGBARGBA...`
    pub fn data(&'_ self) -> &'_ [u8] {
        &self.data[..]
    }

    /// Get R, G, B, A channels of pixel at (x, y)
    pub fn pixel(&self, x: u32, y: u32) -> (u8, u8, u8, u8) {
        debug_assert!(x < self.width);
        debug_assert!(y < self.height);

        let idx = (x + y * self.width) as usize;

        (
            self.data[idx + 0],
            self.data[idx + 1],
            self.data[idx + 2],
            self.data[idx + 3],
        )
    }
}