1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2003, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // *       Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // *       Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // *       Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 //-----------------------------------------------------------------------------
37 //
38 //	CIE (x,y) chromaticities, and conversions between
39 //	RGB tiples and CIE XYZ tristimulus values.
40 //
41 //-----------------------------------------------------------------------------
42 
43 #include <ImfChromaticities.h>
44 #include "ImfNamespace.h"
45 #include <string.h>
46 
47 OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
48 
49 
Chromaticities(const IMATH_NAMESPACE::V2f & red,const IMATH_NAMESPACE::V2f & green,const IMATH_NAMESPACE::V2f & blue,const IMATH_NAMESPACE::V2f & white)50 Chromaticities::Chromaticities (const IMATH_NAMESPACE::V2f &red,
51 				const IMATH_NAMESPACE::V2f &green,
52 				const IMATH_NAMESPACE::V2f &blue,
53 				const IMATH_NAMESPACE::V2f &white)
54 :
55     red (red),
56     green (green),
57     blue (blue),
58     white (white)
59 {
60     // empty
61 }
62 
63 
64 bool
operator ==(const Chromaticities & c) const65 Chromaticities::operator == (const Chromaticities & c) const
66 {
67     return red == c.red && green == c.green && blue == c.blue;
68 }
69 
70 
71 bool
operator !=(const Chromaticities & c) const72 Chromaticities::operator != (const Chromaticities & c) const
73 {
74     return red != c.red || green != c.green || blue != c.blue;
75 }
76 
77 
78 IMATH_NAMESPACE::M44f
RGBtoXYZ(const Chromaticities chroma,float Y)79 RGBtoXYZ (const Chromaticities chroma, float Y)
80 {
81     //
82     // For an explanation of how the color conversion matrix is derived,
83     // see Roy Hall, "Illumination and Color in Computer Generated Imagery",
84     // Springer-Verlag, 1989, chapter 3, "Perceptual Response"; and
85     // Charles A. Poynton, "A Technical Introduction to Digital Video",
86     // John Wiley & Sons, 1996, chapter 7, "Color science for video".
87     //
88 
89     //
90     // X and Z values of RGB value (1, 1, 1), or "white"
91     //
92 
93     float X = chroma.white.x * Y / chroma.white.y;
94     float Z = (1 - chroma.white.x - chroma.white.y) * Y / chroma.white.y;
95 
96     //
97     // Scale factors for matrix rows
98     //
99 
100     float d = chroma.red.x   * (chroma.blue.y  - chroma.green.y) +
101 	      chroma.blue.x  * (chroma.green.y - chroma.red.y) +
102 	      chroma.green.x * (chroma.red.y   - chroma.blue.y);
103 
104     float Sr = (X * (chroma.blue.y - chroma.green.y) -
105 	        chroma.green.x * (Y * (chroma.blue.y - 1) +
106 		chroma.blue.y  * (X + Z)) +
107 		chroma.blue.x  * (Y * (chroma.green.y - 1) +
108 		chroma.green.y * (X + Z))) / d;
109 
110     float Sg = (X * (chroma.red.y - chroma.blue.y) +
111 		chroma.red.x   * (Y * (chroma.blue.y - 1) +
112 		chroma.blue.y  * (X + Z)) -
113 		chroma.blue.x  * (Y * (chroma.red.y - 1) +
114 		chroma.red.y   * (X + Z))) / d;
115 
116     float Sb = (X * (chroma.green.y - chroma.red.y) -
117 		chroma.red.x   * (Y * (chroma.green.y - 1) +
118 		chroma.green.y * (X + Z)) +
119 		chroma.green.x * (Y * (chroma.red.y - 1) +
120 		chroma.red.y   * (X + Z))) / d;
121 
122     //
123     // Assemble the matrix
124     //
125 
126     IMATH_NAMESPACE::M44f M;
127 
128     M[0][0] = Sr * chroma.red.x;
129     M[0][1] = Sr * chroma.red.y;
130     M[0][2] = Sr * (1 - chroma.red.x - chroma.red.y);
131 
132     M[1][0] = Sg * chroma.green.x;
133     M[1][1] = Sg * chroma.green.y;
134     M[1][2] = Sg * (1 - chroma.green.x - chroma.green.y);
135 
136     M[2][0] = Sb * chroma.blue.x;
137     M[2][1] = Sb * chroma.blue.y;
138     M[2][2] = Sb * (1 - chroma.blue.x - chroma.blue.y);
139 
140     return M;
141 }
142 
143 
144 IMATH_NAMESPACE::M44f
XYZtoRGB(const Chromaticities chroma,float Y)145 XYZtoRGB (const Chromaticities chroma, float Y)
146 {
147     return RGBtoXYZ (chroma, Y).inverse();
148 }
149 
150 
151 OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT
152