Coverage Report

Created: 2026-09-03 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openexr/src/lib/OpenEXR/ImfChromaticities.cpp
Line
Count
Source
1
//
2
// SPDX-License-Identifier: BSD-3-Clause
3
// Copyright (c) Contributors to the OpenEXR Project.
4
//
5
6
//-----------------------------------------------------------------------------
7
//
8
//  CIE (x,y) chromaticities, and conversions between
9
//  RGB triples and CIE XYZ tristimulus values.
10
//
11
//-----------------------------------------------------------------------------
12
13
#include "ImfNamespace.h"
14
#include "ImfChromaticities.h"
15
#include <string.h>
16
17
#include <float.h>
18
#include <stdexcept>
19
20
#if defined(_MSC_VER)
21
// suppress warning about non-exported base classes
22
#    pragma warning(disable : 4251)
23
#endif
24
25
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
26
27
Chromaticities::Chromaticities (
28
    const IMATH_NAMESPACE::V2f& red,
29
    const IMATH_NAMESPACE::V2f& green,
30
    const IMATH_NAMESPACE::V2f& blue,
31
    const IMATH_NAMESPACE::V2f& white)
32
3.54k
    : red (red), green (green), blue (blue), white (white)
33
3.54k
{
34
    // empty
35
3.54k
}
36
37
bool
38
Chromaticities::operator== (const Chromaticities& c) const
39
0
{
40
0
    return red == c.red && green == c.green && blue == c.blue &&
41
0
           white == c.white;
42
0
}
43
44
bool
45
Chromaticities::operator!= (const Chromaticities& c) const
46
0
{
47
0
    return red != c.red || green != c.green || blue != c.blue ||
48
0
           white != c.white;
49
0
}
50
51
IMATH_NAMESPACE::M44f
52
RGBtoXYZ (const Chromaticities& chroma, float Y)
53
0
{
54
    //
55
    // For an explanation of how the color conversion matrix is derived,
56
    // see Roy Hall, "Illumination and Color in Computer Generated Imagery",
57
    // Springer-Verlag, 1989, chapter 3, "Perceptual Response"; and
58
    // Charles A. Poynton, "A Technical Introduction to Digital Video",
59
    // John Wiley & Sons, 1996, chapter 7, "Color science for video".
60
    //
61
62
    //
63
    // X and Z values of RGB value (1, 1, 1), or "white"
64
    //
65
66
    // prevent a division that rounds to zero
67
0
    if (std::abs (chroma.white.y) <= 1.f &&
68
0
        std::abs (chroma.white.x * Y) >= std::abs (chroma.white.y) * FLT_MAX)
69
0
    {
70
0
        throw std::invalid_argument (
71
0
            "Bad chromaticities: white.y cannot be zero");
72
0
    }
73
74
0
    float X = chroma.white.x * Y / chroma.white.y;
75
0
    float Z = (1 - chroma.white.x - chroma.white.y) * Y / chroma.white.y;
76
77
    //
78
    // Scale factors for matrix rows, compute numerators and common denominator
79
    //
80
81
0
    float d = chroma.red.x * (chroma.blue.y - chroma.green.y) +
82
0
              chroma.blue.x * (chroma.green.y - chroma.red.y) +
83
0
              chroma.green.x * (chroma.red.y - chroma.blue.y);
84
85
0
    float SrN =
86
0
        (X * (chroma.blue.y - chroma.green.y) -
87
0
         chroma.green.x * (Y * (chroma.blue.y - 1) + chroma.blue.y * (X + Z)) +
88
0
         chroma.blue.x * (Y * (chroma.green.y - 1) + chroma.green.y * (X + Z)));
89
90
0
    float SgN =
91
0
        (X * (chroma.red.y - chroma.blue.y) +
92
0
         chroma.red.x * (Y * (chroma.blue.y - 1) + chroma.blue.y * (X + Z)) -
93
0
         chroma.blue.x * (Y * (chroma.red.y - 1) + chroma.red.y * (X + Z)));
94
95
0
    float SbN =
96
0
        (X * (chroma.green.y - chroma.red.y) -
97
0
         chroma.red.x * (Y * (chroma.green.y - 1) + chroma.green.y * (X + Z)) +
98
0
         chroma.green.x * (Y * (chroma.red.y - 1) + chroma.red.y * (X + Z)));
99
100
0
    if (std::abs (d) < 1.f && (std::abs (SrN) >= std::abs (d) * FLT_MAX ||
101
0
                               std::abs (SgN) >= std::abs (d) * FLT_MAX ||
102
0
                               std::abs (SbN) >= std::abs (d) * FLT_MAX))
103
0
    {
104
        // cannot generate matrix if all RGB primaries have the same y value
105
        // or if they all have the an x value of zero
106
        // in both cases, the primaries are colinear, which makes them unusable
107
0
        throw std::invalid_argument (
108
0
            "Bad chromaticities: RGBtoXYZ matrix is degenerate");
109
0
    }
110
111
0
    float Sr = SrN / d;
112
0
    float Sg = SgN / d;
113
0
    float Sb = SbN / d;
114
115
    //
116
    // Assemble the matrix
117
    //
118
119
0
    IMATH_NAMESPACE::M44f M;
120
121
0
    M[0][0] = Sr * chroma.red.x;
122
0
    M[0][1] = Sr * chroma.red.y;
123
0
    M[0][2] = Sr * (1 - chroma.red.x - chroma.red.y);
124
125
0
    M[1][0] = Sg * chroma.green.x;
126
0
    M[1][1] = Sg * chroma.green.y;
127
0
    M[1][2] = Sg * (1 - chroma.green.x - chroma.green.y);
128
129
0
    M[2][0] = Sb * chroma.blue.x;
130
0
    M[2][1] = Sb * chroma.blue.y;
131
0
    M[2][2] = Sb * (1 - chroma.blue.x - chroma.blue.y);
132
133
0
    return M;
134
0
}
135
136
IMATH_NAMESPACE::M44f
137
XYZtoRGB (const Chromaticities& chroma, float Y)
138
0
{
139
0
    return RGBtoXYZ (chroma, Y).inverse ();
140
0
}
141
142
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT