pltrs_codec/webp/
error.rs1use std::fmt;
4
5#[derive(Clone, Debug, PartialEq, Eq)]
7pub enum DecodeError {
8 NotWebp,
10 Truncated,
12 NoImage,
14 MalformedChunk {
16 fourcc: [u8; 4],
18 },
19 TooLarge {
21 width: u32,
23 height: u32,
25 },
26 LosslessHeader,
28 InvalidPrefixCode,
30 RepeatedTransform,
32 InvalidColorCache,
34 InvalidBackwardReference,
36 LossyHeader,
38 InvalidPartitions,
40 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#[derive(Clone, Debug, PartialEq, Eq)]
78pub enum EncodeError {
79 InvalidDimensions {
81 width: u32,
83 height: u32,
85 },
86 DataLengthMismatch {
88 expected: usize,
90 actual: usize,
92 },
93 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 {}