Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. /**
  2. Blur example : naive square blur.
  3. This example also demonstrates the chaining of multiple kernels (see commented code at the end).
  4. Written by Olivier Chafik, no right reserved :-) */
  5.  
  6. void performBlur(
  7. int blurSize,
  8. read_only image2d_t inputImage,
  9. write_only image2d_t outputImage)
  10. {
  11. // See http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/get_image_dim.html
  12. int2 dimensions = get_image_dim(inputImage);
  13. int width = dimensions.x, height = dimensions.y;
  14.  
  15. int x = get_global_id(0), y = get_global_id(1);
  16.  
  17. float4 transformedPixel = (float4)0;
  18.  
  19. // See http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/sampler_t.html
  20. const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_FILTER_NEAREST | CLK_ADDRESS_CLAMP_TO_EDGE;
  21.  
  22. int minDiff = -(blurSize - 1);
  23. for (int dx = minDiff; dx < blurSize; dx++) {
  24. for (int dy = minDiff; dy < blurSize; dy++) {
  25. // See http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/read_imagef2d.html
  26. float4 pixel = read_imagef(inputImage, sampler, (int2)(x + dx, y + dy));
  27. transformedPixel += pixel;
  28. }
  29. }
  30.  
  31. int n = (2 * blurSize - 1);
  32. n *= n;
  33.  
  34. transformedPixel /= (float)n;
  35.  
  36. // See http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/write_image.html
  37. write_imagef(outputImage, (int2)(x, y), transformedPixel);
  38. }
  39.  
  40. __kernel void test(
  41. read_only image2d_t inputImage,
  42. write_only image2d_t outputImage)
  43. {
  44. const float matrix[] = {
  45. 3.f, 0.f, -3.f,
  46. 10.f, 0.f, -10.f,
  47. 3.f, 0.f, -3.f,
  48. };
  49. convolveImage(inputImage, matrix, 3, outputImage);
  50. }
  51.  
  52. // Perform a blur
  53. __kernel void pass1(read_only image2d_t inputImage, write_only image2d_t outputImage) {
  54. performBlur(10, inputImage, outputImage);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement