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
46
47
use crate::color::Color;
use crate::image::Image;
use crate::ty::Type;

use std::marker::PhantomData;

/// Image implementation backed by a mutable array reference
#[derive(Debug, PartialEq)]
pub struct ImageRef<'a, T: 'a + Type, C: Color> {
    width: usize,
    height: usize,
    data: &'a mut [T],
    _color: PhantomData<C>,
}

impl<'a, T: Type, C: Color> Image<T, C> for ImageRef<'a, T, C> {
    fn shape(&self) -> (usize, usize, usize) {
        (self.width, self.height, C::channels())
    }

    fn data(&self) -> &[T] {
        self.data
    }

    fn data_mut(&mut self) -> &mut [T] {
        self.data
    }
}

impl<'a, T: 'a + Type, C: Color> ImageRef<'a, T, C> {
    /// Create a new ImageRef with the given dimensions and data
    ///
    /// Note: this functions does not do any bounds checking regarding the specified size and
    /// length of the data
    pub fn new(width: usize, height: usize, data: &'a mut [T]) -> Self {
        ImageRef {
            width,
            height,
            data,
            _color: PhantomData,
        }
    }

    pub fn inner(self) -> &'a mut [T] {
        self.data
    }
}