Path: blob/master/3rdparty/openexr/IlmImf/ImfChromaticities.cpp
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2003, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////333435//-----------------------------------------------------------------------------36//37// CIE (x,y) chromaticities, and conversions between38// RGB tiples and CIE XYZ tristimulus values.39//40//-----------------------------------------------------------------------------4142#include <ImfChromaticities.h>4344namespace Imf {454647Chromaticities::Chromaticities (const Imath::V2f &red,48const Imath::V2f &green,49const Imath::V2f &blue,50const Imath::V2f &white)51:52red (red),53green (green),54blue (blue),55white (white)56{57// empty58}596061Imath::M44f62RGBtoXYZ (const Chromaticities chroma, float Y)63{64//65// For an explanation of how the color conversion matrix is derived,66// see Roy Hall, "Illumination and Color in Computer Generated Imagery",67// Springer-Verlag, 1989, chapter 3, "Perceptual Response"; and68// Charles A. Poynton, "A Technical Introduction to Digital Video",69// John Wiley & Sons, 1996, chapter 7, "Color science for video".70//7172//73// X and Z values of RGB value (1, 1, 1), or "white"74//7576float X = chroma.white.x * Y / chroma.white.y;77float Z = (1 - chroma.white.x - chroma.white.y) * Y / chroma.white.y;7879//80// Scale factors for matrix rows81//8283float d = chroma.red.x * (chroma.blue.y - chroma.green.y) +84chroma.blue.x * (chroma.green.y - chroma.red.y) +85chroma.green.x * (chroma.red.y - chroma.blue.y);8687float Sr = (X * (chroma.blue.y - chroma.green.y) -88chroma.green.x * (Y * (chroma.blue.y - 1) +89chroma.blue.y * (X + Z)) +90chroma.blue.x * (Y * (chroma.green.y - 1) +91chroma.green.y * (X + Z))) / d;9293float Sg = (X * (chroma.red.y - chroma.blue.y) +94chroma.red.x * (Y * (chroma.blue.y - 1) +95chroma.blue.y * (X + Z)) -96chroma.blue.x * (Y * (chroma.red.y - 1) +97chroma.red.y * (X + Z))) / d;9899float Sb = (X * (chroma.green.y - chroma.red.y) -100chroma.red.x * (Y * (chroma.green.y - 1) +101chroma.green.y * (X + Z)) +102chroma.green.x * (Y * (chroma.red.y - 1) +103chroma.red.y * (X + Z))) / d;104105//106// Assemble the matrix107//108109Imath::M44f M;110111M[0][0] = Sr * chroma.red.x;112M[0][1] = Sr * chroma.red.y;113M[0][2] = Sr * (1 - chroma.red.x - chroma.red.y);114115M[1][0] = Sg * chroma.green.x;116M[1][1] = Sg * chroma.green.y;117M[1][2] = Sg * (1 - chroma.green.x - chroma.green.y);118119M[2][0] = Sb * chroma.blue.x;120M[2][1] = Sb * chroma.blue.y;121M[2][2] = Sb * (1 - chroma.blue.x - chroma.blue.y);122123return M;124}125126127Imath::M44f128XYZtoRGB (const Chromaticities chroma, float Y)129{130return RGBtoXYZ (chroma, Y).inverse();131}132133134} // namespace Imf135136137