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