1#![allow(clippy::uninlined_format_args)]
10
11use core::fmt::{Debug, Formatter};
12
13use zune_core::bit_depth::BitDepth;
14use zune_core::bytestream::ZByteIoError;
15use zune_core::colorspace::ColorSpace;
16
17const MAX_DIMENSIONS: usize = 1 << 30;
18
19pub enum JxlEncodeErrors {
21 ZeroDimension(&'static str),
23 UnsupportedColorspace(ColorSpace),
26 UnsupportedDepth(BitDepth),
28 TooLargeDimensions(usize),
30 LengthMismatch(usize, usize),
32 Generic(&'static str),
34
35 IoErrors(ZByteIoError)
36}
37
38pub const SUPPORTED_COLORSPACES: [ColorSpace; 4] = [
39 ColorSpace::Luma,
40 ColorSpace::LumaA,
41 ColorSpace::RGBA,
42 ColorSpace::RGB
43];
44pub const SUPPORTED_DEPTHS: [BitDepth; 2] = [BitDepth::Eight, BitDepth::Sixteen];
45
46impl Debug for JxlEncodeErrors {
47 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
48 match self {
49 JxlEncodeErrors::ZeroDimension(param) => writeln!(f, "The {param} is less than 2"),
50 JxlEncodeErrors::UnsupportedColorspace(color) => writeln!(
51 f,
52 "JXL encoder cannot encode images in colorspace {color:?}, supported ones are {:?}",
53 SUPPORTED_COLORSPACES
54 ),
55 JxlEncodeErrors::UnsupportedDepth(depth) => {
56 writeln!(
57 f,
58 "JXL encoder cannot encode images in depth {depth:?},supported ones are {:?}",
59 SUPPORTED_DEPTHS
60 )
61 }
62 JxlEncodeErrors::TooLargeDimensions(value) => {
63 writeln!(
64 f,
65 "Too large dimensions {value} greater than supported dimensions {MAX_DIMENSIONS}"
66 )
67 }
68 JxlEncodeErrors::LengthMismatch(expected, found) => {
69 writeln!(f, "Expected array of length {expected} but found {found}")
70 }
71 JxlEncodeErrors::Generic(msg) => {
72 writeln!(f, "{}", msg)
73 }
74 JxlEncodeErrors::IoErrors(e) => {
75 writeln!(f, "I/O error {:?}", e)
76 }
77 }
78 }
79}
80
81impl From<ZByteIoError> for JxlEncodeErrors {
82 fn from(value: ZByteIoError) -> Self {
83 JxlEncodeErrors::IoErrors(value)
84 }
85}
86
87impl core::fmt::Display for JxlEncodeErrors {
88 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
89 writeln!(f, "{:?}", self)
90 }
91}
92
93#[cfg(feature = "std")]
94impl std::error::Error for JxlEncodeErrors {}