Struct embedded_graphics::image::ImageRaw
source · [−]pub struct ImageRaw<'a, C, BO = BigEndian> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder, { /* private fields */ }
Expand description
An image constructed from a slice of raw pixel data.
The ImageRaw
struct can be used to construct an image from a slice
of raw image data. The storage format is determined by the PixelColor
type C
and the ByteOrder
BO
. The byteorder doesn’t need to be
specified for colors which aren’t stored in multiple bytes.
For color types with less than 8 bits per pixels the start of each row is aligned to the next whole byte.
Details about the conversion of raw data to color types are explained in the
raw
module documentation.
To draw an ImageRaw
object it needs to be wrapped in an Image
object.
Examples
Draw a 1BPP image
This example creates an image from 1 bit per pixel data.
use embedded_graphics::{
image::{Image, ImageRaw},
pixelcolor::BinaryColor,
prelude::*,
};
/// 12 x 5 pixel image with 1 bit per pixel.
/// The data for each row is 12 bits long and is padded with zeros on the
/// end because each row needs to contain a whole number of bytes.
#[rustfmt::skip]
const DATA: &[u8] = &[
0b11101111, 0b0101_0000,
0b10001000, 0b0101_0000,
0b11101011, 0b0101_0000,
0b10001001, 0b0101_0000,
0b11101111, 0b0101_0000,
];
// The image dimensions and the format of the stored raw data must be specified
// when the `new` function is called. The data format can, for example, be specified
// by using the turbofish syntax. For the image dimensions only the width must be
// passed to the `new` function. The image height will be calculated based on the
// length of the image data and the data format.
let raw_image = ImageRaw::<BinaryColor>::new(DATA, 12);
let image = Image::new(&raw_image, Point::zero());
let mut display = Display::default();
image.draw(&mut display)?;
Draw an image that uses multibyte pixel encoding
Colors with more than one byte per pixel need an additional type annotation for the byte order.
For convenience, the ImageRawBE
and ImageRawLE
type aliases can be used to abbreviate
the type.
use embedded_graphics::{
image::{Image, ImageRaw, ImageRawBE, ImageRawLE},
pixelcolor::{
raw::{BigEndian, LittleEndian},
Rgb565, Rgb888,
},
prelude::*,
};
// Rgb888 image with 24 bits per pixel and big endian byte order
let image1 = ImageRawBE::<Rgb888>::new(DATA, 8);
// or:
let image2 = ImageRaw::<Rgb888, BigEndian>::new(DATA, 8);
// Rgb565 image with 16 bits per pixel and little endian byte order
let image1 = ImageRawLE::<Rgb565>::new(DATA, 16);
// or:
let image2 = ImageRaw::<Rgb565, LittleEndian>::new(DATA, 16);
Implementations
sourceimpl<'a, C, BO> ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
impl<'a, C, BO> ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
sourcepub fn new(data: &'a [u8], width: u32) -> Self
pub fn new(data: &'a [u8], width: u32) -> Self
Creates a new image.
Only the width of the image needs to be specified. The height of the image will be calculated based on the length of the given image data. If the length of the image data isn’t an integer multiple of the data length for a single row the last partial row will be ignored.
sourceimpl<'a> ImageRaw<'a, BinaryColor>
impl<'a> ImageRaw<'a, BinaryColor>
sourcepub const fn new_binary(data: &'a [u8], width: u32) -> Self
pub const fn new_binary(data: &'a [u8], width: u32) -> Self
Creates a new binary image.
Due to const fn
limitations the new
method cannot be used in const
contexts. This
method provides a workaround to create ImageRaw
s with BinaryColor
images.
Only the width of the image needs to be specified. The height of the image will be calculated based on the length of the given image data.
Panics
This function panics if width == 0
.
Trait Implementations
sourceimpl<'a, C: Clone, BO: Clone> Clone for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
impl<'a, C: Clone, BO: Clone> Clone for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
sourceimpl<'a, C: Debug, BO: Debug> Debug for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
impl<'a, C: Debug, BO: Debug> Debug for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
sourceimpl<'a, C: Hash, BO: Hash> Hash for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
impl<'a, C: Hash, BO: Hash> Hash for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
sourceimpl<'a, C, BO> ImageDrawable for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
RawDataSlice<'a, C::Raw, BO>: IntoIterator<Item = C::Raw>,
impl<'a, C, BO> ImageDrawable for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
RawDataSlice<'a, C::Raw, BO>: IntoIterator<Item = C::Raw>,
type Color = C
type Color = C
The color type.
sourcefn draw<D>(&self, target: &mut D) -> Result<(), D::Error> where
D: DrawTarget<Color = C>,
fn draw<D>(&self, target: &mut D) -> Result<(), D::Error> where
D: DrawTarget<Color = C>,
Draws the entire image to the target. Read more
sourcefn draw_sub_image<D>(
&self,
target: &mut D,
area: &Rectangle
) -> Result<(), D::Error> where
D: DrawTarget<Color = Self::Color>,
fn draw_sub_image<D>(
&self,
target: &mut D,
area: &Rectangle
) -> Result<(), D::Error> where
D: DrawTarget<Color = Self::Color>,
Draws a part of the image to the target. Read more
sourceimpl<'a, C: Ord, BO: Ord> Ord for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
impl<'a, C: Ord, BO: Ord> Ord for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
sourceimpl<C, BO> OriginDimensions for ImageRaw<'_, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
impl<C, BO> OriginDimensions for ImageRaw<'_, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
sourceimpl<'a, C: PartialEq, BO: PartialEq> PartialEq<ImageRaw<'a, C, BO>> for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
impl<'a, C: PartialEq, BO: PartialEq> PartialEq<ImageRaw<'a, C, BO>> for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
sourceimpl<'a, C: PartialOrd, BO: PartialOrd> PartialOrd<ImageRaw<'a, C, BO>> for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
impl<'a, C: PartialOrd, BO: PartialOrd> PartialOrd<ImageRaw<'a, C, BO>> for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
sourcefn partial_cmp(&self, other: &ImageRaw<'a, C, BO>) -> Option<Ordering>
fn partial_cmp(&self, other: &ImageRaw<'a, C, BO>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl<'a, C: Copy, BO: Copy> Copy for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
impl<'a, C: Eq, BO: Eq> Eq for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
impl<'a, C, BO> StructuralEq for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
impl<'a, C, BO> StructuralPartialEq for ImageRaw<'a, C, BO> where
C: PixelColor + From<<C as PixelColor>::Raw>,
BO: ByteOrder,
Auto Trait Implementations
impl<'a, C, BO> RefUnwindSafe for ImageRaw<'a, C, BO> where
BO: RefUnwindSafe,
C: RefUnwindSafe,
impl<'a, C, BO> Send for ImageRaw<'a, C, BO> where
BO: Send,
C: Send,
impl<'a, C, BO> Sync for ImageRaw<'a, C, BO> where
BO: Sync,
C: Sync,
impl<'a, C, BO> Unpin for ImageRaw<'a, C, BO> where
BO: Unpin,
C: Unpin,
impl<'a, C, BO> UnwindSafe for ImageRaw<'a, C, BO> where
BO: UnwindSafe,
C: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> CheckedAs for T
impl<T> CheckedAs for T
sourcepub fn checked_as<Dst>(self) -> Option<Dst> where
T: CheckedCast<Dst>,
pub fn checked_as<Dst>(self) -> Option<Dst> where
T: CheckedCast<Dst>,
Casts the value.
sourceimpl<Src, Dst> CheckedCastFrom<Src> for Dst where
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dst where
Src: CheckedCast<Dst>,
sourcepub fn checked_cast_from(src: Src) -> Option<Dst>
pub fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
sourceimpl<T> Dimensions for T where
T: OriginDimensions,
impl<T> Dimensions for T where
T: OriginDimensions,
sourcepub fn bounding_box(&self) -> Rectangle
pub fn bounding_box(&self) -> Rectangle
Returns the bounding box.
sourceimpl<T> OverflowingAs for T
impl<T> OverflowingAs for T
sourcepub fn overflowing_as<Dst>(self) -> (Dst, bool) where
T: OverflowingCast<Dst>,
pub fn overflowing_as<Dst>(self) -> (Dst, bool) where
T: OverflowingCast<Dst>,
Casts the value.
sourceimpl<Src, Dst> OverflowingCastFrom<Src> for Dst where
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dst where
Src: OverflowingCast<Dst>,
sourcepub fn overflowing_cast_from(src: Src) -> (Dst, bool)
pub fn overflowing_cast_from(src: Src) -> (Dst, bool)
OverflowingCasts the value.
sourceimpl<T> SaturatingAs for T
impl<T> SaturatingAs for T
sourcepub fn saturating_as<Dst>(self) -> Dst where
T: SaturatingCast<Dst>,
pub fn saturating_as<Dst>(self) -> Dst where
T: SaturatingCast<Dst>,
Casts the value.
sourceimpl<Src, Dst> SaturatingCastFrom<Src> for Dst where
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dst where
Src: SaturatingCast<Dst>,
sourcepub fn saturating_cast_from(src: Src) -> Dst
pub fn saturating_cast_from(src: Src) -> Dst
Casts the value.
sourceimpl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
sourcepub fn unwrapped_as<Dst>(self) -> Dst where
T: UnwrappedCast<Dst>,
pub fn unwrapped_as<Dst>(self) -> Dst where
T: UnwrappedCast<Dst>,
Casts the value.
sourceimpl<Src, Dst> UnwrappedCastFrom<Src> for Dst where
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dst where
Src: UnwrappedCast<Dst>,
sourcepub fn unwrapped_cast_from(src: Src) -> Dst
pub fn unwrapped_cast_from(src: Src) -> Dst
UnwrappedCasts the value.
sourceimpl<T> WrappingAs for T
impl<T> WrappingAs for T
sourcepub fn wrapping_as<Dst>(self) -> Dst where
T: WrappingCast<Dst>,
pub fn wrapping_as<Dst>(self) -> Dst where
T: WrappingCast<Dst>,
Casts the value.
sourceimpl<Src, Dst> WrappingCastFrom<Src> for Dst where
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dst where
Src: WrappingCast<Dst>,
sourcepub fn wrapping_cast_from(src: Src) -> Dst
pub fn wrapping_cast_from(src: Src) -> Dst
WrappingCasts the value.