zune_hdr/
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
9use alloc::string::String;
10use core::convert::From;
11use core::fmt::{Debug, Display, Formatter};
12use core::num::ParseIntError;
13
14use zune_core::bytestream::ZByteIoError;
15use zune_core::colorspace::ColorSpace;
16
17/// HDR decoding errors
18pub enum HdrDecodeErrors {
19    /// Magic bytes do not start with `?#RADIANCE` or `?#RGBE`
20    InvalidMagicBytes,
21    /// The decoder could not convert string to int
22    ParseError(ParseIntError),
23    /// The image contains an unsupported orientation
24    UnsupportedOrientation(String, String),
25    /// Too large dimensions for a given dimension
26    TooLargeDimensions(&'static str, usize, usize),
27    /// Generic message
28    Generic(&'static str),
29    /// The output array is too small to contain the whole
30    /// image
31    TooSmallOutputArray(usize, usize),
32    IoErrors(ZByteIoError)
33}
34
35impl Debug for HdrDecodeErrors {
36    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
37        match self {
38            HdrDecodeErrors::InvalidMagicBytes => {
39                writeln!(
40                    f,
41                    "Invalid magic bytes, file does not start with #?RADIANCE or #?RGBE"
42                )
43            }
44            HdrDecodeErrors::ParseError(err) => {
45                writeln!(f, "Could not parse integer {:?}", err)
46            }
47            HdrDecodeErrors::UnsupportedOrientation(x, y) => {
48                writeln!(f, "Unsupported image orientation of {x} {y}")
49            }
50            HdrDecodeErrors::TooLargeDimensions(dimension, expected, found) => {
51                writeln!(
52                    f,
53                    "Too large dimensions for {dimension} , {found} exceeds {expected}"
54                )
55            }
56            HdrDecodeErrors::Generic(error) => {
57                writeln!(f, "{error}")
58            }
59            HdrDecodeErrors::TooSmallOutputArray(expected, found) => {
60                writeln!(f, "Too small of an output array, expected array of at least length {} but found {}", expected, found)
61            }
62            HdrDecodeErrors::IoErrors(err) => {
63                writeln!(f, "{:?}", err)
64            }
65        }
66    }
67}
68
69impl From<ParseIntError> for HdrDecodeErrors {
70    fn from(value: ParseIntError) -> Self {
71        HdrDecodeErrors::ParseError(value)
72    }
73}
74
75impl From<ZByteIoError> for HdrDecodeErrors {
76    fn from(value: ZByteIoError) -> Self {
77        HdrDecodeErrors::IoErrors(value)
78    }
79}
80impl Display for HdrDecodeErrors {
81    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
82        writeln!(f, "{:?}", self)
83    }
84}
85impl std::error::Error for HdrDecodeErrors {}
86
87impl Display for HdrEncodeErrors {
88    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
89        writeln!(f, "{:?}", self)
90    }
91}
92
93impl std::error::Error for HdrEncodeErrors {}
94
95/// HDR encoding errrors
96pub enum HdrEncodeErrors {
97    /// The colorspace provided by user is not supported by HDR
98    UnsupportedColorspace(ColorSpace),
99    /// The input size was expected to be of a certain size but isn't
100    WrongInputSize(usize, usize),
101    /// Generic message
102    Static(&'static str),
103    IoErrors(ZByteIoError)
104}
105
106impl Debug for HdrEncodeErrors {
107    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
108        match self {
109            HdrEncodeErrors::UnsupportedColorspace(color) => {
110                writeln!(f, "Unsupported colorspace {color:?} for Radiance, Radiance only works with RGB f32 data")
111            }
112            HdrEncodeErrors::WrongInputSize(expected, found) => {
113                writeln!(f, "Input array length {found} doesn't match {expected}")
114            }
115            HdrEncodeErrors::Static(err) => writeln!(f, "{}", err),
116            HdrEncodeErrors::IoErrors(err) => writeln!(f, "I/O error {:?}", err)
117        }
118    }
119}
120
121impl From<&'static str> for HdrEncodeErrors {
122    fn from(value: &'static str) -> Self {
123        HdrEncodeErrors::Static(value)
124    }
125}
126impl From<ZByteIoError> for HdrEncodeErrors {
127    fn from(value: ZByteIoError) -> Self {
128        HdrEncodeErrors::IoErrors(value)
129    }
130}