Coverage Report

Created: 2026-12-01 08:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/image/src/metadata/cicp.rs
Line
Count
Source
1
use std::sync::Arc;
2
3
/// CICP (coding independent code points) defines the colorimetric interpretation of rgb-ish color
4
/// components.
5
use crate::{
6
    color::FromPrimitive,
7
    error::{ParameterError, ParameterErrorKind},
8
    math::multiply_accumulate,
9
    traits::{
10
        private::{LayoutWithColor, SealedPixelWithColorType},
11
        PixelWithColorType,
12
    },
13
    utils::vec_try_with_capacity,
14
    DynamicImage, ImageError, Pixel, Primitive,
15
};
16
17
/// Reference: <https://www.itu.int/rec/T-REC-H.273-202407-I/en> (V4)
18
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
19
pub struct Cicp {
20
    /// Defines the exact color of red, green, blue primary colors.
21
    pub primaries: CicpColorPrimaries,
22
    /// The electro-optical transfer function (EOTF) that maps color components to linear values.
23
    pub transfer: CicpTransferCharacteristics,
24
    /// A matrix between linear values and primary color representation.
25
    ///
26
    /// For an RGB space this is the identity matrix.
27
    pub matrix: CicpMatrixCoefficients,
28
    /// Whether the color components use all bits of the encoded values, or have headroom.
29
    ///
30
    /// For compute purposes, `image` only supports [`CicpVideoFullRangeFlag::FullRange`] and you
31
    /// get errors when trying to pass a non-full-range color profile to transform APIs such as
32
    /// [`DynamicImage::apply_color_space`] or [`CicpTransform::new`].
33
    pub full_range: CicpVideoFullRangeFlag,
34
}
35
36
/// An internal representation of what our `T: PixelWithColorType` can do, i.e. ImageBuffer.
37
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
38
pub(crate) struct CicpRgb {
39
    pub(crate) primaries: CicpColorPrimaries,
40
    pub(crate) transfer: CicpTransferCharacteristics,
41
    pub(crate) luminance: DerivedLuminance,
42
}
43
44
/// Defines the exact color of red, green, blue primary colors.
45
///
46
/// Each set defines the CIE 1931 XYZ (2°) color space coordinates of the primary colors and an
47
/// illuminant/whitepoint under which those colors are viewed.
48
///
49
/// Refer to Rec H.273 Table 2.
50
#[repr(u8)]
51
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
52
#[non_exhaustive]
53
pub enum CicpColorPrimaries {
54
    /// ITU-R BT.709-6
55
    SRgb = 1,
56
    /// Explicitly, the color space is not determined.
57
    Unspecified = 2,
58
    /// ITU-R BT.470-6 System M
59
    RgbM = 4,
60
    /// ITU-R BT.470-6 System B, G
61
    RgbB = 5,
62
    /// SMPTE 170M
63
    /// functionally equivalent to 7
64
    Bt601 = 6,
65
    /// SMPTE 240M
66
    /// functionally equivalent to 6
67
    Rgb240m = 7,
68
    /// Generic film (colour filters using Illuminant C)
69
    GenericFilm = 8,
70
    /// Rec. ITU-R BT.2020-2
71
    /// Rec. ITU-R BT.2100-2
72
    Rgb2020 = 9,
73
    /// SMPTE ST 428-1
74
    ///
75
    /// (CIE 1931 XYZ as in ISO/CIE 11664-1)
76
    Xyz = 10,
77
    /// SMPTE RP 431-2 (aka. DCI P3)
78
    SmpteRp431 = 11,
79
    /// SMPTE EG 432-1, DCI P3 variant with the D65 whitepoint (matching sRGB and BT.2020)
80
    SmpteRp432 = 12,
81
    /// Corresponds to value 22 but
82
    ///
83
    /// > No corresponding industry specification identified
84
    ///
85
    /// But moxcms identifies it as EBU Tech 3213-E: <https://tech.ebu.ch/docs/tech/tech3213.pdf>
86
    ///
87
    /// However, there are some differences in the second digit of red's CIE 1931 and the precision
88
    /// is only 2 digits whereas CICP names three; so unsure if this is fully accurate as the
89
    /// actual source material.
90
    Industry22 = 22,
91
}
92
93
impl CicpColorPrimaries {
94
    /// Do support these primaries in color [`CicpTransform`]?
95
0
    pub const fn is_supported_for_transform(self) -> bool {
96
        // Note: exhaustive match so we actively decide on future additions.
97
0
        match self {
98
            CicpColorPrimaries::SRgb
99
            | CicpColorPrimaries::RgbM
100
            | CicpColorPrimaries::RgbB
101
            | CicpColorPrimaries::Bt601
102
            | CicpColorPrimaries::Rgb240m
103
            | CicpColorPrimaries::GenericFilm
104
            | CicpColorPrimaries::Rgb2020
105
            | CicpColorPrimaries::Xyz
106
            | CicpColorPrimaries::SmpteRp431
107
            | CicpColorPrimaries::SmpteRp432
108
0
            | CicpColorPrimaries::Industry22 => true,
109
0
            CicpColorPrimaries::Unspecified => false,
110
        }
111
0
    }
112
}
113
114
/// The transfer characteristics, expressing relation between encoded values and linear color
115
/// values.
116
///
117
/// Refer to Rec H.273 Table 3.
118
#[repr(u8)]
119
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
120
#[non_exhaustive]
121
pub enum CicpTransferCharacteristics {
122
    /// Rec. ITU-R BT.709-6
123
    /// Rec. ITU-R BT.1361-0 conventional
124
    /// (functionally the same as the values 6, 14 and 15)
125
    Bt709 = 1,
126
    /// Explicitly, the transfer characteristics are not determined.
127
    Unspecified = 2,
128
    /// Rec. ITU-R BT.470-6 System M (historical)
129
    /// United States National Television System Committee 1953 Recommendation for transmission standards for color television
130
    /// United States Federal Communications Commission (2003) Title 47 Code of Federal Regulations 73.682 (a) (20)
131
    /// Rec. ITU-R BT.1700-0 625 PAL and 625 SECAM
132
    ///
133
    /// Assumed gamma of 2.2
134
    Bt470M = 4,
135
    /// Rec. ITU-R BT.470-6 System B, G (historical)
136
    Bt470BG = 5,
137
    /// Rec. ITU-R BT.601-7 525 or 625
138
    /// Rec. ITU-R BT.1358-1 525 or 625 (historical)
139
    /// Rec. ITU-R BT.1700-0 NTSC
140
    /// SMPTE ST 170 (functionally the same as the values 1, 14 and 15)
141
    Bt601 = 6,
142
    /// SMPTE ST 240
143
    Smpte240m = 7,
144
    /// Linear transfer characteristics
145
    Linear = 8,
146
    /// Logarithmic transfer characteristic (100:1 range)
147
    Log100 = 9,
148
    /// Logarithmic transfer characteristic (100 * Sqrt( 10 ) : 1 range)
149
    LogSqrt = 10,
150
    /// IEC 61966-2-4
151
    Iec61966_2_4 = 11,
152
    /// Rec. ITU-R BT.1361-0 extended colour gamut system (historical)
153
    Bt1361 = 12,
154
    /// IEC 61966-2-1 sRGB (with MatrixCoefficients equal to 0)
155
    /// IEC 61966-2-1 sYCC (with MatrixCoefficients equal to 5)
156
    SRgb = 13,
157
    /// Rec. ITU-R BT.2020-2 (10-bit system)
158
    /// (functionally the same as the values 1, 6 and 15)
159
    Bt2020_10bit = 14,
160
    /// Rec. ITU-R BT.2020-2 (12-bit system)
161
    /// (functionally the same as the values 1, 6 and 14)
162
    Bt2020_12bit = 15,
163
    /// SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems
164
    /// Rec. ITU-R BT.2100-2 perceptual quantization (PQ) system
165
    Smpte2084 = 16,
166
    /// SMPTE ST 428-1
167
    Smpte428 = 17,
168
    /// ARIB STD-B67
169
    /// Rec. ITU-R BT.2100-2 hybrid log- gamma (HLG) system
170
    Bt2100Hlg = 18,
171
}
172
173
impl CicpTransferCharacteristics {
174
    /// Do support this transfer function in color [`CicpTransform`]?
175
0
    pub const fn is_supported_for_transform(self) -> bool {
176
        // Note: exhaustive match so we actively decide on future additions.
177
0
        match self {
178
            CicpTransferCharacteristics::Bt709
179
            | CicpTransferCharacteristics::Bt470M
180
            | CicpTransferCharacteristics::Bt470BG
181
            | CicpTransferCharacteristics::Bt601
182
            | CicpTransferCharacteristics::Smpte240m
183
            | CicpTransferCharacteristics::Linear
184
            | CicpTransferCharacteristics::Log100
185
            | CicpTransferCharacteristics::LogSqrt
186
            | CicpTransferCharacteristics::Iec61966_2_4
187
            | CicpTransferCharacteristics::Bt1361
188
            | CicpTransferCharacteristics::SRgb
189
            | CicpTransferCharacteristics::Bt2020_10bit
190
            | CicpTransferCharacteristics::Bt2020_12bit
191
            | CicpTransferCharacteristics::Smpte428
192
0
            | CicpTransferCharacteristics::Bt2100Hlg => true,
193
            CicpTransferCharacteristics::Unspecified
194
            // Dolby patent WO 2022/066242; my problem not _only_ being the patent itself but also
195
            // that the patent does not contain any useful information. It does *not* disclose the
196
            // actual method. You get a graph of roughly the function. Fuck off.
197
            //
198
            // aka. Rec2100 PQ
199
0
            | CicpTransferCharacteristics::Smpte2084 => false,
200
        }
201
0
    }
202
}
203
204
///
205
/// Refer to Rec H.273 Table 4.
206
#[repr(u8)]
207
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
208
#[non_exhaustive]
209
pub enum CicpMatrixCoefficients {
210
    /// The identity matrix.
211
    /// Typically used for GBR (often referred to as RGB); however, may also be used for YZX (often referred to as XYZ);
212
    /// IEC 61966-2-1 sRGB
213
    /// SMPTE ST 428-1
214
    Identity = 0,
215
    /// Rec. ITU-R BT.709-6
216
    /// Rec. ITU-R BT.1361-0 conventional colour gamut system and extended colour gamut system (historical)
217
    /// IEC 61966-2-4 xvYCC709
218
    /// SMPTE RP 177 Annex B
219
    Bt709 = 1,
220
    /// Explicitly, the matrix coefficients are not determined.
221
    Unspecified = 2,
222
    /// United States Federal Communications Commission (2003) Title 47 Code of Federal Regulations 73.682 (a) (20)
223
    UsFCC = 4,
224
    ///  Rec. ITU-R BT.470-6 System B, G (historical)
225
    /// Rec. ITU-R BT.601-7 625
226
    /// Rec. ITU-R BT.1358-0 625 (historical)
227
    /// Rec. ITU-R BT.1700-0 625 PAL and 625 SECAM
228
    /// IEC 61966-2-1 sYCC
229
    /// IEC 61966-2-4 xvYCC601
230
    /// (functionally the same as the value 6)
231
    Bt470BG = 5,
232
    /// (functionally the same as the value 5)
233
    Smpte170m = 6,
234
    /// SMPTE ST 240
235
    Smpte240m = 7,
236
    /// YCgCo
237
    YCgCo = 8,
238
    /// Rec. ITU-R BT.2020-2 (non-constant luminance)
239
    /// Rec. ITU-R BT.2100-2 Y′CbCr
240
    Bt2020NonConstant = 9,
241
    /// Rec. ITU-R BT.2020-2 (constant luminance)
242
    Bt2020Constant = 10,
243
    /// SMPTE ST 2085
244
    Smpte2085 = 11,
245
    /// Chromaticity-derived non-constant luminance system
246
    ChromaticityDerivedNonConstant = 12,
247
    /// Chromaticity-derived constant luminance system
248
    ChromaticityDerivedConstant = 13,
249
    /// Rec. ITU-R BT.2100-2 ICTCp
250
    Bt2100 = 14,
251
    /// Colour representation developed in SMPTE as IPT-PQ-C2.
252
    IptPqC2 = 15,
253
    /// YCgCo with added bit-depth (2-bit).
254
    YCgCoRe = 16,
255
    /// YCgCo with added bit-depth (1-bit).
256
    YCgCoRo = 17,
257
}
258
259
/// The used encoded value range.
260
#[repr(u8)]
261
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
262
#[non_exhaustive]
263
pub enum CicpVideoFullRangeFlag {
264
    /// The color components are encoded in a limited range, e.g., 16-235 for 8-bit.
265
    ///
266
    /// Do note that `image` does not support computing with this setting (yet).
267
    NarrowRange = 0,
268
    /// The color components are encoded in the full range, e.g., 0-255 for 8-bit.
269
    FullRange = 1,
270
}
271
272
#[repr(u8)]
273
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
274
pub(crate) enum DerivedLuminance {
275
    /// Luminance is calculated in linear space:
276
    ///     Y' = dot(K_rgb, RGB)'
277
    #[allow(dead_code)] // We do not support this yet but should prepare call sites for the
278
    // eventuality.
279
    Constant,
280
    /// Luminance is calculated in the transferred space:
281
    ///     Y' = dot(K_rgb, RGB')
282
    NonConstant,
283
}
284
285
/// Apply to colors of the input color space to get output color values.
286
///
287
/// We do not support all possible Cicp color spaces, but when we support one then all builtin
288
/// `Pixel` types can be converted with their respective components. This value is used to signify
289
/// that some particular combination is supported.
290
#[derive(Clone)]
291
pub struct CicpTransform {
292
    pub(crate) from: Cicp,
293
    pub(crate) into: Cicp,
294
    pub(crate) u8: RgbTransforms<u8>,
295
    pub(crate) u16: RgbTransforms<u16>,
296
    pub(crate) f32: RgbTransforms<f32>,
297
    // Converting RGB to Y in the output.
298
    pub(crate) output_coefs: [f32; 3],
299
}
300
301
pub(crate) type CicpApplicable<'lt, C> = dyn Fn(&[C], &mut [C]) + Send + Sync + 'lt;
302
303
#[derive(Clone)]
304
pub(crate) struct RgbTransforms<C> {
305
    pub(crate) slices: [Arc<CicpApplicable<'static, C>>; 4],
306
    pub(crate) luma_rgb: [Arc<CicpApplicable<'static, C>>; 4],
307
    pub(crate) rgb_luma: [Arc<CicpApplicable<'static, C>>; 4],
308
    pub(crate) luma_luma: [Arc<CicpApplicable<'static, C>>; 4],
309
}
310
311
impl CicpTransform {
312
    /// Construct a transform between two color spaces.
313
    ///
314
    /// Returns `Some` if the transform is guaranteed to be supported by `image`. Both color spaces
315
    /// are well understood and can be expected to be supported in future versions. However, we do
316
    /// not make guarantees about adjusting the rounding modes, accuracy, and exact numeric values
317
    /// used in the transform. Also, out-of-gamut colors may be handled differently per API.
318
    ///
319
    /// See the respective methods for supported
320
    /// [primaries](`CicpColorPrimaries::is_supported_for_transform`) and
321
    /// [transfer characteristics](`CicpTransferCharacteristics::is_supported_for_transform`). Note
322
    /// that only identity and chromaticity derived non-constant matrices are supported; and only
323
    /// full range colors.
324
    ///
325
    /// Returns `None` if the transformation is not (yet) supported.
326
    ///
327
    /// This is used with [`ConvertColorOptions`][`crate::ConvertColorOptions`] in
328
    /// [`ImageBuffer::copy_from_color_space`][`crate::ImageBuffer::copy_from_color_space`],
329
    /// [`DynamicImage::copy_from_color_space`][`DynamicImage::copy_from_color_space`].
330
0
    pub fn new(from: Cicp, into: Cicp) -> Option<Self> {
331
0
        crate::metadata::cms_provider().transform(from, into)
332
0
    }
333
334
    /// For a Pixel with known color layout (`ColorType`) get a transform that is accurate.
335
    ///
336
    /// This returns `None` if we do not support the transform. At writing that is true for
337
    /// instance for transforms involved 'Luma` pixels which are interpreted as the `Y` in a
338
    /// `YCbCr` color based off the actual whitepoint, with coefficients according to each
339
    /// primary's luminance. Only Rgb transforms are supported via `moxcms`.
340
    ///
341
    /// Maybe provide publicly?
342
0
    pub(crate) fn supported_transform_fn<From: PixelWithColorType, Into: PixelWithColorType>(
343
0
        &self,
344
0
    ) -> &'_ CicpApplicable<'_, From::Subpixel> {
345
        use crate::traits::private::double_dispatch_transform_from_sealed;
346
0
        double_dispatch_transform_from_sealed::<From, Into>(self)
347
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgb<f32>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgb<f32>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgb<u8>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgb<u8>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgb<u16>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgb<u16>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgba<f32>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgba<f32>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgba<u8>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgba<u8>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgba<u16>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::supported_transform_fn::<image::color::Rgba<u16>, image::color::Rgb<u16>>
348
349
    /// Does this transform realize the conversion `from` to `into`.
350
0
    pub(crate) fn check_applicable(&self, from: Cicp, into: Cicp) -> Result<(), ImageError> {
351
0
        let check_expectation = |expected, found| {
352
0
            if expected == found {
353
0
                Ok(())
354
            } else {
355
0
                Err(ParameterError::from_kind(
356
0
                    ParameterErrorKind::CicpMismatch { expected, found },
357
0
                ))
358
            }
359
0
        };
360
361
0
        check_expectation(self.from, from).map_err(ImageError::Parameter)?;
362
0
        check_expectation(self.into, into).map_err(ImageError::Parameter)?;
363
364
0
        Ok(())
365
0
    }
366
0
    pub(crate) fn transform_dynamic(&self, lhs: &mut DynamicImage, rhs: &DynamicImage) {
367
        const STEP: usize = 256;
368
369
0
        let mut ibuffer = [0.0f32; 4 * STEP];
370
0
        let mut obuffer = [0.0f32; 4 * STEP];
371
372
0
        let pixels = (u64::from(lhs.width()) * u64::from(lhs.height())) as usize;
373
374
        let input_samples;
375
        let output_samples;
376
377
0
        let inner_transform = match (
378
0
            LayoutWithColor::from(lhs.color()),
379
0
            LayoutWithColor::from(rhs.color()),
380
        ) {
381
            (
382
                LayoutWithColor::Luma | LayoutWithColor::Rgb,
383
                LayoutWithColor::Luma | LayoutWithColor::Rgb,
384
            ) => {
385
0
                output_samples = 3;
386
0
                input_samples = 3;
387
0
                &*self.f32.slices[0]
388
            }
389
            (
390
                LayoutWithColor::LumaAlpha | LayoutWithColor::Rgba,
391
                LayoutWithColor::Luma | LayoutWithColor::Rgb,
392
            ) => {
393
0
                output_samples = 4;
394
0
                input_samples = 3;
395
0
                &*self.f32.slices[1]
396
            }
397
            (
398
                LayoutWithColor::Luma | LayoutWithColor::Rgb,
399
                LayoutWithColor::LumaAlpha | LayoutWithColor::Rgba,
400
            ) => {
401
0
                output_samples = 3;
402
0
                input_samples = 4;
403
0
                &*self.f32.slices[2]
404
            }
405
            (
406
                LayoutWithColor::LumaAlpha | LayoutWithColor::Rgba,
407
                LayoutWithColor::LumaAlpha | LayoutWithColor::Rgba,
408
            ) => {
409
0
                output_samples = 4;
410
0
                input_samples = 4;
411
0
                &*self.f32.slices[3]
412
            }
413
        };
414
415
0
        for start_idx in (0..pixels).step_by(STEP) {
416
0
            let end_idx = (start_idx + STEP).min(pixels);
417
0
            let count = end_idx - start_idx;
418
419
            // Expand pixels from `other` into `ibuffer`. All of these have different types, so
420
            // here's two large switch statements.
421
0
            match rhs {
422
0
                DynamicImage::ImageLuma8(buf) => {
423
0
                    CicpTransform::expand_luma_rgb(
424
0
                        &buf.subpixels()[start_idx..end_idx],
425
0
                        &mut ibuffer[..3 * count],
426
0
                    );
427
0
                }
428
0
                DynamicImage::ImageLumaA8(buf) => {
429
0
                    CicpTransform::expand_luma_rgba(
430
0
                        &buf.subpixels()[2 * start_idx..2 * end_idx],
431
0
                        &mut ibuffer[..4 * count],
432
0
                    );
433
0
                }
434
0
                DynamicImage::ImageRgb8(buf) => {
435
0
                    CicpTransform::expand_rgb(
436
0
                        &buf.subpixels()[3 * start_idx..3 * end_idx],
437
0
                        &mut ibuffer[..3 * count],
438
0
                    );
439
0
                }
440
0
                DynamicImage::ImageRgba8(buf) => {
441
0
                    CicpTransform::expand_rgba(
442
0
                        &buf.subpixels()[4 * start_idx..4 * end_idx],
443
0
                        &mut ibuffer[..4 * count],
444
0
                    );
445
0
                }
446
0
                DynamicImage::ImageLuma16(buf) => {
447
0
                    CicpTransform::expand_luma_rgb(
448
0
                        &buf.subpixels()[start_idx..end_idx],
449
0
                        &mut ibuffer[..3 * count],
450
0
                    );
451
0
                }
452
0
                DynamicImage::ImageLumaA16(buf) => {
453
0
                    CicpTransform::expand_luma_rgba(
454
0
                        &buf.subpixels()[2 * start_idx..2 * end_idx],
455
0
                        &mut ibuffer[..4 * count],
456
0
                    );
457
0
                }
458
0
                DynamicImage::ImageRgb16(buf) => {
459
0
                    CicpTransform::expand_rgb(
460
0
                        &buf.subpixels()[3 * start_idx..3 * end_idx],
461
0
                        &mut ibuffer[..3 * count],
462
0
                    );
463
0
                }
464
465
0
                DynamicImage::ImageRgba16(buf) => {
466
0
                    CicpTransform::expand_rgba(
467
0
                        &buf.subpixels()[4 * start_idx..4 * end_idx],
468
0
                        &mut ibuffer[..4 * count],
469
0
                    );
470
0
                }
471
0
                DynamicImage::ImageLuma32F(buf) => {
472
0
                    CicpTransform::expand_luma_rgb(
473
0
                        &buf.subpixels()[start_idx..end_idx],
474
0
                        &mut ibuffer[..3 * count],
475
0
                    );
476
0
                }
477
0
                DynamicImage::ImageLumaA32F(buf) => {
478
0
                    CicpTransform::expand_luma_rgba(
479
0
                        &buf.subpixels()[2 * start_idx..2 * end_idx],
480
0
                        &mut ibuffer[..4 * count],
481
0
                    );
482
0
                }
483
0
                DynamicImage::ImageRgb32F(buf) => {
484
0
                    CicpTransform::expand_rgb(
485
0
                        &buf.subpixels()[3 * start_idx..3 * end_idx],
486
0
                        &mut ibuffer[..3 * count],
487
0
                    );
488
0
                }
489
0
                DynamicImage::ImageRgba32F(buf) => {
490
0
                    CicpTransform::expand_rgba(
491
0
                        &buf.subpixels()[4 * start_idx..4 * end_idx],
492
0
                        &mut ibuffer[..4 * count],
493
0
                    );
494
0
                }
495
            }
496
497
0
            let islice = &ibuffer[..input_samples * count];
498
0
            let oslice = &mut obuffer[..output_samples * count];
499
500
0
            inner_transform(islice, oslice);
501
502
0
            match lhs {
503
0
                DynamicImage::ImageLuma8(buf) => {
504
0
                    CicpTransform::clamp_rgb_luma(
505
0
                        &obuffer[..3 * count],
506
0
                        &mut buf.subpixels_mut()[start_idx..end_idx],
507
0
                        self.output_coefs,
508
0
                    );
509
0
                }
510
0
                DynamicImage::ImageLumaA8(buf) => {
511
0
                    CicpTransform::clamp_rgba_luma(
512
0
                        &obuffer[..4 * count],
513
0
                        &mut buf.subpixels_mut()[2 * start_idx..2 * end_idx],
514
0
                        self.output_coefs,
515
0
                    );
516
0
                }
517
0
                DynamicImage::ImageRgb8(buf) => {
518
0
                    CicpTransform::clamp_rgb(
519
0
                        &obuffer[..3 * count],
520
0
                        &mut buf.subpixels_mut()[3 * start_idx..3 * end_idx],
521
0
                    );
522
0
                }
523
0
                DynamicImage::ImageRgba8(buf) => {
524
0
                    CicpTransform::clamp_rgba(
525
0
                        &obuffer[..4 * count],
526
0
                        &mut buf.subpixels_mut()[4 * start_idx..4 * end_idx],
527
0
                    );
528
0
                }
529
0
                DynamicImage::ImageLuma16(buf) => {
530
0
                    CicpTransform::clamp_rgb_luma(
531
0
                        &obuffer[..3 * count],
532
0
                        &mut buf.subpixels_mut()[start_idx..end_idx],
533
0
                        self.output_coefs,
534
0
                    );
535
0
                }
536
0
                DynamicImage::ImageLumaA16(buf) => {
537
0
                    CicpTransform::clamp_rgba_luma(
538
0
                        &obuffer[..4 * count],
539
0
                        &mut buf.subpixels_mut()[2 * start_idx..2 * end_idx],
540
0
                        self.output_coefs,
541
0
                    );
542
0
                }
543
0
                DynamicImage::ImageRgb16(buf) => {
544
0
                    CicpTransform::clamp_rgb(
545
0
                        &obuffer[..3 * count],
546
0
                        &mut buf.subpixels_mut()[3 * start_idx..3 * end_idx],
547
0
                    );
548
0
                }
549
0
                DynamicImage::ImageRgba16(buf) => {
550
0
                    CicpTransform::clamp_rgba(
551
0
                        &obuffer[..4 * count],
552
0
                        &mut buf.subpixels_mut()[4 * start_idx..4 * end_idx],
553
0
                    );
554
0
                }
555
0
                DynamicImage::ImageLuma32F(buf) => {
556
0
                    CicpTransform::clamp_rgb_luma(
557
0
                        &obuffer[..3 * count],
558
0
                        &mut buf.subpixels_mut()[start_idx..end_idx],
559
0
                        self.output_coefs,
560
0
                    );
561
0
                }
562
0
                DynamicImage::ImageLumaA32F(buf) => {
563
0
                    CicpTransform::clamp_rgba_luma(
564
0
                        &obuffer[..4 * count],
565
0
                        &mut buf.subpixels_mut()[2 * start_idx..2 * end_idx],
566
0
                        self.output_coefs,
567
0
                    );
568
0
                }
569
0
                DynamicImage::ImageRgb32F(buf) => {
570
0
                    CicpTransform::clamp_rgb(
571
0
                        &obuffer[..3 * count],
572
0
                        &mut buf.subpixels_mut()[3 * start_idx..3 * end_idx],
573
0
                    );
574
0
                }
575
0
                DynamicImage::ImageRgba32F(buf) => {
576
0
                    CicpTransform::clamp_rgba(
577
0
                        &obuffer[..4 * count],
578
0
                        &mut buf.subpixels_mut()[4 * start_idx..4 * end_idx],
579
0
                    );
580
0
                }
581
            }
582
        }
583
0
    }
584
585
    // Note on this design: When we dispatch into this function, we have a `Self` type that is
586
    // qualified to have the appropriate bound here. However, for the target type of the transform
587
    // we have, e.g., `Rgba<Self::Subpixel>`. Now we know that these are also with color for the
588
    // most part but we can not convince the compiler (indeed, there is or was an asymmetry with
589
    // gray pixels where they do not have float equivalents). It is hence necessary to provide the
590
    // output layout as a runtime parameter, not a compile-time type.
591
0
    pub(crate) fn select_transform_u8<P: SealedPixelWithColorType<TransformableSubpixel = u8>>(
592
0
        &self,
593
0
        into: LayoutWithColor,
594
0
    ) -> &Arc<CicpApplicable<'static, u8>> {
595
0
        self.u8.select_transform::<P>(into)
596
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::select_transform_u8::<image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::select_transform_u8::<image::color::Rgba<u8>>
597
598
0
    pub(crate) fn select_transform_u16<O: SealedPixelWithColorType<TransformableSubpixel = u16>>(
599
0
        &self,
600
0
        into: LayoutWithColor,
601
0
    ) -> &Arc<CicpApplicable<'static, u16>> {
602
0
        self.u16.select_transform::<O>(into)
603
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::select_transform_u16::<image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::select_transform_u16::<image::color::Rgba<u16>>
604
605
0
    pub(crate) fn select_transform_f32<O: SealedPixelWithColorType<TransformableSubpixel = f32>>(
606
0
        &self,
607
0
        into: LayoutWithColor,
608
0
    ) -> &Arc<CicpApplicable<'static, f32>> {
609
0
        self.f32.select_transform::<O>(into)
610
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::select_transform_f32::<image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::select_transform_f32::<image::color::Rgba<f32>>
611
612
    /// The order of transforms in the conversion tables.
613
    pub(crate) const LAYOUTS: [(LayoutWithColor, LayoutWithColor); 4] = [
614
        (LayoutWithColor::Rgb, LayoutWithColor::Rgb),
615
        (LayoutWithColor::Rgb, LayoutWithColor::Rgba),
616
        (LayoutWithColor::Rgba, LayoutWithColor::Rgb),
617
        (LayoutWithColor::Rgba, LayoutWithColor::Rgba),
618
    ];
619
620
0
    pub(crate) fn expand_luma_rgb<P: ColorComponentForCicp>(luma: &[P], rgb: &mut [f32]) {
621
0
        for (&pix, rgb) in luma.iter().zip(rgb.as_chunks_mut::<3>().0.iter_mut()) {
622
0
            let luma = pix.expand_to_f32();
623
0
            rgb[0] = luma;
624
0
            rgb[1] = luma;
625
0
            rgb[2] = luma;
626
0
        }
627
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_luma_rgb::<f32>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_luma_rgb::<u8>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_luma_rgb::<u16>
628
629
0
    pub(crate) fn expand_luma_rgba<P: ColorComponentForCicp>(luma: &[P], rgb: &mut [f32]) {
630
0
        let luma_chunks = luma.as_chunks::<2>().0.iter();
631
0
        let rgb_chunks = rgb.as_chunks_mut::<4>().0.iter_mut();
632
0
        for (pix, rgb) in luma_chunks.zip(rgb_chunks) {
633
0
            let luma = pix[0].expand_to_f32();
634
0
            rgb[0] = luma;
635
0
            rgb[1] = luma;
636
0
            rgb[2] = luma;
637
0
            rgb[3] = pix[1].expand_to_f32();
638
0
        }
639
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_luma_rgba::<f32>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_luma_rgba::<u8>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_luma_rgba::<u16>
640
641
0
    pub(crate) fn expand_rgb<P: ColorComponentForCicp>(input: &[P], output: &mut [f32]) {
642
0
        for (&component, val) in input.iter().zip(output) {
643
0
            *val = component.expand_to_f32();
644
0
        }
645
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_rgb::<f32>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_rgb::<u8>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_rgb::<u16>
646
647
0
    pub(crate) fn expand_rgba<P: ColorComponentForCicp>(input: &[P], output: &mut [f32]) {
648
0
        for (&component, val) in input.iter().zip(output) {
649
0
            *val = component.expand_to_f32();
650
0
        }
651
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_rgba::<f32>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_rgba::<u8>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::expand_rgba::<u16>
652
653
0
    pub(crate) fn clamp_rgb<P: ColorComponentForCicp>(input: &[f32], output: &mut [P]) {
654
        // Everything is mapped..
655
0
        for (&component, val) in input.iter().zip(output) {
656
0
            *val = P::clamp_from_f32(component);
657
0
        }
658
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgb::<f32>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgb::<u8>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgb::<u16>
659
660
0
    pub(crate) fn clamp_rgba<P: ColorComponentForCicp>(input: &[f32], output: &mut [P]) {
661
0
        for (&component, val) in input.iter().zip(output) {
662
0
            *val = P::clamp_from_f32(component);
663
0
        }
664
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgba::<f32>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgba::<u8>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgba::<u16>
665
666
0
    pub(crate) fn clamp_rgb_luma<P: ColorComponentForCicp>(
667
0
        input: &[f32],
668
0
        output: &mut [P],
669
0
        coef: [f32; 3],
670
0
    ) {
671
0
        for (rgb, pix) in input.as_chunks::<3>().0.iter().zip(output) {
672
0
            let mut luma = 0.0;
673
674
0
            for (&component, coef) in rgb.iter().zip(coef) {
675
0
                luma = multiply_accumulate(luma, component, coef);
676
0
            }
677
678
0
            *pix = P::clamp_from_f32(luma);
679
        }
680
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgb_luma::<f32>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgb_luma::<u8>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgb_luma::<u16>
681
682
0
    pub(crate) fn clamp_rgba_luma<P: ColorComponentForCicp>(
683
0
        input: &[f32],
684
0
        output: &mut [P],
685
0
        coef: [f32; 3],
686
0
    ) {
687
0
        let input_chunks = input.as_chunks::<4>().0.iter();
688
0
        let output_chunks = output.as_chunks_mut::<2>().0.iter_mut();
689
0
        for (rgba, pix) in input_chunks.zip(output_chunks) {
690
0
            let mut luma = 0.0;
691
692
0
            for (&component, coef) in rgba[..3].iter().zip(coef) {
693
0
                luma = multiply_accumulate(luma, component, coef);
694
0
            }
695
696
0
            pix[0] = P::clamp_from_f32(luma);
697
0
            pix[1] = P::clamp_from_f32(rgba[3]);
698
        }
699
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgba_luma::<f32>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgba_luma::<u8>
Unexecuted instantiation: <image::metadata::cicp::CicpTransform>::clamp_rgba_luma::<u16>
700
}
701
702
impl CicpRgb {
703
    /// Internal utility for converting color buffers of different pixel representations, assuming
704
    /// they have this same cicp. This method returns a buffer, avoiding the pre-zeroing
705
    /// the vector.
706
0
    pub(crate) fn cast_pixels<FromColor, IntoColor>(
707
0
        &self,
708
0
        buffer: &[FromColor::Subpixel],
709
0
        // Since this is not performance sensitive, we can use a dyn closure here instead of an
710
0
        // impl closure just in case we call this from multiple different paths.
711
0
        color_space_fallback: &dyn Fn() -> [f32; 3],
712
0
    ) -> Vec<IntoColor::Subpixel>
713
0
    where
714
0
        FromColor: Pixel + SealedPixelWithColorType<TransformableSubpixel = FromColor::Subpixel>,
715
0
        IntoColor: Pixel,
716
0
        IntoColor: CicpPixelCast<FromColor>,
717
0
        FromColor::Subpixel: ColorComponentForCicp,
718
0
        IntoColor::Subpixel: ColorComponentForCicp + FromPrimitive<FromColor::Subpixel>,
719
    {
720
        use crate::traits::private::PrivateToken;
721
0
        let from_layout = <FromColor as SealedPixelWithColorType>::layout(PrivateToken);
722
0
        let into_layout = <IntoColor as SealedPixelWithColorType>::layout(PrivateToken);
723
        // This method is instantiated *a lot*. Consequently every line here matters in terms of
724
        // codegen. We outline all parts into separate methods where they monomorphize only over
725
        // the channel type and not the whole pixel except what we needed here to satisfy the type
726
        // sysstem.
727
0
        self.cast_pixels_by_layout(buffer, color_space_fallback, from_layout, into_layout)
728
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<f32>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u8>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgb<u16>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<f32>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u8>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Luma<u16>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<f32>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u8>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::Rgba<u16>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<f32>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u8>, image::color::Rgba<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::LumaA<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::LumaA<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::LumaA<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::Luma<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::Luma<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::Luma<u16>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels::<image::color::LumaA<u16>, image::color::Rgba<u16>>
729
730
0
    fn cast_pixels_by_layout<FromSubpixel, IntoSubpixel>(
731
0
        &self,
732
0
        buffer: &[FromSubpixel],
733
0
        // Since this is not performance sensitive, we can use a dyn closure here instead of an
734
0
        // impl closure just in case we call this from multiple different paths.
735
0
        color_space_fallback: &dyn Fn() -> [f32; 3],
736
0
        from_layout: LayoutWithColor,
737
0
        into_layout: LayoutWithColor,
738
0
    ) -> Vec<IntoSubpixel>
739
0
    where
740
0
        FromSubpixel: ColorComponentForCicp + Primitive,
741
0
        IntoSubpixel: ColorComponentForCicp + FromPrimitive<FromSubpixel> + Primitive,
742
    {
743
0
        let mut output = match self.cast_pixels_from_subpixels(buffer, from_layout, into_layout) {
744
0
            Ok(ok) => return ok,
745
0
            Err(buffer) => buffer,
746
        };
747
748
        // If we get here we need to transform through Rgb(a) 32F
749
0
        let color_space_coefs = self
750
0
            .derived_luminance()
751
            // Since `cast_pixels` must be infallible we have no choice but to fallback to
752
            // something here. This something is chosen by the caller, which would allow them to
753
            // detect it has happened.
754
0
            .unwrap_or_else(color_space_fallback);
755
756
0
        let pixels = buffer.len() / from_layout.channels();
757
758
        // All of the following is done in-place; so we must allow the buffer space in which the
759
        // output is written ahead of time although such initialization is technically redundant.
760
        // We best do this once to allow for a very efficient memset initialization.
761
0
        Self::create_output::<IntoSubpixel>(&mut output, pixels, into_layout);
762
763
0
        Self::cast_pixels_by_fallback(
764
0
            buffer,
765
0
            output.as_mut_slice(),
766
0
            from_layout,
767
0
            into_layout,
768
0
            color_space_coefs,
769
        );
770
771
0
        output
772
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_layout::<f32, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_layout::<f32, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_layout::<f32, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_layout::<u8, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_layout::<u8, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_layout::<u8, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_layout::<u16, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_layout::<u16, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_layout::<u16, u16>
773
774
0
    fn create_output<Into: Primitive>(
775
0
        output: &mut Vec<Into>,
776
0
        pixels: usize,
777
0
        into_layout: LayoutWithColor,
778
0
    ) {
779
0
        output.resize(pixels * into_layout.channels(), Into::DEFAULT_MIN_VALUE);
780
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::create_output::<f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::create_output::<u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::create_output::<u16>
781
782
0
    fn cast_pixels_by_fallback<
783
0
        From: Primitive + ColorComponentForCicp,
784
0
        Into: ColorComponentForCicp,
785
0
    >(
786
0
        buffer: &[From],
787
0
        output: &mut [Into],
788
0
        from_layout: LayoutWithColor,
789
0
        into_layout: LayoutWithColor,
790
0
        color_space_coefs: [f32; 3],
791
0
    ) {
792
        use LayoutWithColor as Layout;
793
794
        const STEP: usize = 256;
795
0
        let pixels = buffer.len() / from_layout.channels();
796
797
0
        let mut ibuffer = [0.0f32; 4 * STEP];
798
0
        let mut obuffer = [0.0f32; 4 * STEP];
799
800
0
        let ibuf_step = match from_layout {
801
0
            Layout::Rgb | Layout::Luma => 3,
802
0
            Layout::Rgba | Layout::LumaAlpha => 4,
803
        };
804
805
0
        let obuf_step = match into_layout {
806
0
            Layout::Rgb | Layout::Luma => 3,
807
0
            Layout::Rgba | Layout::LumaAlpha => 4,
808
        };
809
810
0
        for start_idx in (0..pixels).step_by(STEP) {
811
0
            let end_idx = (start_idx + STEP).min(pixels);
812
0
            let count = end_idx - start_idx;
813
814
0
            let ibuffer = &mut ibuffer[..ibuf_step * count];
815
816
0
            match from_layout {
817
                Layout::Rgb => {
818
0
                    CicpTransform::expand_rgb(&buffer[3 * start_idx..3 * end_idx], ibuffer)
819
                }
820
                Layout::Rgba => {
821
0
                    CicpTransform::expand_rgba(&buffer[4 * start_idx..4 * end_idx], ibuffer)
822
                }
823
                Layout::Luma => {
824
0
                    CicpTransform::expand_luma_rgb(&buffer[start_idx..end_idx], ibuffer)
825
                }
826
                Layout::LumaAlpha => {
827
0
                    CicpTransform::expand_luma_rgba(&buffer[2 * start_idx..2 * end_idx], ibuffer)
828
                }
829
            }
830
831
            // Add or subtract the alpha channel. We could do that as part of the store but this
832
            // keeps the code simpler—there is a one-to-one correspondence with the methods needed
833
            // for a full conversion.
834
0
            let obuffer = match (ibuf_step, obuf_step) {
835
                (3, 4) => {
836
0
                    let ibuffer_chunks = ibuffer.as_chunks::<3>().0.iter();
837
0
                    let obuffer_chunks = obuffer.as_chunks_mut::<4>().0.iter_mut();
838
0
                    for (rgb, rgba) in ibuffer_chunks.zip(obuffer_chunks).take(count) {
839
0
                        rgba[0] = rgb[0];
840
0
                        rgba[1] = rgb[1];
841
0
                        rgba[2] = rgb[2];
842
0
                        rgba[3] = 1.0;
843
0
                    }
844
845
0
                    &obuffer[..4 * count]
846
                }
847
                (4, 3) => {
848
0
                    let ibuffer_chunks = ibuffer.as_chunks::<4>().0.iter();
849
0
                    let obuffer_chunks = obuffer.as_chunks_mut::<3>().0.iter_mut();
850
0
                    for (rgba, rgb) in ibuffer_chunks.zip(obuffer_chunks).take(count) {
851
0
                        rgb[0] = rgba[0];
852
0
                        rgb[1] = rgba[1];
853
0
                        rgb[2] = rgba[2];
854
0
                    }
855
856
0
                    &obuffer[..3 * count]
857
                }
858
0
                (n, m) => {
859
0
                    debug_assert_eq!(n, m);
860
0
                    &ibuffer[..m * count]
861
                }
862
            };
863
864
0
            match into_layout {
865
0
                Layout::Rgb => {
866
0
                    CicpTransform::clamp_rgb(obuffer, &mut output[3 * start_idx..3 * end_idx]);
867
0
                }
868
0
                Layout::Rgba => {
869
0
                    CicpTransform::clamp_rgba(obuffer, &mut output[4 * start_idx..4 * end_idx]);
870
0
                }
871
0
                Layout::Luma => {
872
0
                    CicpTransform::clamp_rgb_luma(
873
0
                        obuffer,
874
0
                        &mut output[start_idx..end_idx],
875
0
                        color_space_coefs,
876
0
                    );
877
0
                }
878
0
                Layout::LumaAlpha => {
879
0
                    CicpTransform::clamp_rgba_luma(
880
0
                        obuffer,
881
0
                        &mut output[2 * start_idx..2 * end_idx],
882
0
                        color_space_coefs,
883
0
                    );
884
0
                }
885
            }
886
        }
887
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_fallback::<f32, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_fallback::<f32, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_fallback::<f32, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_fallback::<u8, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_fallback::<u8, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_fallback::<u8, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_fallback::<u16, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_fallback::<u16, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_by_fallback::<u16, u16>
888
889
    /// Make sure this is only monomorphized for subpixel combinations, not for every pixel
890
    /// combination! There's ample time to do that in `cast_pixels`.
891
0
    pub(crate) fn cast_pixels_from_subpixels<FromSubpixel, IntoSubpixel>(
892
0
        &self,
893
0
        buffer: &[FromSubpixel],
894
0
        from_layout: LayoutWithColor,
895
0
        into_layout: LayoutWithColor,
896
0
    ) -> Result<Vec<IntoSubpixel>, Vec<IntoSubpixel>>
897
0
    where
898
0
        FromSubpixel: ColorComponentForCicp,
899
0
        IntoSubpixel: ColorComponentForCicp + FromPrimitive<FromSubpixel> + Primitive,
900
    {
901
        use crate::traits::private::LayoutWithColor as Layout;
902
903
0
        assert!(buffer.len().is_multiple_of(from_layout.channels()));
904
0
        let pixels = buffer.len() / from_layout.channels();
905
906
0
        let mut output: Vec<IntoSubpixel> = vec_try_with_capacity(pixels * into_layout.channels())
907
            // Not entirely failsafe, if you expand luma to rgba you can get a factor of 4 but at
908
            // least this will not overflow. And that's why I'm a fan of in-place operations.
909
0
            .expect("input layout already allocated with appropriate layout");
910
911
        // In most cases we perform a seemingly wasteful initialization, by initializing the output
912
        // before writing. However, we gain it back in codegen. If we did not have the vector exist
913
        // then the only safe way to add elements is via `push` or `extend_from_slice`. These
914
        // methods will check the capacity of the vector on every call and branch to reallocate. In
915
        // many cases this throws off loop analysis. LLVM does not seem to trust our capacity or
916
        // any arithmetic checks we do before to ensure that the len does not increase beyond the
917
        // capacity. The loop bodies that result are catastrophically bad and mostly not
918
        // vectorized.
919
        //
920
        // The one case where this does not apply is when both have the same count of channels. In
921
        // this case we can just extend into the output without worrying about the layout at all
922
        // and the Iterator type (and its size_hint) is trivial to work with.
923
924
0
        let map_channel = <IntoSubpixel as FromPrimitive<FromSubpixel>>::from_primitive;
925
926
0
        match (from_layout, into_layout) {
927
            // First detect if we can use simple channel-by-channel component conversion.
928
            (Layout::Rgb, Layout::Rgb)
929
            | (Layout::Rgba, Layout::Rgba)
930
            | (Layout::Luma, Layout::Luma)
931
0
            | (Layout::LumaAlpha, Layout::LumaAlpha) => {
932
0
                // Every component assigned accordingly. We do not care which as there is no
933
0
                // conversion do to be done other than numeric one. (no tone mapping etc.).
934
0
                output.extend(buffer.iter().copied().map(map_channel));
935
0
            }
936
0
            (Layout::Rgb, Layout::Rgba) => {
937
0
                Self::subpixel_cast_rgb_to_rgba(&mut output, buffer);
938
0
            }
939
0
            (Layout::Rgba, Layout::Rgb) => {
940
0
                Self::subpixel_cast_rgba_to_rgb(&mut output, buffer);
941
0
            }
942
0
            (Layout::Luma, Layout::LumaAlpha) => {
943
0
                Self::subpixel_cast_luma_to_luma_alpha(&mut output, buffer);
944
0
            }
945
0
            (Layout::LumaAlpha, Layout::Luma) => {
946
0
                Self::subpixel_cast_luma_alpha_to_luma(&mut output, buffer);
947
0
            }
948
0
            _ => return Err(output),
949
        }
950
951
0
        Ok(output)
952
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_from_subpixels::<f32, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_from_subpixels::<f32, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_from_subpixels::<f32, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_from_subpixels::<u8, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_from_subpixels::<u8, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_from_subpixels::<u8, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_from_subpixels::<u16, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_from_subpixels::<u16, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::cast_pixels_from_subpixels::<u16, u16>
953
954
0
    fn subpixel_cast_rgb_to_rgba<FromSubpixel, IntoSubpixel>(
955
0
        output: &mut Vec<IntoSubpixel>,
956
0
        buffer: &[FromSubpixel],
957
0
    ) where
958
0
        FromSubpixel: ColorComponentForCicp,
959
0
        IntoSubpixel: ColorComponentForCicp + FromPrimitive<FromSubpixel> + Primitive,
960
    {
961
0
        let pixels = buffer.len() / LayoutWithColor::Rgb.channels();
962
0
        Self::create_output::<IntoSubpixel>(output, pixels, LayoutWithColor::Rgba);
963
964
0
        let map_channel = <IntoSubpixel as FromPrimitive<FromSubpixel>>::from_primitive;
965
0
        let default_alpha = <IntoSubpixel as Primitive>::DEFAULT_MAX_VALUE;
966
967
0
        let buffer_chunks = buffer.as_chunks::<3>().0;
968
0
        let output_chunks = output.as_chunks_mut::<4>().0;
969
970
0
        for (&[r, g, b], out) in buffer_chunks.iter().zip(output_chunks) {
971
0
            *out = [
972
0
                map_channel(r),
973
0
                map_channel(g),
974
0
                map_channel(b),
975
0
                default_alpha,
976
0
            ];
977
0
        }
978
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgb_to_rgba::<f32, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgb_to_rgba::<f32, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgb_to_rgba::<f32, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgb_to_rgba::<u8, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgb_to_rgba::<u8, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgb_to_rgba::<u8, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgb_to_rgba::<u16, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgb_to_rgba::<u16, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgb_to_rgba::<u16, u16>
979
980
0
    fn subpixel_cast_rgba_to_rgb<FromSubpixel, IntoSubpixel>(
981
0
        output: &mut Vec<IntoSubpixel>,
982
0
        buffer: &[FromSubpixel],
983
0
    ) where
984
0
        FromSubpixel: ColorComponentForCicp,
985
0
        IntoSubpixel: ColorComponentForCicp + FromPrimitive<FromSubpixel> + Primitive,
986
    {
987
0
        let pixels = buffer.len() / LayoutWithColor::Rgba.channels();
988
0
        Self::create_output::<IntoSubpixel>(output, pixels, LayoutWithColor::Rgb);
989
990
0
        let map_channel = <IntoSubpixel as FromPrimitive<FromSubpixel>>::from_primitive;
991
992
0
        let buffer_chunks = buffer.as_chunks::<4>().0;
993
0
        let output_chunks = output.as_chunks_mut::<3>().0;
994
995
0
        for (&[r, g, b, _], out) in buffer_chunks.iter().zip(output_chunks) {
996
0
            *out = [map_channel(r), map_channel(g), map_channel(b)];
997
0
        }
998
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgba_to_rgb::<f32, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgba_to_rgb::<f32, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgba_to_rgb::<f32, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgba_to_rgb::<u8, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgba_to_rgb::<u8, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgba_to_rgb::<u8, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgba_to_rgb::<u16, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgba_to_rgb::<u16, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_rgba_to_rgb::<u16, u16>
999
1000
    // Note: ~50% faster than the output-based fallback in Luma8->LumaA8 and Luma8->LumaA16 codegen
1001
    // so this one stays with `flat_map` for now.
1002
0
    fn subpixel_cast_luma_to_luma_alpha<FromSubpixel, IntoSubpixel>(
1003
0
        output: &mut Vec<IntoSubpixel>,
1004
0
        buffer: &[FromSubpixel],
1005
0
    ) where
1006
0
        FromSubpixel: ColorComponentForCicp,
1007
0
        IntoSubpixel: ColorComponentForCicp + FromPrimitive<FromSubpixel> + Primitive,
1008
    {
1009
0
        let map_channel = <IntoSubpixel as FromPrimitive<FromSubpixel>>::from_primitive;
1010
1011
0
        output.extend(buffer.iter().copied().flat_map(|l| {
1012
0
            [
1013
0
                map_channel(l),
1014
0
                // Crucially inlined here. When I tried to move this out then it no longer
1015
0
                // optimizes any better than the output method. (#2804).
1016
0
                <IntoSubpixel as Primitive>::DEFAULT_MAX_VALUE,
1017
0
            ]
1018
0
        }));
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<f32, f32>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<f32, u8>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<f32, u16>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u8, f32>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u8, u8>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u8, u16>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u16, f32>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u16, u8>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u16, u16>::{closure#0}
1019
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<f32, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<f32, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<f32, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u8, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u8, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u8, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u16, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u16, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_to_luma_alpha::<u16, u16>
1020
1021
0
    fn subpixel_cast_luma_alpha_to_luma<FromSubpixel, IntoSubpixel>(
1022
0
        output: &mut Vec<IntoSubpixel>,
1023
0
        buffer: &[FromSubpixel],
1024
0
    ) where
1025
0
        FromSubpixel: ColorComponentForCicp,
1026
0
        IntoSubpixel: ColorComponentForCicp + FromPrimitive<FromSubpixel> + Primitive,
1027
    {
1028
0
        let map_channel = <IntoSubpixel as FromPrimitive<FromSubpixel>>::from_primitive;
1029
1030
0
        let buffer_chunks = buffer.as_chunks::<2>().0;
1031
1032
0
        output.extend(buffer_chunks.iter().map(|&[l, _]| map_channel(l)));
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<f32, f32>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<f32, u8>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<f32, u16>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u8, f32>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u8, u8>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u8, u16>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u16, f32>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u16, u8>::{closure#0}
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u16, u16>::{closure#0}
1033
0
    }
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<f32, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<f32, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<f32, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u8, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u8, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u8, u16>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u16, f32>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u16, u8>
Unexecuted instantiation: <image::metadata::cicp::CicpRgb>::subpixel_cast_luma_alpha_to_luma::<u16, u16>
1034
}
1035
1036
/// Color types that can be converted by [`CicpRgb::cast_pixels`].
1037
///
1038
/// This is a utility to avoid dealing with lots of bounds everywhere. In the actual implementation
1039
/// we avoid the concrete pixel types and care just about the layout (as a runtime property) and
1040
/// the channel type to be promotable into a float for normalization. If the pixels have layouts
1041
/// that are convertible with intra-channel numerics we instead try and promote the channels via
1042
/// `Primitive` instead.
1043
pub(crate) trait CicpPixelCast<FromColor>
1044
where
1045
    // Ensure we can get components from both, get the layout, and that all components are
1046
    // compatible with our intermediate connection space (rgba32f).
1047
    Self: Pixel + SealedPixelWithColorType<TransformableSubpixel = <Self as Pixel>::Subpixel>,
1048
    FromColor:
1049
        Pixel + SealedPixelWithColorType<TransformableSubpixel = <FromColor as Pixel>::Subpixel>,
1050
    Self::Subpixel: ColorComponentForCicp + FromPrimitive<FromColor::Subpixel>,
1051
    FromColor::Subpixel: ColorComponentForCicp,
1052
{
1053
}
1054
1055
impl<FromColor, IntoColor> CicpPixelCast<FromColor> for IntoColor
1056
where
1057
    IntoColor: Pixel + SealedPixelWithColorType<TransformableSubpixel = IntoColor::Subpixel>,
1058
    FromColor: Pixel + SealedPixelWithColorType<TransformableSubpixel = FromColor::Subpixel>,
1059
    IntoColor::Subpixel: ColorComponentForCicp + FromPrimitive<FromColor::Subpixel>,
1060
    FromColor::Subpixel: ColorComponentForCicp,
1061
{
1062
}
1063
1064
pub(crate) trait ColorComponentForCicp: Copy {
1065
    fn expand_to_f32(self) -> f32;
1066
1067
    fn clamp_from_f32(val: f32) -> Self;
1068
}
1069
1070
impl ColorComponentForCicp for u8 {
1071
0
    fn expand_to_f32(self) -> f32 {
1072
        const R: f32 = 1.0 / u8::MAX as f32;
1073
0
        self as f32 * R
1074
0
    }
1075
1076
    #[inline]
1077
0
    fn clamp_from_f32(val: f32) -> Self {
1078
        // Standard trick to convert f32 (0.0-1.0) to u8 (0-255) with rounding
1079
        // to nearest. `as u8` saturates, so it will clamp values outside the
1080
        // range [0.0, 1.0] to 0 or 255 (NaN is mapped to 0). `as u8` also
1081
        // truncates towards zero, so adding a bias of 0.5 before the truncation
1082
        // achieves rounding to the nearest integer.
1083
0
        (val * Self::MAX as f32 + 0.5) as u8
1084
0
    }
1085
}
1086
1087
impl ColorComponentForCicp for u16 {
1088
0
    fn expand_to_f32(self) -> f32 {
1089
        const R: f32 = 1.0 / u16::MAX as f32;
1090
0
        self as f32 * R
1091
0
    }
1092
1093
    #[inline]
1094
0
    fn clamp_from_f32(val: f32) -> Self {
1095
        // Same as for u8.
1096
0
        (val * Self::MAX as f32 + 0.5) as u16
1097
0
    }
1098
}
1099
1100
impl ColorComponentForCicp for f32 {
1101
0
    fn expand_to_f32(self) -> f32 {
1102
0
        self
1103
0
    }
1104
1105
0
    fn clamp_from_f32(val: f32) -> Self {
1106
0
        val
1107
0
    }
1108
}
1109
1110
impl<P> RgbTransforms<P> {
1111
0
    fn select_transform<O: SealedPixelWithColorType>(
1112
0
        &self,
1113
0
        into: LayoutWithColor,
1114
0
    ) -> &Arc<CicpApplicable<'static, P>> {
1115
        use crate::traits::private::{LayoutWithColor as Layout, PrivateToken};
1116
0
        let from = O::layout(PrivateToken);
1117
1118
0
        match (from, into) {
1119
0
            (Layout::Rgb, Layout::Rgb) => &self.slices[0],
1120
0
            (Layout::Rgb, Layout::Rgba) => &self.slices[1],
1121
0
            (Layout::Rgba, Layout::Rgb) => &self.slices[2],
1122
0
            (Layout::Rgba, Layout::Rgba) => &self.slices[3],
1123
0
            (Layout::Rgb, Layout::Luma) => &self.rgb_luma[0],
1124
0
            (Layout::Rgb, Layout::LumaAlpha) => &self.rgb_luma[1],
1125
0
            (Layout::Rgba, Layout::Luma) => &self.rgb_luma[2],
1126
0
            (Layout::Rgba, Layout::LumaAlpha) => &self.rgb_luma[3],
1127
0
            (Layout::Luma, Layout::Rgb) => &self.luma_rgb[0],
1128
0
            (Layout::Luma, Layout::Rgba) => &self.luma_rgb[1],
1129
0
            (Layout::LumaAlpha, Layout::Rgb) => &self.luma_rgb[2],
1130
0
            (Layout::LumaAlpha, Layout::Rgba) => &self.luma_rgb[3],
1131
0
            (Layout::Luma, Layout::Luma) => &self.luma_luma[0],
1132
0
            (Layout::Luma, Layout::LumaAlpha) => &self.luma_luma[1],
1133
0
            (Layout::LumaAlpha, Layout::Luma) => &self.luma_luma[2],
1134
0
            (Layout::LumaAlpha, Layout::LumaAlpha) => &self.luma_luma[3],
1135
        }
1136
0
    }
Unexecuted instantiation: <image::metadata::cicp::RgbTransforms<f32>>::select_transform::<image::color::Rgb<f32>>
Unexecuted instantiation: <image::metadata::cicp::RgbTransforms<f32>>::select_transform::<image::color::Rgba<f32>>
Unexecuted instantiation: <image::metadata::cicp::RgbTransforms<u8>>::select_transform::<image::color::Rgb<u8>>
Unexecuted instantiation: <image::metadata::cicp::RgbTransforms<u8>>::select_transform::<image::color::Rgba<u8>>
Unexecuted instantiation: <image::metadata::cicp::RgbTransforms<u16>>::select_transform::<image::color::Rgb<u16>>
Unexecuted instantiation: <image::metadata::cicp::RgbTransforms<u16>>::select_transform::<image::color::Rgba<u16>>
1137
}
1138
1139
impl Cicp {
1140
    /// The sRGB color space, BT.709 transfer function and D65 whitepoint.
1141
    pub const SRGB: Self = Cicp {
1142
        primaries: CicpColorPrimaries::SRgb,
1143
        transfer: CicpTransferCharacteristics::SRgb,
1144
        matrix: CicpMatrixCoefficients::Identity,
1145
        full_range: CicpVideoFullRangeFlag::FullRange,
1146
    };
1147
1148
    /// SRGB primaries and whitepoint with linear samples.
1149
    pub const SRGB_LINEAR: Self = Cicp {
1150
        primaries: CicpColorPrimaries::SRgb,
1151
        transfer: CicpTransferCharacteristics::Linear,
1152
        matrix: CicpMatrixCoefficients::Identity,
1153
        full_range: CicpVideoFullRangeFlag::FullRange,
1154
    };
1155
1156
    /// The  Display-P3 color space, a wide-gamut choice with SMPTE RP 432-2 primaries.
1157
    ///
1158
    /// Note that this modern Display P3 uses a D65 whitepoint. Use the primaries `SmpteRp431` for
1159
    /// the previous standard. The advantage of the new standard is the color system shares its
1160
    /// whitepoint with sRGB and BT.2020.
1161
    pub const DISPLAY_P3: Self = Cicp {
1162
        primaries: CicpColorPrimaries::SmpteRp432,
1163
        transfer: CicpTransferCharacteristics::SRgb,
1164
        matrix: CicpMatrixCoefficients::Identity,
1165
        full_range: CicpVideoFullRangeFlag::FullRange,
1166
    };
1167
1168
    /// Whether we have invested enough testing to ensure that color values can be assumed to be
1169
    /// stable and correspond to an intended effect, in particular if there even is a well-defined
1170
    /// meaning to these color spaces.
1171
    ///
1172
    /// For instance, our current code for the 'luma' equivalent space assumes that the color space
1173
    /// has a shared transfer function for all its color components. Also the judgment should not
1174
    /// depend on whether we can represent the profile in `moxcms` but rather if we understand the
1175
    /// profile well enough so that conversion implemented through another library can be derived.
1176
    /// (Consider the case of a builtin transform-while-encoding that may be more performant for a
1177
    /// format that does not support CICP or ICC profiles.)
1178
    ///
1179
    /// A stable profile should also have `derived_luminance` implemented.
1180
0
    pub(crate) const fn qualify_stability(&self) -> bool {
1181
        const _: () = {
1182
            // Out public constants _should_ be stable.
1183
            assert!(Cicp::SRGB.qualify_stability());
1184
            assert!(Cicp::SRGB_LINEAR.qualify_stability());
1185
            assert!(Cicp::DISPLAY_P3.qualify_stability());
1186
        };
1187
1188
0
        matches!(self.full_range, CicpVideoFullRangeFlag::FullRange)
1189
0
            && matches!(
1190
0
                self.matrix,
1191
                // For pure RGB color
1192
                CicpMatrixCoefficients::Identity
1193
                    // The equivalent of our Luma color as a type..
1194
                    | CicpMatrixCoefficients::ChromaticityDerivedNonConstant
1195
            )
1196
0
            && self.transfer.is_supported_for_transform()
1197
0
            && self.primaries.is_supported_for_transform()
1198
0
    }
1199
1200
    /// Discard matrix and range information.
1201
93.0k
    pub(crate) const fn into_rgb(self) -> CicpRgb {
1202
93.0k
        CicpRgb {
1203
93.0k
            primaries: self.primaries,
1204
93.0k
            transfer: self.transfer,
1205
93.0k
            // NOTE: if we add support for constant luminance (through the CMS having support for
1206
93.0k
            // the Luma->YCbCr->Rgb expansion natively or otherwise) then consider if we should
1207
93.0k
            // track here whether the matrix was `Identity` or `ChromaticityDerivedNonConstant` so
1208
93.0k
            // that the `ImageBuffer::color_space()` function roundtrips the value. It may be
1209
93.0k
            // important to know whether the non-constant chromaticity was an invention by `image`
1210
93.0k
            // or part of the file. The colorimetry is the same either way.
1211
93.0k
            luminance: DerivedLuminance::NonConstant,
1212
93.0k
        }
1213
93.0k
    }
1214
1215
2
    pub(crate) fn try_into_rgb(self) -> Result<CicpRgb, ImageError> {
1216
2
        if Cicp::from(self.into_rgb()) != self {
1217
2
            Err(ImageError::Parameter(ParameterError::from_kind(
1218
2
                ParameterErrorKind::RgbCicpRequired(self),
1219
2
            )))
1220
        } else {
1221
0
            Ok(self.into_rgb())
1222
        }
1223
2
    }
1224
}
1225
1226
impl CicpRgb {
1227
    /// If this CICP corresponds to a well-known named color space, return its name.
1228
0
    pub(crate) fn known_name(&self) -> Option<&'static str> {
1229
0
        match Cicp::from(*self) {
1230
0
            Cicp::SRGB => Some("sRGB"),
1231
0
            Cicp::SRGB_LINEAR => Some("sRGB Linear"),
1232
0
            Cicp::DISPLAY_P3 => Some("Display P3"),
1233
0
            _ => None,
1234
        }
1235
0
    }
1236
1237
    /// Calculate the luminance cofactors according to Rec H.273 (39) and (40).
1238
    ///
1239
    /// Returns cofactors for red, green, and blue in that order.
1240
0
    pub(crate) fn derived_luminance(&self) -> Option<[f32; 3]> {
1241
0
        let primaries = match self.primaries {
1242
0
            CicpColorPrimaries::SRgb => moxcms::ColorPrimaries::BT_709,
1243
0
            CicpColorPrimaries::RgbM => moxcms::ColorPrimaries::BT_470M,
1244
0
            CicpColorPrimaries::RgbB => moxcms::ColorPrimaries::BT_470BG,
1245
0
            CicpColorPrimaries::Bt601 => moxcms::ColorPrimaries::BT_601,
1246
0
            CicpColorPrimaries::Rgb240m => moxcms::ColorPrimaries::SMPTE_240,
1247
0
            CicpColorPrimaries::GenericFilm => moxcms::ColorPrimaries::GENERIC_FILM,
1248
0
            CicpColorPrimaries::Rgb2020 => moxcms::ColorPrimaries::BT_2020,
1249
0
            CicpColorPrimaries::Xyz => moxcms::ColorPrimaries::XYZ,
1250
0
            CicpColorPrimaries::SmpteRp431 => moxcms::ColorPrimaries::DISPLAY_P3,
1251
0
            CicpColorPrimaries::SmpteRp432 => moxcms::ColorPrimaries::DISPLAY_P3,
1252
0
            CicpColorPrimaries::Industry22 => moxcms::ColorPrimaries::EBU_3213,
1253
0
            CicpColorPrimaries::Unspecified => return None,
1254
        };
1255
1256
        const ILLUMINANT_C: moxcms::Chromaticity = moxcms::Chromaticity::new(0.310, 0.316);
1257
1258
0
        let whitepoint = match self.primaries {
1259
0
            CicpColorPrimaries::SRgb => moxcms::Chromaticity::D65,
1260
0
            CicpColorPrimaries::RgbM => ILLUMINANT_C,
1261
0
            CicpColorPrimaries::RgbB => moxcms::Chromaticity::D65,
1262
0
            CicpColorPrimaries::Bt601 => moxcms::Chromaticity::D65,
1263
0
            CicpColorPrimaries::Rgb240m => moxcms::Chromaticity::D65,
1264
0
            CicpColorPrimaries::GenericFilm => ILLUMINANT_C,
1265
0
            CicpColorPrimaries::Rgb2020 => moxcms::Chromaticity::D65,
1266
0
            CicpColorPrimaries::Xyz => moxcms::Chromaticity::new(1. / 3., 1. / 3.),
1267
0
            CicpColorPrimaries::SmpteRp431 => moxcms::Chromaticity::new(0.314, 0.351),
1268
0
            CicpColorPrimaries::SmpteRp432 => moxcms::Chromaticity::D65,
1269
0
            CicpColorPrimaries::Industry22 => moxcms::Chromaticity::D65,
1270
0
            CicpColorPrimaries::Unspecified => return None,
1271
        };
1272
1273
0
        let matrix = primaries.transform_to_xyz(whitepoint);
1274
1275
        // Our result is the Y row of this matrix.
1276
0
        Some(matrix.v[1])
1277
0
    }
1278
}
1279
1280
impl From<CicpRgb> for Cicp {
1281
2
    fn from(cicp: CicpRgb) -> Self {
1282
2
        Cicp {
1283
2
            primaries: cicp.primaries,
1284
2
            transfer: cicp.transfer,
1285
2
            matrix: CicpMatrixCoefficients::Identity,
1286
2
            full_range: CicpVideoFullRangeFlag::FullRange,
1287
2
        }
1288
2
    }
1289
}
1290
1291
#[cfg(test)]
1292
#[test]
1293
fn moxcms() {
1294
    let l = moxcms::TransferCharacteristics::Linear;
1295
    assert_eq!(l.linearize(1.0), 1.0);
1296
    assert_eq!(l.gamma(1.0), 1.0);
1297
1298
    assert_eq!(l.gamma(0.5), 0.5);
1299
}
1300
1301
#[cfg(test)]
1302
#[test]
1303
fn derived_luminance() {
1304
    let luminance = Cicp::SRGB.into_rgb().derived_luminance();
1305
    let [kr, kg, kb] = luminance.unwrap();
1306
    assert!((kr - 0.2126).abs() < 1e-4);
1307
    assert!((kg - 0.7152).abs() < 1e-4);
1308
    assert!((kb - 0.0722).abs() < 1e-4);
1309
}
1310
1311
#[cfg(test)]
1312
mod tests {
1313
    use super::{Cicp, CicpTransform};
1314
    use crate::{Luma, LumaA, Rgb, Rgba};
1315
1316
    #[test]
1317
    fn can_create_transforms() {
1318
        assert!(CicpTransform::new(Cicp::SRGB, Cicp::SRGB).is_some());
1319
        assert!(CicpTransform::new(Cicp::SRGB, Cicp::DISPLAY_P3).is_some());
1320
        assert!(CicpTransform::new(Cicp::DISPLAY_P3, Cicp::SRGB).is_some());
1321
        assert!(CicpTransform::new(Cicp::DISPLAY_P3, Cicp::DISPLAY_P3).is_some());
1322
    }
1323
1324
    fn no_coefficient_fallback() -> [f32; 3] {
1325
        panic!("Fallback coefficients required")
1326
    }
1327
1328
    #[test]
1329
    fn transform_pixels_srgb() {
1330
        // Non-constant luminance so:
1331
        // Y = dot(rgb, coefs)
1332
        let data = [255, 0, 0, 255];
1333
        let color = Cicp::SRGB.into_rgb();
1334
        let rgba = color.cast_pixels::<Rgba<u8>, Rgb<u8>>(&data, &no_coefficient_fallback);
1335
        assert_eq!(rgba, [255, 0, 0]);
1336
        let luma = color.cast_pixels::<Rgba<u8>, Luma<u8>>(&data, &no_coefficient_fallback);
1337
        assert_eq!(luma, [54]); // 255 * 0.2126
1338
        let luma_a = color.cast_pixels::<Rgba<u8>, LumaA<u8>>(&data, &no_coefficient_fallback);
1339
        assert_eq!(luma_a, [54, 255]);
1340
    }
1341
1342
    #[test]
1343
    fn transform_pixels_srgb_16() {
1344
        // Non-constant luminance so:
1345
        // Y = dot(rgb, coefs)
1346
        let data = [u16::MAX / 2];
1347
        let color = Cicp::SRGB.into_rgb();
1348
        let rgba = color.cast_pixels::<Luma<u16>, Rgb<u8>>(&data, &no_coefficient_fallback);
1349
        assert_eq!(rgba, [127; 3]);
1350
        let luma = color.cast_pixels::<Luma<u16>, Luma<u8>>(&data, &no_coefficient_fallback);
1351
        assert_eq!(luma, [127]);
1352
        let luma_a = color.cast_pixels::<Luma<u16>, LumaA<u8>>(&data, &no_coefficient_fallback);
1353
        assert_eq!(luma_a, [127, 255]);
1354
1355
        let data = [u16::MAX / 2 + 1];
1356
        let color = Cicp::SRGB.into_rgb();
1357
        let rgba = color.cast_pixels::<Luma<u16>, Rgb<u8>>(&data, &no_coefficient_fallback);
1358
        assert_eq!(rgba, [128; 3]);
1359
        let luma = color.cast_pixels::<Luma<u16>, Luma<u8>>(&data, &no_coefficient_fallback);
1360
        assert_eq!(luma, [128]);
1361
        let luma_a = color.cast_pixels::<Luma<u16>, LumaA<u8>>(&data, &no_coefficient_fallback);
1362
        assert_eq!(luma_a, [128, 255]);
1363
    }
1364
1365
    #[test]
1366
    fn transform_pixels_srgb_luma_alpha() {
1367
        // Non-constant luminance so:
1368
        // Y = dot(rgb, coefs)
1369
        let data = [u16::MAX / 2, u16::MAX];
1370
        let color = Cicp::SRGB.into_rgb();
1371
        let rgba = color.cast_pixels::<LumaA<u16>, Rgb<u8>>(&data, &no_coefficient_fallback);
1372
        assert_eq!(rgba, [127; 3]);
1373
        let luma = color.cast_pixels::<LumaA<u16>, Luma<u8>>(&data, &no_coefficient_fallback);
1374
        assert_eq!(luma, [127]);
1375
        let luma = color.cast_pixels::<LumaA<u16>, LumaA<u8>>(&data, &no_coefficient_fallback);
1376
        assert_eq!(luma, [127, u8::MAX]);
1377
        let luma_a = color.cast_pixels::<LumaA<u16>, LumaA<u8>>(&data, &no_coefficient_fallback);
1378
        assert_eq!(luma_a, [127, 255]);
1379
1380
        let data = [u16::MAX / 2 + 1, u16::MAX];
1381
        let color = Cicp::SRGB.into_rgb();
1382
        let rgba = color.cast_pixels::<LumaA<u16>, Rgb<u8>>(&data, &no_coefficient_fallback);
1383
        assert_eq!(rgba, [128; 3]);
1384
        let luma = color.cast_pixels::<LumaA<u16>, Luma<u8>>(&data, &no_coefficient_fallback);
1385
        assert_eq!(luma, [128]);
1386
        let luma = color.cast_pixels::<LumaA<u16>, LumaA<u8>>(&data, &no_coefficient_fallback);
1387
        assert_eq!(luma, [128, u8::MAX]);
1388
        let luma_a = color.cast_pixels::<LumaA<u16>, LumaA<u8>>(&data, &no_coefficient_fallback);
1389
        assert_eq!(luma_a, [128, 255]);
1390
    }
1391
}