vf_thumbnail_cuda.cu 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. extern "C" {
  23. __global__ void Thumbnail_uchar(cudaTextureObject_t uchar_tex,
  24. int *histogram, int src_width, int src_height)
  25. {
  26. int x = blockIdx.x * blockDim.x + threadIdx.x;
  27. int y = blockIdx.y * blockDim.y + threadIdx.y;
  28. if (y < src_height && x < src_width)
  29. {
  30. unsigned char pixel = tex2D<unsigned char>(uchar_tex, x, y);
  31. atomicAdd(&histogram[pixel], 1);
  32. }
  33. }
  34. __global__ void Thumbnail_uchar2(cudaTextureObject_t uchar2_tex,
  35. int *histogram, int src_width, int src_height)
  36. {
  37. int x = blockIdx.x * blockDim.x + threadIdx.x;
  38. int y = blockIdx.y * blockDim.y + threadIdx.y;
  39. if (y < src_height && x < src_width)
  40. {
  41. uchar2 pixel = tex2D<uchar2>(uchar2_tex, x, y);
  42. atomicAdd(&histogram[pixel.x], 1);
  43. atomicAdd(&histogram[256 + pixel.y], 1);
  44. }
  45. }
  46. __global__ void Thumbnail_ushort(cudaTextureObject_t ushort_tex,
  47. int *histogram, int src_width, int src_height)
  48. {
  49. int x = blockIdx.x * blockDim.x + threadIdx.x;
  50. int y = blockIdx.y * blockDim.y + threadIdx.y;
  51. if (y < src_height && x < src_width)
  52. {
  53. unsigned short pixel = (tex2D<unsigned short>(ushort_tex, x, y) + 128) >> 8;
  54. atomicAdd(&histogram[pixel], 1);
  55. }
  56. }
  57. __global__ void Thumbnail_ushort2(cudaTextureObject_t ushort2_tex,
  58. int *histogram, int src_width, int src_height)
  59. {
  60. int x = blockIdx.x * blockDim.x + threadIdx.x;
  61. int y = blockIdx.y * blockDim.y + threadIdx.y;
  62. if (y < src_height && x < src_width)
  63. {
  64. ushort2 pixel = tex2D<ushort2>(ushort2_tex, x, y);
  65. atomicAdd(&histogram[(pixel.x + 128) >> 8], 1);
  66. atomicAdd(&histogram[256 + ((pixel.y + 128) >> 8)], 1);
  67. }
  68. }
  69. }