glance_core/img/
iterators.rs

1use super::{Image, pixel::Pixel};
2use rayon::prelude::*;
3
4pub struct PixelIter<'a, P: Pixel> {
5    iter: std::slice::Iter<'a, P>,
6}
7
8impl<'a, P: Pixel> PixelIter<'a, P> {
9    pub fn new(image: &'a Image<P>) -> Self {
10        Self {
11            iter: image.data.iter(),
12        }
13    }
14}
15
16impl<'a, P: Pixel> Iterator for PixelIter<'a, P> {
17    type Item = P;
18
19    fn next(&mut self) -> Option<Self::Item> {
20        self.iter.next().copied()
21    }
22}
23
24pub struct PixelIterMut<'a, P: Pixel> {
25    iter: std::slice::IterMut<'a, P>,
26}
27
28impl<'a, P: Pixel> PixelIterMut<'a, P> {
29    pub fn new(image: &'a mut Image<P>) -> Self {
30        Self {
31            iter: image.data.iter_mut(),
32        }
33    }
34}
35
36impl<'a, P: Pixel> Iterator for PixelIterMut<'a, P> {
37    type Item = &'a mut P;
38
39    fn next(&mut self) -> Option<Self::Item> {
40        self.iter.next()
41    }
42}
43
44impl<P> Image<P>
45where
46    P: Pixel,
47{
48    pub fn pixels(&self) -> PixelIter<P> {
49        PixelIter::new(self)
50    }
51
52    pub fn pixels_mut(&mut self) -> PixelIterMut<P> {
53        PixelIterMut::new(self)
54    }
55
56    pub fn par_pixels(&self) -> rayon::slice::Iter<'_, P> {
57        self.data.par_iter()
58    }
59
60    pub fn par_pixels_mut(&mut self) -> rayon::slice::IterMut<'_, P> {
61        self.data.par_iter_mut()
62    }
63}