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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::*;

/// Wraps image data slices, tagging them with a Color type
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
pub struct Data<'a, T: 'a + Type, C: 'a + Color>(&'a [T], std::marker::PhantomData<C>);

/// Wraps mutable image data slices, tagging them with a Color type
#[derive(Debug, PartialEq, Eq, PartialOrd)]
pub struct DataMut<'a, T: 'a + Type, C: 'a + Color>(&'a mut [T], std::marker::PhantomData<C>);

impl<'a, T: Type, C: Color> Data<'a, T, C> {
    #[inline]
    pub(crate) fn new(data: &'a [T]) -> Self {
        Data(data, std::marker::PhantomData)
    }

    /// Number of elements
    #[inline]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns true when the inner slice is empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Get the number of pixels available
    #[inline]
    pub fn num_pixels(&self) -> usize {
        self.len() / C::CHANNELS
    }

    /// Get the number of channels
    #[inline]
    pub fn channels(&self) -> usize {
        C::CHANNELS
    }

    /// Get information about data
    pub fn meta(&self) -> Meta<T, C> {
        Meta::new((self.num_pixels(), 1))
    }

    /// Convert to pixel
    pub fn to_pixel(&self) -> Pixel<C> {
        Pixel::from_slice(self)
    }

    /// Get inner slice
    pub fn as_slice(&self) -> &[T] {
        self.0
    }
}

impl<'a, T: Type, C: Color> DataMut<'a, T, C> {
    #[inline]
    pub(crate) fn new(data: &'a mut [T]) -> Self {
        DataMut(data, std::marker::PhantomData)
    }

    /// Number of elements
    #[inline]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns true when the inner slice is empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Get the number of pixels availible
    #[inline]
    pub fn num_pixels(&self) -> usize {
        self.len() / C::CHANNELS
    }

    /// Get the number of channels
    #[inline]
    pub fn channels(&self) -> usize {
        C::CHANNELS
    }

    /// Copy values from slice
    #[inline]
    pub fn copy_from_slice(&mut self, slice: impl AsRef<[T]>) {
        self.0.copy_from_slice(slice.as_ref())
    }

    /// Get information about data
    pub fn meta(&self) -> Meta<T, C> {
        Meta::new((self.num_pixels(), 1))
    }

    /// Convert to pixel
    pub fn to_pixel(&self) -> Pixel<C> {
        Pixel::from_slice(self)
    }

    /// Get inner slice
    pub fn as_slice(&self) -> &[T] {
        self.0
    }

    /// Get mutable inner slice
    pub fn as_slice_mut(&mut self) -> &mut [T] {
        self.0
    }

    /// Downcast to `Data` type
    pub fn as_data(&'a self) -> Data<'a, T, C> {
        Data::new(self.0)
    }
}

impl<'a, T: Type, C: Color> AsRef<[T]> for Data<'a, T, C> {
    fn as_ref(&self) -> &[T] {
        self.0
    }
}

impl<'a, T: Type, C: Color> AsRef<[T]> for DataMut<'a, T, C> {
    fn as_ref(&self) -> &[T] {
        self.0
    }
}

impl<'a, T: Type, C: Color> AsMut<[T]> for DataMut<'a, T, C> {
    fn as_mut(&mut self) -> &mut [T] {
        &mut self.0
    }
}

impl<'a, T: Type, C: Color> std::ops::Index<usize> for Data<'a, T, C> {
    type Output = T;
    fn index(&self, i: usize) -> &T {
        &self.0[i]
    }
}

impl<'a, T: Type, C: Color> std::ops::Index<usize> for DataMut<'a, T, C> {
    type Output = T;
    fn index(&self, i: usize) -> &T {
        &self.0[i]
    }
}

impl<'a, T: Type, C: Color> std::ops::IndexMut<usize> for DataMut<'a, T, C> {
    fn index_mut(&mut self, i: usize) -> &mut T {
        &mut self.0[i]
    }
}

impl<'a, T: 'a + Type, C: 'a + Color> IntoIterator for Data<'a, T, C> {
    type Item = &'a T;
    type IntoIter = std::slice::Iter<'a, T>;

    fn into_iter(self) -> std::slice::Iter<'a, T> {
        self.0.iter()
    }
}

impl<'a, T: 'a + Type, C: 'a + Color> IntoIterator for DataMut<'a, T, C> {
    type Item = &'a mut T;
    type IntoIter = std::slice::IterMut<'a, T>;

    fn into_iter(self) -> std::slice::IterMut<'a, T> {
        self.0.iter_mut()
    }
}