Struct embedded_graphics::image::ImageRaw
source · pub struct ImageRaw<'a, C, BO = BigEndian>{ /* 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§
source§impl<'a, C, BO> ImageRaw<'a, C, BO>
impl<'a, C, BO> ImageRaw<'a, C, BO>
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.
source§impl<'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§
source§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>,
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>,
source§impl<'a, C, BO> Ord for ImageRaw<'a, C, BO>
impl<'a, C, BO> Ord for ImageRaw<'a, C, BO>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<C, BO> OriginDimensions for ImageRaw<'_, C, BO>
impl<C, BO> OriginDimensions for ImageRaw<'_, C, BO>
source§impl<'a, C, BO> PartialOrd for ImageRaw<'a, C, BO>
impl<'a, C, BO> PartialOrd for ImageRaw<'a, C, BO>
impl<'a, C, BO> Copy for ImageRaw<'a, C, BO>
impl<'a, C, BO> Eq for ImageRaw<'a, C, BO>
impl<'a, C, BO> StructuralPartialEq for ImageRaw<'a, C, BO>
Auto Trait Implementations§
impl<'a, C, BO> Freeze for ImageRaw<'a, C, BO>
impl<'a, C, BO> RefUnwindSafe for ImageRaw<'a, C, BO>where
C: RefUnwindSafe,
BO: RefUnwindSafe,
impl<'a, C, BO> Send for ImageRaw<'a, C, BO>
impl<'a, C, BO> Sync for ImageRaw<'a, C, BO>
impl<'a, C, BO> Unpin for ImageRaw<'a, C, BO>
impl<'a, C, BO> UnwindSafe for ImageRaw<'a, C, BO>where
C: UnwindSafe,
BO: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)