Git Product home page Git Product logo

tinytga's Introduction

TinyTGA

Build Status Crates.io Docs.rs embedded-graphics on Matrix

A small TGA parser designed for use with embedded-graphics targeting no-std environments but usable anywhere. Beyond parsing the image header, no other allocations are made.

tinytga provides two methods of accessing the pixel data inside a TGA file. The most convenient way is to use a color type provided by embedded-graphics to define the format stored inside the TGA file. But it is also possible to directly access the raw pixel representation instead.

Examples

Using Tga to draw an image

This example demonstrates how a TGA image can be drawn to a embedded-graphics draw target.

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

// Include an image from a local path as bytes
let data = include_bytes!("../tests/chessboard_4px_rle.tga");

let tga: Tga<Rgb888> = Tga::from_slice(data).unwrap();

let image = Image::new(&tga, Point::zero());

image.draw(&mut display)?;

Accessing pixels using an embedded-graphics color type

If embedded-graphics is not used to draw the TGA image, the color types provided by embedded-graphics can still be used to access the pixel data using the pixels method.

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

// Include an image from a local path as bytes
let data = include_bytes!("../tests/chessboard_4px_rle.tga");

// Create a TGA instance from a byte slice.
// The color type is set by defining the type of the `img` variable.
let img: Tga<Rgb888> = Tga::from_slice(data).unwrap();

// Check the size of the image.
assert_eq!(img.size(), Size::new(4, 4));

// Collect pixels into a vector.
let pixels: Vec<_> = img.pixels().collect();

Accessing raw pixel data

If embedded-graphics is not used in the target application, the raw image data can be accessed with the pixels method on RawTga. The returned iterator produces a u32 for each pixel value.

use embedded_graphics::{prelude::*, pixelcolor::Rgb888};
use tinytga::{Bpp, Compression, DataType, ImageOrigin, RawPixel, RawTga, TgaHeader};

// Include an image from a local path as bytes.
let data = include_bytes!("../tests/chessboard_4px_rle.tga");

// Create a TGA instance from a byte slice.
let img = RawTga::from_slice(data).unwrap();

// Take a look at the raw image header.
assert_eq!(
    img.header(),
    TgaHeader {
        id_len: 0,
        has_color_map: false,
        data_type: DataType::TrueColor,
        compression: Compression::Rle,
        color_map_start: 0,
        color_map_len: 0,
        color_map_depth: None,
        x_origin: 0,
        y_origin: 4,
        width: 4,
        height: 4,
        pixel_depth: Bpp::Bits24,
        image_origin: ImageOrigin::TopLeft,
        alpha_channel_depth: 0,
    }
);

// Collect raw pixels into a vector.
let pixels: Vec<_> = img.pixels().collect();

Embedded-graphics drawing performance

tinytga uses different code paths to draw images with different ImageOrigins. The performance difference between the origins will depend on the display driver, but using images with the origin at the top left corner will generally result in the best performance.

Minimum supported Rust version

The minimum supported Rust version for tinytga is 1.61 or greater. Ensure you have the correct version of Rust installed, preferably through https://rustup.rs.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

tinytga's People

Contributors

jamwaffles avatar rfuest avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

rfuest

tinytga's Issues

CI is broken

@jamwaffles Do you have any idea why CI isn't working at the moment? It did work correctly in the PR, but fails on the master branch. At first I suspected this might be a temporary problem, but it seems to be persistent.

Errors in compiling after adding tinytga to Cargo.toml

  • Version of tinytga in use (if applicable): 0.4.1

Description of the problem/feature request/other

After uncommenting tinytga = "0.4.1" in this project https://github.com/RCasatta/embedded_playground/tree/master/aerotemp-f1-rtic-2
I've got this errors

$ cargo check
    Checking memchr v2.3.4
error[E0463]: can't find crate for `std`
  |
  = note: the `thumbv7m-none-eabi` target may not support the standard library
  = note: `std` is required by `memchr` because it does not declare `#![no_std]`



.....



    |

error[E0425]: cannot find value `None` in this scope
   --> /home/casatta/.cargo/registry/src/github.com-1ecc6299db9ec823/memchr-2.3.4/src/lib.rs:447:9
    |
447 |         None
    |         ^^^^ not found in this scope
    |
help: consider importing this unit variant
    |
43  | use core::option::Option::None;
    |

Some errors have detailed explanations: E0405, E0412, E0425, E0463.
For more information about an error, try `rustc --explain E0405`.
error: could not compile `memchr` due to 77 previous errors

Make `from_slice()` const?

  • Version of tinytga in use (if applicable): 0.5.0

Description of the problem/feature request/other

I'm experimenting with embedded-graphics on a very restricted system with only 40 kB available RAM, and while it does work with primitives like circles, fonts etc., using tinytga, the size grows up to 80 kB bytes or so while loading a 2 kB file. I therefore wonder if fn from_slice() could be made const so that image processing is done at compile-time? I realise that this could be an elaborate task due to the limitations of constant expressions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.