| 1 | use crate::image::SubImage; |
| 2 | use embedded_graphics_core::{image::ImageDrawable, primitives::Rectangle}; |
| 3 | |
| 4 | /// Extension trait for image drawables. |
| 5 | pub trait ImageDrawableExt: Sized { |
| 6 | /// Returns a sub image of this image drawable. |
| 7 | /// |
| 8 | /// If any of the given `area` lies outside the bounding box of the parent image, the |
| 9 | /// intersection of `area` and the bounding box will be used. |
| 10 | /// |
| 11 | /// # Examples |
| 12 | /// |
| 13 | /// This example loads a raw image containing multiple 16x16px sprites and draws two of them to |
| 14 | /// a display, with their top-left corners positioned at `(0, 0)` and `(32, 8)`. |
| 15 | /// |
| 16 | /// ```rust |
| 17 | /// use embedded_graphics::{ |
| 18 | /// image::{Image, ImageRaw, ImageRawBE}, |
| 19 | /// pixelcolor::Rgb565, |
| 20 | /// prelude::*, |
| 21 | /// primitives::Rectangle, |
| 22 | /// }; |
| 23 | /// # use embedded_graphics::mock_display::MockDisplay as Display; |
| 24 | /// # let mut display: Display<Rgb565> = Display::default(); |
| 25 | /// |
| 26 | /// let data = [ 0xF8, 0x00, 0x07, 0xE0, 0xFF, 0xE0, /* ... */ ]; |
| 27 | /// // or: let data = include_bytes!("sprite_atlas.raw"); |
| 28 | /// |
| 29 | /// # let data = [0u8; 32 * 16 * 2]; |
| 30 | /// let sprite_atlas = ImageRawBE::<Rgb565>::new(&data, 32); |
| 31 | /// |
| 32 | /// let sprite_1 = sprite_atlas.sub_image(&Rectangle::new(Point::new(0, 0), Size::new(16, 16))); |
| 33 | /// let sprite_2 = sprite_atlas.sub_image(&Rectangle::new(Point::new(16, 0), Size::new(16, 16))); |
| 34 | /// |
| 35 | /// Image::new(&sprite_1, Point::new(0, 0)).draw(&mut display)?; |
| 36 | /// Image::new(&sprite_2, Point::new(32, 8)).draw(&mut display)?; |
| 37 | /// |
| 38 | /// # Ok::<(), core::convert::Infallible>(()) |
| 39 | /// ``` |
| 40 | fn sub_image(&self, area: &Rectangle) -> SubImage<Self>; |
| 41 | } |
| 42 | |
| 43 | impl<T> ImageDrawableExt for T |
| 44 | where |
| 45 | T: ImageDrawable, |
| 46 | { |
| 47 | fn sub_image(&self, area: &Rectangle) -> SubImage<T> { |
| 48 | SubImage::new(self, area) |
| 49 | } |
| 50 | } |
| 51 | |