zune_jpegxl/
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#![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
19/// Errors that may arise during encoding
20pub enum JxlEncodeErrors {
21    /// One of the dimensions is less than 2
22    ZeroDimension(&'static str),
23    /// The colorspace of the image isn't supported by
24    /// the library
25    UnsupportedColorspace(ColorSpace),
26    /// Image depth isn't supported by the library
27    UnsupportedDepth(BitDepth),
28    /// A given width or height is too big to be encoded
29    TooLargeDimensions(usize),
30    /// Mismatch in length expected vs what was found
31    LengthMismatch(usize, usize),
32    /// Generic error
33    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 {}