gpumat.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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_GPUMAT_DETAIL_HPP
  45. #define OPENCV_CUDEV_PTR2D_GPUMAT_DETAIL_HPP
  46. #include "../gpumat.hpp"
  47. namespace cv { namespace cudev {
  48. template <typename T>
  49. __host__ GpuMat_<T>::GpuMat_(Allocator* allocator)
  50. : GpuMat(allocator)
  51. {
  52. flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<T>::type;
  53. }
  54. template <typename T>
  55. __host__ GpuMat_<T>::GpuMat_(int arows, int acols, Allocator* allocator)
  56. : GpuMat(arows, acols, DataType<T>::type, allocator)
  57. {
  58. }
  59. template <typename T>
  60. __host__ GpuMat_<T>::GpuMat_(Size asize, Allocator* allocator)
  61. : GpuMat(asize.height, asize.width, DataType<T>::type, allocator)
  62. {
  63. }
  64. template <typename T>
  65. __host__ GpuMat_<T>::GpuMat_(int arows, int acols, Scalar val, Allocator* allocator)
  66. : GpuMat(arows, acols, DataType<T>::type, val, allocator)
  67. {
  68. }
  69. template <typename T>
  70. __host__ GpuMat_<T>::GpuMat_(Size asize, Scalar val, Allocator* allocator)
  71. : GpuMat(asize.height, asize.width, DataType<T>::type, val, allocator)
  72. {
  73. }
  74. template <typename T>
  75. __host__ GpuMat_<T>::GpuMat_(const GpuMat_& m)
  76. : GpuMat(m)
  77. {
  78. }
  79. template <typename T>
  80. __host__ GpuMat_<T>::GpuMat_(const GpuMat& m, Allocator* allocator)
  81. : GpuMat(allocator)
  82. {
  83. flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<T>::type;
  84. if (DataType<T>::type == m.type())
  85. {
  86. GpuMat::operator =(m);
  87. return;
  88. }
  89. if (DataType<T>::depth == m.depth())
  90. {
  91. GpuMat::operator =(m.reshape(DataType<T>::channels, m.rows));
  92. return;
  93. }
  94. CV_Assert( DataType<T>::channels == m.channels() );
  95. m.convertTo(*this, type());
  96. }
  97. template <typename T>
  98. __host__ GpuMat_<T>::GpuMat_(int arows, int acols, T* adata, size_t astep)
  99. : GpuMat(arows, acols, DataType<T>::type, adata, astep)
  100. {
  101. }
  102. template <typename T>
  103. __host__ GpuMat_<T>::GpuMat_(Size asize, T* adata, size_t astep)
  104. : GpuMat(asize.height, asize.width, DataType<T>::type, adata, astep)
  105. {
  106. }
  107. template <typename T>
  108. __host__ GpuMat_<T>::GpuMat_(const GpuMat_& m, Range arowRange, Range acolRange)
  109. : GpuMat(m, arowRange, acolRange)
  110. {
  111. }
  112. template <typename T>
  113. __host__ GpuMat_<T>::GpuMat_(const GpuMat_& m, Rect roi)
  114. : GpuMat(m, roi)
  115. {
  116. }
  117. template <typename T>
  118. __host__ GpuMat_<T>::GpuMat_(InputArray arr, Allocator* allocator)
  119. : GpuMat(allocator)
  120. {
  121. flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<T>::type;
  122. upload(arr);
  123. }
  124. template <typename T>
  125. __host__ GpuMat_<T>& GpuMat_<T>::operator =(const GpuMat_& m)
  126. {
  127. GpuMat::operator =(m);
  128. return *this;
  129. }
  130. template <typename T>
  131. __host__ void GpuMat_<T>::create(int arows, int acols)
  132. {
  133. GpuMat::create(arows, acols, DataType<T>::type);
  134. }
  135. template <typename T>
  136. __host__ void GpuMat_<T>::create(Size asize)
  137. {
  138. GpuMat::create(asize, DataType<T>::type);
  139. }
  140. template <typename T>
  141. __host__ void GpuMat_<T>::swap(GpuMat_& mat)
  142. {
  143. GpuMat::swap(mat);
  144. }
  145. template <typename T>
  146. __host__ void GpuMat_<T>::upload(InputArray arr)
  147. {
  148. CV_Assert( arr.type() == DataType<T>::type );
  149. GpuMat::upload(arr);
  150. }
  151. template <typename T>
  152. __host__ void GpuMat_<T>::upload(InputArray arr, Stream& stream)
  153. {
  154. CV_Assert( arr.type() == DataType<T>::type );
  155. GpuMat::upload(arr, stream);
  156. }
  157. template <typename T>
  158. __host__ GpuMat_<T>::operator GlobPtrSz<T>() const
  159. {
  160. return globPtr((T*) data, step, rows, cols);
  161. }
  162. template <typename T>
  163. __host__ GpuMat_<T>::operator GlobPtr<T>() const
  164. {
  165. return globPtr((T*) data, step);
  166. }
  167. template <typename T>
  168. __host__ GpuMat_<T> GpuMat_<T>::clone() const
  169. {
  170. return GpuMat_(GpuMat::clone());
  171. }
  172. template <typename T>
  173. __host__ GpuMat_<T> GpuMat_<T>::row(int y) const
  174. {
  175. return GpuMat_(*this, Range(y, y+1), Range::all());
  176. }
  177. template <typename T>
  178. __host__ GpuMat_<T> GpuMat_<T>::col(int x) const
  179. {
  180. return GpuMat_(*this, Range::all(), Range(x, x+1));
  181. }
  182. template <typename T>
  183. __host__ GpuMat_<T> GpuMat_<T>::rowRange(int startrow, int endrow) const
  184. {
  185. return GpuMat_(*this, Range(startrow, endrow), Range::all());
  186. }
  187. template <typename T>
  188. __host__ GpuMat_<T> GpuMat_<T>::rowRange(Range r) const
  189. {
  190. return GpuMat_(*this, r, Range::all());
  191. }
  192. template <typename T>
  193. __host__ GpuMat_<T> GpuMat_<T>::colRange(int startcol, int endcol) const
  194. {
  195. return GpuMat_(*this, Range::all(), Range(startcol, endcol));
  196. }
  197. template <typename T>
  198. __host__ GpuMat_<T> GpuMat_<T>::colRange(Range r) const
  199. {
  200. return GpuMat_(*this, Range::all(), r);
  201. }
  202. template <typename T>
  203. __host__ GpuMat_<T> GpuMat_<T>::operator ()(Range _rowRange, Range _colRange) const
  204. {
  205. return GpuMat_(*this, _rowRange, _colRange);
  206. }
  207. template <typename T>
  208. __host__ GpuMat_<T> GpuMat_<T>::operator ()(Rect roi) const
  209. {
  210. return GpuMat_(*this, roi);
  211. }
  212. template <typename T>
  213. __host__ GpuMat_<T>& GpuMat_<T>::adjustROI(int dtop, int dbottom, int dleft, int dright)
  214. {
  215. return (GpuMat_<T>&)(GpuMat::adjustROI(dtop, dbottom, dleft, dright));
  216. }
  217. template <typename T>
  218. __host__ size_t GpuMat_<T>::elemSize() const
  219. {
  220. CV_DbgAssert( GpuMat::elemSize() == sizeof(T) );
  221. return sizeof(T);
  222. }
  223. template <typename T>
  224. __host__ size_t GpuMat_<T>::elemSize1() const
  225. {
  226. CV_DbgAssert( GpuMat::elemSize1() == sizeof(T) / DataType<T>::channels );
  227. return sizeof(T) / DataType<T>::channels;
  228. }
  229. template <typename T>
  230. __host__ int GpuMat_<T>::type() const
  231. {
  232. CV_DbgAssert( GpuMat::type() == DataType<T>::type );
  233. return DataType<T>::type;
  234. }
  235. template <typename T>
  236. __host__ int GpuMat_<T>::depth() const
  237. {
  238. CV_DbgAssert( GpuMat::depth() == DataType<T>::depth );
  239. return DataType<T>::depth;
  240. }
  241. template <typename T>
  242. __host__ int GpuMat_<T>::channels() const
  243. {
  244. CV_DbgAssert( GpuMat::channels() == DataType<T>::channels );
  245. return DataType<T>::channels;
  246. }
  247. template <typename T>
  248. __host__ size_t GpuMat_<T>::stepT() const
  249. {
  250. return step / elemSize();
  251. }
  252. template <typename T>
  253. __host__ size_t GpuMat_<T>::step1() const
  254. {
  255. return step / elemSize1();
  256. }
  257. template <typename T>
  258. __host__ T* GpuMat_<T>::operator [](int y)
  259. {
  260. return (T*)ptr(y);
  261. }
  262. template <typename T>
  263. __host__ const T* GpuMat_<T>::operator [](int y) const
  264. {
  265. return (const T*)ptr(y);
  266. }
  267. template <typename T> template <class Body>
  268. __host__ GpuMat_<T>::GpuMat_(const Expr<Body>& expr)
  269. : GpuMat()
  270. {
  271. flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<T>::type;
  272. *this = expr;
  273. }
  274. template <typename T> template <class Body>
  275. __host__ GpuMat_<T>& GpuMat_<T>::operator =(const Expr<Body>& expr)
  276. {
  277. expr.body.assignTo(*this);
  278. return *this;
  279. }
  280. template <typename T> template <class Body>
  281. __host__ GpuMat_<T>& GpuMat_<T>::assign(const Expr<Body>& expr, Stream& stream)
  282. {
  283. expr.body.assignTo(*this, stream);
  284. return *this;
  285. }
  286. }}
  287. // Input / Output Arrays
  288. namespace cv {
  289. template<typename _Tp>
  290. __host__ _InputArray::_InputArray(const cudev::GpuMat_<_Tp>& m)
  291. : flags(FIXED_TYPE + CUDA_GPU_MAT + DataType<_Tp>::type), obj((void*)&m)
  292. {}
  293. template<typename _Tp>
  294. __host__ _OutputArray::_OutputArray(cudev::GpuMat_<_Tp>& m)
  295. : _InputArray(m)
  296. {}
  297. template<typename _Tp>
  298. __host__ _OutputArray::_OutputArray(const cudev::GpuMat_<_Tp>& m)
  299. : _InputArray(m)
  300. {
  301. flags |= FIXED_SIZE;
  302. }
  303. }
  304. #endif