Debian Codegraph @

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

raw
1 /*
2 * // Copyright (c) Radzivon Bartoshyk 6/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::conversions::LutBarycentricReduction;
30 use crate::conversions::katana::{
31 CopyAlphaStage, InjectAlphaStage, Katana, KatanaInitialStage, KatanaIntermediateStage,
32 KatanaPostFinalizationStage, KatanaStageLabToXyz, KatanaStageXyzToLab,
33 katana_create_rgb_lin_lut, katana_input_make_lut_nx3, katana_multi_dimensional_3xn_to_device,
34 katana_multi_dimensional_nx3_to_pcs, katana_output_make_lut_3xn, katana_pcs_lab_v2_to_v4,
35 katana_pcs_lab_v4_to_v2, katana_prepare_inverse_lut_rgb_xyz,
36 };
37 use crate::{
38 CmsError, ColorProfile, DataColorSpace, GammaLutInterpolate, Layout, LutWarehouse,
39 PointeeSizeExpressible, TransformExecutor, TransformOptions,
40 };
41 use num_traits::AsPrimitive;
42 use std::sync::Arc;
43
44 pub(crate) fn do_any_to_any<
45 T: Copy
46 + Default
47 + AsPrimitive<f32>
48 + Send
49 + Sync
50 + AsPrimitive<usize>
51 + PointeeSizeExpressible
52 + GammaLutInterpolate,
53 const BIT_DEPTH: usize,
54 const LINEAR_CAP: usize,
55 const GAMMA_LUT: usize,
56 >(
57 src_layout: Layout,
58 source: &ColorProfile,
59 dst_layout: Layout,
60 dest: &ColorProfile,
61 options: TransformOptions,
62 ) -> Result<Arc<dyn TransformExecutor<T> + Send + Sync>, CmsError>
63 where
64 f32: AsPrimitive<T>,
65 u32: AsPrimitive<T>,
66 (): LutBarycentricReduction<T, u8>,
67 (): LutBarycentricReduction<T, u16>,
68 {
69 let mut stages: Vec<Box<dyn KatanaIntermediateStage<f32> + Send + Sync>> = Vec::new();
70
71 let initial_stage: Box<dyn KatanaInitialStage<f32, T> + Send + Sync> = match source
72 .is_matrix_shaper()
73 {
74 true => {
75 let state =
76 katana_create_rgb_lin_lut::<T, BIT_DEPTH, LINEAR_CAP>(src_layout, source, options)?;
77 stages.extend(state.stages);
78 state.initial_stage
79 }
80 false => match source.get_device_to_pcs(options.rendering_intent).ok_or(
81 CmsError::UnsupportedLutRenderingIntent(source.rendering_intent),
82 )? {
83 LutWarehouse::Lut(lut) => katana_input_make_lut_nx3::<T>(
84 src_layout,
85 src_layout.channels(),
86 lut,
87 options,
88 source.pcs,
89 BIT_DEPTH,
90 )?,
91 LutWarehouse::Multidimensional(mab) => katana_multi_dimensional_nx3_to_pcs::<T>(
92 src_layout, mab, options, source.pcs, BIT_DEPTH,
93 )?,
94 },
95 };
96
97 stages.push(katana_pcs_lab_v2_to_v4(source));
98 if source.pcs == DataColorSpace::Lab {
99 stages.push(Box::new(KatanaStageLabToXyz::default()));
100 }
101 if dest.pcs == DataColorSpace::Lab {
102 stages.push(Box::new(KatanaStageXyzToLab::default()));
103 }
104 stages.push(katana_pcs_lab_v4_to_v2(dest));
105
106 let final_stage = if dest.has_pcs_to_device_lut() {
107 let pcs_to_device = dest
108 .get_pcs_to_device(options.rendering_intent)
109 .ok_or(CmsError::UnsupportedProfileConnection)?;
110 match pcs_to_device {
111 LutWarehouse::Lut(lut) => katana_output_make_lut_3xn::<T>(
112 dst_layout,
113 lut,
114 options,
115 dest.color_space,
116 BIT_DEPTH,
117 )?,
118 LutWarehouse::Multidimensional(mab) => katana_multi_dimensional_3xn_to_device::<T>(
119 dst_layout, mab, options, dest.pcs, BIT_DEPTH,
120 )?,
121 }
122 } else if dest.is_matrix_shaper() {
123 let state = katana_prepare_inverse_lut_rgb_xyz::<T, BIT_DEPTH, GAMMA_LUT>(
124 dest, dst_layout, options,
125 )?;
126 stages.extend(state.stages);
127 state.final_stage
128 } else {
129 return Err(CmsError::UnsupportedProfileConnection);
130 };
131
132 let mut post_finalization: Vec<Box<dyn KatanaPostFinalizationStage<T> + Send + Sync>> =
133 Vec::new();
134 if let Some(stage) =
135 prepare_alpha_finalizer::<T>(src_layout, source, dst_layout, dest, BIT_DEPTH)
136 {
137 post_finalization.push(stage);
138 }
139
140 Ok(Arc::new(Katana::<f32, T> {
141 initial_stage,
142 final_stage,
143 stages,
144 post_finalization,
145 }))
146 }
147
148 pub(crate) fn prepare_alpha_finalizer<
149 T: Copy
150 + Default
151 + AsPrimitive<f32>
152 + Send
153 + Sync
154 + AsPrimitive<usize>
155 + PointeeSizeExpressible
156 + GammaLutInterpolate,
157 >(
158 src_layout: Layout,
159 source: &ColorProfile,
160 dst_layout: Layout,
161 dest: &ColorProfile,
162 bit_depth: usize,
163 ) -> Option<Box<dyn KatanaPostFinalizationStage<T> + Send + Sync>>
164 where
165 f32: AsPrimitive<T>,
166 {
167 if (dst_layout == Layout::GrayAlpha && dest.color_space == DataColorSpace::Gray)
168 || (dst_layout == Layout::Rgba || dest.color_space == DataColorSpace::Rgb)
169 {
170 return if (src_layout == Layout::GrayAlpha && source.color_space == DataColorSpace::Gray)
171 || (src_layout == Layout::Rgba || source.color_space == DataColorSpace::Rgb)
172 {
173 Some(Box::new(CopyAlphaStage {
174 src_layout,
175 dst_layout,
176 target_color_space: dest.color_space,
177 _phantom: Default::default(),
178 }))
179 } else {
180 Some(Box::new(InjectAlphaStage {
181 dst_layout,
182 target_color_space: dest.color_space,
183 _phantom: Default::default(),
184 bit_depth,
185 }))
186 };
187 }
188 None
189 }