1 | //! Pixel iterator. |
2 | |
3 | use crate::{geometry::Point, pixelcolor::PixelColor, Pixel}; |
4 | |
5 | /// Translated pixel iterator. |
6 | #[derive (Debug, PartialEq)] |
7 | #[cfg_attr (feature = "defmt" , derive(::defmt::Format))] |
8 | pub struct Translated<I> { |
9 | iter: I, |
10 | offset: Point, |
11 | } |
12 | |
13 | impl<I, C> Translated<I> |
14 | where |
15 | I: Iterator<Item = Pixel<C>>, |
16 | C: PixelColor, |
17 | { |
18 | pub(super) const fn new(iter: I, offset: Point) -> Self { |
19 | Self { iter, offset } |
20 | } |
21 | } |
22 | |
23 | impl<I, C> Iterator for Translated<I> |
24 | where |
25 | I: Iterator<Item = Pixel<C>>, |
26 | C: PixelColor, |
27 | { |
28 | type Item = I::Item; |
29 | |
30 | fn next(&mut self) -> Option<Self::Item> { |
31 | self.iter |
32 | .next() |
33 | .map(|Pixel(p: Point, c: C)| Pixel(p + self.offset, c)) |
34 | } |
35 | } |
36 | |
37 | #[cfg (test)] |
38 | mod tests { |
39 | use super::*; |
40 | use crate::{iterator::PixelIteratorExt, pixelcolor::BinaryColor}; |
41 | |
42 | #[test ] |
43 | fn translate() { |
44 | let pixels = [ |
45 | Pixel(Point::new(1, 2), BinaryColor::On), |
46 | Pixel(Point::new(3, 4), BinaryColor::On), |
47 | Pixel(Point::new(5, 6), BinaryColor::On), |
48 | ]; |
49 | let pixels = pixels.iter().copied(); |
50 | |
51 | let expected = [ |
52 | Pixel(Point::new(1 + 4, 2 + 5), BinaryColor::On), |
53 | Pixel(Point::new(3 + 4, 4 + 5), BinaryColor::On), |
54 | Pixel(Point::new(5 + 4, 6 + 5), BinaryColor::On), |
55 | ]; |
56 | let expected = expected.iter().copied(); |
57 | |
58 | assert!(pixels.translated(Point::new(4, 5)).eq(expected)); |
59 | } |
60 | } |
61 | |