pltrs_codec/jpeg/decode/
error.rs1use std::fmt;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum TableKind {
8 Quantization,
10 DcHuffman,
12 AcHuffman,
14}
15
16#[derive(Clone, Debug, PartialEq, Eq)]
18pub enum DecodeError {
19 NotJpeg,
21 Truncated,
23 MalformedSegment {
25 marker: u8,
27 },
28 UnknownMarker(u8),
30 UnsupportedProcess {
33 marker: u8,
35 },
36 UnsupportedPrecision(u8),
38 UnsupportedComponents(u8),
40 EmptyImage,
42 TooLarge {
45 width: u16,
47 height: u16,
49 },
50 InvalidSampling,
53 DuplicateMarker(u8),
55 MissingFrame,
57 MissingTable {
59 kind: TableKind,
61 index: u8,
63 },
64 InvalidHuffmanTable,
67 InvalidHuffmanCode,
69 CoefficientOverflow,
72 InvalidScan,
76 MissingRestart {
78 expected: u8,
80 },
81}
82
83impl fmt::Display for DecodeError {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 match self {
86 Self::NotJpeg => f.write_str("not a JPEG file"),
87 Self::Truncated => f.write_str("the JPEG data ends early"),
88 Self::MalformedSegment { marker } => {
89 write!(f, "malformed JPEG marker segment 0xFF{marker:02X}")
90 }
91 Self::UnknownMarker(marker) => write!(f, "unknown JPEG marker 0xFF{marker:02X}"),
92 Self::UnsupportedProcess { marker } => write!(
93 f,
94 "unsupported JPEG process (marker 0xFF{marker:02X}); only Huffman-coded \
95 baseline, extended and progressive JPEG can be read"
96 ),
97 Self::UnsupportedPrecision(bits) => write!(f, "cannot handle {bits}-bit samples"),
98 Self::UnsupportedComponents(count) => {
99 write!(f, "cannot handle JPEG images of {count} components")
100 }
101 Self::EmptyImage => f.write_str("the JPEG image has no pixels"),
102 Self::TooLarge { width, height } => {
103 write!(f, "a {width} × {height} JPEG image is too large to decode")
104 }
105 Self::InvalidSampling => f.write_str("unsupported JPEG sampling factors"),
106 Self::DuplicateMarker(marker) => {
107 write!(f, "repeated JPEG marker 0xFF{marker:02X}")
108 }
109 Self::MissingFrame => f.write_str("the JPEG data has no frame or no scan"),
110 Self::MissingTable { kind, index } => {
111 let kind = match kind {
112 TableKind::Quantization => "quantization",
113 TableKind::DcHuffman => "DC Huffman",
114 TableKind::AcHuffman => "AC Huffman",
115 };
116 write!(f, "the JPEG data uses undefined {kind} table {index}")
117 }
118 Self::InvalidHuffmanTable => f.write_str("invalid JPEG Huffman table"),
119 Self::InvalidHuffmanCode => f.write_str("corrupt JPEG data: invalid Huffman code"),
120 Self::CoefficientOverflow => {
121 f.write_str("corrupt JPEG data: a DC coefficient is out of range")
122 }
123 Self::InvalidScan => f.write_str("invalid JPEG scan header"),
124 Self::MissingRestart { expected } => {
125 write!(f, "corrupt JPEG data: restart marker {expected} is missing")
126 }
127 }
128 }
129}
130
131impl std::error::Error for DecodeError {}