extrapolation.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_PTR2D_EXTRAPOLATION_HPP
  45. #define OPENCV_CUDEV_PTR2D_EXTRAPOLATION_HPP
  46. #include "../common.hpp"
  47. #include "../util/vec_traits.hpp"
  48. #include "traits.hpp"
  49. namespace cv { namespace cudev {
  50. //! @addtogroup cudev
  51. //! @{
  52. // BrdConstant
  53. template <class SrcPtr> struct BrdConstant
  54. {
  55. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  56. typedef int index_type;
  57. SrcPtr src;
  58. int rows, cols;
  59. typename PtrTraits<SrcPtr>::value_type val;
  60. __device__ __forceinline__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
  61. {
  62. return (x >= 0 && x < cols && y >= 0 && y < rows) ? src(y, x) : val;
  63. }
  64. };
  65. template <class SrcPtr>
  66. __host__ BrdConstant<typename PtrTraits<SrcPtr>::ptr_type> brdConstant(const SrcPtr& src, typename PtrTraits<SrcPtr>::value_type val)
  67. {
  68. BrdConstant<typename PtrTraits<SrcPtr>::ptr_type> b;
  69. b.src = shrinkPtr(src);
  70. b.rows = getRows(src);
  71. b.cols = getCols(src);
  72. b.val = val;
  73. return b;
  74. }
  75. template <class SrcPtr>
  76. __host__ BrdConstant<typename PtrTraits<SrcPtr>::ptr_type> brdConstant(const SrcPtr& src)
  77. {
  78. return brdConstant(src, VecTraits<typename PtrTraits<SrcPtr>::value_type>::all(0));
  79. }
  80. // BrdBase
  81. template <class BrdImpl, class SrcPtr> struct BrdBase
  82. {
  83. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  84. typedef int index_type;
  85. SrcPtr src;
  86. int rows, cols;
  87. __device__ __forceinline__ int idx_row(int y) const
  88. {
  89. return BrdImpl::idx_low(BrdImpl::idx_high(y, rows), rows);
  90. }
  91. __device__ __forceinline__ int idx_col(int x) const
  92. {
  93. return BrdImpl::idx_low(BrdImpl::idx_high(x, cols), cols);
  94. }
  95. __device__ __forceinline__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
  96. {
  97. return src(idx_row(y), idx_col(x));
  98. }
  99. };
  100. // BrdReplicate
  101. struct BrdReplicate
  102. {
  103. __device__ __forceinline__ static int idx_low(int i, int len)
  104. {
  105. return ::max(i, 0);
  106. }
  107. __device__ __forceinline__ static int idx_high(int i, int len)
  108. {
  109. return ::min(i, len - 1);
  110. }
  111. };
  112. template <class SrcPtr>
  113. __host__ BrdBase<BrdReplicate, typename PtrTraits<SrcPtr>::ptr_type> brdReplicate(const SrcPtr& src)
  114. {
  115. BrdBase<BrdReplicate, typename PtrTraits<SrcPtr>::ptr_type> b;
  116. b.src = shrinkPtr(src);
  117. b.rows = getRows(src);
  118. b.cols = getCols(src);
  119. return b;
  120. }
  121. // BrdReflect101
  122. struct BrdReflect101
  123. {
  124. __device__ __forceinline__ static int idx_low(int i, int len)
  125. {
  126. return ::abs(i) % len;
  127. }
  128. __device__ __forceinline__ static int idx_high(int i, int len)
  129. {
  130. const int last_ind = len - 1;
  131. return ::abs(last_ind - ::abs(last_ind - i)) % len;
  132. }
  133. };
  134. template <class SrcPtr>
  135. __host__ BrdBase<BrdReflect101, typename PtrTraits<SrcPtr>::ptr_type> brdReflect101(const SrcPtr& src)
  136. {
  137. BrdBase<BrdReflect101, typename PtrTraits<SrcPtr>::ptr_type> b;
  138. b.src = shrinkPtr(src);
  139. b.rows = getRows(src);
  140. b.cols = getCols(src);
  141. return b;
  142. }
  143. // BrdReflect
  144. struct BrdReflect
  145. {
  146. __device__ __forceinline__ static int idx_low(int i, int len)
  147. {
  148. return (::abs(i) - (i < 0)) % len;
  149. }
  150. __device__ __forceinline__ static int idx_high(int i, int len)
  151. {
  152. const int last_ind = len - 1;
  153. return (last_ind - ::abs(last_ind - i) + (i > last_ind));
  154. }
  155. };
  156. template <class SrcPtr>
  157. __host__ BrdBase<BrdReflect, typename PtrTraits<SrcPtr>::ptr_type> brdReflect(const SrcPtr& src)
  158. {
  159. BrdBase<BrdReflect, typename PtrTraits<SrcPtr>::ptr_type> b;
  160. b.src = shrinkPtr(src);
  161. b.rows = getRows(src);
  162. b.cols = getCols(src);
  163. return b;
  164. }
  165. // BrdWrap
  166. struct BrdWrap
  167. {
  168. __device__ __forceinline__ static int idx_low(int i, int len)
  169. {
  170. return (i >= 0) ? i : (i - ((i - len + 1) / len) * len);
  171. }
  172. __device__ __forceinline__ static int idx_high(int i, int len)
  173. {
  174. return (i < len) ? i : (i % len);
  175. }
  176. };
  177. template <class SrcPtr>
  178. __host__ BrdBase<BrdWrap, typename PtrTraits<SrcPtr>::ptr_type> brdWrap(const SrcPtr& src)
  179. {
  180. BrdBase<BrdWrap, typename PtrTraits<SrcPtr>::ptr_type> b;
  181. b.src = shrinkPtr(src);
  182. b.rows = getRows(src);
  183. b.cols = getCols(src);
  184. return b;
  185. }
  186. //! @}
  187. }}
  188. #endif