moxcms/
err.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 2/2025. All rights reserved.
3 * //
4 * // Redistribution and use in source and binary forms, with or without modification,
5 * // are permitted provided that the following conditions are met:
6 * //
7 * // 1.  Redistributions of source code must retain the above copyright notice, this
8 * // list of conditions and the following disclaimer.
9 * //
10 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
11 * // this list of conditions and the following disclaimer in the documentation
12 * // and/or other materials provided with the distribution.
13 * //
14 * // 3.  Neither the name of the copyright holder nor the names of its
15 * // contributors may be used to endorse or promote products derived from
16 * // this software without specific prior written permission.
17 * //
18 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29use crate::RenderingIntent;
30use std::error::Error;
31use std::fmt::Display;
32
33#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
34pub struct MalformedSize {
35    pub size: usize,
36    pub expected: usize,
37}
38
39#[derive(Debug, Clone, PartialOrd, PartialEq)]
40pub enum CmsError {
41    LaneSizeMismatch,
42    LaneMultipleOfChannels,
43    InvalidProfile,
44    InvalidTrcCurve,
45    InvalidCicp,
46    CurveLutIsTooLarge,
47    ParametricCurveZeroDivision,
48    InvalidRenderingIntent,
49    DivisionByZero,
50    UnsupportedColorPrimaries(u8),
51    UnsupportedTrc(u8),
52    InvalidLayout,
53    UnsupportedProfileConnection,
54    BuildTransferFunction,
55    UnsupportedChannelConfiguration,
56    UnknownTag(u32),
57    UnknownTagTypeDefinition(u32),
58    UnsupportedLutRenderingIntent(RenderingIntent),
59    InvalidAtoBLut,
60    OverflowingError,
61    LUTTablesInvalidKind,
62    MalformedClut(MalformedSize),
63    MalformedCurveLutTable(MalformedSize),
64    InvalidInksCountForProfile,
65    MalformedTrcCurve(String),
66}
67
68impl Display for CmsError {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        match self {
71            CmsError::LaneSizeMismatch => f.write_str("Lanes length must match"),
72            CmsError::LaneMultipleOfChannels => {
73                f.write_str("Lane length must not be multiple of channel count")
74            }
75            CmsError::InvalidProfile => f.write_str("Invalid ICC profile"),
76            CmsError::InvalidCicp => {
77                f.write_str("Invalid Code Independent point (CICP) in ICC profile")
78            }
79            CmsError::InvalidTrcCurve => f.write_str("Invalid TRC curve"),
80            CmsError::CurveLutIsTooLarge => f.write_str("Curve Lut is too large"),
81            CmsError::ParametricCurveZeroDivision => {
82                f.write_str("Parametric Curve definition causes division by zero")
83            }
84            CmsError::InvalidRenderingIntent => f.write_str("Invalid rendering intent"),
85            CmsError::DivisionByZero => f.write_str("Division by zero"),
86            CmsError::UnsupportedColorPrimaries(value) => {
87                f.write_fmt(format_args!("Unsupported color primaries, {value}"))
88            }
89            CmsError::UnsupportedTrc(value) => f.write_fmt(format_args!("Unsupported TRC {value}")),
90            CmsError::InvalidLayout => f.write_str("Invalid layout"),
91            CmsError::UnsupportedProfileConnection => f.write_str("Unsupported profile connection"),
92            CmsError::BuildTransferFunction => f.write_str("Can't reconstruct transfer function"),
93            CmsError::UnsupportedChannelConfiguration => {
94                f.write_str("Can't reconstruct channel configuration")
95            }
96            CmsError::UnknownTag(t) => f.write_fmt(format_args!("Unknown tag: {t}")),
97            CmsError::UnknownTagTypeDefinition(t) => {
98                f.write_fmt(format_args!("Unknown tag type definition: {t}"))
99            }
100            CmsError::UnsupportedLutRenderingIntent(intent) => f.write_fmt(format_args!(
101                "Can't find LUT for rendering intent: {intent:?}"
102            )),
103            CmsError::InvalidAtoBLut => f.write_str("Invalid A to B Lut"),
104            CmsError::OverflowingError => {
105                f.write_str("Overflowing was happen, that is not allowed")
106            }
107            CmsError::LUTTablesInvalidKind => f.write_str("All LUT curves must have same kind"),
108            CmsError::MalformedClut(size) => {
109                f.write_fmt(format_args!("Invalid CLUT size: {size:?}"))
110            }
111            CmsError::MalformedCurveLutTable(size) => {
112                f.write_fmt(format_args!("Malformed curve LUT size: {size:?}"))
113            }
114            CmsError::InvalidInksCountForProfile => {
115                f.write_str("Invalid inks count for profile was provided")
116            }
117            CmsError::MalformedTrcCurve(str) => f.write_str(str),
118        }
119    }
120}
121
122impl Error for CmsError {}