pyr_up.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #pragma once
  44. #ifndef OPENCV_CUDEV_GRID_PYR_UP_DETAIL_HPP
  45. #define OPENCV_CUDEV_GRID_PYR_UP_DETAIL_HPP
  46. #include "../../common.hpp"
  47. #include "../../util/vec_traits.hpp"
  48. #include "../../util/saturate_cast.hpp"
  49. #include "../../util/type_traits.hpp"
  50. #include "../../ptr2d/glob.hpp"
  51. #include "../../ptr2d/traits.hpp"
  52. namespace cv { namespace cudev {
  53. namespace pyramids_detail
  54. {
  55. template <class SrcPtr, typename DstType>
  56. __global__ void pyrUp(const SrcPtr src, GlobPtr<DstType> dst, const int src_rows, const int src_cols, const int dst_rows, const int dst_cols)
  57. {
  58. typedef typename PtrTraits<SrcPtr>::value_type src_type;
  59. typedef typename VecTraits<src_type>::elem_type src_elem_type;
  60. typedef typename LargerType<float, src_elem_type>::type work_elem_type;
  61. typedef typename MakeVec<work_elem_type, VecTraits<src_type>::cn>::type work_type;
  62. const int x = blockIdx.x * blockDim.x + threadIdx.x;
  63. const int y = blockIdx.y * blockDim.y + threadIdx.y;
  64. __shared__ work_type s_srcPatch[10][10];
  65. __shared__ work_type s_dstPatch[20][16];
  66. if (threadIdx.x < 10 && threadIdx.y < 10)
  67. {
  68. int srcx = static_cast<int>((blockIdx.x * blockDim.x) / 2 + threadIdx.x) - 1;
  69. int srcy = static_cast<int>((blockIdx.y * blockDim.y) / 2 + threadIdx.y) - 1;
  70. srcx = ::abs(srcx);
  71. srcx = ::min(src_cols - 1, srcx);
  72. srcy = ::abs(srcy);
  73. srcy = ::min(src_rows - 1, srcy);
  74. s_srcPatch[threadIdx.y][threadIdx.x] = saturate_cast<work_type>(src(srcy, srcx));
  75. }
  76. __syncthreads();
  77. work_type sum = VecTraits<work_type>::all(0);
  78. const int evenFlag = static_cast<int>((threadIdx.x & 1) == 0);
  79. const int oddFlag = static_cast<int>((threadIdx.x & 1) != 0);
  80. const bool eveny = ((threadIdx.y & 1) == 0);
  81. const int tidx = threadIdx.x;
  82. if (eveny)
  83. {
  84. sum = sum + (evenFlag * 0.0625f) * s_srcPatch[1 + (threadIdx.y >> 1)][1 + ((tidx - 2) >> 1)];
  85. sum = sum + ( oddFlag * 0.25f ) * s_srcPatch[1 + (threadIdx.y >> 1)][1 + ((tidx - 1) >> 1)];
  86. sum = sum + (evenFlag * 0.375f ) * s_srcPatch[1 + (threadIdx.y >> 1)][1 + ((tidx ) >> 1)];
  87. sum = sum + ( oddFlag * 0.25f ) * s_srcPatch[1 + (threadIdx.y >> 1)][1 + ((tidx + 1) >> 1)];
  88. sum = sum + (evenFlag * 0.0625f) * s_srcPatch[1 + (threadIdx.y >> 1)][1 + ((tidx + 2) >> 1)];
  89. }
  90. s_dstPatch[2 + threadIdx.y][threadIdx.x] = sum;
  91. if (threadIdx.y < 2)
  92. {
  93. sum = VecTraits<work_type>::all(0);
  94. if (eveny)
  95. {
  96. sum = sum + (evenFlag * 0.0625f) * s_srcPatch[0][1 + ((tidx - 2) >> 1)];
  97. sum = sum + ( oddFlag * 0.25f ) * s_srcPatch[0][1 + ((tidx - 1) >> 1)];
  98. sum = sum + (evenFlag * 0.375f ) * s_srcPatch[0][1 + ((tidx ) >> 1)];
  99. sum = sum + ( oddFlag * 0.25f ) * s_srcPatch[0][1 + ((tidx + 1) >> 1)];
  100. sum = sum + (evenFlag * 0.0625f) * s_srcPatch[0][1 + ((tidx + 2) >> 1)];
  101. }
  102. s_dstPatch[threadIdx.y][threadIdx.x] = sum;
  103. }
  104. if (threadIdx.y > 13)
  105. {
  106. sum = VecTraits<work_type>::all(0);
  107. if (eveny)
  108. {
  109. sum = sum + (evenFlag * 0.0625f) * s_srcPatch[9][1 + ((tidx - 2) >> 1)];
  110. sum = sum + ( oddFlag * 0.25f ) * s_srcPatch[9][1 + ((tidx - 1) >> 1)];
  111. sum = sum + (evenFlag * 0.375f ) * s_srcPatch[9][1 + ((tidx ) >> 1)];
  112. sum = sum + ( oddFlag * 0.25f ) * s_srcPatch[9][1 + ((tidx + 1) >> 1)];
  113. sum = sum + (evenFlag * 0.0625f) * s_srcPatch[9][1 + ((tidx + 2) >> 1)];
  114. }
  115. s_dstPatch[4 + threadIdx.y][threadIdx.x] = sum;
  116. }
  117. __syncthreads();
  118. sum = VecTraits<work_type>::all(0);
  119. const int tidy = threadIdx.y;
  120. sum = sum + 0.0625f * s_dstPatch[2 + tidy - 2][threadIdx.x];
  121. sum = sum + 0.25f * s_dstPatch[2 + tidy - 1][threadIdx.x];
  122. sum = sum + 0.375f * s_dstPatch[2 + tidy ][threadIdx.x];
  123. sum = sum + 0.25f * s_dstPatch[2 + tidy + 1][threadIdx.x];
  124. sum = sum + 0.0625f * s_dstPatch[2 + tidy + 2][threadIdx.x];
  125. if (x < dst_cols && y < dst_rows)
  126. dst(y, x) = saturate_cast<DstType>(4.0f * sum);
  127. }
  128. template <class SrcPtr, typename DstType>
  129. __host__ void pyrUp(const SrcPtr& src, const GlobPtr<DstType>& dst, int src_rows, int src_cols, int dst_rows, int dst_cols, cudaStream_t stream)
  130. {
  131. const dim3 block(16, 16);
  132. const dim3 grid(divUp(dst_cols, block.x), divUp(dst_rows, block.y));
  133. pyrUp<<<grid, block, 0, stream>>>(src, dst, src_rows, src_cols, dst_rows, dst_cols);
  134. CV_CUDEV_SAFE_CALL( cudaGetLastError() );
  135. if (stream == 0)
  136. CV_CUDEV_SAFE_CALL( cudaDeviceSynchronize() );
  137. }
  138. }
  139. }}
  140. #endif