embedded_graphics/pixelcolor/raw/
iter.rs

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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use crate::pixelcolor::raw::{
    BigEndian, LittleEndian, RawData, RawU1, RawU16, RawU2, RawU24, RawU32, RawU4, RawU8,
};
use byteorder::{ByteOrder, BE, LE};
use core::marker::PhantomData;

/// Iterator over a slice of raw pixel data.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct RawDataIter<'a, R, BO> {
    /// Pixel data.
    data: &'a [u8],

    /// Index into `data` for next read.
    byte_position: usize,

    /// Remaining bits in the current byte (only used for bpp < 8).
    bits_left: u8,

    raw_type: PhantomData<R>,
    byte_order: PhantomData<BO>,
}

impl<'a, R, BO> RawDataIter<'a, R, BO> {
    /// Creates a new raw data iterator.
    pub fn new(data: &'a [u8]) -> Self {
        Self {
            data,
            byte_position: 0,
            bits_left: 8,
            raw_type: PhantomData,
            byte_order: PhantomData,
        }
    }

    /// Sets the read position.
    pub fn set_byte_position(&mut self, position: usize) {
        self.byte_position = position;
        self.bits_left = 8;
    }

    /// Align the read position to the next whole byte.
    ///
    /// If the read position is already at the beginning of a byte this is a noop.
    pub fn align(&mut self) {
        if self.bits_left == 8 {
            return;
        }

        self.byte_position = self.data.len().min(self.byte_position + 1);
        self.bits_left = 8;
    }

    /// Returns the next `bit_count` bits.
    fn next_bits(&mut self, bit_count: u8) -> Option<u8> {
        if self.bits_left == 0 || self.byte_position >= self.data.len() {
            return None;
        }

        let current_byte = self.data[self.byte_position];
        let ret = current_byte >> (self.bits_left - bit_count);

        self.bits_left -= bit_count;

        if self.bits_left == 0 && self.byte_position < self.data.len() {
            self.byte_position += 1;
            self.bits_left = 8;
        }

        Some(ret)
    }

    /// Returns the a slice of the next `byte_count` bytes.
    fn next_bytes(&mut self, byte_count: usize) -> Option<&[u8]> {
        if (self.data.len() - self.byte_position) >= byte_count {
            let ret = &self.data[self.byte_position..];

            self.byte_position += byte_count;

            Some(ret)
        } else {
            None
        }
    }
}

impl<'a, R> Iterator for RawDataIter<'a, R, LittleEndian>
where
    R: RawData,
{
    type Item = R;

    fn next(&mut self) -> Option<Self::Item> {
        R::next(self)
    }
}

impl<'a, R> Iterator for RawDataIter<'a, R, BigEndian>
where
    R: RawData,
{
    type Item = R;

    fn next(&mut self) -> Option<Self::Item> {
        R::next(self)
    }
}

/// Helper trait to implement the `next` method.
pub trait RawDataIterNext<BO>: Sized {
    /// Advances the iterator and returns the next raw value.
    fn next<'a>(iter: &mut RawDataIter<'a, Self, BO>) -> Option<Self>;
}

macro_rules! impl_next_for_bits {
    ($raw_type:ident, $bit_count:expr) => {
        impl<BO> RawDataIterNext<BO> for $raw_type {
            fn next<'a>(iter: &mut RawDataIter<'a, $raw_type, BO>) -> Option<$raw_type> {
                iter.next_bits($bit_count).map($raw_type::new)
            }
        }
    };
}

impl_next_for_bits!(RawU1, 1);
impl_next_for_bits!(RawU2, 2);
impl_next_for_bits!(RawU4, 4);

impl<BO> RawDataIterNext<BO> for RawU8 {
    fn next<'a>(iter: &mut RawDataIter<'a, RawU8, BO>) -> Option<RawU8> {
        iter.next_bytes(1).map(|data| RawU8::new(data[0]))
    }
}

macro_rules! impl_next_for_bytes {
    ($raw_type:ident, $byte_count:expr, $endian:ident, $read_function:path) => {
        impl RawDataIterNext<$endian> for $raw_type {
            fn next<'a>(iter: &mut RawDataIter<'a, $raw_type, $endian>) -> Option<$raw_type> {
                iter.next_bytes($byte_count)
                    .map(|data| $raw_type::new($read_function(data)))
            }
        }
    };
    ($raw_type:ident, $byte_count:expr, $read_function:ident) => {
        impl_next_for_bytes!($raw_type, $byte_count, BigEndian, BE::$read_function);
        impl_next_for_bytes!($raw_type, $byte_count, LittleEndian, LE::$read_function);
    };
}

impl_next_for_bytes!(RawU16, 2, read_u16);
impl_next_for_bytes!(RawU24, 3, read_u24);
impl_next_for_bytes!(RawU32, 4, read_u32);

