Skip to main content

AnimationDecoder

Struct AnimationDecoder 

Source
pub struct AnimationDecoder<'d> { /* private fields */ }
Expand description

A decoder of a WebP animation into full canvases (C: WebPAnimDecoder).

It borrows the file’s bytes. A still image is an animation of one frame. Each AnimationDecoder::next_frame lends the canvas until the next call. The canvas starts transparent black; the background color is reported in AnimationInfo, not applied.

§Examples

use webp::api::decode::{AnimationDecoder, AnimationOptions};

// `file` is a 2 by 2 animation of a red frame for 40 ms and a brighter one
// for 60 ms, looping 3 times.
let mut decoder = AnimationDecoder::new(&file, AnimationOptions::default())?;
let info = decoder.info();
assert_eq!((info.frame_count, info.loop_count, info.background_color), (2, 3, 0xff30_2010));
let mut frames = Vec::new();
while let Some(frame) = decoder.next_frame()? {
    frames.push((frame.canvas[0], frame.timestamp_ms));
}
assert_eq!(frames, [(100, 40), (200, 100)]);

Implementations§

Source§

impl<'d> AnimationDecoder<'d>

Source

pub fn new( data: &'d [u8], options: AnimationOptions, ) -> Result<Self, DecodeError>

Parses the file and prepares its decoding (C: WebPAnimDecoderNew).

This allocates two canvases of 4 * canvas_width * canvas_height bytes and zeroes them; a small file can declare a canvas of up to 2^32 pixels. To budget that memory before it is allocated, read the canvas size first with Features::parse or Container::canvas_width and Container::canvas_height.

§Errors

The errors of Features::parse; DecodeError::NotEnoughData for a file that ends early and DecodeError::BitstreamError for one that does not parse; DecodeError::OutOfMemory when the canvases cannot be allocated.

§Examples
use webp::api::decode::{AnimationDecoder, AnimationMode, AnimationOptions, Features};

let features = Features::parse(&file)?;
let canvases = 2 * 4 * u64::from(features.width) * u64::from(features.height);
assert!(canvases <= 1 << 20, "within the budget");
let options = AnimationOptions { mode: AnimationMode::Bgra, multithreaded: false };
let decoder = AnimationDecoder::new(&file, options)?;
Source

pub fn info(&self) -> AnimationInfo

The animation’s parameters (C: WebPAnimDecoderGetInfo).

§Examples
use webp::api::decode::{AnimationDecoder, AnimationOptions};

assert_eq!(AnimationDecoder::new(&file, AnimationOptions::default())?.info().loop_count, 1);
Source

pub fn has_more_frames(&self) -> bool

Whether frames remain to be decoded (C: WebPAnimDecoderHasMoreFrames).

§Examples
use webp::api::decode::{AnimationDecoder, AnimationOptions};

let mut decoder = AnimationDecoder::new(&file, AnimationOptions::default())?;
assert!(decoder.has_more_frames());
decoder.next_frame()?;
assert!(!decoder.has_more_frames());
Source

pub fn next_frame(&mut self) -> Result<Option<Frame<'_>>, DecodeError>

Decodes the next frame and lends the canvas until the next call (C: WebPAnimDecoderGetNext); Ok(None) after the last frame.

§Errors

DecodeError::FrameFailed when the frame cannot be decoded; the decoder stays at that frame.

§Examples
use webp::api::decode::{AnimationDecoder, AnimationOptions};

let mut decoder = AnimationDecoder::new(&file, AnimationOptions::default())?;
assert_eq!(decoder.next_frame()?.map(|frame| frame.canvas.to_vec()), Some(vec![8_u8; 4]));
assert!(decoder.next_frame()?.is_none());
Source

pub fn reset(&mut self)

Restarts from the first frame (C: WebPAnimDecoderReset).

§Examples
use webp::api::decode::{AnimationDecoder, AnimationOptions};

let mut decoder = AnimationDecoder::new(&file, AnimationOptions::default())?;
decoder.next_frame()?;
decoder.reset();
assert!(decoder.has_more_frames());

Trait Implementations§

Source§

impl Debug for AnimationDecoder<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'d> Forget for AnimationDecoder<'d>

§

impl<'d> Freeze for AnimationDecoder<'d>

§

impl<'d> Move for AnimationDecoder<'d>

§

impl<'d> RefUnwindSafe for AnimationDecoder<'d>

§

impl<'d> Send for AnimationDecoder<'d>

§

impl<'d> Sync for AnimationDecoder<'d>

§

impl<'d> Unpin for AnimationDecoder<'d>

§

impl<'d> UnsafeUnpin for AnimationDecoder<'d>

§

impl<'d> UnwindSafe for AnimationDecoder<'d>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.