convolution.cl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 convolution_global(__write_only image2d_t dst,
  21. __read_only image2d_t src,
  22. int coef_matrix_dim,
  23. __constant float *coef_matrix,
  24. float div,
  25. float bias)
  26. {
  27. const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE |
  28. CLK_ADDRESS_CLAMP_TO_EDGE |
  29. CLK_FILTER_NEAREST);
  30. const int half_matrix_dim = (coef_matrix_dim / 2);
  31. int2 loc = (int2)(get_global_id(0), get_global_id(1));
  32. float4 convPix = (float4)(0.0f, 0.0f, 0.0f, 0.0f);
  33. for (int conv_i = -half_matrix_dim; conv_i <= half_matrix_dim; conv_i++) {
  34. for (int conv_j = -half_matrix_dim; conv_j <= half_matrix_dim; conv_j++) {
  35. float4 px = read_imagef(src, sampler, loc + (int2)(conv_j, conv_i));
  36. convPix += px * coef_matrix[(conv_i + half_matrix_dim) * coef_matrix_dim +
  37. (conv_j + half_matrix_dim)];
  38. }
  39. }
  40. float4 dstPix = convPix * div + bias;
  41. write_imagef(dst, loc, dstPix);
  42. }
  43. __kernel void sobel_global(__write_only image2d_t dst,
  44. __read_only image2d_t src,
  45. float div,
  46. float bias)
  47. {
  48. const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE |
  49. CLK_ADDRESS_CLAMP_TO_EDGE |
  50. CLK_FILTER_NEAREST);
  51. int2 loc = (int2)(get_global_id(0), get_global_id(1));
  52. float4 sum1 = read_imagef(src, sampler, loc + (int2)(-1,-1)) * -1 +
  53. read_imagef(src, sampler, loc + (int2)( 0,-1)) * -2 +
  54. read_imagef(src, sampler, loc + (int2)( 1,-1)) * -1 +
  55. read_imagef(src, sampler, loc + (int2)(-1, 1)) * 1 +
  56. read_imagef(src, sampler, loc + (int2)( 0, 1)) * 2 +
  57. read_imagef(src, sampler, loc + (int2)( 1, 1)) * 1;
  58. float4 sum2 = read_imagef(src, sampler, loc + (int2)(-1,-1)) * -1 +
  59. read_imagef(src, sampler, loc + (int2)(-1, 0)) * -2 +
  60. read_imagef(src, sampler, loc + (int2)(-1, 1)) * -1 +
  61. read_imagef(src, sampler, loc + (int2)( 1,-1)) * 1 +
  62. read_imagef(src, sampler, loc + (int2)( 1, 0)) * 2 +
  63. read_imagef(src, sampler, loc + (int2)( 1, 1)) * 1;
  64. float4 dstPix = hypot(sum1, sum2) * div + bias;
  65. write_imagef(dst, loc, dstPix);
  66. }
  67. __kernel void prewitt_global(__write_only image2d_t dst,
  68. __read_only image2d_t src,
  69. float div,
  70. float bias)
  71. {
  72. const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE |
  73. CLK_ADDRESS_CLAMP_TO_EDGE |
  74. CLK_FILTER_NEAREST);
  75. int2 loc = (int2)(get_global_id(0), get_global_id(1));
  76. float4 sum1 = read_imagef(src, sampler, loc + (int2)(-1,-1)) * 1 +
  77. read_imagef(src, sampler, loc + (int2)( 0,-1)) * 1 +
  78. read_imagef(src, sampler, loc + (int2)( 1,-1)) * 1 +
  79. read_imagef(src, sampler, loc + (int2)(-1, 1)) * -1 +
  80. read_imagef(src, sampler, loc + (int2)( 0, 1)) * -1 +
  81. read_imagef(src, sampler, loc + (int2)( 1, 1)) * -1;
  82. float4 sum2 = read_imagef(src, sampler, loc + (int2)(-1,-1)) * 1 +
  83. read_imagef(src, sampler, loc + (int2)(-1, 0)) * 1 +
  84. read_imagef(src, sampler, loc + (int2)(-1, 1)) * 1 +
  85. read_imagef(src, sampler, loc + (int2)( 1,-1)) * -1 +
  86. read_imagef(src, sampler, loc + (int2)( 1, 0)) * -1 +
  87. read_imagef(src, sampler, loc + (int2)( 1, 1)) * -1;
  88. float4 dstPix = hypot(sum1, sum2) * div + bias;
  89. write_imagef(dst, loc, dstPix);
  90. }
  91. __kernel void roberts_global(__write_only image2d_t dst,
  92. __read_only image2d_t src,
  93. float div,
  94. float bias)
  95. {
  96. const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE |
  97. CLK_ADDRESS_CLAMP_TO_EDGE |
  98. CLK_FILTER_NEAREST);
  99. int2 loc = (int2)(get_global_id(0), get_global_id(1));
  100. float4 sum1 = read_imagef(src, sampler, loc + (int2)(-1,-1)) * 1 +
  101. read_imagef(src, sampler, loc + (int2)( 0,-1)) * -1;
  102. float4 sum2 = read_imagef(src, sampler, loc + (int2)(-1, 0)) * -1 +
  103. read_imagef(src, sampler, loc + (int2)( 0, 0)) * 1;
  104. float4 dstPix = hypot(sum1, sum2) * div + bias;
  105. write_imagef(dst, loc, dstPix);
  106. }