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