/src/freeimage-svn/FreeImage/trunk/Source/OpenEXR/IlmImf/ImfChromaticities.cpp
Line  | Count  | Source  | 
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  |  |      | 
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  | 0  |     red (red),  | 
56  | 0  |     green (green),  | 
57  | 0  |     blue (blue),  | 
58  | 0  |     white (white)  | 
59  | 0  | { | 
60  |  |     // empty  | 
61  | 0  | }  | 
62  |  |  | 
63  |  |       | 
64  |  | bool  | 
65  |  | Chromaticities::operator == (const Chromaticities & c) const  | 
66  | 0  | { | 
67  | 0  |     return red == c.red && green == c.green && blue == c.blue;  | 
68  | 0  | }  | 
69  |  |  | 
70  |  |       | 
71  |  | bool  | 
72  |  | Chromaticities::operator != (const Chromaticities & c) const  | 
73  | 0  | { | 
74  | 0  |     return red != c.red || green != c.green || blue != c.blue;  | 
75  | 0  | }  | 
76  |  |       | 
77  |  |       | 
78  |  | IMATH_NAMESPACE::M44f  | 
79  |  | RGBtoXYZ (const Chromaticities chroma, float Y)  | 
80  | 0  | { | 
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  | 0  |     float X = chroma.white.x * Y / chroma.white.y;  | 
94  | 0  |     float Z = (1 - chroma.white.x - chroma.white.y) * Y / chroma.white.y;  | 
95  |  |  | 
96  |  |     //  | 
97  |  |     // Scale factors for matrix rows  | 
98  |  |     //  | 
99  |  | 
  | 
100  | 0  |     float d = chroma.red.x   * (chroma.blue.y  - chroma.green.y) +  | 
101  | 0  |         chroma.blue.x  * (chroma.green.y - chroma.red.y) +  | 
102  | 0  |         chroma.green.x * (chroma.red.y   - chroma.blue.y);  | 
103  |  | 
  | 
104  | 0  |     float Sr = (X * (chroma.blue.y - chroma.green.y) -  | 
105  | 0  |           chroma.green.x * (Y * (chroma.blue.y - 1) +  | 
106  | 0  |     chroma.blue.y  * (X + Z)) +  | 
107  | 0  |     chroma.blue.x  * (Y * (chroma.green.y - 1) +  | 
108  | 0  |     chroma.green.y * (X + Z))) / d;  | 
109  |  | 
  | 
110  | 0  |     float Sg = (X * (chroma.red.y - chroma.blue.y) +  | 
111  | 0  |     chroma.red.x   * (Y * (chroma.blue.y - 1) +  | 
112  | 0  |     chroma.blue.y  * (X + Z)) -  | 
113  | 0  |     chroma.blue.x  * (Y * (chroma.red.y - 1) +  | 
114  | 0  |     chroma.red.y   * (X + Z))) / d;  | 
115  |  | 
  | 
116  | 0  |     float Sb = (X * (chroma.green.y - chroma.red.y) -  | 
117  | 0  |     chroma.red.x   * (Y * (chroma.green.y - 1) +  | 
118  | 0  |     chroma.green.y * (X + Z)) +  | 
119  | 0  |     chroma.green.x * (Y * (chroma.red.y - 1) +  | 
120  | 0  |     chroma.red.y   * (X + Z))) / d;  | 
121  |  |  | 
122  |  |     //  | 
123  |  |     // Assemble the matrix  | 
124  |  |     //  | 
125  |  | 
  | 
126  | 0  |     IMATH_NAMESPACE::M44f M;  | 
127  |  | 
  | 
128  | 0  |     M[0][0] = Sr * chroma.red.x;  | 
129  | 0  |     M[0][1] = Sr * chroma.red.y;  | 
130  | 0  |     M[0][2] = Sr * (1 - chroma.red.x - chroma.red.y);  | 
131  |  | 
  | 
132  | 0  |     M[1][0] = Sg * chroma.green.x;  | 
133  | 0  |     M[1][1] = Sg * chroma.green.y;  | 
134  | 0  |     M[1][2] = Sg * (1 - chroma.green.x - chroma.green.y);  | 
135  |  | 
  | 
136  | 0  |     M[2][0] = Sb * chroma.blue.x;  | 
137  | 0  |     M[2][1] = Sb * chroma.blue.y;  | 
138  | 0  |     M[2][2] = Sb * (1 - chroma.blue.x - chroma.blue.y);  | 
139  |  | 
  | 
140  | 0  |     return M;  | 
141  | 0  | }  | 
142  |  |  | 
143  |  |  | 
144  |  | IMATH_NAMESPACE::M44f  | 
145  |  | XYZtoRGB (const Chromaticities chroma, float Y)  | 
146  | 0  | { | 
147  | 0  |     return RGBtoXYZ (chroma, Y).inverse();  | 
148  | 0  | }  | 
149  |  |  | 
150  |  |  | 
151  |  | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT  |