transfer_function.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. constexpr float key = 0.18f;
  24. constexpr float eps = 1e-8f;
  25. constexpr int K = 16; // downsampling amount
  26. // Downsample the image to minimize sensitivity to noise
  27. const int H = color.height; // original height
  28. const int W = color.width; // original width
  29. const int HK = (H + K/2) / K; // downsampled height
  30. const int WK = (W + K/2) / K; // downsampled width
  31. // Compute the average log luminance of the downsampled image
  32. using Sum = std::pair<float, int>;
  33. // -- GODOT start --
  34. // Sum sum =
  35. // tbb::parallel_reduce(
  36. // tbb::blocked_range2d<int>(0, HK, 0, WK),
  37. // Sum(0.f, 0),
  38. // [&](const tbb::blocked_range2d<int>& r, Sum sum) -> Sum
  39. // {
  40. // // Iterate over blocks
  41. // for (int i = r.rows().begin(); i != r.rows().end(); ++i)
  42. // {
  43. // for (int j = r.cols().begin(); j != r.cols().end(); ++j)
  44. // {
  45. Sum sum = Sum(0.0f, 0);
  46. for (int i = 0; i != HK; ++i)
  47. {
  48. for (int j = 0; j != WK; ++j)
  49. {
  50. // Compute the average luminance in the current block
  51. const int beginH = int(ptrdiff_t(i) * H / HK);
  52. const int beginW = int(ptrdiff_t(j) * W / WK);
  53. const int endH = int(ptrdiff_t(i+1) * H / HK);
  54. const int endW = int(ptrdiff_t(j+1) * W / WK);
  55. float L = 0.f;
  56. for (int h = beginH; h < endH; ++h)
  57. {
  58. for (int w = beginW; w < endW; ++w)
  59. {
  60. const float* rgb = (const float*)color.get(h, w);
  61. const float r = maxSafe(rgb[0], 0.f);
  62. const float g = maxSafe(rgb[1], 0.f);
  63. const float b = maxSafe(rgb[2], 0.f);
  64. L += luminance(r, g, b);
  65. }
  66. }
  67. L /= (endH - beginH) * (endW - beginW);
  68. // Accumulate the log luminance
  69. if (L > eps)
  70. {
  71. sum.first += log2(L);
  72. sum.second++;
  73. }
  74. }
  75. }
  76. // return sum;
  77. // },
  78. // [](Sum a, Sum b) -> Sum { return Sum(a.first+b.first, a.second+b.second); },
  79. // tbb::static_partitioner()
  80. // );
  81. // -- GODOT end --
  82. return (sum.second > 0) ? (key / exp2(sum.first / float(sum.second))) : 1.f;
  83. }
  84. } // namespace oidn