zune_qoi/
errors.rs

1/*
2 * Copyright (c) 2023.
3 *
4 * This software is free software; You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
5 */
6
7use alloc::string::String;
8/// Errors possible during decoding.
9use core::fmt::{Debug, Display, Formatter};
10
11use zune_core::bytestream::ZByteIoError;
12use zune_core::colorspace::ColorSpace;
13
14/// Possible Errors that may occur during decoding
15pub enum QoiErrors {
16    /// The image does not start with QOI magic bytes `qoif`
17    ///
18    /// Indicates that image is not a qoi file
19    WrongMagicBytes,
20    /// The input buffer doesn't have enough bytes to fully
21    /// reconstruct the image
22    ///
23    /// # Arguments
24    /// - 1st argument is the number of bytes we expected
25    /// - 2nd argument is number of bytes actually left
26    InsufficientData(usize, usize),
27    /// The header contains an invalid channel number
28    ///
29    /// The only supported types are `3` and `4`
30    UnknownChannels(u8),
31    /// The header contains an invalid colorspace value
32    ///
33    /// The should be `0` or `1`
34    /// but this can be ignored if strict is set to false
35    UnknownColorspace(u8),
36    /// Generic message
37    Generic(String),
38    /// Generic message does not need heap allocation
39    GenericStatic(&'static str),
40    /// To small output size
41    TooSmallOutput(usize, usize),
42    IoErrors(ZByteIoError)
43}
44
45impl Debug for QoiErrors {
46    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
47        match self {
48            QoiErrors::WrongMagicBytes => {
49                writeln!(f, "Wrong magic bytes, expected `qoif` as image start")
50            }
51            QoiErrors::InsufficientData(expected, found) => {
52                writeln!(
53                    f,
54                    "Insufficient data required {expected} but remaining stream has {found}"
55                )
56            }
57            QoiErrors::UnknownChannels(channel) => {
58                writeln!(
59                    f,
60                    "Unknown channel number {channel}, expected either 3 or 4"
61                )
62            }
63            QoiErrors::UnknownColorspace(colorspace) => {
64                writeln!(
65                    f,
66                    "Unknown colorspace number {colorspace}, expected either 0 or 1"
67                )
68            }
69            QoiErrors::Generic(val) => {
70                writeln!(f, "{val}")
71            }
72            QoiErrors::GenericStatic(val) => {
73                writeln!(f, "{val}")
74            }
75            QoiErrors::TooSmallOutput(expected, found) => {
76                writeln!(
77                    f,
78                    "Too small output size, expected {expected}, but found {found}"
79                )
80            }
81            QoiErrors::IoErrors(value) => {
82                writeln!(f, "I/O error {:?}", value)
83            }
84        }
85    }
86}
87
88impl From<&'static str> for QoiErrors {
89    fn from(r: &'static str) -> Self {
90        Self::GenericStatic(r)
91    }
92}
93
94impl From<ZByteIoError> for QoiErrors {
95    fn from(value: ZByteIoError) -> Self {
96        QoiErrors::IoErrors(value)
97    }
98}
99/// Errors encountered during encoding
100pub enum QoiEncodeErrors {
101    /// Unsupported colorspace
102    ///
103    /// The first argument is the colorspace encountered
104    /// The second argument is list of supported colorspaces
105    UnsupportedColorspace(ColorSpace, &'static [ColorSpace]),
106
107    /// Too large dimensions
108    /// The dimensions cannot be correctly encoded to a width
109    TooLargeDimensions(usize),
110
111    Generic(&'static str),
112
113    IoError(ZByteIoError)
114}
115
116impl Debug for QoiEncodeErrors {
117    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
118        match self {
119            QoiEncodeErrors::UnsupportedColorspace(found, supported) => {
120                writeln!(f, "Cannot encode image with colorspace {found:?} into QOI, supported ones are {supported:?}")
121            }
122            QoiEncodeErrors::TooLargeDimensions(found) => {
123                writeln!(
124                    f,
125                    "Too large image dimensions {found}, QOI can only encode images less than {}",
126                    u32::MAX
127                )
128            }
129            QoiEncodeErrors::Generic(val) => {
130                writeln!(f, "{}", val)
131            }
132            QoiEncodeErrors::IoError(v) => {
133                writeln!(f, "I/O error {:?}", v)
134            }
135        }
136    }
137}
138
139impl Display for QoiEncodeErrors {
140    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
141        writeln!(f, "{:?}", self)
142    }
143}
144impl Display for QoiErrors {
145    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
146        writeln!(f, "{:?}", self)
147    }
148}
149
150#[cfg(feature = "std")]
151impl std::error::Error for QoiEncodeErrors {}
152
153#[cfg(feature = "std")]
154impl std::error::Error for QoiErrors {}
155
156impl From<ZByteIoError> for QoiEncodeErrors {
157    fn from(value: ZByteIoError) -> Self {
158        Self::IoError(value)
159    }
160}