Skip to main content

moxcms/conversions/
rgbxyz_fixed.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 3/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::Layout;
30use crate::matrix::Matrix3;
31use crate::{CmsError, TransformExecutor};
32use num_traits::AsPrimitive;
33use std::sync::Arc;
34
35/// Fixed point conversion Q2.13
36#[allow(dead_code)]
37pub(crate) struct TransformMatrixShaperFp<R, T> {
38    pub(crate) r_linear: Vec<R>,
39    pub(crate) g_linear: Vec<R>,
40    pub(crate) b_linear: Vec<R>,
41    pub(crate) r_gamma: Box<[T; 65536]>,
42    pub(crate) g_gamma: Box<[T; 65536]>,
43    pub(crate) b_gamma: Box<[T; 65536]>,
44    pub(crate) adaptation_matrix: Matrix3<i16>,
45}
46
47/// Fixed point conversion Q2.13
48///
49/// Optimized routine for *all same curves* matrix shaper.
50pub(crate) struct TransformMatrixShaperFixedPointOpt<R, W, T, const LINEAR_CAP: usize> {
51    pub(crate) linear: Box<[R; LINEAR_CAP]>,
52    pub(crate) gamma: Box<[T; 65536]>,
53    pub(crate) adaptation_matrix: Matrix3<W>,
54}
55
56/// Fixed point conversion Q2.13
57///
58/// Optimized routine for *all same curves* matrix shaper.
59#[allow(dead_code)]
60pub(crate) struct TransformMatrixShaperFpOptVec<R, W, T> {
61    pub(crate) linear: Vec<R>,
62    pub(crate) gamma: Box<[T; 65536]>,
63    pub(crate) adaptation_matrix: Matrix3<W>,
64}
65
66#[allow(unused)]
67struct TransformMatrixShaperQ2_13Optimized<
68    T: Copy,
69    const SRC_LAYOUT: u8,
70    const DST_LAYOUT: u8,
71    const LINEAR_CAP: usize,
72    const PRECISION: i32,
73> {
74    pub(crate) profile: TransformMatrixShaperFixedPointOpt<i16, i16, T, LINEAR_CAP>,
75    pub(crate) bit_depth: usize,
76    pub(crate) gamma_lut: usize,
77}
78
79#[allow(unused)]
80impl<
81    T: Clone + PointeeSizeExpressible + Copy + Default + 'static,
82    const SRC_LAYOUT: u8,
83    const DST_LAYOUT: u8,
84    const LINEAR_CAP: usize,
85    const PRECISION: i32,
86> TransformExecutor<T>
87    for TransformMatrixShaperQ2_13Optimized<T, SRC_LAYOUT, DST_LAYOUT, LINEAR_CAP, PRECISION>
88where
89    u32: AsPrimitive<T>,
90{
91    fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
92        let src_cn = Layout::from(SRC_LAYOUT);
93        let dst_cn = Layout::from(DST_LAYOUT);
94        let src_channels = src_cn.channels();
95        let dst_channels = dst_cn.channels();
96
97        if src.len() / src_channels != dst.len() / dst_channels {
98            return Err(CmsError::LaneSizeMismatch);
99        }
100        if src.len() % src_channels != 0 {
101            return Err(CmsError::LaneMultipleOfChannels);
102        }
103        if dst.len() % dst_channels != 0 {
104            return Err(CmsError::LaneMultipleOfChannels);
105        }
106
107        let transform = self.profile.adaptation_matrix;
108        let max_colors: T = ((1 << self.bit_depth as u32) - 1u32).as_();
109        let rnd: i32 = (1i32 << (PRECISION - 1));
110
111        let v_gamma_max = self.gamma_lut as i32 - 1;
112
113        for (src, dst) in src
114            .chunks_exact(src_channels)
115            .zip(dst.chunks_exact_mut(dst_channels))
116        {
117            let r = self.profile.linear[src[src_cn.r_i()]._as_usize()];
118            let g = self.profile.linear[src[src_cn.g_i()]._as_usize()];
119            let b = self.profile.linear[src[src_cn.b_i()]._as_usize()];
120            let a = if src_channels == 4 {
121                src[src_cn.a_i()]
122            } else {
123                max_colors
124            };
125
126            let new_r = r as i32 * transform.v[0][0] as i32
127                + g as i32 * transform.v[0][1] as i32
128                + b as i32 * transform.v[0][2] as i32
129                + rnd;
130
131            let r_q2_13 = (new_r >> PRECISION).min(v_gamma_max).max(0) as u16;
132
133            let new_g = r as i32 * transform.v[1][0] as i32
134                + g as i32 * transform.v[1][1] as i32
135                + b as i32 * transform.v[1][2] as i32
136                + rnd;
137
138            let g_q2_13 = (new_g >> PRECISION).min(v_gamma_max).max(0) as u16;
139
140            let new_b = r as i32 * transform.v[2][0] as i32
141                + g as i32 * transform.v[2][1] as i32
142                + b as i32 * transform.v[2][2] as i32
143                + rnd;
144
145            let b_q2_13 = (new_b >> PRECISION).min(v_gamma_max).max(0) as u16;
146
147            dst[dst_cn.r_i()] = self.profile.gamma[r_q2_13 as usize];
148            dst[dst_cn.g_i()] = self.profile.gamma[g_q2_13 as usize];
149            dst[dst_cn.b_i()] = self.profile.gamma[b_q2_13 as usize];
150            if dst_channels == 4 {
151                dst[dst_cn.a_i()] = a;
152            }
153        }
154        Ok(())
155    }
156}
157
158#[allow(unused_macros)]
159macro_rules! create_rgb_xyz_dependant_q2_13_executor {
160    ($dep_name: ident, $dependant: ident, $resolution: ident, $shaper: ident) => {
161        pub(crate) fn $dep_name<
162            T: Clone + Send + Sync + AsPrimitive<usize> + Default + PointeeSizeExpressible,
163            const LINEAR_CAP: usize,
164            const PRECISION: i32,
165        >(
166            src_layout: Layout,
167            dst_layout: Layout,
168            profile: $shaper<T, LINEAR_CAP>,
169            gamma_lut: usize,
170            bit_depth: usize,
171        ) -> Result<Arc<dyn TransformExecutor<T> + Send + Sync>, CmsError>
172        where
173            u32: AsPrimitive<T>,
174        {
175            let q2_13_profile =
176                profile.to_q2_13_n::<$resolution, PRECISION, LINEAR_CAP>(gamma_lut, bit_depth);
177            if (src_layout == Layout::Rgba) && (dst_layout == Layout::Rgba) {
178                return Ok(Arc::new($dependant::<
179                    T,
180                    { Layout::Rgba as u8 },
181                    { Layout::Rgba as u8 },
182                    LINEAR_CAP,
183                    PRECISION,
184                > {
185                    profile: q2_13_profile,
186                    bit_depth,
187                    gamma_lut,
188                }));
189            } else if (src_layout == Layout::Rgb) && (dst_layout == Layout::Rgba) {
190                return Ok(Arc::new($dependant::<
191                    T,
192                    { Layout::Rgb as u8 },
193                    { Layout::Rgba as u8 },
194                    LINEAR_CAP,
195                    PRECISION,
196                > {
197                    profile: q2_13_profile,
198                    bit_depth,
199                    gamma_lut,
200                }));
201            } else if (src_layout == Layout::Rgba) && (dst_layout == Layout::Rgb) {
202                return Ok(Arc::new($dependant::<
203                    T,
204                    { Layout::Rgba as u8 },
205                    { Layout::Rgb as u8 },
206                    LINEAR_CAP,
207                    PRECISION,
208                > {
209                    profile: q2_13_profile,
210                    bit_depth,
211                    gamma_lut,
212                }));
213            } else if (src_layout == Layout::Rgb) && (dst_layout == Layout::Rgb) {
214                return Ok(Arc::new($dependant::<
215                    T,
216                    { Layout::Rgb as u8 },
217                    { Layout::Rgb as u8 },
218                    LINEAR_CAP,
219                    PRECISION,
220                > {
221                    profile: q2_13_profile,
222                    bit_depth,
223                    gamma_lut,
224                }));
225            }
226            Err(CmsError::UnsupportedProfileConnection)
227        }
228    };
229}
230
231#[allow(unused_macros)]
232macro_rules! create_rgb_xyz_dependant_q2_13_executor_fp {
233    ($dep_name: ident, $dependant: ident, $resolution: ident, $shaper: ident) => {
234        pub(crate) fn $dep_name<
235            T: Clone + Send + Sync + AsPrimitive<usize> + Default + PointeeSizeExpressible,
236            const LINEAR_CAP: usize,
237            const PRECISION: i32,
238        >(
239            src_layout: Layout,
240            dst_layout: Layout,
241            profile: $shaper<T, LINEAR_CAP>,
242            gamma_lut: usize,
243            bit_depth: usize,
244        ) -> Result<Arc<dyn TransformExecutor<T> + Send + Sync>, CmsError>
245        where
246            u32: AsPrimitive<T>,
247        {
248            let q2_13_profile = profile.to_q2_13_i::<$resolution, PRECISION>(gamma_lut, bit_depth);
249            if (src_layout == Layout::Rgba) && (dst_layout == Layout::Rgba) {
250                return Ok(Arc::new($dependant::<
251                    T,
252                    { Layout::Rgba as u8 },
253                    { Layout::Rgba as u8 },
254                    PRECISION,
255                > {
256                    profile: q2_13_profile,
257                    bit_depth,
258                    gamma_lut,
259                }));
260            } else if (src_layout == Layout::Rgb) && (dst_layout == Layout::Rgba) {
261                return Ok(Arc::new($dependant::<
262                    T,
263                    { Layout::Rgb as u8 },
264                    { Layout::Rgba as u8 },
265                    PRECISION,
266                > {
267                    profile: q2_13_profile,
268                    bit_depth,
269                    gamma_lut,
270                }));
271            } else if (src_layout == Layout::Rgba) && (dst_layout == Layout::Rgb) {
272                return Ok(Arc::new($dependant::<
273                    T,
274                    { Layout::Rgba as u8 },
275                    { Layout::Rgb as u8 },
276                    PRECISION,
277                > {
278                    profile: q2_13_profile,
279                    bit_depth,
280                    gamma_lut,
281                }));
282            } else if (src_layout == Layout::Rgb) && (dst_layout == Layout::Rgb) {
283                return Ok(Arc::new($dependant::<
284                    T,
285                    { Layout::Rgb as u8 },
286                    { Layout::Rgb as u8 },
287                    PRECISION,
288                > {
289                    profile: q2_13_profile,
290                    bit_depth,
291                    gamma_lut,
292                }));
293            }
294            Err(CmsError::UnsupportedProfileConnection)
295        }
296    };
297}
298
299#[cfg(all(target_arch = "aarch64", feature = "neon_shaper_fixed_point_paths"))]
300macro_rules! create_rgb_xyz_dependant_q1_30_executor {
301    ($dep_name: ident, $dependant: ident, $resolution: ident, $shaper: ident) => {
302        pub(crate) fn $dep_name<
303            T: Clone + Send + Sync + AsPrimitive<usize> + Default + PointeeSizeExpressible,
304            const LINEAR_CAP: usize,
305            const PRECISION: i32,
306        >(
307            src_layout: Layout,
308            dst_layout: Layout,
309            profile: $shaper<T, LINEAR_CAP>,
310            gamma_lut: usize,
311            bit_depth: usize,
312        ) -> Result<Arc<dyn TransformExecutor<T> + Send + Sync>, CmsError>
313        where
314            u32: AsPrimitive<T>,
315        {
316            let q1_30_profile = profile.to_q1_30_n::<$resolution, PRECISION>(gamma_lut, bit_depth);
317            if (src_layout == Layout::Rgba) && (dst_layout == Layout::Rgba) {
318                return Ok(Arc::new($dependant::<
319                    T,
320                    { Layout::Rgba as u8 },
321                    { Layout::Rgba as u8 },
322                > {
323                    profile: q1_30_profile,
324                    gamma_lut,
325                    bit_depth,
326                }));
327            } else if (src_layout == Layout::Rgb) && (dst_layout == Layout::Rgba) {
328                return Ok(Arc::new($dependant::<
329                    T,
330                    { Layout::Rgb as u8 },
331                    { Layout::Rgba as u8 },
332                > {
333                    profile: q1_30_profile,
334                    gamma_lut,
335                    bit_depth,
336                }));
337            } else if (src_layout == Layout::Rgba) && (dst_layout == Layout::Rgb) {
338                return Ok(Arc::new($dependant::<
339                    T,
340                    { Layout::Rgba as u8 },
341                    { Layout::Rgb as u8 },
342                > {
343                    profile: q1_30_profile,
344                    gamma_lut,
345                    bit_depth,
346                }));
347            } else if (src_layout == Layout::Rgb) && (dst_layout == Layout::Rgb) {
348                return Ok(Arc::new($dependant::<
349                    T,
350                    { Layout::Rgb as u8 },
351                    { Layout::Rgb as u8 },
352                > {
353                    profile: q1_30_profile,
354                    gamma_lut,
355                    bit_depth,
356                }));
357            }
358            Err(CmsError::UnsupportedProfileConnection)
359        }
360    };
361}
362
363#[cfg(all(target_arch = "aarch64", feature = "neon_shaper_fixed_point_paths"))]
364use crate::conversions::neon::{TransformShaperQ1_30NeonOpt, TransformShaperQ2_13NeonOpt};
365
366#[cfg(all(target_arch = "aarch64", feature = "neon_shaper_fixed_point_paths"))]
367create_rgb_xyz_dependant_q2_13_executor_fp!(
368    make_rgb_xyz_q2_13_opt,
369    TransformShaperQ2_13NeonOpt,
370    i16,
371    TransformMatrixShaperOptimized
372);
373
374#[cfg(all(target_arch = "aarch64", feature = "neon_shaper_fixed_point_paths"))]
375create_rgb_xyz_dependant_q1_30_executor!(
376    make_rgb_xyz_q1_30_opt,
377    TransformShaperQ1_30NeonOpt,
378    i32,
379    TransformMatrixShaperOptimized
380);
381
382#[cfg(not(all(target_arch = "aarch64", feature = "neon_shaper_fixed_point_paths")))]
383create_rgb_xyz_dependant_q2_13_executor!(
384    make_rgb_xyz_q2_13_opt,
385    TransformMatrixShaperQ2_13Optimized,
386    i16,
387    TransformMatrixShaperOptimized
388);
389
390#[cfg(all(
391    any(target_arch = "x86", target_arch = "x86_64"),
392    feature = "sse_shaper_fixed_point_paths"
393))]
394use crate::conversions::sse::TransformShaperQ2_13OptSse;
395
396#[cfg(all(
397    any(target_arch = "x86", target_arch = "x86_64"),
398    feature = "sse_shaper_fixed_point_paths"
399))]
400create_rgb_xyz_dependant_q2_13_executor_fp!(
401    make_rgb_xyz_q2_13_transform_sse_41_opt,
402    TransformShaperQ2_13OptSse,
403    i32,
404    TransformMatrixShaperOptimized
405);
406
407#[cfg(all(target_arch = "x86_64", feature = "avx_shaper_fixed_point_paths"))]
408use crate::conversions::avx::TransformShaperRgbQ2_13OptAvx;
409use crate::conversions::rgbxyz::TransformMatrixShaperOptimized;
410use crate::transform::PointeeSizeExpressible;
411
412#[cfg(all(target_arch = "x86_64", feature = "avx_shaper_fixed_point_paths"))]
413create_rgb_xyz_dependant_q2_13_executor_fp!(
414    make_rgb_xyz_q2_13_transform_avx2_opt,
415    TransformShaperRgbQ2_13OptAvx,
416    i32,
417    TransformMatrixShaperOptimized
418);
419
420#[cfg(all(target_arch = "x86_64", feature = "avx512_shaper_fixed_point_paths"))]
421use crate::conversions::avx512::TransformShaperRgbQ2_13OptAvx512;
422
423#[cfg(all(target_arch = "x86_64", feature = "avx512_shaper_fixed_point_paths"))]
424create_rgb_xyz_dependant_q2_13_executor!(
425    make_rgb_xyz_q2_13_transform_avx512_opt,
426    TransformShaperRgbQ2_13OptAvx512,
427    i32,
428    TransformMatrixShaperOptimized
429);