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>
impl<'d> AnimationDecoder<'d>
Sourcepub fn new(
data: &'d [u8],
options: AnimationOptions,
) -> Result<Self, DecodeError>
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)?;Sourcepub fn info(&self) -> AnimationInfo
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);Sourcepub fn has_more_frames(&self) -> bool
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());Sourcepub fn next_frame(&mut self) -> Result<Option<Frame<'_>>, DecodeError>
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());Sourcepub fn reset(&mut self)
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());