Debian Codegraph @

debian/missing-sources/moxcms/src/conversions/neon/t_lut3_to_3_q0_15.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::neon::interpolator_q0_15::*;
33 use crate::transform::PointeeSizeExpressible;
34 use crate::{CmsError, DataColorSpace, InterpolationMethod, Layout, TransformExecutor};
35 use num_traits::AsPrimitive;
36 use std::arch::aarch64::*;
37 use std::marker::PhantomData;
38
39 pub(crate) struct TransformLut3x3NeonQ0_15<
40 T,
41 U,
42 const SRC_LAYOUT: u8,
43 const DST_LAYOUT: u8,
44 const GRID_SIZE: usize,
45 const BIT_DEPTH: usize,
46 const BINS: usize,
47 const BARYCENTRIC_BINS: usize,
48 > {
49 pub(crate) lut: Vec<NeonAlignedI16x4>,
50 pub(crate) _phantom: PhantomData<T>,
51 pub(crate) _phantom1: PhantomData<U>,
52 pub(crate) interpolation_method: InterpolationMethod,
53 pub(crate) weights: Box<[BarycentricWeight<i16>; BINS]>,
54 pub(crate) color_space: DataColorSpace,
55 pub(crate) is_linear: bool,
56 }
57
58 impl<
59 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible,
60 U: AsPrimitive<usize>,
61 const SRC_LAYOUT: u8,
62 const DST_LAYOUT: u8,
63 const GRID_SIZE: usize,
64 const BIT_DEPTH: usize,
65 const BINS: usize,
66 const BARYCENTRIC_BINS: usize,
67 >
68 TransformLut3x3NeonQ0_15<
69 T,
70 U,
71 SRC_LAYOUT,
72 DST_LAYOUT,
73 GRID_SIZE,
74 BIT_DEPTH,
75 BINS,
76 BARYCENTRIC_BINS,
77 >
78 where
79 f32: AsPrimitive<T>,
80 u32: AsPrimitive<T>,
81 (): LutBarycentricReduction<T, U>,
82 {
83 #[allow(unused_unsafe)]
84 #[inline(never)]
85 #[target_feature(enable = "rdm")]
86 unsafe fn transform_chunk(
87 &self,
88 src: &[T],
89 dst: &mut [T],
90 interpolator: Box<dyn NeonMdInterpolationQ0_15 + Send + Sync>,
91 ) {
92 unsafe {
93 let src_cn = Layout::from(SRC_LAYOUT);
94 let src_channels = src_cn.channels();
95
96 let dst_cn = Layout::from(DST_LAYOUT);
97 let dst_channels = dst_cn.channels();
98
99 let f_value_scale = vdupq_n_f32(1. / ((1 << 14i32) - 1) as f32);
100 let max_value = ((1u32 << BIT_DEPTH) - 1).as_();
101 let v_max_scale = if T::FINITE {
102 vdup_n_s16(((1i32 << BIT_DEPTH) - 1) as i16)
103 } else {
104 vdup_n_s16(((1i32 << 14i32) - 1) as i16)
105 };
106
107 for (src, dst) in src
108 .chunks_exact(src_channels)
109 .zip(dst.chunks_exact_mut(dst_channels))
110 {
111 let x = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
112 src[src_cn.r_i()],
113 );
114 let y = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
115 src[src_cn.g_i()],
116 );
117 let z = <() as LutBarycentricReduction<T, U>>::reduce::<BIT_DEPTH, BARYCENTRIC_BINS>(
118 src[src_cn.b_i()],
119 );
120
121 let a = if src_channels == 4 {
122 src[src_cn.a_i()]
123 } else {
124 max_value
125 };
126
127 let v = interpolator.inter3_neon(
128 &self.lut,
129 x.as_(),
130 y.as_(),
131 z.as_(),
132 self.weights.as_slice(),
133 );
134 if T::FINITE {
135 let mut o = vmax_s16(v.v, vdup_n_s16(0));
136 o = vmin_s16(o, v_max_scale);
137 dst[dst_cn.r_i()] = (vget_lane_s16::<0>(o) as u32).as_();
138 dst[dst_cn.g_i()] = (vget_lane_s16::<1>(o) as u32).as_();
139 dst[dst_cn.b_i()] = (vget_lane_s16::<2>(o) as u32).as_();
140 } else {
141 let o = vcvtq_f32_s32(vmovl_s16(v.v));
142 let r = vmulq_f32(o, f_value_scale);
143 dst[dst_cn.r_i()] = vgetq_lane_f32::<0>(r).as_();
144 dst[dst_cn.g_i()] = vgetq_lane_f32::<1>(r).as_();
145 dst[dst_cn.b_i()] = vgetq_lane_f32::<2>(r).as_();
146 }
147 if dst_channels == 4 {
148 dst[dst_cn.a_i()] = a;
149 }
150 }
151 }
152 }
153 }
154
155 impl<
156 T: Copy + AsPrimitive<f32> + Default + PointeeSizeExpressible,
157 U: AsPrimitive<usize>,
158 const SRC_LAYOUT: u8,
159 const DST_LAYOUT: u8,
160 const GRID_SIZE: usize,
161 const BIT_DEPTH: usize,
162 const BINS: usize,
163 const BARYCENTRIC_BINS: usize,
164 > TransformExecutor<T>
165 for TransformLut3x3NeonQ0_15<
166 T,
167 U,
168 SRC_LAYOUT,
169 DST_LAYOUT,
170 GRID_SIZE,
171 BIT_DEPTH,
172 BINS,
173 BARYCENTRIC_BINS,
174 >
175 where
176 f32: AsPrimitive<T>,
177 u32: AsPrimitive<T>,
178 (): LutBarycentricReduction<T, U>,
179 {
180 fn transform(&self, src: &[T], dst: &mut [T]) -> Result<(), CmsError> {
181 let src_cn = Layout::from(SRC_LAYOUT);
182 let src_channels = src_cn.channels();
183
184 let dst_cn = Layout::from(DST_LAYOUT);
185 let dst_channels = dst_cn.channels();
186 if src.len() % src_channels != 0 {
187 return Err(CmsError::LaneMultipleOfChannels);
188 }
189 if dst.len() % dst_channels != 0 {
190 return Err(CmsError::LaneMultipleOfChannels);
191 }
192 let src_chunks = src.len() / src_channels;
193 let dst_chunks = dst.len() / dst_channels;
194 if src_chunks != dst_chunks {
195 return Err(CmsError::LaneSizeMismatch);
196 }
197
198 unsafe {
199 if self.color_space == DataColorSpace::Lab
200 || (self.is_linear && self.color_space == DataColorSpace::Rgb)
201 || self.color_space == DataColorSpace::Xyz
202 {
203 self.transform_chunk(src, dst, Box::new(TrilinearNeonQ0_15::<GRID_SIZE> {}));
204 } else {
205 match self.interpolation_method {
206 #[cfg(feature = "options")]
207 InterpolationMethod::Tetrahedral => {
208 self.transform_chunk(
209 src,
210 dst,
211 Box::new(TetrahedralNeonQ0_15::<GRID_SIZE> {}),
212 );
213 }
214 #[cfg(feature = "options")]
215 InterpolationMethod::Pyramid => {
216 self.transform_chunk(
217 src,
218 dst,
219 Box::new(PyramidalNeonQ0_15::<GRID_SIZE> {}),
220 );
221 }
222 #[cfg(feature = "options")]
223 InterpolationMethod::Prism => {
224 self.transform_chunk(
225 src,
226 dst,
227 Box::new(PrismaticNeonQ0_15::<GRID_SIZE> {}),
228 );
229 }
230 InterpolationMethod::Linear => {
231 self.transform_chunk(
232 src,
233 dst,
234 Box::new(TrilinearNeonQ0_15::<GRID_SIZE> {}),
235 );
236 }
237 }
238 }
239 }
240
241 Ok(())
242 }
243 }