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.
- GIF87a and GIF89a decoding
- Frame iterator via
Gif::frames() - Streaming decoder from
Iterator<Item = u8>viaGifIter - Global and local color tables, transparency and frame delays
-
embedded_graphics::Transformsupport forFrame - Interlaced images
- Some highly compressed GIFs (see
DecodingDictinsrc/lzw.rs)
[dependencies]
tinygif = "0.0.5"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)?;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.
MIT or Apache-2.0 at your option.
Animated Ferris in Action Happy as a Rustacean at Rust Fest Berlin 2016 (www.rustfest.eu)
- CC BY 4.0 Animated Ferris for Rust Fest Berlin by A. L. Palmer
- Resized by ezgif