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::colorspace::ColorSpace;
15
16const MAX_DIMENSIONS: usize = 1 << 30;
17
18/// Errors that may arise during encoding
19pub enum JxlEncodeErrors {
20    /// One of the dimensions is less than 2
21    ZeroDimension(&'static str),
22    /// The colorspace of the image isn't supported by
23    /// the library
24    UnsupportedColorspace(ColorSpace),
25    /// Image depth isn't supported by the library
26    UnsupportedDepth(BitDepth),
27    /// A given width or height is too big to be encoded
28    TooLargeDimensions(usize),
29    /// Mismatch in length expected vs what was found
30    LengthMismatch(usize, usize),
31    /// Generic error
32    Generic(&'static str)
33}
34
35pub const SUPPORTED_COLORSPACES: [ColorSpace; 4] = [
36    ColorSpace::Luma,
37    ColorSpace::LumaA,
38    ColorSpace::RGBA,
39    ColorSpace::RGB
40];
41pub const SUPPORTED_DEPTHS: [BitDepth; 2] = [BitDepth::Eight, BitDepth::Sixteen];
42
43impl Debug for JxlEncodeErrors {
44    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
45        match self {
46            JxlEncodeErrors::ZeroDimension(param) => writeln!(f, "The {param} is less than 2"),
47            JxlEncodeErrors::UnsupportedColorspace(color) => writeln!(
48                f,
49                "JXL encoder cannot encode images in colorspace {color:?}, supported ones are {:?}",
50                SUPPORTED_COLORSPACES
51            ),
52            JxlEncodeErrors::UnsupportedDepth(depth) => {
53                writeln!(
54                    f,
55                    "JXL encoder cannot encode images in depth {depth:?},supported ones are {:?}",
56                    SUPPORTED_DEPTHS
57                )
58            }
59            JxlEncodeErrors::TooLargeDimensions(value) => {
60                writeln!(
61                        f,
62                        "Too large dimensions {value} greater than supported dimensions {MAX_DIMENSIONS}"
63                    )
64            }
65            JxlEncodeErrors::LengthMismatch(expected, found) => {
66                writeln!(f, "Expected array of length {expected} but found {found}")
67            }
68            JxlEncodeErrors::Generic(msg) => {
69                writeln!(f, "{}", msg)
70            }
71        }
72    }
73}