| 1 |
/* |
| 2 |
* // Copyright (c) Radzivon Bartoshyk 6/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::conversions::katana::md3x3::MultidimensionalDirection; |
| 31 |
use crate::conversions::katana::md4x3::{execute_matrix_stage3, execute_simple_curves3}; |
| 32 |
use 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 |
}; |
| 38 |
use crate::safe_math::SafeMul; |
| 39 |
use crate::trc::lut_interp_linear_float; |
| 40 |
use crate::{ |
| 41 |
CmsError, DataColorSpace, Layout, LutMultidimensionalType, MalformedSize, Matrix3d, Matrix3f, |
| 42 |
PointeeSizeExpressible, TransformOptions, Vector3d, Vector3f, |
| 43 |
}; |
| 44 |
use num_traits::AsPrimitive; |
| 45 |
use std::marker::PhantomData; |
| 46 |
|
| 47 |
struct 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)] |
| 64 |
pub(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 |
|
| 87 |
impl<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 |
// A -> B |
| 103 |
// OR B - A A - curves stage |
| 104 |
|
| 105 |
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 |
// Matrix stage |
| 137 |
|
| 138 |
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 |
// B-curves is mandatory |
| 144 |
if let Some(b_curves) = &self.b_curves.as_ref() { |
| 145 |
execute_simple_curves3(dst, b_curves); |
| 146 |
} |
| 147 |
|
| 148 |
Ok(()) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
impl<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 |
|
| 167 |
fn 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 |
|
| 267 |
pub(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 |
} |