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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::color::Color;
use crate::image::Image;
use crate::ty::Type;

use std::marker::PhantomData;

/// Image implementation using `Vec<T>` to store data
#[cfg_attr(feature = "ser", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, PartialEq, Clone)]
pub struct ImageBuf<T: Type, C: Color> {
    width: usize,
    height: usize,
    data: Vec<T>,
    _color: PhantomData<C>,
}

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

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

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

impl<T: Type, C: Color> ImageBuf<T, C> {
    /// Create a new ImageBuf with the given size
    pub fn new(width: usize, height: usize) -> Self {
        ImageBuf {
            width,
            height,
            data: vec![T::zero(); width * height * C::channels()],
            _color: PhantomData,
        }
    }

    /// Convert the ImageBuf back to the underlying data buffer
    pub fn inner(self) -> Vec<T> {
        self.data
    }

    /// Create a new image with the same type, shape and color as an existing image
    pub fn new_like(&self) -> Self {
        Self::new(self.width, self.height)
    }

    /// Create a new image with the given type and the same shape and color
    pub fn new_like_with_type<U: Type>(&self) -> ImageBuf<U, C> {
        ImageBuf::new(self.width, self.height)
    }

    /// Create a new image with the given color and the same shape and type
    pub fn new_like_with_color<D: Color>(&self) -> ImageBuf<T, D> {
        ImageBuf::new(self.width, self.height)
    }

    /// Create a new image from existing data
    ///
    /// Note: This function does not do bounds checking, so you need to ensure that `data` is the
    /// correct length to handle the specified width and height
    pub fn new_from(width: usize, height: usize, data: Vec<T>) -> Self {
        ImageBuf {
            width,
            height,
            data,
            _color: PhantomData,
        }
    }
}