moxcms/conversions/katana/
md_nx3.rs1use crate::conversions::katana::KatanaInitialStage;
30use crate::conversions::katana::md3x3::MultidimensionalDirection;
31use crate::conversions::katana::md4x3::{execute_matrix_stage3, execute_simple_curves3};
32use crate::conversions::md_lut::{
33 MultidimensionalLut, NVector, linear_1i_vec3f, linear_2i_vec3f_direct, linear_3i_vec3f_direct,
34 linear_4i_vec3f, linear_5i_vec3f, linear_6i_vec3f, linear_7i_vec3f, linear_8i_vec3f,
35 linear_9i_vec3f, linear_10i_vec3f, linear_11i_vec3f, linear_12i_vec3f, linear_13i_vec3f,
36 linear_14i_vec3f, linear_15i_vec3f,
37};
38use crate::safe_math::SafeMul;
39use crate::trc::lut_interp_linear_float;
40use crate::{
41 CmsError, DataColorSpace, Layout, LutMultidimensionalType, MalformedSize, Matrix3d, Matrix3f,
42 PointeeSizeExpressible, TransformOptions, Vector3d, Vector3f,
43};
44use num_traits::AsPrimitive;
45use std::marker::PhantomData;
46
47struct MultidimensionalNx3<
48 T: Copy + Default + AsPrimitive<f32> + PointeeSizeExpressible + Send + Sync,
49> {
50 a_curves: Option<Vec<Vec<f32>>>,
51 m_curves: Option<Box<[Vec<f32>; 3]>>,
52 b_curves: Option<Box<[Vec<f32>; 3]>>,
53 clut: Option<Vec<f32>>,
54 matrix: Matrix3f,
55 bias: Vector3f,
56 direction: MultidimensionalDirection,
57 grid_size: [u8; 16],
58 input_inks: usize,
59 _phantom: PhantomData<T>,
60 bit_depth: usize,
61}
62
63#[inline(never)]
64pub(crate) fn interpolate_out_function(
65 layout: Layout,
66) -> fn(lut: &MultidimensionalLut, arr: &[f32], inputs: &[f32]) -> NVector<f32, 3> {
67 const OUT: usize = 3;
68 match layout {
69 Layout::Rgb => linear_3i_vec3f_direct::<OUT>,
70 Layout::Rgba => linear_4i_vec3f::<OUT>,
71 Layout::Gray => linear_1i_vec3f::<OUT>,
72 Layout::GrayAlpha => linear_2i_vec3f_direct::<OUT>,
73 Layout::Inks5 => linear_5i_vec3f::<OUT>,
74 Layout::Inks6 => linear_6i_vec3f::<OUT>,
75 Layout::Inks7 => linear_7i_vec3f::<OUT>,
76 Layout::Inks8 => linear_8i_vec3f::<OUT>,
77 Layout::Inks9 => linear_9i_vec3f::<OUT>,
78 Layout::Inks10 => linear_10i_vec3f::<OUT>,
79 Layout::Inks11 => linear_11i_vec3f::<OUT>,
80 Layout::Inks12 => linear_12i_vec3f::<OUT>,
81 Layout::Inks13 => linear_13i_vec3f::<OUT>,
82 Layout::Inks14 => linear_14i_vec3f::<OUT>,
83 Layout::Inks15 => linear_15i_vec3f::<OUT>,
84 }
85}
86
87impl<T: Copy + Default + AsPrimitive<f32> + PointeeSizeExpressible + Send + Sync>
88 MultidimensionalNx3<T>
89{
90 fn to_pcs_impl(&self, input: &[T], dst: &mut [f32]) -> Result<(), CmsError> {
91 let norm_value = if T::FINITE {
92 1.0 / ((1u32 << self.bit_depth) - 1) as f32
93 } else {
94 1.0
95 };
96 assert_eq!(
97 self.direction,
98 MultidimensionalDirection::DeviceToPcs,
99 "PCS to device cannot be used on `to pcs` stage"
100 );
101
102 if let (Some(a_curves), Some(clut)) = (self.a_curves.as_ref(), self.clut.as_ref()) {
106 let layout = Layout::from_inks(self.input_inks);
107
108 let mut inks = vec![0.; self.input_inks];
109
110 if clut.is_empty() {
111 return Err(CmsError::InvalidAtoBLut);
112 }
113
114 let fetcher = interpolate_out_function(layout);
115
116 let md_lut = MultidimensionalLut::new(self.grid_size, self.input_inks, 3);
117
118 for (src, dst) in input
119 .chunks_exact(layout.channels())
120 .zip(dst.chunks_exact_mut(3))
121 {
122 for ((ink, src_ink), curve) in inks.iter_mut().zip(src).zip(a_curves.iter()) {
123 *ink = lut_interp_linear_float(src_ink.as_() * norm_value, curve);
124 }
125
126 let interpolated = fetcher(&md_lut, clut, &inks);
127
128 dst[0] = interpolated.v[0];
129 dst[1] = interpolated.v[1];
130 dst[2] = interpolated.v[2];
131 }
132 } else {
133 return Err(CmsError::InvalidAtoBLut);
134 }
135
136 if let Some(m_curves) = self.m_curves.as_ref() {
139 execute_simple_curves3(dst, m_curves);
140 execute_matrix_stage3(self.matrix, self.bias, dst);
141 }
142
143 if let Some(b_curves) = &self.b_curves.as_ref() {
145 execute_simple_curves3(dst, b_curves);
146 }
147
148 Ok(())
149 }
150}
151
152impl<T: Copy + Default + AsPrimitive<f32> + PointeeSizeExpressible + Send + Sync>
153 KatanaInitialStage<f32, T> for MultidimensionalNx3<T>
154{
155 fn to_pcs(&self, input: &[T]) -> Result<Vec<f32>, CmsError> {
156 if input.len() % self.input_inks != 0 {
157 return Err(CmsError::LaneMultipleOfChannels);
158 }
159
160 let mut new_dst = vec![0f32; (input.len() / self.input_inks) * 3];
161
162 self.to_pcs_impl(input, &mut new_dst)?;
163 Ok(new_dst)
164 }
165}
166
167fn make_multidimensional_nx3<
168 T: Copy + Default + AsPrimitive<f32> + PointeeSizeExpressible + Send + Sync,
169>(
170 mab: &LutMultidimensionalType,
171 _: TransformOptions,
172 _: DataColorSpace,
173 direction: MultidimensionalDirection,
174 bit_depth: usize,
175) -> Result<MultidimensionalNx3<T>, CmsError> {
176 if mab.num_output_channels != 3 {
177 return Err(CmsError::UnsupportedProfileConnection);
178 }
179 if mab.b_curves.is_empty() || mab.b_curves.len() != 3 {
180 return Err(CmsError::InvalidAtoBLut);
181 }
182
183 let clut: Option<Vec<f32>> =
184 if mab.a_curves.len() == mab.num_input_channels as usize && mab.clut.is_some() {
185 let clut = mab.clut.as_ref().map(|x| x.to_clut_f32()).unwrap();
186 let mut lut_grid = 1usize;
187 for grid in mab.grid_points.iter().take(mab.num_input_channels as usize) {
188 lut_grid = lut_grid.safe_mul(*grid as usize)?;
189 }
190 let lut_grid = lut_grid.safe_mul(mab.num_output_channels as usize)?;
191 if clut.len() != lut_grid {
192 return Err(CmsError::MalformedCurveLutTable(MalformedSize {
193 size: clut.len(),
194 expected: lut_grid,
195 }));
196 }
197 Some(clut)
198 } else {
199 return Err(CmsError::InvalidAtoBLut);
200 };
201
202 let a_curves: Option<Vec<Vec<f32>>> =
203 if mab.a_curves.len() == mab.num_input_channels as usize && mab.clut.is_some() {
204 let mut arr = Vec::new();
205 for a_curve in mab.a_curves.iter() {
206 arr.push(a_curve.to_clut()?);
207 }
208 Some(arr)
209 } else {
210 None
211 };
212
213 let b_curves: Option<Box<[Vec<f32>; 3]>> = if mab.b_curves.len() == 3 {
214 let mut arr = Box::<[Vec<f32>; 3]>::default();
215 let all_curves_linear = mab.b_curves.iter().all(|curve| curve.is_linear());
216 if all_curves_linear {
217 None
218 } else {
219 for (c_curve, dst) in mab.b_curves.iter().zip(arr.iter_mut()) {
220 *dst = c_curve.to_clut()?;
221 }
222 Some(arr)
223 }
224 } else {
225 return Err(CmsError::InvalidAtoBLut);
226 };
227
228 let matrix = mab.matrix.to_f32();
229
230 let m_curves: Option<Box<[Vec<f32>; 3]>> = if mab.m_curves.len() == 3 {
231 let all_curves_linear = mab.m_curves.iter().all(|curve| curve.is_linear());
232 if !all_curves_linear
233 || !mab.matrix.test_equality(Matrix3d::IDENTITY)
234 || mab.bias.ne(&Vector3d::default())
235 {
236 let mut arr = Box::<[Vec<f32>; 3]>::default();
237 for (curve, dst) in mab.m_curves.iter().zip(arr.iter_mut()) {
238 *dst = curve.to_clut()?;
239 }
240 Some(arr)
241 } else {
242 None
243 }
244 } else {
245 None
246 };
247
248 let bias = mab.bias.cast();
249
250 let transform = MultidimensionalNx3::<T> {
251 a_curves,
252 b_curves,
253 m_curves,
254 matrix,
255 direction,
256 clut,
257 grid_size: mab.grid_points,
258 bias,
259 input_inks: mab.num_input_channels as usize,
260 _phantom: PhantomData,
261 bit_depth,
262 };
263
264 Ok(transform)
265}
266
267pub(crate) fn katana_multi_dimensional_nx3_to_pcs<
268 T: Copy + Default + AsPrimitive<f32> + PointeeSizeExpressible + Send + Sync,
269>(
270 src_layout: Layout,
271 mab: &LutMultidimensionalType,
272 options: TransformOptions,
273 pcs: DataColorSpace,
274 bit_depth: usize,
275) -> Result<Box<dyn KatanaInitialStage<f32, T> + Send + Sync>, CmsError> {
276 if pcs == DataColorSpace::Rgb {
277 if mab.num_input_channels != 3 {
278 return Err(CmsError::InvalidAtoBLut);
279 }
280 if src_layout != Layout::Rgba && src_layout != Layout::Rgb {
281 return Err(CmsError::InvalidInksCountForProfile);
282 }
283 } else if mab.num_input_channels != src_layout.channels() as u8 {
284 return Err(CmsError::InvalidInksCountForProfile);
285 }
286 let transform = make_multidimensional_nx3::<T>(
287 mab,
288 options,
289 pcs,
290 MultidimensionalDirection::DeviceToPcs,
291 bit_depth,
292 )?;
293 Ok(Box::new(transform))
294}