1#![cfg(feature = "lut")]
30use crate::err::try_vec;
31use crate::profile::LutDataType;
32use crate::safe_math::{SafeMul, SafePowi};
33use crate::trc::lut_interp_linear_float;
34use crate::{
35 CmsError, Cube, DataColorSpace, InterpolationMethod, MalformedSize, Stage, TransformOptions,
36 Vector4f,
37};
38use num_traits::AsPrimitive;
39
40#[derive(Default)]
41struct Lut3x4 {
42 input: [Vec<f32>; 3],
43 clut: Vec<f32>,
44 grid_size: u8,
45 gamma: [Vec<f32>; 4],
46 interpolation_method: InterpolationMethod,
47 pcs: DataColorSpace,
48}
49
50fn make_lut_3x4(
51 lut: &LutDataType,
52 options: TransformOptions,
53 pcs: DataColorSpace,
54) -> Result<Lut3x4, CmsError> {
55 let clut_length: usize = (lut.num_clut_grid_points as usize)
56 .safe_powi(lut.num_input_channels as u32)?
57 .safe_mul(lut.num_output_channels as usize)?;
58
59 let clut_table = lut.clut_table.to_clut_f32();
60 if clut_table.len() != clut_length {
61 return Err(CmsError::MalformedClut(MalformedSize {
62 size: clut_table.len(),
63 expected: clut_length,
64 }));
65 }
66
67 let linearization_table = lut.input_table.to_clut_f32();
68
69 if linearization_table.len() < lut.num_input_table_entries as usize * 3 {
70 return Err(CmsError::MalformedCurveLutTable(MalformedSize {
71 size: linearization_table.len(),
72 expected: lut.num_input_table_entries as usize * 3,
73 }));
74 }
75
76 let linear_curve0 = linearization_table[..lut.num_input_table_entries as usize].to_vec();
77 let linear_curve1 = linearization_table
78 [lut.num_input_table_entries as usize..lut.num_input_table_entries as usize * 2]
79 .to_vec();
80 let linear_curve2 = linearization_table
81 [lut.num_input_table_entries as usize * 2..lut.num_input_table_entries as usize * 3]
82 .to_vec();
83
84 let gamma_table = lut.output_table.to_clut_f32();
85
86 if gamma_table.len() < lut.num_output_table_entries as usize * 4 {
87 return Err(CmsError::MalformedCurveLutTable(MalformedSize {
88 size: gamma_table.len(),
89 expected: lut.num_output_table_entries as usize * 4,
90 }));
91 }
92
93 let gamma_curve0 = gamma_table[..lut.num_output_table_entries as usize].to_vec();
94 let gamma_curve1 = gamma_table
95 [lut.num_output_table_entries as usize..lut.num_output_table_entries as usize * 2]
96 .to_vec();
97 let gamma_curve2 = gamma_table
98 [lut.num_output_table_entries as usize * 2..lut.num_output_table_entries as usize * 3]
99 .to_vec();
100 let gamma_curve3 = gamma_table
101 [lut.num_output_table_entries as usize * 3..lut.num_output_table_entries as usize * 4]
102 .to_vec();
103
104 let transform = Lut3x4 {
105 input: [linear_curve0, linear_curve1, linear_curve2],
106 interpolation_method: options.interpolation_method,
107 clut: clut_table,
108 grid_size: lut.num_clut_grid_points,
109 pcs,
110 gamma: [gamma_curve0, gamma_curve1, gamma_curve2, gamma_curve3],
111 };
112 Ok(transform)
113}
114
115fn stage_lut_3x4(
116 lut: &LutDataType,
117 options: TransformOptions,
118 pcs: DataColorSpace,
119) -> Result<Box<dyn Stage>, CmsError> {
120 let lut = make_lut_3x4(lut, options, pcs)?;
121
122 let transform = Lut3x4 {
123 input: lut.input,
124 interpolation_method: lut.interpolation_method,
125 clut: lut.clut,
126 grid_size: lut.grid_size,
127 pcs: lut.pcs,
128 gamma: lut.gamma,
129 };
130 Ok(Box::new(transform))
131}
132
133impl Lut3x4 {
134 fn transform_impl<Fetch: Fn(f32, f32, f32) -> Vector4f>(
135 &self,
136 src: &[f32],
137 dst: &mut [f32],
138 fetch: Fetch,
139 ) -> Result<(), CmsError> {
140 let linearization_0 = &self.input[0];
141 let linearization_1 = &self.input[1];
142 let linearization_2 = &self.input[2];
143 for (dest, src) in dst.chunks_exact_mut(4).zip(src.chunks_exact(3)) {
144 debug_assert!(self.grid_size as i32 >= 1);
145 let linear_x = lut_interp_linear_float(src[0], linearization_0);
146 let linear_y = lut_interp_linear_float(src[1], linearization_1);
147 let linear_z = lut_interp_linear_float(src[2], linearization_2);
148
149 let clut = fetch(linear_x, linear_y, linear_z);
150
151 let pcs_x = lut_interp_linear_float(clut.v[0], &self.gamma[0]);
152 let pcs_y = lut_interp_linear_float(clut.v[1], &self.gamma[1]);
153 let pcs_z = lut_interp_linear_float(clut.v[2], &self.gamma[2]);
154 let pcs_w = lut_interp_linear_float(clut.v[3], &self.gamma[3]);
155 dest[0] = pcs_x;
156 dest[1] = pcs_y;
157 dest[2] = pcs_z;
158 dest[3] = pcs_w;
159 }
160 Ok(())
161 }
162}
163
164impl Stage for Lut3x4 {
165 fn transform(&self, src: &[f32], dst: &mut [f32]) -> Result<(), CmsError> {
166 let l_tbl = Cube::new(&self.clut, self.grid_size as usize, 4)?;
167
168 if self.pcs == DataColorSpace::Lab || self.pcs == DataColorSpace::Xyz {
170 return self.transform_impl(src, dst, |x, y, z| l_tbl.trilinear_vec4(x, y, z));
171 }
172
173 match self.interpolation_method {
174 #[cfg(feature = "options")]
175 InterpolationMethod::Tetrahedral => {
176 self.transform_impl(src, dst, |x, y, z| l_tbl.tetra_vec4(x, y, z))?;
177 }
178 #[cfg(feature = "options")]
179 InterpolationMethod::Pyramid => {
180 self.transform_impl(src, dst, |x, y, z| l_tbl.pyramid_vec4(x, y, z))?;
181 }
182 #[cfg(feature = "options")]
183 InterpolationMethod::Prism => {
184 self.transform_impl(src, dst, |x, y, z| l_tbl.prism_vec4(x, y, z))?;
185 }
186 InterpolationMethod::Linear => {
187 self.transform_impl(src, dst, |x, y, z| l_tbl.trilinear_vec4(x, y, z))?;
188 }
189 }
190 Ok(())
191 }
192}
193
194pub(crate) fn create_lut3_samples<T: Copy + 'static, const SAMPLES: usize>() -> Vec<T>
195where
196 u32: AsPrimitive<T>,
197{
198 let lut_size: u32 = (3 * SAMPLES * SAMPLES * SAMPLES) as u32;
199
200 assert!(SAMPLES >= 1);
201
202 let mut src = Vec::with_capacity(lut_size as usize);
203 for x in 0..SAMPLES as u32 {
204 for y in 0..SAMPLES as u32 {
205 for z in 0..SAMPLES as u32 {
206 src.push(x.as_());
207 src.push(y.as_());
208 src.push(z.as_());
209 }
210 }
211 }
212 src
213}
214
215pub(crate) fn create_lut3_samples_norm<const SAMPLES: usize>() -> Vec<f32> {
216 let lut_size: u32 = (3 * SAMPLES * SAMPLES * SAMPLES) as u32;
217
218 assert!(SAMPLES >= 1);
219
220 let scale = 1. / (SAMPLES as f32 - 1.0);
221
222 let mut src = Vec::with_capacity(lut_size as usize);
223 for x in 0..SAMPLES as u32 {
224 for y in 0..SAMPLES as u32 {
225 for z in 0..SAMPLES as u32 {
226 src.push(x as f32 * scale);
227 src.push(y as f32 * scale);
228 src.push(z as f32 * scale);
229 }
230 }
231 }
232 src
233}
234
235pub(crate) fn create_lut3x4(
236 lut: &LutDataType,
237 src: &[f32],
238 options: TransformOptions,
239 pcs: DataColorSpace,
240) -> Result<Vec<f32>, CmsError> {
241 if lut.num_input_channels != 3 || lut.num_output_channels != 4 {
242 return Err(CmsError::UnsupportedProfileConnection);
243 }
244
245 let mut dest = try_vec![0.; (src.len() / 3) * 4];
246
247 let lut_stage = stage_lut_3x4(lut, options, pcs)?;
248 lut_stage.transform(src, &mut dest)?;
249 Ok(dest)
250}