ImfLut.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IMF_LUT_H
  6. #define INCLUDED_IMF_LUT_H
  7. //-----------------------------------------------------------------------------
  8. //
  9. // Lookup tables for efficient application
  10. // of half --> half functions to pixel data,
  11. // and some commonly applied functions.
  12. //
  13. //-----------------------------------------------------------------------------
  14. #include "ImfExport.h"
  15. #include "ImfNamespace.h"
  16. #include "ImfRgbaFile.h"
  17. #include <ImathBox.h>
  18. #include <halfFunction.h>
  19. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  20. //
  21. // Lookup table for individual half channels.
  22. //
  23. class HalfLut
  24. {
  25. public:
  26. //------------
  27. // Constructor
  28. //------------
  29. template <class Function>
  30. HalfLut (Function f);
  31. //----------------------------------------------------------------------
  32. // Apply the table to data[0], data[stride] ... data[(nData-1) * stride]
  33. //----------------------------------------------------------------------
  34. IMF_EXPORT
  35. void apply (half *data,
  36. int nData,
  37. int stride = 1) const;
  38. //---------------------------------------------------------------
  39. // Apply the table to a frame buffer slice (see ImfFrameBuffer.h)
  40. //---------------------------------------------------------------
  41. IMF_EXPORT
  42. void apply (const Slice &data,
  43. const IMATH_NAMESPACE::Box2i &dataWindow) const;
  44. private:
  45. halfFunction <half> _lut;
  46. };
  47. //
  48. // Lookup table for combined RGBA data.
  49. //
  50. class RgbaLut
  51. {
  52. public:
  53. //------------
  54. // Constructor
  55. //------------
  56. template <class Function>
  57. RgbaLut (Function f, RgbaChannels chn = WRITE_RGB);
  58. //----------------------------------------------------------------------
  59. // Apply the table to data[0], data[stride] ... data[(nData-1) * stride]
  60. //----------------------------------------------------------------------
  61. IMF_EXPORT
  62. void apply (Rgba *data,
  63. int nData,
  64. int stride = 1) const;
  65. //-----------------------------------------------------------------------
  66. // Apply the table to a frame buffer (see RgbaOutpuFile.setFrameBuffer())
  67. //-----------------------------------------------------------------------
  68. IMF_EXPORT
  69. void apply (Rgba *base,
  70. int xStride,
  71. int yStride,
  72. const IMATH_NAMESPACE::Box2i &dataWindow) const;
  73. private:
  74. halfFunction <half> _lut;
  75. RgbaChannels _chn;
  76. };
  77. //
  78. // 12bit log rounding reduces data to 20 stops with 200 steps per stop.
  79. // That makes 4000 numbers. An extra 96 just come along for the ride.
  80. // Zero explicitly remains zero. The first non-zero half will map to 1
  81. // in the 0-4095 12log space. A nice power of two number is placed at
  82. // the center [2000] and that number is near 0.18.
  83. //
  84. IMF_EXPORT
  85. half round12log (half x);
  86. //
  87. // Round to n-bit precision (n should be between 0 and 10).
  88. // After rounding, the significand's 10-n least significant
  89. // bits will be zero.
  90. //
  91. struct roundNBit
  92. {
  93. roundNBit (int n): n(n) {}
  94. half operator () (half x) {return x.round(n);}
  95. int n;
  96. };
  97. //
  98. // Template definitions
  99. //
  100. template <class Function>
  101. HalfLut::HalfLut (Function f):
  102. _lut(f, -HALF_MAX, HALF_MAX, half (0),
  103. half::posInf(), half::negInf(), half::qNan())
  104. {
  105. // empty
  106. }
  107. template <class Function>
  108. RgbaLut::RgbaLut (Function f, RgbaChannels chn):
  109. _lut(f, -HALF_MAX, HALF_MAX, half (0),
  110. half::posInf(), half::negInf(), half::qNan()),
  111. _chn(chn)
  112. {
  113. // empty
  114. }
  115. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  116. #endif