Skip to main content

box_image_pyramid/
imageview.rs

1/// Minimal borrowed grayscale image view (u8, row-major).
2#[derive(Copy, Clone, Debug)]
3pub struct ImageView<'a> {
4    pub data: &'a [u8],
5    pub width: usize,
6    pub height: usize,
7}
8
9impl<'a> ImageView<'a> {
10    /// Create a view from a raw u8 slice.
11    ///
12    /// Returns `None` if `data.len() != width * height`.
13    pub fn new(width: usize, height: usize, data: &'a [u8]) -> Option<Self> {
14        if width.checked_mul(height)? != data.len() {
15            return None;
16        }
17        Some(Self {
18            data,
19            width,
20            height,
21        })
22    }
23}
24
25/// Owned grayscale image buffer (u8).
26#[derive(Clone, Debug)]
27pub struct ImageBuffer {
28    pub width: usize,
29    pub height: usize,
30    pub data: Vec<u8>,
31}
32
33impl ImageBuffer {
34    pub fn new(width: usize, height: usize) -> Self {
35        Self {
36            width,
37            height,
38            data: vec![0; width.saturating_mul(height)],
39        }
40    }
41
42    pub fn as_view(&self) -> ImageView<'_> {
43        ImageView::new(self.width, self.height, &self.data).unwrap()
44    }
45}