transfer_function.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // ======================================================================== //
  2. // Copyright 2009-2019 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #include "transfer_function.h"
  17. namespace oidn {
  18. const float LogTransferFunction::xScale = 1.f / log(LogTransferFunction::yMax + 1.f);
  19. const float PQXTransferFunction::xScale = 1.f / PQXTransferFunction::pqxForward(PQXTransferFunction::yMax * PQXTransferFunction::yScale);
  20. float AutoexposureNode::autoexposure(const Image& color)
  21. {
  22. assert(color.format == Format::Float3);
  23. // -- GODOT start --
  24. // We don't want to mess with TTB and we don't use autoexposure, so we disable this code
  25. #if 0
  26. // -- GODOT end --
  27. constexpr float key = 0.18f;
  28. constexpr float eps = 1e-8f;
  29. constexpr int K = 16; // downsampling amount
  30. // Downsample the image to minimize sensitivity to noise
  31. const int H = color.height; // original height
  32. const int W = color.width; // original width
  33. const int HK = (H + K/2) / K; // downsampled height
  34. const int WK = (W + K/2) / K; // downsampled width
  35. // Compute the average log luminance of the downsampled image
  36. using Sum = std::pair<float, int>;
  37. Sum sum =
  38. tbb::parallel_reduce(
  39. tbb::blocked_range2d<int>(0, HK, 0, WK),
  40. Sum(0.f, 0),
  41. [&](const tbb::blocked_range2d<int>& r, Sum sum) -> Sum
  42. {
  43. // Iterate over blocks
  44. for (int i = r.rows().begin(); i != r.rows().end(); ++i)
  45. {
  46. for (int j = r.cols().begin(); j != r.cols().end(); ++j)
  47. {
  48. // Compute the average luminance in the current block
  49. const int beginH = int(ptrdiff_t(i) * H / HK);
  50. const int beginW = int(ptrdiff_t(j) * W / WK);
  51. const int endH = int(ptrdiff_t(i+1) * H / HK);
  52. const int endW = int(ptrdiff_t(j+1) * W / WK);
  53. float L = 0.f;
  54. for (int h = beginH; h < endH; ++h)
  55. {
  56. for (int w = beginW; w < endW; ++w)
  57. {
  58. const float* rgb = (const float*)color.get(h, w);
  59. const float r = maxSafe(rgb[0], 0.f);
  60. const float g = maxSafe(rgb[1], 0.f);
  61. const float b = maxSafe(rgb[2], 0.f);
  62. L += luminance(r, g, b);
  63. }
  64. }
  65. L /= (endH - beginH) * (endW - beginW);
  66. // Accumulate the log luminance
  67. if (L > eps)
  68. {
  69. sum.first += log2(L);
  70. sum.second++;
  71. }
  72. }
  73. }
  74. return sum;
  75. },
  76. [](Sum a, Sum b) -> Sum { return Sum(a.first+b.first, a.second+b.second); },
  77. tbb::static_partitioner()
  78. );
  79. return (sum.second > 0) ? (key / exp2(sum.first / float(sum.second))) : 1.f;
  80. // -- GODOT start --
  81. #endif
  82. return 1.0;
  83. // -- GODOT end --
  84. }
  85. } // namespace oidn