/rust/registry/src/index.crates.io-1949cf8c6b5b557f/moxcms-0.7.7/src/err.rs
Line | Count | Source |
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 | | */ |
29 | | use crate::RenderingIntent; |
30 | | use std::error::Error; |
31 | | use std::fmt::Display; |
32 | | |
33 | | #[derive(Debug, Copy, Clone, PartialOrd, PartialEq)] |
34 | | pub struct MalformedSize { |
35 | | pub size: usize, |
36 | | pub expected: usize, |
37 | | } |
38 | | |
39 | | #[derive(Debug, Clone, PartialOrd, PartialEq)] |
40 | | pub 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 | | OutOfMemory(usize), |
67 | | } |
68 | | |
69 | | impl Display for CmsError { |
70 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
71 | 0 | match self { |
72 | 0 | CmsError::LaneSizeMismatch => f.write_str("Lanes length must match"), |
73 | | CmsError::LaneMultipleOfChannels => { |
74 | 0 | f.write_str("Lane length must not be multiple of channel count") |
75 | | } |
76 | 0 | CmsError::InvalidProfile => f.write_str("Invalid ICC profile"), |
77 | | CmsError::InvalidCicp => { |
78 | 0 | f.write_str("Invalid Code Independent point (CICP) in ICC profile") |
79 | | } |
80 | 0 | CmsError::InvalidTrcCurve => f.write_str("Invalid TRC curve"), |
81 | 0 | CmsError::CurveLutIsTooLarge => f.write_str("Curve Lut is too large"), |
82 | | CmsError::ParametricCurveZeroDivision => { |
83 | 0 | f.write_str("Parametric Curve definition causes division by zero") |
84 | | } |
85 | 0 | CmsError::InvalidRenderingIntent => f.write_str("Invalid rendering intent"), |
86 | 0 | CmsError::DivisionByZero => f.write_str("Division by zero"), |
87 | 0 | CmsError::UnsupportedColorPrimaries(value) => { |
88 | 0 | f.write_fmt(format_args!("Unsupported color primaries, {value}")) |
89 | | } |
90 | 0 | CmsError::UnsupportedTrc(value) => f.write_fmt(format_args!("Unsupported TRC {value}")), |
91 | 0 | CmsError::InvalidLayout => f.write_str("Invalid layout"), |
92 | 0 | CmsError::UnsupportedProfileConnection => f.write_str("Unsupported profile connection"), |
93 | 0 | CmsError::BuildTransferFunction => f.write_str("Can't reconstruct transfer function"), |
94 | | CmsError::UnsupportedChannelConfiguration => { |
95 | 0 | f.write_str("Can't reconstruct channel configuration") |
96 | | } |
97 | 0 | CmsError::UnknownTag(t) => f.write_fmt(format_args!("Unknown tag: {t}")), |
98 | 0 | CmsError::UnknownTagTypeDefinition(t) => { |
99 | 0 | f.write_fmt(format_args!("Unknown tag type definition: {t}")) |
100 | | } |
101 | 0 | CmsError::UnsupportedLutRenderingIntent(intent) => f.write_fmt(format_args!( |
102 | 0 | "Can't find LUT for rendering intent: {intent:?}" |
103 | | )), |
104 | 0 | CmsError::InvalidAtoBLut => f.write_str("Invalid A to B Lut"), |
105 | | CmsError::OverflowingError => { |
106 | 0 | f.write_str("Overflowing was happen, that is not allowed") |
107 | | } |
108 | 0 | CmsError::LUTTablesInvalidKind => f.write_str("All LUT curves must have same kind"), |
109 | 0 | CmsError::MalformedClut(size) => { |
110 | 0 | f.write_fmt(format_args!("Invalid CLUT size: {size:?}")) |
111 | | } |
112 | 0 | CmsError::MalformedCurveLutTable(size) => { |
113 | 0 | f.write_fmt(format_args!("Malformed curve LUT size: {size:?}")) |
114 | | } |
115 | | CmsError::InvalidInksCountForProfile => { |
116 | 0 | f.write_str("Invalid inks count for profile was provided") |
117 | | } |
118 | 0 | CmsError::MalformedTrcCurve(str) => f.write_str(str), |
119 | 0 | CmsError::OutOfMemory(capacity) => f.write_fmt(format_args!( |
120 | 0 | "There is no enough memory to allocate {capacity} bytes" |
121 | | )), |
122 | | } |
123 | 0 | } |
124 | | } |
125 | | |
126 | | impl Error for CmsError {} |
127 | | |
128 | | macro_rules! try_vec { |
129 | | () => { |
130 | | Vec::new() |
131 | | }; |
132 | | ($elem:expr; $n:expr) => {{ |
133 | | let mut v = Vec::new(); |
134 | | v.try_reserve_exact($n) |
135 | | .map_err(|_| crate::err::CmsError::OutOfMemory($n))?; |
136 | | v.resize($n, $elem); |
137 | | v |
138 | | }}; |
139 | | } |
140 | | |
141 | | pub(crate) use try_vec; |