Coverage Report

Created: 2025-10-10 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/moxcms-0.7.6/src/conversions/lut4.rs
Line
Count
Source
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
0
    fn transform_impl<Fetch: Fn(f32, f32, f32, f32) -> Vector3f>(
68
0
        &self,
69
0
        src: &[f32],
70
0
        dst: &mut [f32],
71
0
        fetch: Fetch,
72
0
    ) -> Result<(), CmsError> {
73
0
        let linearization_0 = &self.linearization[0];
74
0
        let linearization_1 = &self.linearization[1];
75
0
        let linearization_2 = &self.linearization[2];
76
0
        let linearization_3 = &self.linearization[3];
77
0
        for (dest, src) in dst.chunks_exact_mut(3).zip(src.chunks_exact(4)) {
78
0
            debug_assert!(self.grid_size as i32 >= 1);
79
0
            let linear_x = lut_interp_linear_float(src[0], linearization_0);
80
0
            let linear_y = lut_interp_linear_float(src[1], linearization_1);
81
0
            let linear_z = lut_interp_linear_float(src[2], linearization_2);
82
0
            let linear_w = lut_interp_linear_float(src[3], linearization_3);
83
84
0
            let clut = fetch(linear_x, linear_y, linear_z, linear_w);
85
86
0
            let pcs_x = lut_interp_linear_float(clut.v[0], &self.output[0]);
87
0
            let pcs_y = lut_interp_linear_float(clut.v[1], &self.output[1]);
88
0
            let pcs_z = lut_interp_linear_float(clut.v[2], &self.output[2]);
89
0
            dest[0] = pcs_x;
90
0
            dest[1] = pcs_y;
91
0
            dest[2] = pcs_z;
92
        }
93
0
        Ok(())
94
0
    }
Unexecuted instantiation: <moxcms::conversions::lut4::Lut4x3>::transform_impl::<<moxcms::conversions::lut4::Lut4x3 as moxcms::transform::Stage>::transform::{closure#0}>
Unexecuted instantiation: <moxcms::conversions::lut4::Lut4x3>::transform_impl::<<moxcms::conversions::lut4::Lut4x3 as moxcms::transform::Stage>::transform::{closure#1}>
95
}
96
97
macro_rules! define_lut4_dispatch {
98
    ($dispatcher: ident) => {
99
        impl Stage for $dispatcher {
100
0
            fn transform(&self, src: &[f32], dst: &mut [f32]) -> Result<(), CmsError> {
101
0
                let l_tbl = Hypercube::new(&self.clut, self.grid_size as usize);
102
103
                // If Source PCS is LAB trilinear should be used
104
0
                if self.pcs == DataColorSpace::Lab || self.pcs == DataColorSpace::Xyz {
105
0
                    return self
106
0
                        .transform_impl(src, dst, |x, y, z, w| l_tbl.quadlinear_vec3(x, y, z, w));
107
0
                }
108
109
0
                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
0
                        self.transform_impl(src, dst, |x, y, z, w| {
124
0
                            l_tbl.quadlinear_vec3(x, y, z, w)
125
0
                        })?
126
                    }
127
                }
128
0
                Ok(())
129
0
            }
130
        }
131
    };
