All formats

tinytga

LanguageRust (no_std)
LicenseMIT or Apache 2.0
Repositorygithub.com/embedded-graphics/tinytga
Documentationdocs.rs/tinytga
Depends onembedded-graphics, nom
Read / writeyes / no; palette, grayscale and true-color, raw or RLE

tinytga parses a TGA file from a byte slice and yields pixels one at a time, with no heap allocation beyond parsing the header. It’s made for microcontrollers: embed images in flash with include_bytes! and draw them with embedded-graphics. TGA suits this use because it decodes with almost no memory, unlike PNG.

Use the image crate on desktops and servers; tinytga can’t write files and has no image-processing functions.

Installation#

Shell
cargo add tinytga embedded-graphics

Basic usage: draw a TGA on a display#

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

// display: any embedded-graphics DrawTarget<Color = Rgb565>
fn draw_logo<D: DrawTarget<Color = Rgb565>>(display: &mut D) -> Result<(), D::Error> {
    let data = include_bytes!("logo.tga");
    let tga: Tga<Rgb565> = Tga::from_slice(data).unwrap();  // colors converted to Rgb565
    Image::new(&tga, Point::new(10, 10)).draw(display)?;
    Ok(())
}

How to get the image size and pixels#

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

let data = include_bytes!("icon.tga");
let img: Tga<Rgb888> = Tga::from_slice(data).unwrap();
let size = img.size();  // Size { width, height }
for Pixel(point, color) in img.pixels() {
    // point: position, color: Rgb888
}

How to read the TGA header and raw pixel values#

RawTga gives the header and pixel values as u32 without converting colors:

Rust
use tinytga::{RawTga, Compression, ImageOrigin};

let data = include_bytes!("icon.tga");
let raw = RawTga::from_slice(data).unwrap();
let header = raw.header();
println!("{}x{}, {:?}, rle: {}, top-left: {}",
    header.width, header.height, header.pixel_depth,
    header.compression == Compression::Rle,
    header.image_origin == ImageOrigin::TopLeft);
for pixel in raw.pixels() {
    let _value: u32 = pixel.color;  // pixel.position holds the coordinates
}

Tips#