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

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 176 lines · 101 code · 35 blank · 40 comment · 22 complexity · 96b62f106d37a86c50f077cbcb44b775 MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, 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. // Lookup tables for efficient application
  37. // of half --> half functions to pixel data,
  38. // and some commonly applied functions.
  39. //
  40. //-----------------------------------------------------------------------------
  41. #include <ImfLut.h>
  42. #include <math.h>
  43. #include <assert.h>
  44. namespace Imf {
  45. void
  46. HalfLut::apply (half *data, int nData, int stride) const
  47. {
  48. while (nData)
  49. {
  50. *data = _lut (*data);
  51. data += stride;
  52. nData -= 1;
  53. }
  54. }
  55. void
  56. HalfLut::apply (const Slice &data, const Imath::Box2i &dataWindow) const
  57. {
  58. assert (data.type == HALF);
  59. assert (dataWindow.min.x % data.xSampling == 0);
  60. assert (dataWindow.min.y % data.ySampling == 0);
  61. assert ((dataWindow.max.x - dataWindow.min.x + 1) % data.xSampling == 0);
  62. assert ((dataWindow.max.y - dataWindow.min.y + 1) % data.ySampling == 0);
  63. char *base = data.base + data.yStride *
  64. (dataWindow.min.y / data.ySampling);
  65. for (int y = dataWindow.min.y;
  66. y <= dataWindow.max.y;
  67. y += data.ySampling)
  68. {
  69. char *pixel = base + data.xStride *
  70. (dataWindow.min.x / data.xSampling);
  71. for (int x = dataWindow.min.x;
  72. x <= dataWindow.max.x;
  73. x += data.xSampling)
  74. {
  75. *(half *)pixel = _lut (*(half *)pixel);
  76. pixel += data.xStride;
  77. }
  78. base += data.yStride;
  79. }
  80. }
  81. void
  82. RgbaLut::apply (Rgba *data, int nData, int stride) const
  83. {
  84. while (nData)
  85. {
  86. if (_chn & WRITE_R)
  87. data->r = _lut (data->r);
  88. if (_chn & WRITE_G)
  89. data->g = _lut (data->g);
  90. if (_chn & WRITE_B)
  91. data->b = _lut (data->b);
  92. if (_chn & WRITE_A)
  93. data->a = _lut (data->a);
  94. data += stride;
  95. nData -= 1;
  96. }
  97. }
  98. void
  99. RgbaLut::apply (Rgba *base,
  100. int xStride, int yStride,
  101. const Imath::Box2i &dataWindow) const
  102. {
  103. base += dataWindow.min.y * yStride;
  104. for (int y = dataWindow.min.y; y <= dataWindow.max.y; ++y)
  105. {
  106. Rgba *pixel = base + dataWindow.min.x * xStride;
  107. for (int x = dataWindow.min.x; x <= dataWindow.max.x; ++x)
  108. {
  109. if (_chn & WRITE_R)
  110. pixel->r = _lut (pixel->r);
  111. if (_chn & WRITE_G)
  112. pixel->g = _lut (pixel->g);
  113. if (_chn & WRITE_B)
  114. pixel->b = _lut (pixel->b);
  115. if (_chn & WRITE_A)
  116. pixel->a = _lut (pixel->a);
  117. pixel += xStride;
  118. }
  119. base += yStride;
  120. }
  121. }
  122. half
  123. round12log (half x)
  124. {
  125. const float middleval = pow (2.0, -2.5);
  126. int int12log;
  127. if (x <= 0)
  128. {
  129. return 0;
  130. }
  131. else
  132. {
  133. int12log = int (2000.5 + 200.0 * log (x / middleval) / log (2.0));
  134. if (int12log > 4095)
  135. int12log = 4095;
  136. if (int12log < 1)
  137. int12log = 1;
  138. }
  139. return middleval * pow (2.0, (int12log - 2000.0) / 200.0);
  140. }
  141. } // namespace Imf