Skip to main content

zune_image/
errors.rs

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//! Errors possible during image processing
10use std::any::TypeId;
11use std::fmt::{Debug, Display, Formatter};
12use std::io::Error;
13
14use zune_core::bit_depth::BitType;
15use zune_core::bytestream::ZByteIoError;
16use zune_core::colorspace::ColorSpace;
17
18use crate::channel::ChannelErrors;
19use crate::codecs::ImageFormat;
20
21/// All possible image errors that can occur.
22///
23/// This is the grandfather of image errors and contains
24/// all decoding,processing and encoding errors possible
25pub enum ImageErrors {
26    ImageDecodeErrors(String),
27    DimensionsMisMatch(usize, usize),
28    UnsupportedColorspace(ColorSpace, &'static str, &'static [ColorSpace]),
29    NoImageForOperations,
30    NoImageForEncoding,
31    NoImageBuffer,
32    OperationsError(ImageOperationsErrors),
33    EncodeErrors(ImgEncodeErrors),
34    GenericString(String),
35    GenericStr(&'static str),
36    WrongTypeId(TypeId, TypeId),
37    ChannelErrors(ChannelErrors),
38    ImageDecoderNotIncluded(ImageFormat),
39    ImageDecoderNotImplemented(ImageFormat),
40    IoError(std::io::Error),
41    ImageOperationNotImplemented(&'static str, BitType)
42}
43
44/// Errors that may occur during image operations
45pub enum ImageOperationsErrors {
46    /// Unexpected colorspace
47    WrongColorspace(ColorSpace, ColorSpace),
48    /// Wrong number of components
49    WrongComponents(usize, usize),
50    /// Channel layout does not match expected
51    InvalidChannelLayout(&'static str),
52    /// Unsupported bit depth for an operation
53    ///
54    /// The current operation does not support the bit depth
55    UnsupportedType(&'static str, BitType),
56    /// Generic errors
57    Generic(&'static str),
58    /// Generic errors which have more context
59    GenericString(String)
60}
61
62/// All errors possible during image encoding
63pub enum ImgEncodeErrors {
64    Generic(String),
65    GenericStatic(&'static str),
66    UnsupportedColorspace(ColorSpace, &'static [ColorSpace]),
67    ImageEncodeErrors(String),
68    NoEncoderForFormat(ImageFormat)
69}
70
71impl Debug for ImageErrors {
72    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
73        match self {
74            Self::ImageDecodeErrors(err) => {
75                writeln!(f, "{err}")
76            }
77
78            Self::GenericStr(err) => {
79                writeln!(f, "{err}")
80            }
81
82            Self::GenericString(err) => {
83                writeln!(f, "{err}")
84            }
85            Self::NoImageForOperations => {
86                writeln!(f, "No image found for which we can execute operations")
87            }
88            Self::NoImageForEncoding => {
89                writeln!(f, "No image found for which we can encode")
90            }
91            Self::NoImageBuffer => writeln!(f, "No image buffer present"),
92
93            Self::OperationsError(ref error) => writeln!(f, "{error:?}"),
94
95            Self::EncodeErrors(ref err) => writeln!(f, "{err:?}"),
96            ImageErrors::UnsupportedColorspace(present, operation, supported) => {
97                writeln!(f, "Unsupported colorspace {present:?}, for the operation {operation}\nSupported colorspaces are {supported:?}")
98            }
99            ImageErrors::DimensionsMisMatch(expected, found) => {
100                writeln!(
101                    f,
102                    "Dimensions mismatch, expected {expected} but found {found}"
103                )
104            }
105            ImageErrors::WrongTypeId(expected, found) => {
106                writeln!(
107                    f,
108                    "Expected type with ID of {expected:?} but found {found:?}"
109                )
110            }
111            ImageErrors::IoError(reason) => {
112                writeln!(f, "IO error, {:?}", reason)
113            }
114            ImageErrors::ImageDecoderNotIncluded(format) => {
115                writeln!(
116                    f,
117                    "The feature required to decode {format:?} has not been included"
118                )
119            }
120            ImageErrors::ImageDecoderNotImplemented(format) => {
121                writeln!(
122                    f,
123                    "The decoder to parse {format:?} has not been implemented"
124                )
125            }
126            ImageErrors::ChannelErrors(err) => {
127                writeln!(f, "Channel error : {:?}", err)
128            }
129            ImageErrors::ImageOperationNotImplemented(op_type, depth) => {
130                writeln!(
131                    f,
132                    "Image operation {} for depth {:?}  not implemented",
133                    op_type, depth
134                )
135            }
136        }
137    }
138}
139
140impl From<std::io::Error> for ImageErrors {
141    fn from(value: Error) -> Self {
142        Self::IoError(value)
143    }
144}
145
146impl From<ImageOperationsErrors> for ImageErrors {
147    fn from(from: ImageOperationsErrors) -> Self {
148        ImageErrors::OperationsError(from)
149    }
150}
151
152impl From<ImgEncodeErrors> for ImageErrors {
153    fn from(from: ImgEncodeErrors) -> Self {
154        ImageErrors::EncodeErrors(from)
155    }
156}
157
158impl Debug for ImageOperationsErrors {
159    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
160        match self {
161            Self::UnsupportedType(operation, depth) => {
162                writeln!(
163                    f,
164                    "Unsupported bit type {depth:?} for operation {operation}"
165                )
166            }
167            Self::InvalidChannelLayout(reason) => {
168                writeln!(f, "{reason:}")
169            }
170            Self::Generic(reason) => {
171                writeln!(f, "{reason:}")
172            }
173            Self::GenericString(err) => {
174                writeln!(f, "{err}")
175            }
176            Self::WrongColorspace(ref expected, ref found) => {
177                writeln!(f, "Expected {expected:?} colorspace but found {found:?}")
178            }
179            Self::WrongComponents(expected, found) => {
180                writeln!(f, "Expected {expected} components and found {found}")
181            }
182        }
183    }
184}
185
186impl From<String> for ImageErrors {
187    fn from(s: String) -> ImageErrors {
188        ImageErrors::GenericString(s)
189    }
190}
191
192impl From<&'static str> for ImageErrors {
193    fn from(s: &'static str) -> ImageErrors {
194        ImageErrors::GenericStr(s)
195    }
196}
197
198impl From<ChannelErrors> for ImageErrors {
199    fn from(value: ChannelErrors) -> Self {
200        ImageErrors::ChannelErrors(value)
201    }
202}
203impl From<ZByteIoError> for ImageErrors {
204    fn from(value: ZByteIoError) -> Self {
205        Self::GenericString(format!("{:?}", value))
206    }
207}
208
209impl Debug for ImgEncodeErrors {
210    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
211        match self {
212            Self::Generic(ref string) => writeln!(f, "{string}"),
213            Self::GenericStatic(ref string) => writeln!(f, "{string}"),
214            Self::UnsupportedColorspace(ref found, ref expected) => {
215                writeln!(
216                    f,
217                    "Found colorspace {found:?} but the encoder supports {expected:?}"
218                )
219            }
220            Self::ImageEncodeErrors(err) => {
221                writeln!(f, "Image could not be encoded, reason: {err}")
222            }
223            Self::NoEncoderForFormat(format) => {
224                writeln!(f, "No encoder for image format {:?}", format)
225            }
226        }
227    }
228}
229
230impl Display for ImageErrors {
231    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
232        writeln!(f, "{:?}", self)
233    }
234}
235
236impl std::error::Error for ImageErrors {}