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