FastGaussianBlur1.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Math.h>
  9. // The algorithms here are based on solving the linear heat equation using
  10. // finite differences in scale, not in time. The following document has
  11. // a brief summary of the concept,
  12. // https://www.geometrictools.com/Documentation/FastGaussianBlur.pdf
  13. // The idea is to represent the blurred image as f(x,s) in terms of position
  14. // x and scale s. Gaussian blurring is accomplished by using the input image
  15. // I(x,s0) as the initial image (of scale s0 > 0) for the partial differential
  16. // equation
  17. // s*df/ds = s^2*Laplacian(f)
  18. // where the Laplacian operator is
  19. // Laplacian = (d/dx)^2, dimension 1
  20. // Laplacian = (d/dx)^2+(d/dy)^2, dimension 2
  21. // Laplacian = (d/dx)^2+(d/dy)^2+(d/dz)^2, dimension 3
  22. //
  23. // The term s*df/ds is approximated by
  24. // s*df(x,s)/ds = (f(x,b*s)-f(x,s))/ln(b)
  25. // for b > 1, but close to 1, where ln(b) is the natural logarithm of b. If
  26. // you take the limit of the right-hand side as b approaches 1, you get the
  27. // left-hand side.
  28. //
  29. // The term s^2*((d/dx)^2)f is approximated by
  30. // s^2*((d/dx)^2)f = (f(x+h*s,s)-2*f(x,s)+f(x-h*s,s))/h^2
  31. // for h > 0, but close to zero.
  32. //
  33. // Equating the approximations for the left-hand side and the right-hand side
  34. // of the partial differential equation leads to the numerical method used in
  35. // this code.
  36. //
  37. // For iterative application of these functions, the caller is responsible
  38. // for constructing a geometric sequence of scales,
  39. // s0, s1 = s0*b, s2 = s1*b = s0*b^2, ...
  40. // where the base b satisfies 1 < b < exp(0.5*d) where d is the dimension of
  41. // the image. The upper bound on b guarantees stability of the finite
  42. // difference method used to approximate the partial differential equation.
  43. // The method assumes a pixel size of h = 1.
  44. namespace WwiseGTE
  45. {
  46. // The image type must be one of short, int, float or double. The
  47. // computations are performed using double. The input and output images
  48. // must both have xBound elements.
  49. template <typename T>
  50. class FastGaussianBlur1
  51. {
  52. public:
  53. void Execute(int xBound, T const* input, T* output,
  54. double scale, double logBase)
  55. {
  56. int xBoundM1 = xBound - 1;
  57. for (int x = 0; x < xBound; ++x)
  58. {
  59. double rxps = static_cast<double>(x) + scale;
  60. double rxms = static_cast<double>(x) - scale;
  61. int xp1 = static_cast<int>(std::floor(rxps));
  62. int xm1 = static_cast<int>(std::ceil(rxms));
  63. double center = static_cast<double>(input[x]);
  64. double xsum = -2.0 * center;
  65. if (xp1 >= xBoundM1) // use boundary value
  66. {
  67. xsum += static_cast<double>(input[xBoundM1]);
  68. }
  69. else // linearly interpolate
  70. {
  71. double imgXp1 = static_cast<double>(input[xp1]);
  72. double imgXp2 = static_cast<double>(input[xp1 + 1]);
  73. double delta = rxps - static_cast<double>(xp1);
  74. xsum += imgXp1 + delta * (imgXp2 - imgXp1);
  75. }
  76. if (xm1 <= 0) // use boundary value
  77. {
  78. xsum += static_cast<double>(input[0]);
  79. }
  80. else // linearly interpolate
  81. {
  82. double imgXm1 = static_cast<double>(input[xm1]);
  83. double imgXm2 = static_cast<double>(input[xm1 - 1]);
  84. double delta = rxms - static_cast<double>(xm1);
  85. xsum += imgXm1 + delta * (imgXm1 - imgXm2);
  86. }
  87. output[x] = static_cast<T>(center + logBase * xsum);
  88. }
  89. }
  90. };
  91. }