GMLscripts.com

ds_grid_filter_gaussian_wrap

Performs a Gaussian convolution on a given grid, wrapping around the boundaries of the grid if needed.

Number keys select different values of sigma.Download
ds_grid_filter_gaussian_wrap(grid, sigma)
Performs a Gaussian convolution on a given grid, wrapping around the boundaries of the grid if needed.
COPY
  1. /// @func ds_grid_filter_gaussian_wrap(grid, sigma)
  2. ///
  3. /// @desc Performs a Gaussian convolution on a given grid,
  4. /// wrapping around the boundaries of the grid if needed.
  5. /// If sigma <= 0, no operation is performed.
  6. ///
  7. /// @param {grid} grid grid data structure
  8. /// @param {real} sigma standard deviation
  9. ///
  10. /// GMLscripts.com/license
  11.  
  12. function ds_grid_filter_gaussian_wrap(grid, sigma)
  13. {
  14. if (sigma > 0)
  15. {
  16. // Select suitable filter size for sigma
  17. var size = ceil(6 * sigma);
  18. var n = 2 * sqr(sigma);
  19. var d = 1 / (sqrt(2*pi) * sigma);
  20.  
  21. // Compute Gaussian coefficients
  22. var g;
  23. for (var i=size; i>=0; i--)
  24. {
  25. g[i] = exp(-sqr(i) / n) * d;
  26. }
  27.  
  28. // Create working grids
  29. var w = ds_grid_width(grid);
  30. var h = ds_grid_height(grid);
  31.  
  32. var sw = w + 2 * size;
  33. var sh = h + 2 * size;
  34.  
  35. var work1 = ds_grid_create(sw, sh);
  36. var work2 = ds_grid_create(sw, sh);
  37. var grid2 = ds_grid_create(sw, sh);
  38.  
  39. // Duplicate opposite edge data beyond bounds of input grid
  40. ds_grid_set_grid_region(grid2, grid, 0, 0, w-1, h-1, size-w, size-h);
  41. ds_grid_set_grid_region(grid2, grid, 0, 0, w-1, h-1, size, size-h);
  42. ds_grid_set_grid_region(grid2, grid, 0, 0, w-1, h-1, size+w, size-h);
  43. ds_grid_set_grid_region(grid2, grid, 0, 0, w-1, h-1, size-w, size);
  44. ds_grid_set_grid_region(grid2, grid, 0, 0, w-1, h-1, size, size);
  45. ds_grid_set_grid_region(grid2, grid, 0, 0, w-1, h-1, size+w, size);
  46. ds_grid_set_grid_region(grid2, grid, 0, 0, w-1, h-1, size-w, size+h);
  47. ds_grid_set_grid_region(grid2, grid, 0, 0, w-1, h-1, size, size+h);
  48. ds_grid_set_grid_region(grid2, grid, 0, 0, w-1, h-1, size+w, size+h);
  49.  
  50. // Filter horizontally
  51. ds_grid_copy(work1, grid2);
  52. ds_grid_multiply_region(work1, 0, 0, sw-1, sh-1, g[0]);
  53. for (i=1; i<=size; i++)
  54. {
  55. ds_grid_copy(work2, grid2);
  56. ds_grid_multiply_region(work2, 0, 0, sw-1, sh-1, g[i]);
  57. ds_grid_add_grid_region(work1, work2, 0, 0, sw-1, sh-1, i,0);
  58. ds_grid_add_grid_region(work1, work2, 0, 0, sw-1, sh-1, -i,0);
  59. }
  60.  
  61. // Filter vertically
  62. ds_grid_copy(grid2, work1);
  63. ds_grid_multiply_region(grid2, 0, 0, sw-1, sh-1, g[0]);
  64. for (i=1; i<=size; i++)
  65. {
  66. ds_grid_copy(work2, work1);
  67. ds_grid_multiply_region(work2, 0, 0, sw-1, sh-1, g[i]);
  68. ds_grid_add_grid_region(grid2, work2, 0, 0, sw-1, sh-1, 0, i);
  69. ds_grid_add_grid_region(grid2, work2, 0, 0, sw-1, sh-1, 0, -i);
  70. }
  71.  
  72. // Copy filtered data back to input grid
  73. ds_grid_set_grid_region(grid, grid2, size, size, size+w-1, size+h-1, 0, 0);
  74.  
  75. // Clean up
  76. ds_grid_destroy(work1);
  77. ds_grid_destroy(work2);
  78. ds_grid_destroy(grid2);
  79. }
  80. }

Contributors: xot

GitHub: View · Commits · Blame · Raw