[][src]Module embedded_graphics::image

Image support for embedded-graphics

The two main types used to draw images are ImageDrawable and Image.

ImageDrawable is implemented to add support for different image formats. This crate includes an implementation for raw pixel data. Additional implementations for other image formats are provided by external crates like tinybmp and tinytga.

The Image object is used to specify the location at which an ImageDrawable is drawn. Images are drawn relative to their top-left corner.

Examples

Load a TGA image and draw it to a display

This example loads a TGA-formatted image using the tinytga crate and draws it to the display using an Image object.

The graphics feature of tinytga needs to be enabled in Cargo.toml to use the Tga object with embedded-graphics.

use embedded_graphics::{image::Image, pixelcolor::Rgb888, prelude::*};
use tinytga::Tga;

let mut display: Display<Rgb888> = Display::default();

// Load the TGA file.
// Note that the color type is set explicitly to match the format used in the TGA file,
// otherwise the compiler might infer an incorrect type.
let tga: Tga<Rgb888> = Tga::from_slice(include_bytes!(
    "../../../simulator/examples/assets/rust-pride.tga"
))
.unwrap();

// Create a `Image` object to position the image at `Point::zero()`.
let image = Image::new(&tga, Point::zero());

// Draw the image to the display.
image.draw(&mut display)?;

Sub images

SubImages are used to split a larger image drawables into multiple parts, e.g. to draw a single sprite from a sprite atlas. Use the sub_image method provided by ImageDrawableExt to get a sub image from an image drawable. ImageDrawableExt is included in the prelude which this example takes advantage of.

use embedded_graphics::{image::Image, pixelcolor::Rgb888, prelude::*, primitives::Rectangle};
use tinytga::Tga;

let mut display: Display<Rgb888> = Display::default();

// Load the TGA file with the sprite atlas.
// Note that the color type is set explicitly to match the format used in the TGA file,
// otherwise the compiler might infer an incorrect type.
let sprite_atlas: Tga<Rgb888> = Tga::from_slice(include_bytes!(
    "../../../assets/tiles.tga"
))
.unwrap();

// Create individual sub images for each sprite in the sprite atlas.
// The position and size of the sub images is defined by a `Rectangle`.
let sprite_1 = sprite_atlas.sub_image(&Rectangle::new(Point::new(0, 0), Size::new(32, 32)));
let sprite_2 = sprite_atlas.sub_image(&Rectangle::new(Point::new(32, 0), Size::new(32, 32)));

// Create `Image` objects to draw the sprites at different positions on the display.
Image::new(&sprite_1, Point::new(100, 100)).draw(&mut display)?;
Image::new(&sprite_2, Point::new(100, 140)).draw(&mut display)?;

Implementing new image formats

To add embedded-graphics support for an new image format the ImageDrawable and OriginDimensions traits must be implemented. See the ImageDrawable documentation for more information.

Structs

Image

Image object.

ImageRaw

An image constructed from a slice of raw pixel data.

SubImage

Sub image.

Traits

ImageDrawable

Image drawable.

ImageDrawableExt

Extension trait for image drawables.

Type Definitions

ImageRawBE

Image with big endian data.

ImageRawLE

Image with little endian data.