neighbor.cl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2018 Danil Iashchenko
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. __kernel void erosion_global(__write_only image2d_t dst,
  21. __read_only image2d_t src,
  22. float threshold,
  23. __constant int *coord)
  24. {
  25. const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE |
  26. CLK_ADDRESS_CLAMP_TO_EDGE |
  27. CLK_FILTER_NEAREST);
  28. int2 loc = (int2)(get_global_id(0), get_global_id(1));
  29. float4 px = read_imagef(src, sampler, loc);
  30. float limit = px.x - threshold;
  31. if (limit < 0) {
  32. limit = 0;
  33. }
  34. for (int i = -1; i <= 1; i++) {
  35. for (int j = -1; j <= 1; j++) {
  36. if (coord[(j + 1) * 3 + (i + 1)] == 1) {
  37. float4 cur = read_imagef(src, sampler, loc + (int2)(i, j));
  38. if (cur.x < px.x) {
  39. px = cur;
  40. }
  41. }
  42. }
  43. }
  44. if (limit > px.x) {
  45. px = (float4)(limit);
  46. }
  47. write_imagef(dst, loc, px);
  48. }
  49. __kernel void dilation_global(__write_only image2d_t dst,
  50. __read_only image2d_t src,
  51. float threshold,
  52. __constant int *coord)
  53. {
  54. const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE |
  55. CLK_ADDRESS_CLAMP_TO_EDGE |
  56. CLK_FILTER_NEAREST);
  57. int2 loc = (int2)(get_global_id(0), get_global_id(1));
  58. float4 px = read_imagef(src, sampler, loc);
  59. float limit = px.x + threshold;
  60. if (limit > 1) {
  61. limit = 1;
  62. }
  63. for (int i = -1; i <= 1; i++) {
  64. for (int j = -1; j <= 1; j++) {
  65. if (coord[(j + 1) * 3 + (i + 1)] == 1) {
  66. float4 cur = read_imagef(src, sampler, loc + (int2)(i, j));
  67. if (cur.x > px.x) {
  68. px = cur;
  69. }
  70. }
  71. }
  72. }
  73. if (limit < px.x) {
  74. px = (float4)(limit);
  75. }
  76. write_imagef(dst, loc, px);
  77. }