/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zune-jpeg-0.5.5/src/errors.rs
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023. |
3 | | * |
4 | | * This software is free software; |
5 | | * |
6 | | * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license |
7 | | */ |
8 | | |
9 | | //! Contains most common errors that may be encountered in decoding a Decoder |
10 | | //! image |
11 | | |
12 | | use alloc::string::String; |
13 | | use core::fmt::{Debug, Display, Formatter}; |
14 | | |
15 | | use zune_core::bytestream::ZByteIoError; |
16 | | |
17 | | use crate::misc::{ |
18 | | START_OF_FRAME_EXT_AR, START_OF_FRAME_EXT_SEQ, START_OF_FRAME_LOS_SEQ, |
19 | | START_OF_FRAME_LOS_SEQ_AR, START_OF_FRAME_PROG_DCT_AR |
20 | | }; |
21 | | |
22 | | /// Common Decode errors |
23 | | #[allow(clippy::module_name_repetitions)] |
24 | | pub enum DecodeErrors { |
25 | | /// Any other thing we do not know |
26 | | Format(String), |
27 | | /// Any other thing we do not know but we |
28 | | /// don't need to allocate space on the heap |
29 | | FormatStatic(&'static str), |
30 | | /// Illegal Magic Bytes |
31 | | IllegalMagicBytes(u16), |
32 | | /// problems with the Huffman Tables in a Decoder file |
33 | | HuffmanDecode(String), |
34 | | /// Image has zero width |
35 | | ZeroError, |
36 | | /// Discrete Quantization Tables error |
37 | | DqtError(String), |
38 | | /// Start of scan errors |
39 | | SosError(String), |
40 | | /// Start of frame errors |
41 | | SofError(String), |
42 | | /// UnsupportedImages |
43 | | Unsupported(UnsupportedSchemes), |
44 | | /// MCU errors |
45 | | MCUError(String), |
46 | | /// Exhausted data |
47 | | ExhaustedData, |
48 | | /// Large image dimensions(Corrupted data)? |
49 | | LargeDimensions(usize), |
50 | | /// Too small output for size |
51 | | TooSmallOutput(usize, usize), |
52 | | |
53 | | IoErrors(ZByteIoError) |
54 | | } |
55 | | |
56 | | #[cfg(feature = "std")] |
57 | | impl std::error::Error for DecodeErrors {} |
58 | | |
59 | | impl From<&'static str> for DecodeErrors { |
60 | 0 | fn from(data: &'static str) -> Self { |
61 | 0 | return Self::FormatStatic(data); |
62 | 0 | } |
63 | | } |
64 | | |
65 | | impl From<ZByteIoError> for DecodeErrors { |
66 | 1.08k | fn from(data: ZByteIoError) -> Self { |
67 | 1.08k | return Self::IoErrors(data); |
68 | 1.08k | } |
69 | | } |
70 | | impl Debug for DecodeErrors { |
71 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { |
72 | 0 | match &self |
73 | | { |
74 | 0 | Self::Format(ref a) => write!(f, "{a:?}"), |
75 | 0 | Self::FormatStatic(a) => write!(f, "{:?}", &a), |
76 | | |
77 | 0 | Self::HuffmanDecode(ref reason) => |
78 | | { |
79 | 0 | write!(f, "Error decoding huffman values: {reason}") |
80 | | } |
81 | 0 | Self::ZeroError => write!(f, "Image width or height is set to zero, cannot continue"), |
82 | 0 | Self::DqtError(ref reason) => write!(f, "Error parsing DQT segment. Reason:{reason}"), |
83 | 0 | Self::SosError(ref reason) => write!(f, "Error parsing SOS Segment. Reason:{reason}"), |
84 | 0 | Self::SofError(ref reason) => write!(f, "Error parsing SOF segment. Reason:{reason}"), |
85 | 0 | Self::IllegalMagicBytes(bytes) => |
86 | | { |
87 | 0 | write!(f, "Error parsing image. Illegal start bytes:{bytes:X}") |
88 | | } |
89 | 0 | Self::MCUError(ref reason) => write!(f, "Error in decoding MCU. Reason {reason}"), |
90 | 0 | Self::Unsupported(ref image_type) => |
91 | | { |
92 | 0 | write!(f, "{image_type:?}") |
93 | | } |
94 | 0 | Self::ExhaustedData => write!(f, "Exhausted data in the image"), |
95 | 0 | Self::LargeDimensions(ref dimensions) => write!( |
96 | 0 | f, |
97 | 0 | "Too large dimensions {dimensions},library supports up to {}", crate::decoder::MAX_DIMENSIONS |
98 | | ), |
99 | 0 | Self::TooSmallOutput(expected, found) => write!(f, "Too small output, expected buffer with at least {expected} bytes but got one with {found} bytes"), |
100 | 0 | Self::IoErrors(error)=>write!(f,"I/O errors {error:?}"), |
101 | | } |
102 | 0 | } |
103 | | } |
104 | | |
105 | | impl Display for DecodeErrors { |
106 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { |
107 | 0 | write!(f, "{self:?}") |
108 | 0 | } |
109 | | } |
110 | | |
111 | | /// Contains Unsupported/Yet-to-be supported Decoder image encoding types. |
112 | | #[derive(Eq, PartialEq, Copy, Clone)] |
113 | | pub enum UnsupportedSchemes { |
114 | | /// SOF_1 Extended sequential DCT,Huffman coding |
115 | | ExtendedSequentialHuffman, |
116 | | /// Lossless (sequential), huffman coding, |
117 | | LosslessHuffman, |
118 | | /// Extended sequential DEC, arithmetic coding |
119 | | ExtendedSequentialDctArithmetic, |
120 | | /// Progressive DCT, arithmetic coding, |
121 | | ProgressiveDctArithmetic, |
122 | | /// Lossless ( sequential), arithmetic coding |
123 | | LosslessArithmetic |
124 | | } |
125 | | |
126 | | impl Debug for UnsupportedSchemes { |
127 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { |
128 | 0 | match &self { |
129 | | Self::ExtendedSequentialHuffman => { |
130 | 0 | write!(f, "The library cannot yet decode images encoded using Extended Sequential Huffman encoding scheme yet.") |
131 | | } |
132 | | Self::LosslessHuffman => { |
133 | 0 | write!(f, "The library cannot yet decode images encoded with Lossless Huffman encoding scheme") |
134 | | } |
135 | | Self::ExtendedSequentialDctArithmetic => { |
136 | 0 | write!(f,"The library cannot yet decode Images Encoded with Extended Sequential DCT Arithmetic scheme") |
137 | | } |
138 | | Self::ProgressiveDctArithmetic => { |
139 | 0 | write!(f,"The library cannot yet decode images encoded with Progressive DCT Arithmetic scheme") |
140 | | } |
141 | | Self::LosslessArithmetic => { |
142 | 0 | write!(f,"The library cannot yet decode images encoded with Lossless Arithmetic encoding scheme") |
143 | | } |
144 | | } |
145 | 0 | } |
146 | | } |
147 | | |
148 | | impl UnsupportedSchemes { |
149 | | #[must_use] |
150 | | /// Create an unsupported scheme from an integer |
151 | | /// |
152 | | /// # Returns |
153 | | /// `Some(UnsupportedScheme)` if the int refers to a specific scheme, |
154 | | /// otherwise returns `None` |
155 | 0 | pub fn from_int(int: u8) -> Option<UnsupportedSchemes> { |
156 | 0 | let int = u16::from_be_bytes([0xff, int]); |
157 | | |
158 | 0 | match int { |
159 | 0 | START_OF_FRAME_PROG_DCT_AR => Some(Self::ProgressiveDctArithmetic), |
160 | 0 | START_OF_FRAME_LOS_SEQ => Some(Self::LosslessHuffman), |
161 | 0 | START_OF_FRAME_LOS_SEQ_AR => Some(Self::LosslessArithmetic), |
162 | 0 | START_OF_FRAME_EXT_SEQ => Some(Self::ExtendedSequentialHuffman), |
163 | 0 | START_OF_FRAME_EXT_AR => Some(Self::ExtendedSequentialDctArithmetic), |
164 | 0 | _ => None |
165 | | } |
166 | 0 | } |
167 | | } |