Debian Codegraph @

debian/missing-sources/moxcms/src/conversions/rgbxyz_float.rs

raw
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 #![cfg(feature = "extended_range")]
30 use crate::trc::ToneCurveEvaluator;
31 use crate::{CmsError, Layout, Matrix3f, PointeeSizeExpressible, Rgb, TransformExecutor};
32 use num_traits::AsPrimitive;
33 use std::marker::PhantomData;
34 use std::sync::Arc;
35
36 pub(crate) struct TransformShaperRgbFloat<T: Clone, const BUCKET: usize> {
37 pub(crate) r_linear: Box<[f32; BUCKET]>,
38 pub(crate) g_linear: Box<[f32; BUCKET]>,
39 pub(crate) b_linear: Box<[f32; BUCKET]>,
40 pub(crate) gamma_evaluator: Box<dyn ToneCurveEvaluator + Send + Sync>,
41 pub(crate) adaptation_matrix: Matrix3f,
42 pub(crate) phantom_data: PhantomData<T>,
43 }
44
45 #[cfg(feature = "extended_range")]
46 pub(crate) struct TransformShaperFloatInOut<T: Clone> {
47 pub(crate) linear_evaluator: Box<dyn ToneCurveEvaluator + Send + Sync>,
48 pub(crate) gamma_evaluator: Box<dyn ToneCurveEvaluator + Send + Sync>,
49 pub(crate) adaptation_matrix: Matrix3f,
50 pub(crate) phantom_data: PhantomData<T>,
51 }
52
53 struct TransformShaperFloatScalar<
54 T: Clone,
55 const SRC_LAYOUT: u8,
56 const DST_LAYOUT: u8,
57 const LINEAR_CAP: usize,
58 > {
59 pub(crate) profile: TransformShaperRgbFloat<T, LINEAR_CAP>,
60 pub(crate) bit_depth: usize,
61 }
62
63 struct TransformShaperRgbFloatInOut<T: Clone, const SRC_LAYOUT: u8, const DST_LAYOUT: u8> {
64 pub(crate) profile: TransformShaperFloatInOut<T>,
65 pub(crate) bit_depth: usize,
66 }
67
68 pub(crate) fn make_rgb_xyz_rgb_transform_float<
69 T: Clone + Send + Sync + PointeeSizeExpressible + 'static + Copy + Default,
70 const LINEAR_CAP: usize,
71 >(
72 src_layout: Layout,
73 dst_layout: Layout,
74 profile: TransformShaperRgbFloat<T, LINEAR_CAP>,
75 bit_depth: usize,
76 ) -> Result<Arc<dyn TransformExecutor<T> + Send + Sync>, CmsError>
77 where
78 u32: AsPrimitive<T>,
79 f32: AsPrimitive<T>,
80 {
81 if (src_layout == Layout::Rgba) && (dst_layout == Layout::Rgba) {
82 return Ok(Arc::new(TransformShaperFloatScalar::<
83 T,
84 { Layout::Rgba as u8 },
85 { Layout::Rgba as u8 },
86 LINEAR_CAP,
87 > {
88 profile,
89 bit_depth,
90 }));
91 } else if (src_layout == Layout::Rgb) && (dst_layout == Layout::Rgba) {
92 return Ok(Arc::new(TransformShaperFloatScalar::<
93 T,
94 { Layout::Rgb as u8 },
95 { Layout::Rgba as u8 },
96 LINEAR_CAP,
97 > {
98 profile,
99 bit_depth,
100 }));
101 } else if (src_layout == Layout::Rgba) && (dst_layout == Layout::Rgb) {
102 return Ok(Arc::new(TransformShaperFloatScalar::<
103 T,
104 { Layout::Rgba as u8 },
105 { Layout::Rgb as u8 },
106 LINEAR_CAP,
107 > {
108 profile,
109 bit_depth,
110 }));
111 } else if (src_layout == Layout::Rgb) && (dst_layout == Layout::Rgb) {
112 return Ok(Arc::new(TransformShaperFloatScalar::<
113 T,
114 { Layout::Rgb as u8 },
115 { Layout::Rgb as u8 },
116 LINEAR_CAP,
117 > {
118 profile,
119 bit_depth,
120 }));
121 }
122 Err(CmsError::UnsupportedProfileConnection)
123 }
124
125 pub(crate) fn make_rgb_xyz_rgb_transform_float_in_out<
126 T: Clone + Send + Sync + PointeeSizeExpressible + 'static + Copy + Default + AsPrimitive<f32>,
127 >(
128 src_layout: Layout,
129 dst_layout: Layout,
130 profile: TransformShaperFloatInOut<T>,
131 bit_depth: usize,
132 ) -> Result<Arc<dyn TransformExecutor<T> + Send + Sync>, CmsError>
133 where
134 u32: AsPrimitive<T>,
135 f32: AsPrimitive<T>,
136 {
137 if (src_layout == Layout::Rgba) && (dst_layout == Layout::Rgba) {
138 return Ok(Arc::new(TransformShaperRgbFloatInOut::<
139 T,
140 { Layout::Rgba as u8 },
141 { Layout::Rgba as u8 },
142 > {
143 profile,
144 bit_depth,
145 }));
146 } else if (src_layout == Layout::Rgb) && (dst_layout == Layout::Rgba) {
147 return Ok(Arc::new(TransformShaperRgbFloatInOut::<
148 T,
149 { Layout::Rgb as u8 },
150 { Layout::Rgba as u8 },
151 > {
152 profile,
153 bit_depth,
154 }));
155 } else if (src_layout == Layout::Rgba) && (dst_layout == Layout::Rgb) {
156 return Ok(Arc::new(TransformShaperRgbFloatInOut::<
157 T,
158 { Layout::Rgba as u8 },
159 { Layout::Rgb as u8 },
160 > {
161 profile,
162 bit_depth,
163 }));
164 } else if (src_layout == Layout::Rgb) && (dst_layout == Layout::Rgb) {
165 return Ok(Arc::new(TransformShaperRgbFloatInOut::<
166 T,
167 { Layout::Rgb as u8 },
168 { Layout::Rgb as u8 },
169 > {
170 profile,
171 bit_depth,
172 }));
173 }
174 Err(CmsError::UnsupportedProfileConnection)
175 }
176
177 impl<
178 T: Clone + PointeeSizeExpressible + Copy + Default + 'static,
179 const SRC_LAYOUT: u8,
180 const DST_LAYOUT: u8,
181 const LINEAR_CAP: usize,
182 > TransformExecutor<T> for TransformShaperFloatScalar<T, SRC_LAYOUT, DST_LAYOUT, LINEAR_CAP>
183 where
184 u32: AsPrimitive<T>,
185 f32: AsPrimitive<T>,
186 {
187 fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
188 use crate::mlaf::mlaf;
189 let src_cn = Layout::from(SRC_LAYOUT);
190 let dst_cn = Layout::from(DST_LAYOUT);
191 let src_channels = src_cn.channels();
192 let dst_channels = dst_cn.channels();
193
194 if src.len() / src_channels != dst.len() / dst_channels {
195 return Err(CmsError::LaneSizeMismatch);
196 }
197 if src.len() % src_channels != 0 {
198 return Err(CmsError::LaneMultipleOfChannels);
199 }
200 if dst.len() % dst_channels != 0 {
201 return Err(CmsError::LaneMultipleOfChannels);
202 }
203
204 let transform = self.profile.adaptation_matrix;
205 let max_colors: T = ((1 << self.bit_depth) - 1).as_();
206
207 for (src, dst) in src
208 .chunks_exact(src_channels)
209 .zip(dst.chunks_exact_mut(dst_channels))
210 {
211 let r = self.profile.r_linear[src[src_cn.r_i()]._as_usize()];
212 let g = self.profile.g_linear[src[src_cn.g_i()]._as_usize()];
213 let b = self.profile.b_linear[src[src_cn.b_i()]._as_usize()];
214 let a = if src_channels == 4 {
215 src[src_cn.a_i()]
216 } else {
217 max_colors
218 };
219
220 let new_r = mlaf(
221 mlaf(r * transform.v[0][0], g, transform.v[0][1]),
222 b,
223 transform.v[0][2],
224 );
225
226 let new_g = mlaf(
227 mlaf(r * transform.v[1][0], g, transform.v[1][1]),
228 b,
229 transform.v[1][2],
230 );
231
232 let new_b = mlaf(
233 mlaf(r * transform.v[2][0], g, transform.v[2][1]),
234 b,
235 transform.v[2][2],
236 );
237
238 let mut rgb = Rgb::new(new_r, new_g, new_b);
239 rgb = self.profile.gamma_evaluator.evaluate_tristimulus(rgb);
240
241 dst[dst_cn.r_i()] = rgb.r.as_();
242 dst[dst_cn.g_i()] = rgb.g.as_();
243 dst[dst_cn.b_i()] = rgb.b.as_();
244 if dst_channels == 4 {
245 dst[dst_cn.a_i()] = a;
246 }
247 }
248
249 Ok(())
250 }
251 }
252
253 impl<
254 T: Clone + PointeeSizeExpressible + Copy + Default + 'static + AsPrimitive<f32>,
255 const SRC_LAYOUT: u8,
256 const DST_LAYOUT: u8,
257 > TransformExecutor<T> for TransformShaperRgbFloatInOut<T, SRC_LAYOUT, DST_LAYOUT>
258 where
259 u32: AsPrimitive<T>,
260 f32: AsPrimitive<T>,
261 {
262 fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
263 use crate::mlaf::mlaf;
264 let src_cn = Layout::from(SRC_LAYOUT);
265 let dst_cn = Layout::from(DST_LAYOUT);
266 let src_channels = src_cn.channels();
267 let dst_channels = dst_cn.channels();
268
269 if src.len() / src_channels != dst.len() / dst_channels {
270 return Err(CmsError::LaneSizeMismatch);
271 }
272 if src.len() % src_channels != 0 {
273 return Err(CmsError::LaneMultipleOfChannels);
274 }
275 if dst.len() % dst_channels != 0 {
276 return Err(CmsError::LaneMultipleOfChannels);
277 }
278
279 let transform = self.profile.adaptation_matrix;
280 let max_colors: T = ((1 << self.bit_depth) - 1).as_();
281
282 for (src, dst) in src
283 .chunks_exact(src_channels)
284 .zip(dst.chunks_exact_mut(dst_channels))
285 {
286 let mut src_rgb = Rgb::new(
287 src[src_cn.r_i()].as_(),
288 src[src_cn.g_i()].as_(),
289 src[src_cn.b_i()].as_(),
290 );
291 src_rgb = self.profile.linear_evaluator.evaluate_tristimulus(src_rgb);
292 let r = src_rgb.r;
293 let g = src_rgb.g;
294 let b = src_rgb.b;
295 let a = if src_channels == 4 {
296 src[src_cn.a_i()]
297 } else {
298 max_colors
299 };
300
301 let new_r = mlaf(
302 mlaf(r * transform.v[0][0], g, transform.v[0][1]),
303 b,
304 transform.v[0][2],
305 );
306
307 let new_g = mlaf(
308 mlaf(r * transform.v[1][0], g, transform.v[1][1]),
309 b,
310 transform.v[1][2],
311 );
312
313 let new_b = mlaf(
314 mlaf(r * transform.v[2][0], g, transform.v[2][1]),
315 b,
316 transform.v[2][2],
317 );
318
319 let mut rgb = Rgb::new(new_r, new_g, new_b);
320 rgb = self.profile.gamma_evaluator.evaluate_tristimulus(rgb);
321
322 dst[dst_cn.r_i()] = rgb.r.as_();
323 dst[dst_cn.g_i()] = rgb.g.as_();
324 dst[dst_cn.b_i()] = rgb.b.as_();
325
326 if dst_channels == 4 {
327 dst[dst_cn.a_i()] = a;
328 }
329 }
330
331 Ok(())
332 }
333 }