Debian Codegraph @

.cargo/vendor/moxcms/src/conversions/lut3x4.rs

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