/// Dummy implementation to allow () as `PixelColor::Raw`.
impl<BO> RawDataIterNext<BO> for () {
    fn next<'a>(_iter: &mut RawDataIter<'a, (), BO>) -> Option<()> {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const BITS_DATA: &[u8] = &[0x12, 0x48, 0x5A, 0x0F];
    const BYTES_DATA_1: &[u8] = &[0x10, 0x20, 0x30, 0x40, 0x50, 0x60];
    const BYTES_DATA_2: &[u8] = &[0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80];

    #[test]
    fn align_advances_to_next_byte() {
        let data = &[0x80, 0x80];

        let mut iter: RawDataIter<RawU1, LittleEndian> = RawDataIter::new(data);
        assert_eq!(iter.next(), Some(RawU1::new(1)));
        assert_eq!(iter.next(), Some(RawU1::new(0)));

        let mut iter: RawDataIter<RawU1, LittleEndian> = RawDataIter::new(data);
        assert_eq!(iter.next(), Some(RawU1::new(1)));
        iter.align();
        assert_eq!(iter.next(), Some(RawU1::new(1)));
    }

    #[test]
    fn calling_align_again_is_a_noop() {
        let data = &[0x80, 0xFF, 0x00];

        let mut iter: RawDataIter<RawU1, LittleEndian> = RawDataIter::new(data);
        assert_eq!(iter.next(), Some(RawU1::new(1)));
        iter.align();
        iter.align();
        assert_eq!(iter.next(), Some(RawU1::new(1)));
    }

    #[test]
    fn set_byte_position_resets_bit_position() {
        let data = &[0x0F, 0x0F];

        let mut iter: RawDataIter<RawU4, LittleEndian> = RawDataIter::new(data);
        assert_eq!(iter.next(), Some(RawU4::new(0)));
        iter.set_byte_position(1);
        assert_eq!(iter.next(), Some(RawU4::new(0)));
    }

    #[test]
    fn raw_u1() {
        #[rustfmt::skip]
        let expected = [
            0, 0, 0, 1,
            0, 0, 1, 0,
            0, 1, 0, 0,
            1, 0, 0, 0,
            0, 1, 0, 1,
            1, 0, 1, 0,
            0, 0, 0, 0,
            1, 1, 1, 1,
        ]
        .iter()
        .copied()
        .map(RawU1::new);

        let data: RawDataIter<RawU1, LittleEndian> = RawDataIter::new(BITS_DATA);
        assert!(data.eq(expected));
    }

    #[test]
    fn raw_u2() {
        let expected = [0, 1, 0, 2, 1, 0, 2, 0, 1, 1, 2, 2, 0, 0, 3, 3]
            .iter()
            .copied()
            .map(RawU2::new);

        let iter: RawDataIter<RawU2, LittleEndian> = RawDataIter::new(BITS_DATA);
        assert!(iter.eq(expected));
    }

    #[test]
    fn raw_u4() {
        let expected = [0x1, 0x2, 0x4, 0x8, 0x5, 0xA, 0x0, 0xF]
            .iter()
            .copied()
            .map(RawU4::new);

        let iter: RawDataIter<RawU4, LittleEndian> = RawDataIter::new(BITS_DATA);
        assert!(iter.eq(expected));
    }

    #[test]
    fn raw_u8() {
        let expected = BYTES_DATA_1.iter().map(|&v| RawU8::new(v));

        let iter: RawDataIter<RawU8, LittleEndian> = RawDataIter::new(BYTES_DATA_1);
        assert!(iter.eq(expected));
    }

    #[test]
    fn raw_u16_le() {
        let expected = [0x2010, 0x4030, 0x6050].iter().copied().map(RawU16::new);

        let iter: RawDataIter<RawU16, LittleEndian> = RawDataIter::new(BYTES_DATA_1);
        assert!(iter.eq(expected));
    }

    #[test]
    fn raw_u16_be() {
        let expected = [0x1020, 0x3040, 0x5060].iter().copied().map(RawU16::new);

        let iter: RawDataIter<RawU16, BigEndian> = RawDataIter::new(BYTES_DATA_1);
        assert!(iter.eq(expected));
    }

    #[test]
    fn raw_u16_excess_bytes_are_ignored() {
        let iter: RawDataIter<RawU16, LittleEndian> = RawDataIter::new(&[0; 3]);
        assert_eq!(iter.count(), 1);
    }

    #[test]
    fn raw_u24_le() {
        let expected = [0x302010, 0x605040].iter().copied().map(RawU24::new);

        let iter: RawDataIter<RawU24, LittleEndian> = RawDataIter::new(BYTES_DATA_1);
        assert!(iter.into_iter().eq(expected));
    }

    #[test]
    fn raw_u24_be() {
        let expected = [0x102030, 0x405060].iter().copied().map(RawU24::new);

        let iter: RawDataIter<RawU24, BigEndian> = RawDataIter::new(BYTES_DATA_1);
        assert!(iter.into_iter().eq(expected));
    }

    #[test]
    fn raw_u24_excess_bytes_are_ignored() {
        let iter: RawDataIter<RawU24, LittleEndian> = RawDataIter::new(&[0; 7]);
        assert_eq!(iter.count(), 2);
    }

    #[test]
    fn raw_u32_le() {
        let expected = [0x40302010, 0x80706050].iter().copied().map(RawU32::new);

        let iter: RawDataIter<RawU32, LittleEndian> = RawDataIter::new(BYTES_DATA_2);
        assert!(iter.into_iter().eq(expected));
    }

    #[test]
    fn raw_u32_be() {
        let expected = [0x10203040, 0x50607080].iter().copied().map(RawU32::new);

        let iter: RawDataIter<RawU32, BigEndian> = RawDataIter::new(BYTES_DATA_2);
        assert!(iter.into_iter().eq(expected));
    }

    #[test]
    fn raw_u32_excess_bytes_are_ignored() {
        let iter: RawDataIter<RawU32, LittleEndian> = RawDataIter::new(&[0; 13]);
        assert_eq!(iter.count(), 3);
    }
}