1use alloc::boxed::Box;
2use alloc::fmt;
3use alloc::string::String;
4use core::result;
5use std::error::Error as StdError;
6use std::io::Error as IoError;
7
8use crate::ColorTransform;
9
10pub type Result<T> = result::Result<T, Error>;
11
12/// An enumeration over JPEG features (currently) unsupported by this library.
13///
14/// Support for features listed here may be included in future versions of this library.
15#[derive(Debug, Clone, PartialEq, Eq, Hash)]
16pub enum UnsupportedFeature {
17 /// Hierarchical JPEG.
18 Hierarchical,
19 /// JPEG using arithmetic entropy coding instead of Huffman coding.
20 ArithmeticEntropyCoding,
21 /// Sample precision in bits. 8 bit sample precision is what is currently supported in non-lossless coding process.
22 SamplePrecision(u8),
23 /// Number of components in an image. 1, 3 and 4 components are currently supported.
24 ComponentCount(u8),
25 /// An image can specify a zero height in the frame header and use the DNL (Define Number of
26 /// Lines) marker at the end of the first scan to define the number of lines in the frame.
27 DNL,
28 /// Subsampling ratio.
29 SubsamplingRatio,
30 /// A subsampling ratio not representable as an integer.
31 NonIntegerSubsamplingRatio,
32 /// Colour transform
33 ColorTransform(ColorTransform),
34}
35
36/// Errors that can occur while decoding a JPEG image.
37#[derive(Debug)]
38pub enum Error {
39 /// The image is not formatted properly. The string contains detailed information about the
40 /// error.
41 Format(String),
42 /// The image makes use of a JPEG feature not (currently) supported by this library.
43 Unsupported(UnsupportedFeature),
44 /// An I/O error occurred while decoding the image.
45 Io(IoError),
46 /// An internal error occurred while decoding the image.
47 Internal(Box<dyn StdError + Send + Sync + 'static>), //TODO: not used, can be removed with the next version bump
48}
49
50impl fmt::Display for Error {
51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 match *self {
53 Error::Format(ref desc: &String) => write!(f, "invalid JPEG format: {}", desc),
54 Error::Unsupported(ref feat: &UnsupportedFeature) => write!(f, "unsupported JPEG feature: {:?}", feat),
55 Error::Io(ref err: &Error) => err.fmt(f),
56 Error::Internal(ref err: &Box) => err.fmt(f),
57 }
58 }
59}
60
61impl StdError for Error {
62 fn source(&self) -> Option<&(dyn StdError + 'static)> {
63 match *self {
64 Error::Io(ref err: &Error) => Some(err),
65 Error::Internal(ref err: &Box) => Some(&**err),
66 _ => None,
67 }
68 }
69}
70
71impl From<IoError> for Error {
72 fn from(err: IoError) -> Error {
73 Error::Io(err)
74 }
75}
76