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
//! Iterators.

pub mod contiguous;
pub mod pixel;

use crate::{
    draw_target::DrawTarget, geometry::Point, pixelcolor::PixelColor, primitives::Rectangle, Pixel,
};

/// Produce an iterator over all pixels in an object.
///
/// This trait is implemented for _references_ to all styled items in embedded-graphics, therefore
/// does not consume the original item.
pub trait IntoPixels {
    /// The type of color for each pixel produced by the iterator returned from [`into_pixels`].
    ///
    /// [`into_pixels`]: #tymethod.into_pixels
    type Color: PixelColor;

    /// The iterator produced when calling [`into_pixels`].
    ///
    /// [`into_pixels`]: #tymethod.into_pixels
    type Iter: Iterator<Item = Pixel<Self::Color>>;

    /// Create an iterator over all pixels in the object.
    ///
    /// The iterator may return pixels in any order, however it may be beneficial for performance
    /// reasons to return them starting at the top left corner in row-first order.
    fn into_pixels(self) -> Self::Iter;
}

/// Extension trait for contiguous iterators.
pub trait ContiguousIteratorExt
where
    Self: Iterator + Sized,
    <Self as Iterator>::Item: PixelColor,
{
    /// Converts a contiguous iterator into a pixel iterator.
    fn into_pixels(self, bounding_box: &Rectangle) -> contiguous::IntoPixels<Self>;
}

impl<I> ContiguousIteratorExt for I
where
    I: Iterator,
    I::Item: PixelColor,
{
    fn into_pixels(self, bounding_box: &Rectangle) -> contiguous::IntoPixels<Self> {
        contiguous::IntoPixels::new(self, *bounding_box)
    }
}

/// Extension trait for pixel iterators.
pub trait PixelIteratorExt<C>
where
    Self: Sized,
    C: PixelColor,
{
    /// Draws the pixel iterator to a draw target.
    fn draw<D>(self, target: &mut D) -> Result<(), D::Error>
    where
        D: DrawTarget<Color = C>;

    /// Returns a translated version of the iterator.
    fn translate(self, offset: Point) -> pixel::Translate<Self>;
}

impl<I, C> PixelIteratorExt<C> for I
where
    C: PixelColor,
    I: Iterator<Item = Pixel<C>>,
{
    fn draw<D>(self, target: &mut D) -> Result<(), D::Error>
    where
        D: DrawTarget<Color = C>,
    {
        target.draw_iter(self)
    }

    fn translate(self, offset: Point) -> pixel::Translate<Self> {
        pixel::Translate::new(self, offset)
    }
}

#[cfg(test)]
mod tests {
    // NOTE: `crate` cannot be used here due to circular dependency resolution behaviour.
    use embedded_graphics::{
        geometry::Point, iterator::PixelIteratorExt, mock_display::MockDisplay,
        pixelcolor::BinaryColor, Pixel,
    };

    #[test]
    fn draw_pixel_iterator() {
        let pixels = [
            Pixel(Point::new(0, 0), BinaryColor::On),
            Pixel(Point::new(1, 0), BinaryColor::Off),
            Pixel(Point::new(2, 0), BinaryColor::On),
            Pixel(Point::new(2, 1), BinaryColor::Off),
        ];

        let mut display = MockDisplay::new();
        pixels.iter().copied().draw(&mut display).unwrap();

        assert_eq!(
            display,
            MockDisplay::from_pattern(&[
                "#.#", //
                "  .", //
            ])
        );
    }
}