Skip to main content

webp_anim/
model.rs

1use std::{num::NonZeroU16, time::Duration};
2
3/// Pixel dimensions of a full animation canvas.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub struct CanvasSize {
6    /// Canvas width in pixels.
7    pub width: u32,
8    /// Canvas height in pixels.
9    pub height: u32,
10}
11
12impl CanvasSize {
13    /// Returns `width * height`, or `None` if the multiplication overflows.
14    pub fn pixel_count(self) -> Option<u64> {
15        u64::from(self.width).checked_mul(u64::from(self.height))
16    }
17
18    /// Returns the tightly packed RGBA8 buffer size, or `None` on overflow.
19    pub fn rgba_bytes(self) -> Option<usize> {
20        self.pixel_count()
21            .and_then(|pixels| pixels.checked_mul(4))
22            .and_then(|bytes| usize::try_from(bytes).ok())
23    }
24}
25
26/// WebP loop count without exposing the file format's `0 == infinite` sentinel.
27#[derive(Clone, Copy, Debug, Eq, PartialEq)]
28pub enum LoopCount {
29    /// The animation repeats indefinitely according to the WebP loop value.
30    Infinite,
31    /// A finite, non-zero loop count stored by WebP.
32    Finite(NonZeroU16),
33}
34
35/// Background colour stored verbatim in libwebp's `bgcolor` field.
36///
37/// This crate deliberately does not assign a channel order or color-space
38/// interpretation to this value.
39#[derive(Clone, Copy, Debug, Eq, PartialEq)]
40pub struct BackgroundColor {
41    /// Raw 32-bit value stored in the WebP ANIM background-color field.
42    pub raw: u32,
43}
44
45/// Semantics associated with one stored animation sequence.
46#[derive(Clone, Copy, Debug, Eq, PartialEq)]
47pub struct AnimationInfo {
48    /// Full canvas dimensions shared by every composited frame.
49    pub canvas: CanvasSize,
50    /// Number of frames in the stored animation sequence.
51    pub frame_count: u32,
52    /// Loop policy stored in the source animation.
53    pub loop_count: LoopCount,
54    /// Raw ANIM background color stored in the source animation.
55    pub background_color: BackgroundColor,
56}
57
58/// Information available for a non-animated WebP image.
59#[derive(Clone, Copy, Debug, Eq, PartialEq)]
60pub struct StaticWebpInfo {
61    /// Canvas dimensions of the static image.
62    pub canvas: CanvasSize,
63}
64
65/// A composited, full-canvas RGBA frame and its unmodified source duration.
66#[derive(Clone, Debug, Eq, PartialEq)]
67pub struct AnimationFrame {
68    /// Tightly packed RGBA8 pixels in row-major, full-canvas order.
69    pub rgba: Vec<u8>,
70    /// Canvas dimensions of [`Self::rgba`].
71    pub canvas: CanvasSize,
72    /// Source frame duration, without playback-time normalization.
73    pub duration: Duration,
74}