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 45 namespace Imf { 46 47 48 Chromaticities::Chromaticities (const Imath::V2f &red, 49 const Imath::V2f &green, 50 const Imath::V2f &blue, 51 const Imath::V2f &white) 52 : 53 red (red), 54 green (green), 55 blue (blue), 56 white (white) 57 { 58 // empty 59 } 60 61 62 Imath::M44f 63 RGBtoXYZ (const Chromaticities chroma, float Y) 64 { 65 // 66 // For an explanation of how the color conversion matrix is derived, 67 // see Roy Hall, "Illumination and Color in Computer Generated Imagery", 68 // Springer-Verlag, 1989, chapter 3, "Perceptual Response"; and 69 // Charles A. Poynton, "A Technical Introduction to Digital Video", 70 // John Wiley & Sons, 1996, chapter 7, "Color science for video". 71 // 72 73 // 74 // X and Z values of RGB value (1, 1, 1), or "white" 75 // 76 77 float X = chroma.white.x * Y / chroma.white.y; 78 float Z = (1 - chroma.white.x - chroma.white.y) * Y / chroma.white.y; 79 80 // 81 // Scale factors for matrix rows 82 // 83 84 float d = chroma.red.x * (chroma.blue.y - chroma.green.y) + 85 chroma.blue.x * (chroma.green.y - chroma.red.y) + 86 chroma.green.x * (chroma.red.y - chroma.blue.y); 87 88 float Sr = (X * (chroma.blue.y - chroma.green.y) - 89 chroma.green.x * (Y * (chroma.blue.y - 1) + 90 chroma.blue.y * (X + Z)) + 91 chroma.blue.x * (Y * (chroma.green.y - 1) + 92 chroma.green.y * (X + Z))) / d; 93 94 float Sg = (X * (chroma.red.y - chroma.blue.y) + 95 chroma.red.x * (Y * (chroma.blue.y - 1) + 96 chroma.blue.y * (X + Z)) - 97 chroma.blue.x * (Y * (chroma.red.y - 1) + 98 chroma.red.y * (X + Z))) / d; 99 100 float Sb = (X * (chroma.green.y - chroma.red.y) - 101 chroma.red.x * (Y * (chroma.green.y - 1) + 102 chroma.green.y * (X + Z)) + 103 chroma.green.x * (Y * (chroma.red.y - 1) + 104 chroma.red.y * (X + Z))) / d; 105 106 // 107 // Assemble the matrix 108 // 109 110 Imath::M44f M; 111 112 M[0][0] = Sr * chroma.red.x; 113 M[0][1] = Sr * chroma.red.y; 114 M[0][2] = Sr * (1 - chroma.red.x - chroma.red.y); 115 116 M[1][0] = Sg * chroma.green.x; 117 M[1][1] = Sg * chroma.green.y; 118 M[1][2] = Sg * (1 - chroma.green.x - chroma.green.y); 119 120 M[2][0] = Sb * chroma.blue.x; 121 M[2][1] = Sb * chroma.blue.y; 122 M[2][2] = Sb * (1 - chroma.blue.x - chroma.blue.y); 123 124 return M; 125 } 126 127 128 Imath::M44f 129 XYZtoRGB (const Chromaticities chroma, float Y) 130 { 131 return RGBtoXYZ (chroma, Y).inverse(); 132 } 133 134 135 } // namespace Imf 136