132
}
133
134
impl<T: Copy + PointeeSizeExpressible + AsPrimitive<f32>> KatanaLut4x3<T> {
135
0
    fn to_pcs_impl<Fetch: Fn(f32, f32, f32, f32) -> Vector3f>(
136
0
        &self,
137
0
        input: &[T],
138
0
        fetch: Fetch,
139
0
    ) -> Result<Vec<f32>, CmsError> {
140
0
        if input.len() % 4 != 0 {
141
0
            return Err(CmsError::LaneMultipleOfChannels);
142
0
        }
143
0
        let norm_value = if T::FINITE {
144
0
            1.0 / ((1u32 << self.bit_depth) - 1) as f32
145
        } else {
146
0
            1.0
147
        };
148
0
        let mut dst = try_vec![0.; (input.len() / 4) * 3];
149
0
        let linearization_0 = &self.linearization[0];
150
0
        let linearization_1 = &self.linearization[1];
151
0
        let linearization_2 = &self.linearization[2];
152
0
        let linearization_3 = &self.linearization[3];
153
0
        for (dest, src) in dst.chunks_exact_mut(3).zip(input.chunks_exact(4)) {
154
0
            let linear_x = lut_interp_linear_float(src[0].as_() * norm_value, linearization_0);
155
0
            let linear_y = lut_interp_linear_float(src[1].as_() * norm_value, linearization_1);
156
0
            let linear_z = lut_interp_linear_float(src[2].as_() * norm_value, linearization_2);
157
0
            let linear_w = lut_interp_linear_float(src[3].as_() * norm_value, linearization_3);
158
0
159
0
            let clut = fetch(linear_x, linear_y, linear_z, linear_w);
160
0
161
0
            let pcs_x = lut_interp_linear_float(clut.v[0], &self.output[0]);
162
0
            let pcs_y = lut_interp_linear_float(clut.v[1], &self.output[1]);
163
0
            let pcs_z = lut_interp_linear_float(clut.v[2], &self.output[2]);
164
0
            dest[0] = pcs_x;
165
0
            dest[1] = pcs_y;
166
0
            dest[2] = pcs_z;
167
0
        }
168
0
        Ok(dst)
169
0
    }
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<f64>>::to_pcs_impl::<<moxcms::conversions::lut4::KatanaLut4x3<f64> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, f64>>::to_pcs::{closure#0}>
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<f64>>::to_pcs_impl::<<moxcms::conversions::lut4::KatanaLut4x3<f64> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, f64>>::to_pcs::{closure#1}>
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<f32>>::to_pcs_impl::<<moxcms::conversions::lut4::KatanaLut4x3<f32> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, f32>>::to_pcs::{closure#0}>
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<f32>>::to_pcs_impl::<<moxcms::conversions::lut4::KatanaLut4x3<f32> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, f32>>::to_pcs::{closure#1}>
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<u8>>::to_pcs_impl::<<moxcms::conversions::lut4::KatanaLut4x3<u8> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, u8>>::to_pcs::{closure#0}>
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<u8>>::to_pcs_impl::<<moxcms::conversions::lut4::KatanaLut4x3<u8> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, u8>>::to_pcs::{closure#1}>
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<u16>>::to_pcs_impl::<<moxcms::conversions::lut4::KatanaLut4x3<u16> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, u16>>::to_pcs::{closure#0}>
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<u16>>::to_pcs_impl::<<moxcms::conversions::lut4::KatanaLut4x3<u16> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, u16>>::to_pcs::{closure#1}>
170
}
171
172
impl<T: Copy + PointeeSizeExpressible + AsPrimitive<f32>> KatanaInitialStage<f32, T>
173
    for KatanaLut4x3<T>
174
{
175
0
    fn to_pcs(&self, input: &[T]) -> Result<Vec<f32>, CmsError> {
176
0
        if input.len() % 4 != 0 {
177
0
            return Err(CmsError::LaneMultipleOfChannels);
178
0
        }
179
0
        let l_tbl = Hypercube::new(&self.clut, self.grid_size as usize);
180
181
        // If Source PCS is LAB trilinear should be used
182
0
        if self.pcs == DataColorSpace::Lab || self.pcs == DataColorSpace::Xyz {
183
0
            return self.to_pcs_impl(input, |x, y, z, w| l_tbl.quadlinear_vec3(x, y, z, w));
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<f64> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, f64>>::to_pcs::{closure#0}
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<f32> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, f32>>::to_pcs::{closure#0}
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<u8> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, u8>>::to_pcs::{closure#0}
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<u16> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, u16>>::to_pcs::{closure#0}
184
0
        }
185
186
0
        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
0
                self.to_pcs_impl(input, |x, y, z, w| l_tbl.quadlinear_vec3(x, y, z, w))
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<f64> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, f64>>::to_pcs::{closure#1}
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<f32> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, f32>>::to_pcs::{closure#1}
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<u8> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, u8>>::to_pcs::{closure#1}
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<u16> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, u16>>::to_pcs::{closure#1}
201
            }
202
        }
203
0
    }
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<f64> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, f64>>::to_pcs
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<f32> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, f32>>::to_pcs
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<u8> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, u8>>::to_pcs
Unexecuted instantiation: <moxcms::conversions::lut4::KatanaLut4x3<u16> as moxcms::conversions::katana::stages::KatanaInitialStage<f32, u16>>::to_pcs
204
}
205
206
define_lut4_dispatch!(Lut4x3);
207
208
0
fn make_lut_4x3(
209
0
    lut: &LutDataType,
210
0
    options: TransformOptions,
211
0
    pcs: DataColorSpace,
212
0
) -> 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
0
    let clut_length: usize = (lut.num_clut_grid_points as usize)
219
0
        .safe_powi(lut.num_input_channels as u32)?
220
0
        .safe_mul(lut.num_output_channels as usize)?;
221
222
0
    let clut_table = lut.clut_table.to_clut_f32();
223
0
    if clut_table.len() != clut_length {
224
0
        return Err(CmsError::MalformedClut(MalformedSize {
225
0
            size: clut_table.len(),
226
0
            expected: clut_length,
227
0
        }));
228
0
    }
229
230
0
    let linearization_table = lut.input_table.to_clut_f32();
231
232
0
    if linearization_table.len() < lut.num_input_table_entries as usize * 4 {
233
0
        return Err(CmsError::MalformedCurveLutTable(MalformedSize {
234
0
            size: linearization_table.len(),
235
0
            expected: lut.num_input_table_entries as usize * 4,
236
0
        }));
237
0
    }
238
239
0
    let lin_curve0 = linearization_table[0..lut.num_input_table_entries as usize].to_vec();
240
0
    let lin_curve1 = linearization_table
241
0
        [lut.num_input_table_entries as usize..lut.num_input_table_entries as usize * 2]
242
0
        .to_vec();
243
0
    let lin_curve2 = linearization_table
244
0
        [lut.num_input_table_entries as usize * 2..lut.num_input_table_entries as usize * 3]
245
0
        .to_vec();
246
0
    let lin_curve3 = linearization_table
247
0
        [lut.num_input_table_entries as usize * 3..lut.num_input_table_entries as usize * 4]
248
0
        .to_vec();
249
250
0
    let gamma_table = lut.output_table.to_clut_f32();
251
252
0
    if gamma_table.len() < lut.num_output_table_entries as usize * 3 {
253
0
        return Err(CmsError::MalformedCurveLutTable(MalformedSize {
254
0
            size: gamma_table.len(),
255
0
            expected: lut.num_output_table_entries as usize * 3,
256
0
        }));
257
0
    }
258
259
0
    let gamma_curve0 = gamma_table[..lut.num_output_table_entries as usize].to_vec();
260
0
    let gamma_curve1 = gamma_table
261
0
        [lut.num_output_table_entries as usize..lut.num_output_table_entries as usize * 2]
262
0
        .to_vec();
263
0
    let gamma_curve2 = gamma_table
264
0
        [lut.num_output_table_entries as usize * 2..lut.num_output_table_entries as usize * 3]
265
0
        .to_vec();
266
267
0
    let transform = Lut4x3 {
268
0
        linearization: [lin_curve0, lin_curve1, lin_curve2, lin_curve3],
269
0
        interpolation_method: options.interpolation_method,
270
0
        pcs,
271
0
        clut: clut_table,
272
0
        grid_size: lut.num_clut_grid_points,
273
0
        output: [gamma_curve0, gamma_curve1, gamma_curve2],
274
0
    };
275
0
    Ok(transform)
276
0
}
277
278
0
fn stage_lut_4x3(
279
0
    lut: &LutDataType,
280
0
    options: TransformOptions,
281
0
    pcs: DataColorSpace,
282
0
) -> Result<Box<dyn Stage>, CmsError> {
283
0
    let lut = make_lut_4x3(lut, options, pcs)?;
284
0
    let transform = Lut4x3 {
285
0
        linearization: lut.linearization,
286
0
        interpolation_method: lut.interpolation_method,
287
0
        pcs: lut.pcs,
288
0
        clut: lut.clut,
289
0
        grid_size: lut.grid_size,
290
0
        output: lut.output,
291
0
    };
292
0
    Ok(Box::new(transform))
293
0
}
294
295
0
pub(crate) fn katana_input_stage_lut_4x3<
296
0
    T: Copy + PointeeSizeExpressible + AsPrimitive<f32> + Send + Sync,
297
0
>(
298
0
    lut: &LutDataType,
299
0
    options: TransformOptions,
300
0
    pcs: DataColorSpace,
301
0
    bit_depth: usize,
302
0
) -> 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
0
    let lut = make_lut_4x3(lut, options, pcs)?;
309
310
0
    let transform = KatanaLut4x3::<T> {
311
0
        linearization: lut.linearization,
312
0
        interpolation_method: lut.interpolation_method,
313
0
        pcs: lut.pcs,
314
0
        clut: lut.clut,
315
0
        grid_size: lut.grid_size,
316
0
        output: lut.output,
317
0
        _phantom: PhantomData,
318
0
        bit_depth,
319
0
    };
320
0
    Ok(Box::new(transform))
321
0
}
Unexecuted instantiation: moxcms::conversions::lut4::katana_input_stage_lut_4x3::<f64>
Unexecuted instantiation: moxcms::conversions::lut4::katana_input_stage_lut_4x3::<f32>
Unexecuted instantiation: moxcms::conversions::lut4::katana_input_stage_lut_4x3::<u8>
Unexecuted instantiation: moxcms::conversions::lut4::katana_input_stage_lut_4x3::<u16>
322
323
0
pub(crate) fn create_lut4_norm_samples<const SAMPLES: usize>() -> Vec<f32> {
324
0
    let lut_size: u32 = (4 * SAMPLES * SAMPLES * SAMPLES * SAMPLES) as u32;
325
326
0
    let mut src = Vec::with_capacity(lut_size as usize);
327
328
0
    let recpeq = 1f32 / (SAMPLES - 1) as f32;
329
0
    for k in 0..SAMPLES {
330
0
        for c in 0..SAMPLES {
331
0
            for m in 0..SAMPLES {
332
0
                for y in 0..SAMPLES {
333
0
                    src.push(c as f32 * recpeq);
334
0
                    src.push(m as f32 * recpeq);
335
0
                    src.push(y as f32 * recpeq);
336
0
                    src.push(k as f32 * recpeq);
337
0
                }
338
            }
339
        }
340
    }
341
0
    src
342
0
}
343
344
0
pub(crate) fn create_lut4<const SAMPLES: usize>(
345
0
    lut: &LutDataType,
346
0
    options: TransformOptions,
347
0
    pcs: DataColorSpace,
348
0
) -> Result<Vec<f32>, CmsError> {
349
0
    if lut.num_input_channels != 4 {
350
0
        return Err(CmsError::UnsupportedProfileConnection);
351
0
    }
352
0
    let lut_size: u32 = (4 * SAMPLES * SAMPLES * SAMPLES * SAMPLES) as u32;
353
354
0
    let src = create_lut4_norm_samples::<SAMPLES>();
355
0
    let mut dest = try_vec![0.; (lut_size as usize) / 4 * 3];
356
357
0
    let lut_stage = stage_lut_4x3(lut, options, pcs)?;
358
0
    lut_stage.transform(&src, &mut dest)?;
359
0
    Ok(dest)
360
0
}