Skip to main content

pltrs_codec/webp/
error.rs

1//! Why WebP data could not be decoded or encoded.
2
3use std::fmt;
4
5/// Why a WebP file could not be decoded.
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub enum DecodeError {
8    /// The data does not start with a RIFF `WEBP` header.
9    NotWebp,
10    /// The data ends inside a chunk or a bitstream.
11    Truncated,
12    /// The file holds no `VP8 ` or `VP8L` image.
13    NoImage,
14    /// A chunk too short for its fields, or contradicting the others.
15    MalformedChunk {
16        /// The chunk's four-character code.
17        fourcc: [u8; 4],
18    },
19    /// The image has more pixels than Pillow accepts.
20    TooLarge {
21        /// The image width.
22        width: u32,
23        /// The image height.
24        height: u32,
25    },
26    /// A VP8L header with a wrong signature or version.
27    LosslessHeader,
28    /// A prefix code that is empty, incomplete or over-subscribed.
29    InvalidPrefixCode,
30    /// A VP8L image declaring the same transform twice.
31    RepeatedTransform,
32    /// A colour cache of more than 11 bits, or of none.
33    InvalidColorCache,
34    /// A backward reference before the image start or past its end.
35    InvalidBackwardReference,
36    /// A VP8 frame with a wrong frame tag, start code or dimensions.
37    LossyHeader,
38    /// A VP8 frame whose partitions do not fit in the data.
39    InvalidPartitions,
40    /// An `ALPH` chunk with an unknown compression or filter.
41    InvalidAlpha,
42}
43
44impl fmt::Display for DecodeError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            Self::NotWebp => f.write_str("not a WebP file"),
48            Self::Truncated => f.write_str("the WebP data ends early"),
49            Self::NoImage => f.write_str("the WebP file holds no image"),
50            Self::MalformedChunk { fourcc } => {
51                write!(
52                    f,
53                    "malformed WebP {} chunk",
54                    String::from_utf8_lossy(fourcc)
55                )
56            }
57            Self::TooLarge { width, height } => {
58                write!(f, "a {width} × {height} WebP image is too large to decode")
59            }
60            Self::LosslessHeader => f.write_str("invalid VP8L header"),
61            Self::InvalidPrefixCode => f.write_str("invalid prefix code in VP8L data"),
62            Self::RepeatedTransform => f.write_str("a VP8L transform appears twice"),
63            Self::InvalidColorCache => f.write_str("invalid VP8L colour cache size"),
64            Self::InvalidBackwardReference => {
65                f.write_str("a VP8L backward reference leaves the image")
66            }
67            Self::LossyHeader => f.write_str("invalid VP8 frame header"),
68            Self::InvalidPartitions => f.write_str("VP8 partitions exceed the data"),
69            Self::InvalidAlpha => f.write_str("invalid WebP alpha data"),
70        }
71    }
72}
73
74impl std::error::Error for DecodeError {}
75
76/// Why pixels could not be encoded as WebP.
77#[derive(Clone, Debug, PartialEq, Eq)]
78pub enum EncodeError {
79    /// A side is zero or larger than WebP's 16 384 pixels.
80    InvalidDimensions {
81        /// The requested width.
82        width: u32,
83        /// The requested height.
84        height: u32,
85    },
86    /// The pixel data does not have `width × height × 4` bytes.
87    DataLengthMismatch {
88        /// The bytes needed.
89        expected: usize,
90        /// The bytes given.
91        actual: usize,
92    },
93    /// An animation without frames.
94    NoFrames,
95}
96
97impl fmt::Display for EncodeError {
98    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99        match self {
100            Self::InvalidDimensions { width, height } => write!(
101                f,
102                "a WebP cannot be {width} × {height} pixels (each side must be 1 to 16384)"
103            ),
104            Self::DataLengthMismatch { expected, actual } => {
105                write!(f, "{actual} bytes of pixel data, expected {expected}")
106            }
107            Self::NoFrames => f.write_str("an animation needs at least one frame"),
108        }
109    }
110}
111
112impl std::error::Error for EncodeError {}