rom_thumbnails/
image.rs

1//! Types for handling images.
2//!
3
4/// SRGBA 8bits pixel.
5#[derive(Clone, Copy)]
6pub struct Pixel {
7    /// Red channel
8    pub r: u8,
9    /// Green channel
10    pub g: u8,
11    /// Blue channel
12    pub b: u8,
13    /// Alpha channel
14    pub a: u8,
15}
16
17/// SRGBA 8bits image.
18pub struct Image {
19    pixels: Vec<Pixel>,
20    width: usize,
21    #[allow(dead_code)]
22    height: usize,
23}
24
25impl Image {
26    /// Returns a new image with dimensions `width`✕`height`.
27    pub fn new(width: usize, height: usize) -> Self {
28        Self {
29            pixels: vec![
30                Pixel {
31                    r: 0,
32                    g: 0,
33                    b: 0,
34                    a: 0
35                };
36                width * height
37            ],
38            width,
39            height,
40        }
41    }
42
43    /// Set the value of pixel at coordinates (`x`,`y`).
44    pub fn put_pixel(&mut self, x: usize, y: usize, pixel: Pixel) {
45        self.pixels[self.width * y + x] = pixel;
46    }
47
48    /// Get a reference to the underlying pixels as a slice.
49    pub fn pixels(&self) -> &[Pixel] {
50        &self.pixels
51    }
52
53    /// Get a mutable reference to the underlying pixels `Vec<Pixel>`.
54    pub fn pixels_mut(&mut self) -> &mut Vec<Pixel> {
55        &mut self.pixels
56    }
57
58    /// Get image height
59    pub fn height(&self) -> usize {
60        self.height
61    }
62
63    /// Get image width
64    pub fn width(&self) -> usize {
65        self.height
66    }
67}
68
69mod tests {}