| 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::katana::KatanaInitialStage; |
| 30 |
use crate::err::try_vec; |
| 31 |
use crate::profile::LutDataType; |
| 32 |
use crate::safe_math::{SafeMul, SafePowi}; |
| 33 |
use crate::trc::lut_interp_linear_float; |
| 34 |
use crate::{ |
| 35 |
CmsError, DataColorSpace, Hypercube, InterpolationMethod, MalformedSize, |
| 36 |
PointeeSizeExpressible, Stage, TransformOptions, Vector3f, |
| 37 |
}; |
| 38 |
use num_traits::AsPrimitive; |
| 39 |
use std::marker::PhantomData; |
| 40 |
|
| 41 |
#[allow(unused)] |
| 42 |
#[derive(Default)] |
| 43 |
struct Lut4x3 { |
| 44 |
linearization: [Vec<f32>; 4], |
| 45 |
clut: Vec<f32>, |
| 46 |
grid_size: u8, |
| 47 |
output: [Vec<f32>; 3], |
| 48 |
interpolation_method: InterpolationMethod, |
| 49 |
pcs: DataColorSpace, |
| 50 |
} |
| 51 |
|
| 52 |
#[allow(unused)] |
| 53 |
#[derive(Default)] |
| 54 |
struct KatanaLut4x3<T: Copy + PointeeSizeExpressible + AsPrimitive<f32>> { |
| 55 |
linearization: [Vec<f32>; 4], |
| 56 |
clut: Vec<f32>, |
| 57 |
grid_size: u8, |
| 58 |
output: [Vec<f32>; 3], |
| 59 |
interpolation_method: InterpolationMethod, |
| 60 |
pcs: DataColorSpace, |
| 61 |
_phantom: PhantomData<T>, |
| 62 |
bit_depth: usize, |
| 63 |
} |
| 64 |
|
| 65 |
#[allow(unused)] |
| 66 |
impl Lut4x3 { |
| 67 |
fn transform_impl<Fetch: Fn(f32, f32, f32, f32) -> Vector3f>( |
| 68 |
&self, |
| 69 |
src: &[f32], |
| 70 |
dst: &mut [f32], |
| 71 |
fetch: Fetch, |
| 72 |
) -> Result<(), CmsError> { |
| 73 |
let linearization_0 = &self.linearization[0]; |
| 74 |
let linearization_1 = &self.linearization[1]; |
| 75 |
let linearization_2 = &self.linearization[2]; |
| 76 |
let linearization_3 = &self.linearization[3]; |
| 77 |
for (dest, src) in dst.chunks_exact_mut(3).zip(src.chunks_exact(4)) { |
| 78 |
debug_assert!(self.grid_size as i32 >= 1); |
| 79 |
let linear_x = lut_interp_linear_float(src[0], linearization_0); |
| 80 |
let linear_y = lut_interp_linear_float(src[1], linearization_1); |
| 81 |
let linear_z = lut_interp_linear_float(src[2], linearization_2); |
| 82 |
let linear_w = lut_interp_linear_float(src[3], linearization_3); |
| 83 |
|
| 84 |
let clut = fetch(linear_x, linear_y, linear_z, linear_w); |
| 85 |
|
| 86 |
let pcs_x = lut_interp_linear_float(clut.v[0], &self.output[0]); |
| 87 |
let pcs_y = lut_interp_linear_float(clut.v[1], &self.output[1]); |
| 88 |
let pcs_z = lut_interp_linear_float(clut.v[2], &self.output[2]); |
| 89 |
dest[0] = pcs_x; |
| 90 |
dest[1] = pcs_y; |
| 91 |
dest[2] = pcs_z; |
| 92 |
} |
| 93 |
Ok(()) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
macro_rules! define_lut4_dispatch { |
| 98 |
($dispatcher: ident) => { |
| 99 |
impl Stage for $dispatcher { |
| 100 |
fn transform(&self, src: &[f32], dst: &mut [f32]) -> Result<(), CmsError> { |
| 101 |
let l_tbl = Hypercube::new_checked(&self.clut, self.grid_size as usize, 3)?; |
| 102 |
|
| 103 |
// If Source PCS is LAB trilinear should be used |
| 104 |
if self.pcs == DataColorSpace::Lab || self.pcs == DataColorSpace::Xyz { |
| 105 |
return self |
| 106 |
.transform_impl(src, dst, |x, y, z, w| l_tbl.quadlinear_vec3(x, y, z, w)); |
| 107 |
} |
| 108 |
|
| 109 |
match self.interpolation_method { |
| 110 |
#[cfg(feature = "options")] |
| 111 |
InterpolationMethod::Tetrahedral => { |
| 112 |
self.transform_impl(src, dst, |x, y, z, w| l_tbl.tetra_vec3(x, y, z, w))?; |
| 113 |
} |
| 114 |
#[cfg(feature = "options")] |
| 115 |
InterpolationMethod::Pyramid => { |
| 116 |
self.transform_impl(src, dst, |x, y, z, w| l_tbl.pyramid_vec3(x, y, z, w))?; |
| 117 |
} |
| 118 |
#[cfg(feature = "options")] |
| 119 |
InterpolationMethod::Prism => { |
| 120 |
self.transform_impl(src, dst, |x, y, z, w| l_tbl.prism_vec3(x, y, z, w))? |
| 121 |
} |
| 122 |
InterpolationMethod::Linear => { |
| 123 |
self.transform_impl(src, dst, |x, y, z, w| { |
| 124 |
l_tbl.quadlinear_vec3(x, y, z, w) |
| 125 |
})? |
| 126 |
} |
| 127 |
} |
| 128 |
Ok(()) |
| 129 |
} |
| 130 |
} |
| 131 |
}; |
| 132 |
} |
| 133 |
|
| 134 |
impl<T: Copy + PointeeSizeExpressible + AsPrimitive<f32>> KatanaLut4x3<T> { |
| 135 |
fn to_pcs_impl<Fetch: Fn(f32, f32, f32, f32) -> Vector3f>( |
| 136 |
&self, |
| 137 |
input: &[T], |
| 138 |
fetch: Fetch, |
| 139 |
) -> Result<Vec<f32>, CmsError> { |
| 140 |
if input.len() % 4 != 0 { |
| 141 |
return Err(CmsError::LaneMultipleOfChannels); |
| 142 |
} |
| 143 |
let norm_value = if T::FINITE { |
| 144 |
1.0 / ((1u32 << self.bit_depth) - 1) as f32 |
| 145 |
} else { |
| 146 |
1.0 |
| 147 |
}; |
| 148 |
let mut dst = try_vec![0.; (input.len() / 4) * 3]; |
| 149 |
let linearization_0 = &self.linearization[0]; |
| 150 |
let linearization_1 = &self.linearization[1]; |
| 151 |
let linearization_2 = &self.linearization[2]; |
| 152 |
let linearization_3 = &self.linearization[3]; |
| 153 |
for (dest, src) in dst.chunks_exact_mut(3).zip(input.chunks_exact(4)) { |
| 154 |
let linear_x = lut_interp_linear_float(src[0].as_() * norm_value, linearization_0); |
| 155 |
let linear_y = lut_interp_linear_float(src[1].as_() * norm_value, linearization_1); |
| 156 |
let linear_z = lut_interp_linear_float(src[2].as_() * norm_value, linearization_2); |
| 157 |
let linear_w = lut_interp_linear_float(src[3].as_() * norm_value, linearization_3); |
| 158 |
|
| 159 |
let clut = fetch(linear_x, linear_y, linear_z, linear_w); |
| 160 |
|
| 161 |
let pcs_x = lut_interp_linear_float(clut.v[0], &self.output[0]); |
| 162 |
let pcs_y = lut_interp_linear_float(clut.v[1], &self.output[1]); |
| 163 |
let pcs_z = lut_interp_linear_float(clut.v[2], &self.output[2]); |
| 164 |
dest[0] = pcs_x; |
| 165 |
dest[1] = pcs_y; |
| 166 |
dest[2] = pcs_z; |
| 167 |
} |
| 168 |
Ok(dst) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
impl<T: Copy + PointeeSizeExpressible + AsPrimitive<f32>> KatanaInitialStage<f32, T> |
| 173 |
for KatanaLut4x3<T> |
| 174 |
{ |
| 175 |
fn to_pcs(&self, input: &[T]) -> Result<Vec<f32>, CmsError> { |
| 176 |
if input.len() % 4 != 0 { |
| 177 |
return Err(CmsError::LaneMultipleOfChannels); |
| 178 |
} |
| 179 |
let l_tbl = Hypercube::new_checked(&self.clut, self.grid_size as usize, 3)?; |
| 180 |
|
| 181 |
// If Source PCS is LAB trilinear should be used |
| 182 |
if self.pcs == DataColorSpace::Lab || self.pcs == DataColorSpace::Xyz { |
| 183 |
return self.to_pcs_impl(input, |x, y, z, w| l_tbl.quadlinear_vec3(x, y, z, w)); |
| 184 |
} |
| 185 |
|
| 186 |
match self.interpolation_method { |
| 187 |
#[cfg(feature = "options")] |
| 188 |
InterpolationMethod::Tetrahedral => { |
| 189 |
self.to_pcs_impl(input, |x, y, z, w| l_tbl.tetra_vec3(x, y, z, w)) |
| 190 |
} |
| 191 |
#[cfg(feature = "options")] |
| 192 |
InterpolationMethod::Pyramid => { |
| 193 |
self.to_pcs_impl(input, |x, y, z, w| l_tbl.pyramid_vec3(x, y, z, w)) |
| 194 |
} |
| 195 |
#[cfg(feature = "options")] |
| 196 |
InterpolationMethod::Prism => { |
| 197 |
self.to_pcs_impl(input, |x, y, z, w| l_tbl.prism_vec3(x, y, z, w)) |
| 198 |
} |
| 199 |
InterpolationMethod::Linear => { |
| 200 |
self.to_pcs_impl(input, |x, y, z, w| l_tbl.quadlinear_vec3(x, y, z, w)) |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
define_lut4_dispatch!(Lut4x3); |
| 207 |
|
| 208 |
fn make_lut_4x3( |
| 209 |
lut: &LutDataType, |
| 210 |
options: TransformOptions, |
| 211 |
pcs: DataColorSpace, |
| 212 |
) -> Result<Lut4x3, CmsError> { |
| 213 |
// There is 4 possible cases: |
| 214 |
// - All curves are non-linear |
| 215 |
// - Linearization curves are non-linear, but gamma is linear |
| 216 |
// - Gamma curves are non-linear, but linearization is linear |
| 217 |
// - All curves linear |
| 218 |
let clut_length: usize = (lut.num_clut_grid_points as usize) |
| 219 |
.safe_powi(lut.num_input_channels as u32)? |
| 220 |
.safe_mul(lut.num_output_channels as usize)?; |
| 221 |
|
| 222 |
let clut_table = lut.clut_table.to_clut_f32(); |
| 223 |
if clut_table.len() != clut_length { |
| 224 |
return Err(CmsError::MalformedClut(MalformedSize { |
| 225 |
size: clut_table.len(), |
| 226 |
expected: clut_length, |
| 227 |
})); |
| 228 |
} |
| 229 |
|
| 230 |
let linearization_table = lut.input_table.to_clut_f32(); |
| 231 |
|
| 232 |
if linearization_table.len() < lut.num_input_table_entries as usize * 4 { |
| 233 |
return Err(CmsError::MalformedCurveLutTable(MalformedSize { |
| 234 |
size: linearization_table.len(), |
| 235 |
expected: lut.num_input_table_entries as usize * 4, |
| 236 |
})); |
| 237 |
} |
| 238 |
|
| 239 |
let lin_curve0 = linearization_table[0..lut.num_input_table_entries as usize].to_vec(); |
| 240 |
let lin_curve1 = linearization_table |
| 241 |
[lut.num_input_table_entries as usize..lut.num_input_table_entries as usize * 2] |
| 242 |
.to_vec(); |
| 243 |
let lin_curve2 = linearization_table |
| 244 |
[lut.num_input_table_entries as usize * 2..lut.num_input_table_entries as usize * 3] |
| 245 |
.to_vec(); |
| 246 |
let lin_curve3 = linearization_table |
| 247 |
[lut.num_input_table_entries as usize * 3..lut.num_input_table_entries as usize * 4] |
| 248 |
.to_vec(); |
| 249 |
|
| 250 |
let gamma_table = lut.output_table.to_clut_f32(); |
| 251 |
|
| 252 |
if gamma_table.len() < lut.num_output_table_entries as usize * 3 { |
| 253 |
return Err(CmsError::MalformedCurveLutTable(MalformedSize { |
| 254 |
size: gamma_table.len(), |
| 255 |
expected: lut.num_output_table_entries as usize * 3, |
| 256 |
})); |
| 257 |
} |
| 258 |
|
| 259 |
let gamma_curve0 = gamma_table[..lut.num_output_table_entries as usize].to_vec(); |
| 260 |
let gamma_curve1 = gamma_table |
| 261 |
[lut.num_output_table_entries as usize..lut.num_output_table_entries as usize * 2] |
| 262 |
.to_vec(); |
| 263 |
let gamma_curve2 = gamma_table |
| 264 |
[lut.num_output_table_entries as usize * 2..lut.num_output_table_entries as usize * 3] |
| 265 |
.to_vec(); |
| 266 |
|
| 267 |
let transform = Lut4x3 { |
| 268 |
linearization: [lin_curve0, lin_curve1, lin_curve2, lin_curve3], |
| 269 |
interpolation_method: options.interpolation_method, |
| 270 |
pcs, |
| 271 |
clut: clut_table, |
| 272 |
grid_size: lut.num_clut_grid_points, |
| 273 |
output: [gamma_curve0, gamma_curve1, gamma_curve2], |
| 274 |
}; |
| 275 |
Ok(transform) |
| 276 |
} |
| 277 |
|
| 278 |
fn stage_lut_4x3( |
| 279 |
lut: &LutDataType, |
| 280 |
options: TransformOptions, |
| 281 |
pcs: DataColorSpace, |
| 282 |
) -> Result<Box<dyn Stage>, CmsError> { |
| 283 |
let lut = make_lut_4x3(lut, options, pcs)?; |
| 284 |
let transform = Lut4x3 { |
| 285 |
linearization: lut.linearization, |
| 286 |
interpolation_method: lut.interpolation_method, |
| 287 |
pcs: lut.pcs, |
| 288 |
clut: lut.clut, |
| 289 |
grid_size: lut.grid_size, |
| 290 |
output: lut.output, |
| 291 |
}; |
| 292 |
Ok(Box::new(transform)) |
| 293 |
} |
| 294 |
|
| 295 |
pub(crate) fn katana_input_stage_lut_4x3< |
| 296 |
T: Copy + PointeeSizeExpressible + AsPrimitive<f32> + Send + Sync, |
| 297 |
>( |
| 298 |
lut: &LutDataType, |
| 299 |
options: TransformOptions, |
| 300 |
pcs: DataColorSpace, |
| 301 |
bit_depth: usize, |
| 302 |
) -> Result<Box<dyn KatanaInitialStage<f32, T> + Send + Sync>, CmsError> { |
| 303 |
// There is 4 possible cases: |
| 304 |
// - All curves are non-linear |
| 305 |
// - Linearization curves are non-linear, but gamma is linear |
| 306 |
// - Gamma curves are non-linear, but linearization is linear |
| 307 |
// - All curves linear |
| 308 |
let lut = make_lut_4x3(lut, options, pcs)?; |
| 309 |
|
| 310 |
let transform = KatanaLut4x3::<T> { |
| 311 |
linearization: lut.linearization, |
| 312 |
interpolation_method: lut.interpolation_method, |
| 313 |
pcs: lut.pcs, |
| 314 |
clut: lut.clut, |
| 315 |
grid_size: lut.grid_size, |
| 316 |
output: lut.output, |
| 317 |
_phantom: PhantomData, |
| 318 |
bit_depth, |
| 319 |
}; |
| 320 |
Ok(Box::new(transform)) |
| 321 |
} |
| 322 |
|
| 323 |
pub(crate) fn create_lut4_norm_samples<const SAMPLES: usize>() -> Vec<f32> { |
| 324 |
let lut_size: u32 = (4 * SAMPLES * SAMPLES * SAMPLES * SAMPLES) as u32; |
| 325 |
|
| 326 |
let mut src = Vec::with_capacity(lut_size as usize); |
| 327 |
|
| 328 |
let recpeq = 1f32 / (SAMPLES - 1) as f32; |
| 329 |
for k in 0..SAMPLES { |
| 330 |
for c in 0..SAMPLES { |
| 331 |
for m in 0..SAMPLES { |
| 332 |
for y in 0..SAMPLES { |
| 333 |
src.push(c as f32 * recpeq); |
| 334 |
src.push(m as f32 * recpeq); |
| 335 |
src.push(y as f32 * recpeq); |
| 336 |
src.push(k as f32 * recpeq); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
src |
| 342 |
} |
| 343 |
|
| 344 |
pub(crate) fn create_lut4<const SAMPLES: usize>( |
| 345 |
lut: &LutDataType, |
| 346 |
options: TransformOptions, |
| 347 |
pcs: DataColorSpace, |
| 348 |
) -> Result<Vec<f32>, CmsError> { |
| 349 |
if lut.num_input_channels != 4 { |
| 350 |
return Err(CmsError::UnsupportedProfileConnection); |
| 351 |
} |
| 352 |
let lut_size: u32 = (4 * SAMPLES * SAMPLES * SAMPLES * SAMPLES) as u32; |
| 353 |
|
| 354 |
let src = create_lut4_norm_samples::<SAMPLES>(); |
| 355 |
let mut dest = try_vec![0.; (lut_size as usize) / 4 * 3]; |
| 356 |
|
| 357 |
let lut_stage = stage_lut_4x3(lut, options, pcs)?; |
| 358 |
lut_stage.transform(&src, &mut dest)?; |
| 359 |
Ok(dest) |
| 360 |
} |