ImfLut.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #ifndef INCLUDED_IMF_LUT_H
  35. #define INCLUDED_IMF_LUT_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // Lookup tables for efficient application
  39. // of half --> half functions to pixel data,
  40. // and some commonly applied functions.
  41. //
  42. //-----------------------------------------------------------------------------
  43. #include "ImfRgbaFile.h"
  44. #include "ImfFrameBuffer.h"
  45. #include "ImathBox.h"
  46. #include "halfFunction.h"
  47. #include "ImfNamespace.h"
  48. #include "ImfExport.h"
  49. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  50. //
  51. // Lookup table for individual half channels.
  52. //
  53. class HalfLut
  54. {
  55. public:
  56. //------------
  57. // Constructor
  58. //------------
  59. template <class Function>
  60. HalfLut (Function f);
  61. //----------------------------------------------------------------------
  62. // Apply the table to data[0], data[stride] ... data[(nData-1) * stride]
  63. //----------------------------------------------------------------------
  64. IMF_EXPORT
  65. void apply (half *data,
  66. int nData,
  67. int stride = 1) const;
  68. //---------------------------------------------------------------
  69. // Apply the table to a frame buffer slice (see ImfFrameBuffer.h)
  70. //---------------------------------------------------------------
  71. IMF_EXPORT
  72. void apply (const Slice &data,
  73. const IMATH_NAMESPACE::Box2i &dataWindow) const;
  74. private:
  75. halfFunction <half> _lut;
  76. };
  77. //
  78. // Lookup table for combined RGBA data.
  79. //
  80. class RgbaLut
  81. {
  82. public:
  83. //------------
  84. // Constructor
  85. //------------
  86. template <class Function>
  87. RgbaLut (Function f, RgbaChannels chn = WRITE_RGB);
  88. //----------------------------------------------------------------------
  89. // Apply the table to data[0], data[stride] ... data[(nData-1) * stride]
  90. //----------------------------------------------------------------------
  91. IMF_EXPORT
  92. void apply (Rgba *data,
  93. int nData,
  94. int stride = 1) const;
  95. //-----------------------------------------------------------------------
  96. // Apply the table to a frame buffer (see RgbaOutpuFile.setFrameBuffer())
  97. //-----------------------------------------------------------------------
  98. IMF_EXPORT
  99. void apply (Rgba *base,
  100. int xStride,
  101. int yStride,
  102. const IMATH_NAMESPACE::Box2i &dataWindow) const;
  103. private:
  104. halfFunction <half> _lut;
  105. RgbaChannels _chn;
  106. };
  107. //
  108. // 12bit log rounding reduces data to 20 stops with 200 steps per stop.
  109. // That makes 4000 numbers. An extra 96 just come along for the ride.
  110. // Zero explicitly remains zero. The first non-zero half will map to 1
  111. // in the 0-4095 12log space. A nice power of two number is placed at
  112. // the center [2000] and that number is near 0.18.
  113. //
  114. IMF_EXPORT
  115. half round12log (half x);
  116. //
  117. // Round to n-bit precision (n should be between 0 and 10).
  118. // After rounding, the significand's 10-n least significant
  119. // bits will be zero.
  120. //
  121. struct roundNBit
  122. {
  123. roundNBit (int n): n(n) {}
  124. half operator () (half x) {return x.round(n);}
  125. int n;
  126. };
  127. //
  128. // Template definitions
  129. //
  130. template <class Function>
  131. HalfLut::HalfLut (Function f):
  132. _lut(f, -HALF_MAX, HALF_MAX, half (0),
  133. half::posInf(), half::negInf(), half::qNan())
  134. {
  135. // empty
  136. }
  137. template <class Function>
  138. RgbaLut::RgbaLut (Function f, RgbaChannels chn):
  139. _lut(f, -HALF_MAX, HALF_MAX, half (0),
  140. half::posInf(), half::negInf(), half::qNan()),
  141. _chn(chn)
  142. {
  143. // empty
  144. }
  145. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  146. #endif