1use 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 OutOfMemory(usize),
67 IncorrectlyFormedLut(String),
68}
69
70impl 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
128impl Error for CmsError {}
129
130macro_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
143pub(crate) use try_vec;