Skip to content
andelfPublic

About

A no_std GIF library for embedded applications(embedded-graphics)

Topics

Resources

Stars

30 stars

Watchers

1 watching

Forks

Latest commit

 

History

31 Commits

Folders and files

Repository files navigation

tinygif

CI crates.io docs.rs

A small no_std GIF decoder for embedded systems. Frames are decoded straight into an embedded-graphics draw target; decoding needs only a few tens of kB of RAM.

Features

  • GIF87a and GIF89a decoding
  • Frame iterator via Gif::frames()
  • Streaming decoder from Iterator<Item = u8> via GifIter
  • Global and local color tables, transparency and frame delays
  • embedded_graphics::Transform support for Frame
  • Interlaced images
  • Some highly compressed GIFs (see DecodingDict in src/lzw.rs)

Installation

[dependencies]
tinygif = "0.0.5"

Decode from a byte slice

The example assumes display is an embedded-graphics draw target and lives in examples/, hence the asset path.

use embedded_graphics::pixelcolor::Rgb565;
use tinygif::Gif;

let image = Gif::<Rgb565>::from_slice(include_bytes!("../assets/Ferris-240x240.gif"))?;

for frame in image.frames() {
    frame.draw(&mut display)?;
    // frame.delay_centis is in hundredths of a second.
}

To respect frame delays, subtract the time spent drawing. Instant and Timer below come from Embassy; replace them with your own clock and delay:

let start = Instant::now();
frame.draw(&mut display)?;
let remaining = ((frame.delay_centis as u64) * 10)
    .saturating_sub(start.elapsed().as_millis());
Timer::after_millis(remaining).await;

Offsets can be applied either to the draw target or to the frame:

use embedded_graphics::prelude::*;

frame.draw(&mut display.translated(Point::new(30, 50)))?;
frame.translate(Point::new(30, 50)).draw(&mut display)?;

Stream from an iterator

GifIter reads GIF bytes on demand, so a whole file never needs to fit in memory. The third type parameter is the per-frame buffer size in bytes; if a frame does not fit, next_frame() returns ParseError::FrameTooLarge.

use embedded_graphics::pixelcolor::Rgb565;
use tinygif::GifIter;

// `reader` implements Iterator<Item = u8>, e.g. bytes from an SD card.
let mut gif = GifIter::<_, Rgb565, 8192>::from_iter(reader)?;

while let Some(frame) = gif.next_frame()? {
    frame.draw(&mut display)?;
}

next_frame() lends the frame until the next call, so it is an inherent method rather than a std::iter::Iterator implementation.

License

MIT or Apache-2.0 at your option.

GIF assets used in tests

Animated Ferris in Action Happy as a Rustacean at Rust Fest Berlin 2016 (www.rustfest.eu)

About

A no_std GIF library for embedded applications(embedded-graphics)

Topics

Resources

Stars

30 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages