ImfLut.cpp 4.5 KB

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