Skip to main content

pltrs_codec/tiff/decode/
error.rs

1//! Why a TIFF file could not be decoded.
2
3use std::fmt;
4
5/// Why a TIFF file could not be decoded.
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub enum DecodeError {
8    /// The data does not start with a TIFF header.
9    NotTiff,
10    /// The file has no image directory.
11    NoImage,
12    /// A tag the image needs is missing.
13    MissingTag(u16),
14    /// A tag has a value of the wrong type or out of range.
15    InvalidTag(u16),
16    /// The layout of samples is not one Pillow reads: the combination of
17    /// photometric interpretation, sample format, fill order, bits per
18    /// sample and extra samples.
19    UnsupportedLayout,
20    /// A compression scheme other than none, `PackBits`, LZW and Deflate.
21    UnsupportedCompression(u16),
22    /// A predictor other than none and horizontal differencing of 8- or
23    /// 16-bit samples.
24    UnsupportedPredictor(u16),
25    /// Samples stored in separate planes.
26    UnsupportedPlanarConfiguration,
27    /// A side of the image is zero.
28    EmptyImage,
29    /// The image has more pixels than the input can hold, or than Pillow
30    /// accepts.
31    TooLarge {
32        /// The declared width.
33        width: u32,
34        /// The declared height.
35        height: u32,
36    },
37    /// A strip or tile lies outside the file.
38    Truncated,
39    /// A strip or tile does not decompress to the data it should hold.
40    CorruptData,
41}
42
43impl fmt::Display for DecodeError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            Self::NotTiff => f.write_str("not a TIFF file"),
47            Self::NoImage => f.write_str("the TIFF file has no image"),
48            Self::MissingTag(tag) => write!(f, "the TIFF image lacks tag {tag}"),
49            Self::InvalidTag(tag) => write!(f, "invalid value of TIFF tag {tag}"),
50            Self::UnsupportedLayout => f.write_str("unknown TIFF pixel layout"),
51            Self::UnsupportedCompression(scheme) => {
52                write!(f, "unsupported TIFF compression {scheme}")
53            }
54            Self::UnsupportedPredictor(predictor) => {
55                write!(f, "unsupported TIFF predictor {predictor}")
56            }
57            Self::UnsupportedPlanarConfiguration => {
58                f.write_str("TIFF images with separate sample planes are not supported")
59            }
60            Self::EmptyImage => f.write_str("the TIFF image has no pixels"),
61            Self::TooLarge { width, height } => {
62                write!(f, "a {width} × {height} TIFF image is too large to decode")
63            }
64            Self::Truncated => f.write_str("the TIFF image data lies outside the file"),
65            Self::CorruptData => f.write_str("corrupt TIFF image data"),
66        }
67    }
68}
69
70impl std::error::Error for DecodeError {}