image2/
data.rs

1use crate::*;
2
3/// Wraps image data slices, tagging them with a Color type
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
5pub struct Data<'a, T: 'a + Type, C: 'a + Color>(&'a [T], std::marker::PhantomData<C>);
6
7/// Wraps mutable image data slices, tagging them with a Color type
8#[derive(Debug, PartialEq, Eq, PartialOrd)]
9pub struct DataMut<'a, T: 'a + Type, C: 'a + Color>(&'a mut [T], std::marker::PhantomData<C>);
10
11impl<'a, T: Type, C: Color> Data<'a, T, C> {
12    #[inline]
13    pub(crate) fn new(data: &'a [T]) -> Self {
14        Data(data, std::marker::PhantomData)
15    }
16
17    /// Number of elements
18    #[inline]
19    pub fn len(&self) -> usize {
20        self.0.len()
21    }
22
23    /// Returns true when the inner slice is empty
24    #[inline]
25    pub fn is_empty(&self) -> bool {
26        self.0.is_empty()
27    }
28
29    /// Get the number of pixels available
30    #[inline]
31    pub fn num_pixels(&self) -> usize {
32        self.len() / C::CHANNELS
33    }
34
35    /// Get the number of channels
36    #[inline]
37    pub fn channels(&self) -> usize {
38        C::CHANNELS
39    }
40
41    /// Get information about data
42    pub fn meta(&self) -> Meta<T, C> {
43        Meta::new((self.num_pixels(), 1))
44    }
45
46    /// Convert to pixel
47    pub fn to_pixel(&self) -> Pixel<C> {
48        Pixel::from_slice(self)
49    }
50
51    /// Get inner slice
52    pub fn as_slice(&self) -> &[T] {
53        self.0
54    }
55}
56
57impl<'a, T: Type, C: Color> DataMut<'a, T, C> {
58    #[inline]
59    pub(crate) fn new(data: &'a mut [T]) -> Self {
60        DataMut(data, std::marker::PhantomData)
61    }
62
63    /// Number of elements
64    #[inline]
65    pub fn len(&self) -> usize {
66        self.0.len()
67    }
68
69    /// Returns true when the inner slice is empty
70    #[inline]
71    pub fn is_empty(&self) -> bool {
72        self.0.is_empty()
73    }
74
75    /// Get the number of pixels availible
76    #[inline]
77    pub fn num_pixels(&self) -> usize {
78        self.len() / C::CHANNELS
79    }
80
81    /// Get the number of channels
82    #[inline]
83    pub fn channels(&self) -> usize {
84        C::CHANNELS
85    }
86
87    /// Copy values from slice
88    #[inline]
89    pub fn copy_from_slice(&mut self, slice: impl AsRef<[T]>) {
90        self.0.copy_from_slice(slice.as_ref())
91    }
92
93    /// Get information about data
94    pub fn meta(&self) -> Meta<T, C> {
95        Meta::new((self.num_pixels(), 1))
96    }
97
98    /// Convert to pixel
99    pub fn to_pixel(&self) -> Pixel<C> {
100        Pixel::from_slice(self)
101    }
102
103    /// Get inner slice
104    pub fn as_slice(&self) -> &[T] {
105        self.0
106    }
107
108    /// Get mutable inner slice
109    pub fn as_slice_mut(&mut self) -> &mut [T] {
110        self.0
111    }
112
113    /// Downcast to `Data` type
114    pub fn as_data(&'a self) -> Data<'a, T, C> {
115        Data::new(self.0)
116    }
117}
118
119impl<'a, T: Type, C: Color> AsRef<[T]> for Data<'a, T, C> {
120    fn as_ref(&self) -> &[T] {
121        self.0
122    }
123}
124
125impl<'a, T: Type, C: Color> AsRef<[T]> for DataMut<'a, T, C> {
126    fn as_ref(&self) -> &[T] {
127        self.0
128    }
129}
130
131impl<'a, T: Type, C: Color> AsMut<[T]> for DataMut<'a, T, C> {
132    fn as_mut(&mut self) -> &mut [T] {
133        self.0
134    }
135}
136
137impl<'a, T: Type, C: Color> std::ops::Index<usize> for Data<'a, T, C> {
138    type Output = T;
139    fn index(&self, i: usize) -> &T {
140        &self.0[i]
141    }
142}
143
144impl<'a, T: Type, C: Color> std::ops::Index<usize> for DataMut<'a, T, C> {
145    type Output = T;
146    fn index(&self, i: usize) -> &T {
147        &self.0[i]
148    }
149}
150
151impl<'a, T: Type, C: Color> std::ops::IndexMut<usize> for DataMut<'a, T, C> {
152    fn index_mut(&mut self, i: usize) -> &mut T {
153        &mut self.0[i]
154    }
155}
156
157impl<'a, T: 'a + Type, C: 'a + Color> IntoIterator for Data<'a, T, C> {
158    type Item = &'a T;
159    type IntoIter = std::slice::Iter<'a, T>;
160
161    fn into_iter(self) -> std::slice::Iter<'a, T> {
162        self.0.iter()
163    }
164}
165
166impl<'a, T: 'a + Type, C: 'a + Color> IntoIterator for DataMut<'a, T, C> {
167    type Item = &'a mut T;
168    type IntoIter = std::slice::IterMut<'a, T>;
169
170    fn into_iter(self) -> std::slice::IterMut<'a, T> {
171        self.0.iter_mut()
172    }
173}