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