/src/FreeImage/Source/OpenEXR/IlmImf/ImfChromaticities.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 135 lines · 54 code · 25 blank · 56 comment · 0 complexity · 792a376eb41324ebf75882d62c131529 MD5 · raw file

  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. // CIE (x,y) chromaticities, and conversions between
  37. // RGB tiples and CIE XYZ tristimulus values.
  38. //
  39. //-----------------------------------------------------------------------------
  40. #include <ImfChromaticities.h>
  41. namespace Imf {
  42. Chromaticities::Chromaticities (const Imath::V2f &red,
  43. const Imath::V2f &green,
  44. const Imath::V2f &blue,
  45. const Imath::V2f &white)
  46. :
  47. red (red),
  48. green (green),
  49. blue (blue),
  50. white (white)
  51. {
  52. // empty
  53. }
  54. Imath::M44f
  55. RGBtoXYZ (const Chromaticities chroma, float Y)
  56. {
  57. //
  58. // For an explanation of how the color conversion matrix is derived,
  59. // see Roy Hall, "Illumination and Color in Computer Generated Imagery",
  60. // Springer-Verlag, 1989, chapter 3, "Perceptual Response"; and
  61. // Charles A. Poynton, "A Technical Introduction to Digital Video",
  62. // John Wiley & Sons, 1996, chapter 7, "Color science for video".
  63. //
  64. //
  65. // X and Z values of RGB value (1, 1, 1), or "white"
  66. //
  67. float X = chroma.white.x * Y / chroma.white.y;
  68. float Z = (1 - chroma.white.x - chroma.white.y) * Y / chroma.white.y;
  69. //
  70. // Scale factors for matrix rows
  71. //
  72. float d = chroma.red.x * (chroma.blue.y - chroma.green.y) +
  73. chroma.blue.x * (chroma.green.y - chroma.red.y) +
  74. chroma.green.x * (chroma.red.y - chroma.blue.y);
  75. float Sr = (X * (chroma.blue.y - chroma.green.y) -
  76. chroma.green.x * (Y * (chroma.blue.y - 1) +
  77. chroma.blue.y * (X + Z)) +
  78. chroma.blue.x * (Y * (chroma.green.y - 1) +
  79. chroma.green.y * (X + Z))) / d;
  80. float Sg = (X * (chroma.red.y - chroma.blue.y) +
  81. chroma.red.x * (Y * (chroma.blue.y - 1) +
  82. chroma.blue.y * (X + Z)) -
  83. chroma.blue.x * (Y * (chroma.red.y - 1) +
  84. chroma.red.y * (X + Z))) / d;
  85. float Sb = (X * (chroma.green.y - chroma.red.y) -
  86. chroma.red.x * (Y * (chroma.green.y - 1) +
  87. chroma.green.y * (X + Z)) +
  88. chroma.green.x * (Y * (chroma.red.y - 1) +
  89. chroma.red.y * (X + Z))) / d;
  90. //
  91. // Assemble the matrix
  92. //
  93. Imath::M44f M;
  94. M[0][0] = Sr * chroma.red.x;
  95. M[0][1] = Sr * chroma.red.y;
  96. M[0][2] = Sr * (1 - chroma.red.x - chroma.red.y);
  97. M[1][0] = Sg * chroma.green.x;
  98. M[1][1] = Sg * chroma.green.y;
  99. M[1][2] = Sg * (1 - chroma.green.x - chroma.green.y);
  100. M[2][0] = Sb * chroma.blue.x;
  101. M[2][1] = Sb * chroma.blue.y;
  102. M[2][2] = Sb * (1 - chroma.blue.x - chroma.blue.y);
  103. return M;
  104. }
  105. Imath::M44f
  106. XYZtoRGB (const Chromaticities chroma, float Y)
  107. {
  108. return RGBtoXYZ (chroma, Y).inverse();
  109. }
  110. } // namespace Imf