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
use std::slice;
use std::marker::PhantomData;

/// Rows of the image. Call `Img.rows()` to create it.
///
/// Each element is a slice `width` pixels wide. Ignores padding, if there's any.
#[derive(Debug)]
pub struct RowsIter<'a, T: 'a> {
    pub(crate) width: usize,
    pub(crate) inner: slice::Chunks<'a, T>,
}

impl<'a, T: 'a> Iterator for RowsIter<'a, T> {
    type Item = &'a [T];

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match self.inner.next() {
            Some(s) => Some(&s[0..self.width]),
            None => None,
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }

    #[inline]
    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        match self.inner.nth(n) {
            Some(s) => Some(&s[0..self.width]),
            None => None,
        }
    }

    #[inline]
    fn count(self) -> usize {
        self.inner.count()
    }
}

impl<'a, T> ExactSizeIterator for RowsIter<'a, T> {}

/// Rows of the image. Call `Img.rows_mut()` to create it.
///
/// Each element is a slice `width` pixels wide. Ignores padding, if there's any.
#[derive(Debug)]
pub struct RowsIterMut<'a, T: 'a> {
    pub(crate) width: usize,
    pub(crate) inner: slice::ChunksMut<'a, T>,
}

impl<'a, T: 'a> Iterator for RowsIterMut<'a, T> {
    type Item = &'a mut [T];

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match self.inner.next() {
            Some(s) => Some(&mut s[0..self.width]),
            None => None,
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }

    #[inline]
    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        match self.inner.nth(n) {
            Some(s) => Some(&mut s[0..self.width]),
            None => None,
        }
    }

    #[inline]
    fn count(self) -> usize {
        self.inner.count()
    }
}

impl<'a, T> ExactSizeIterator for RowsIterMut<'a, T> {}

/// Iterates over pixels in the (sub)image. Call `Img.pixels()` to create it.
///
/// Ignores padding, if there's any.
#[derive(Debug)]
pub struct PixelsIter<'a, T: Copy + 'a> {
    current: *const T,
    current_line_end: *const T,
    y: usize,
    width: usize,
    pad: usize,
    _dat: PhantomData<&'a [T]>,
}

impl<'a, T: Copy + 'a> PixelsIter<'a, T> {
    pub(crate) fn new(img: super::ImgRef<'a, T>) -> Self {
        let width = img.width();
        let stride = img.stride();
        debug_assert!(img.buf.len() > 0 && img.buf.len() >= stride * img.height() + width - stride);
        Self {
           current: img.buf[0..].as_ptr(),
           current_line_end: img.buf[width..].as_ptr(),
           width,
           y: img.height(),
           pad: stride - width,
           _dat: PhantomData,
       }
    }
}

impl<'a, T: Copy + 'a> Iterator for PixelsIter<'a, T> {
    type Item = T;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        unsafe {
            if self.current >= self.current_line_end {
                self.y -= 1;
                if self.y == 0 {
                    return None;
                }
                self.current = self.current_line_end.offset(self.pad as isize);
                self.current_line_end = self.current.offset(self.width as isize);
            }
            let px = *self.current;
            self.current = self.current.offset(1);
            Some(px)
        }
    }
}

#[test]
fn iter() {
    let img = super::Img::new(vec![1u8,2], 1,2);
    let mut it = img.pixels();
    assert_eq!(Some(1), it.next());
    assert_eq!(Some(2), it.next());
    assert_eq!(None, it.next());

    let buf = vec![1u8; (16+3)*(8+1)];
    for width in 1..16 {
        for height in 1..8 {
            for pad in 0..3 {
                let img = super::Img::new_stride(&buf[..], width, height, width+pad);
                assert_eq!(width*height, img.pixels().count());
                assert_eq!(height, img.rows().count());
                assert_eq!(width*height, img.pixels().map(|a| a as usize).sum());

                let mut iter1 = img.pixels();
                iter1.next();
                assert_eq!(width*height - 1, iter1.filter(|_| true).count());

                let mut iter2 = img.rows();
                iter2.next();
                assert_eq!(height - 1, iter2.size_hint().0);
                assert_eq!(height - 1, iter2.filter(|_| true).count());
            }
        }
    }
}