Debian Codegraph @

debian/missing-sources/moxcms/src/conversions/neon/t_lut3_to_3.rs

raw
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 */
29 #![cfg(feature = "neon_luts")]
30 use crate::conversions::LutBarycentricReduction;
31 use crate::conversions::interpolator::BarycentricWeight;
32 use crate::conversions::lut_transforms::Lut3x3Factory;
33 use crate::conversions::neon::interpolator::*;
34 use crate::conversions::neon::interpolator_q0_15::NeonAlignedI16x4;
35 use crate::conversions::neon::t_lut3_to_3_q0_15::TransformLut3x3NeonQ0_15;
36 use crate::conversions::neon::{NeonAlignedF32, assert_barycentric_lut_size_precondition};
37 use crate::transform::PointeeSizeExpressible;
38 use crate::{
39 BarycentricWeightScale, CmsError, DataColorSpace, InterpolationMethod, Layout,
40 TransformExecutor, TransformOptions,
41 };
42 use num_traits::AsPrimitive;
43 use std::arch::aarch64::*;
44 use std::marker::PhantomData;
45 use std::sync::Arc;
46
47 struct TransformLut3x3Neon<
48 T,
49 U,
50 const SRC_LAYOUT: u8,
51 const DST_LAYOUT: u8,
52 const GRID_SIZE: usize,
53 const BIT_DEPTH: usize,
54 const BINS: usize,
55 const BARYCENTRIC_BINS: usize,
56 > {
57 lut: Vec<NeonAlignedF32>,
58 _phantom: PhantomData<T>,
59 _phantom1: PhantomData<U>,
60 interpolation_method: InterpolationMethod,
61 weights: Box<[BarycentricWeight<f32>; BINS]>,
62 color_space: DataColorSpace,
63 is_linear: bool,
64 }
65
66 impl<
67 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible,
68 U: AsPrimitive<usize>,
69 const SRC_LAYOUT: u8,
70 const DST_LAYOUT: u8,
71 const GRID_SIZE: usize,
72 const BIT_DEPTH: usize,
73 const BINS: usize,
74 const BARYCENTRIC_BINS: usize,
75 > TransformLut3x3Neon<T, U, SRC_LAYOUT, DST_LAYOUT, GRID_SIZE, BIT_DEPTH, BINS, BARYCENTRIC_BINS>
76 where
77 f32: AsPrimitive<T>,
78 u32: AsPrimitive<T>,
79 (): LutBarycentricReduction<T, U>,
80 {
81 #[inline(never)]
82 fn transform_chunk(
83 &self,
84 src: &[T],
85 dst: &mut [T],
86 interpolator: Box<dyn NeonMdInterpolation + Send + Sync>,
87 ) {
88 unsafe {
89 let src_cn = Layout::from(SRC_LAYOUT);
90 let src_channels = src_cn.channels();
91
92 let dst_cn = Layout::from(DST_LAYOUT);
93 let dst_channels = dst_cn.channels();
94
95 let value_scale = vdupq_n_f32(((1 << BIT_DEPTH) - 1) as f32);
96 let max_value = ((1u32 << BIT_DEPTH) - 1).as_();
97
98 for (src, dst) in src
99 .chunks_exact(src_channels)
100 .zip(dst.chunks_exact_mut(dst_channels))
101 {
102 let x = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
103 src[src_cn.r_i()],
104 );
105 let y = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
106 src[src_cn.g_i()],
107 );
108 let z = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
109 src[src_cn.b_i()],
110 );
111
112 let a = if src_channels == 4 {
113 src[src_cn.a_i()]
114 } else {
115 max_value
116 };
117
118 let v = interpolator.inter3_neon(
119 &self.lut,
120 x.as_(),
121 y.as_(),
122 z.as_(),
123 self.weights.as_slice(),
124 );
125 if T::FINITE {
126 let mut r = vfmaq_f32(vdupq_n_f32(0.5f32), v.v, value_scale);
127 r = vminq_f32(r, value_scale);
128 let jvx = vcvtaq_u32_f32(r);
129
130 dst[dst_cn.r_i()] = vgetq_lane_u32::<0>(jvx).as_();
131 dst[dst_cn.g_i()] = vgetq_lane_u32::<1>(jvx).as_();
132 dst[dst_cn.b_i()] = vgetq_lane_u32::<2>(jvx).as_();
133 } else {
134 dst[dst_cn.r_i()] = vgetq_lane_f32::<0>(v.v).as_();
135 dst[dst_cn.g_i()] = vgetq_lane_f32::<1>(v.v).as_();
136 dst[dst_cn.b_i()] = vgetq_lane_f32::<2>(v.v).as_();
137 }
138 if dst_channels == 4 {
139 dst[dst_cn.a_i()] = a;
140 }
141 }
142 }
143 }
144 }
145
146 impl<
147 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible,
148 U: AsPrimitive<usize>,
149 const SRC_LAYOUT: u8,
150 const DST_LAYOUT: u8,
151 const GRID_SIZE: usize,
152 const BIT_DEPTH: usize,
153 const BINS: usize,
154 const BARYCENTRIC_BINS: usize,
155 > TransformExecutor<T>
156 for TransformLut3x3Neon<
157 T,
158 U,
159 SRC_LAYOUT,
160 DST_LAYOUT,
161 GRID_SIZE,
162 BIT_DEPTH,
163 BINS,
164 BARYCENTRIC_BINS,
165 >
166 where
167 f32: AsPrimitive<T>,
168 u32: AsPrimitive<T>,
169 (): LutBarycentricReduction<T, U>,
170 {
171 fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
172 let src_cn = Layout::from(SRC_LAYOUT);
173 let src_channels = src_cn.channels();
174
175 let dst_cn = Layout::from(DST_LAYOUT);
176 let dst_channels = dst_cn.channels();
177 if src.len() % src_channels != 0 {
178 return Err(CmsError::LaneMultipleOfChannels);
179 }
180 if dst.len() % dst_channels != 0 {
181 return Err(CmsError::LaneMultipleOfChannels);
182 }
183 let src_chunks = src.len() / src_channels;
184 let dst_chunks = dst.len() / dst_channels;
185 if src_chunks != dst_chunks {
186 return Err(CmsError::LaneSizeMismatch);
187 }
188
189 if self.color_space == DataColorSpace::Lab
190 || (self.is_linear && self.color_space == DataColorSpace::Rgb)
191 || self.color_space == DataColorSpace::Xyz
192 {
193 self.transform_chunk(src, dst, Box::new(TrilinearNeon::<GRID_SIZE> {}));
194 } else {
195 match self.interpolation_method {
196 #[cfg(feature = "options")]
197 InterpolationMethod::Tetrahedral => {
198 self.transform_chunk(src, dst, Box::new(TetrahedralNeon::<GRID_SIZE> {}));
199 }
200 #[cfg(feature = "options")]
201 InterpolationMethod::Pyramid => {
202 self.transform_chunk(src, dst, Box::new(PyramidalNeon::<GRID_SIZE> {}));
203 }
204 #[cfg(feature = "options")]
205 InterpolationMethod::Prism => {
206 self.transform_chunk(src, dst, Box::new(PrismaticNeon::<GRID_SIZE> {}));
207 }
208 InterpolationMethod::Linear => {
209 self.transform_chunk(src, dst, Box::new(TrilinearNeon::<GRID_SIZE> {}));
210 }
211 }
212 }
213
214 Ok(())
215 }
216 }
217
218 pub(crate) struct NeonLut3x3Factory {}
219
220 impl Lut3x3Factory for NeonLut3x3Factory {
221 fn make_transform_3x3<
222 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible + 'static + Send + Sync,
223 const SRC_LAYOUT: u8,
224 const DST_LAYOUT: u8,
225 const GRID_SIZE: usize,
226 const BIT_DEPTH: usize,
227 >(
228 lut: Vec<f32>,
229 options: TransformOptions,
230 color_space: DataColorSpace,
231 is_linear: bool,
232 ) -> Arc<dyn TransformExecutor<T> + Send + Sync>
233 where
234 f32: AsPrimitive<T>,
235 u32: AsPrimitive<T>,
236 (): LutBarycentricReduction<T, u8>,
237 (): LutBarycentricReduction<T, u16>,
238 {
239 if options.prefer_fixed_point
240 && BIT_DEPTH < 16
241 && std::arch::is_aarch64_feature_detected!("rdm")
242 {
243 let q: f32 = if T::FINITE {
244 ((1i32 << BIT_DEPTH as i32) - 1) as f32
245 } else {
246 ((1i32 << 14i32) - 1) as f32
247 };
248 let lut = lut
249 .chunks_exact(3)
250 .map(|x| {
251 NeonAlignedI16x4([
252 (x[0] * q).round() as i16,
253 (x[1] * q).round() as i16,
254 (x[2] * q).round() as i16,
255 0,
256 ])
257 })
258 .collect::<Vec<_>>();
259
260 return match options.barycentric_weight_scale {
261 BarycentricWeightScale::Low => {
262 let bins = BarycentricWeight::<i16>::create_ranged_256::<GRID_SIZE>();
263 assert_barycentric_lut_size_precondition::<i16, GRID_SIZE>(bins.as_slice());
264 Arc::new(TransformLut3x3NeonQ0_15::<
265 T,
266 u8,
267 SRC_LAYOUT,
268 DST_LAYOUT,
269 GRID_SIZE,
270 BIT_DEPTH,
271 256,
272 256,
273 > {
274 lut,
275 _phantom: PhantomData,
276 _phantom1: PhantomData,
277 interpolation_method: options.interpolation_method,
278 weights: bins,
279 color_space,
280 is_linear,
281 })
282 }
283 #[cfg(feature = "options")]
284 BarycentricWeightScale::High => {
285 let bins = BarycentricWeight::<i16>::create_binned::<GRID_SIZE, 65536>();
286 assert_barycentric_lut_size_precondition::<i16, GRID_SIZE>(bins.as_slice());
287 Arc::new(TransformLut3x3NeonQ0_15::<
288 T,
289 u16,
290 SRC_LAYOUT,
291 DST_LAYOUT,
292 GRID_SIZE,
293 BIT_DEPTH,
294 65536,
295 65536,
296 > {
297 lut,
298 _phantom: PhantomData,
299 _phantom1: PhantomData,
300 interpolation_method: options.interpolation_method,
301 weights: bins,
302 color_space,
303 is_linear,
304 })
305 }
306 };
307 }
308 let lut = lut
309 .chunks_exact(3)
310 .map(|x| NeonAlignedF32([x[0], x[1], x[2], 0f32]))
311 .collect::<Vec<_>>();
312 match options.barycentric_weight_scale {
313 BarycentricWeightScale::Low => {
314 let bins = BarycentricWeight::<f32>::create_ranged_256::<GRID_SIZE>();
315 assert_barycentric_lut_size_precondition::<f32, GRID_SIZE>(bins.as_slice());
316 Arc::new(TransformLut3x3Neon::<
317 T,
318 u8,
319 SRC_LAYOUT,
320 DST_LAYOUT,
321 GRID_SIZE,
322 BIT_DEPTH,
323 256,
324 256,
325 > {
326 lut,
327 _phantom: PhantomData,
328 _phantom1: PhantomData,
329 interpolation_method: options.interpolation_method,
330 weights: bins,
331 color_space,
332 is_linear,
333 })
334 }
335 #[cfg(feature = "options")]
336 BarycentricWeightScale::High => {
337 let bins = BarycentricWeight::<f32>::create_binned::<GRID_SIZE, 65536>();
338 assert_barycentric_lut_size_precondition::<f32, GRID_SIZE>(bins.as_slice());
339 Arc::new(TransformLut3x3Neon::<
340 T,
341 u16,
342 SRC_LAYOUT,
343 DST_LAYOUT,
344 GRID_SIZE,
345 BIT_DEPTH,
346 65536,
347 65536,
348 > {
349 lut,
350 _phantom: PhantomData,
351 _phantom1: PhantomData,
352 interpolation_method: options.interpolation_method,
353 weights: bins,
354 color_space,
355 is_linear,
356 })
357 }
358 }
359 }
360 }