vec_traits.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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_UTIL_VEC_TRAITS_HPP
  45. #define OPENCV_CUDEV_UTIL_VEC_TRAITS_HPP
  46. #include "../common.hpp"
  47. namespace cv { namespace cudev {
  48. //! @addtogroup cudev
  49. //! @{
  50. // MakeVec
  51. template<typename T, int CN> struct MakeVec;
  52. #define CV_CUDEV_MAKE_VEC_INST(elem_type) \
  53. template<> struct MakeVec<elem_type, 1> { typedef elem_type type; }; \
  54. template<> struct MakeVec<elem_type, 2> { typedef elem_type ## 2 type; }; \
  55. template<> struct MakeVec<elem_type, 3> { typedef elem_type ## 3 type; }; \
  56. template<> struct MakeVec<elem_type, 4> { typedef elem_type ## 4 type; };
  57. CV_CUDEV_MAKE_VEC_INST(uchar)
  58. CV_CUDEV_MAKE_VEC_INST(ushort)
  59. CV_CUDEV_MAKE_VEC_INST(short)
  60. CV_CUDEV_MAKE_VEC_INST(int)
  61. CV_CUDEV_MAKE_VEC_INST(uint)
  62. CV_CUDEV_MAKE_VEC_INST(float)
  63. CV_CUDEV_MAKE_VEC_INST(double)
  64. #undef CV_CUDEV_MAKE_VEC_INST
  65. template<> struct MakeVec<schar, 1> { typedef schar type; };
  66. template<> struct MakeVec<schar, 2> { typedef char2 type; };
  67. template<> struct MakeVec<schar, 3> { typedef char3 type; };
  68. template<> struct MakeVec<schar, 4> { typedef char4 type; };
  69. template<> struct MakeVec<bool, 1> { typedef uchar type; };
  70. template<> struct MakeVec<bool, 2> { typedef uchar2 type; };
  71. template<> struct MakeVec<bool, 3> { typedef uchar3 type; };
  72. template<> struct MakeVec<bool, 4> { typedef uchar4 type; };
  73. // VecTraits
  74. template<typename T> struct VecTraits;
  75. #define CV_CUDEV_VEC_TRAITS_INST(type) \
  76. template <> struct VecTraits<type> \
  77. { \
  78. typedef type elem_type; \
  79. enum {cn=1}; \
  80. __host__ __device__ __forceinline__ static type all(type v) {return v;} \
  81. __host__ __device__ __forceinline__ static type make(type x) {return x;} \
  82. __host__ __device__ __forceinline__ static type make(const type* v) {return *v;} \
  83. }; \
  84. template <> struct VecTraits<type ## 1> \
  85. { \
  86. typedef type elem_type; \
  87. enum {cn=1}; \
  88. __host__ __device__ __forceinline__ static type ## 1 all(type v) {return make_ ## type ## 1(v);} \
  89. __host__ __device__ __forceinline__ static type ## 1 make(type x) {return make_ ## type ## 1(x);} \
  90. __host__ __device__ __forceinline__ static type ## 1 make(const type* v) {return make_ ## type ## 1(*v);} \
  91. }; \
  92. template <> struct VecTraits<type ## 2> \
  93. { \
  94. typedef type elem_type; \
  95. enum {cn=2}; \
  96. __host__ __device__ __forceinline__ static type ## 2 all(type v) {return make_ ## type ## 2(v, v);} \
  97. __host__ __device__ __forceinline__ static type ## 2 make(type x, type y) {return make_ ## type ## 2(x, y);} \
  98. __host__ __device__ __forceinline__ static type ## 2 make(const type* v) {return make_ ## type ## 2(v[0], v[1]);} \
  99. }; \
  100. template <> struct VecTraits<type ## 3> \
  101. { \
  102. typedef type elem_type; \
  103. enum {cn=3}; \
  104. __host__ __device__ __forceinline__ static type ## 3 all(type v) {return make_ ## type ## 3(v, v, v);} \
  105. __host__ __device__ __forceinline__ static type ## 3 make(type x, type y, type z) {return make_ ## type ## 3(x, y, z);} \
  106. __host__ __device__ __forceinline__ static type ## 3 make(const type* v) {return make_ ## type ## 3(v[0], v[1], v[2]);} \
  107. }; \
  108. template <> struct VecTraits<type ## 4> \
  109. { \
  110. typedef type elem_type; \
  111. enum {cn=4}; \
  112. __host__ __device__ __forceinline__ static type ## 4 all(type v) {return make_ ## type ## 4(v, v, v, v);} \
  113. __host__ __device__ __forceinline__ static type ## 4 make(type x, type y, type z, type w) {return make_ ## type ## 4(x, y, z, w);} \
  114. __host__ __device__ __forceinline__ static type ## 4 make(const type* v) {return make_ ## type ## 4(v[0], v[1], v[2], v[3]);} \
  115. };
  116. CV_CUDEV_VEC_TRAITS_INST(uchar)
  117. CV_CUDEV_VEC_TRAITS_INST(ushort)
  118. CV_CUDEV_VEC_TRAITS_INST(short)
  119. CV_CUDEV_VEC_TRAITS_INST(int)
  120. CV_CUDEV_VEC_TRAITS_INST(uint)
  121. CV_CUDEV_VEC_TRAITS_INST(float)
  122. CV_CUDEV_VEC_TRAITS_INST(double)
  123. #undef CV_CUDEV_VEC_TRAITS_INST
  124. template<> struct VecTraits<schar>
  125. {
  126. typedef schar elem_type;
  127. enum {cn=1};
  128. __host__ __device__ __forceinline__ static schar all(schar v) {return v;}
  129. __host__ __device__ __forceinline__ static schar make(schar x) {return x;}
  130. __host__ __device__ __forceinline__ static schar make(const schar* x) {return *x;}
  131. };
  132. template<> struct VecTraits<char1>
  133. {
  134. typedef schar elem_type;
  135. enum {cn=1};
  136. __host__ __device__ __forceinline__ static char1 all(schar v) {return make_char1(v);}
  137. __host__ __device__ __forceinline__ static char1 make(schar x) {return make_char1(x);}
  138. __host__ __device__ __forceinline__ static char1 make(const schar* v) {return make_char1(v[0]);}
  139. };
  140. template<> struct VecTraits<char2>
  141. {
  142. typedef schar elem_type;
  143. enum {cn=2};
  144. __host__ __device__ __forceinline__ static char2 all(schar v) {return make_char2(v, v);}
  145. __host__ __device__ __forceinline__ static char2 make(schar x, schar y) {return make_char2(x, y);}
  146. __host__ __device__ __forceinline__ static char2 make(const schar* v) {return make_char2(v[0], v[1]);}
  147. };
  148. template<> struct VecTraits<char3>
  149. {
  150. typedef schar elem_type;
  151. enum {cn=3};
  152. __host__ __device__ __forceinline__ static char3 all(schar v) {return make_char3(v, v, v);}
  153. __host__ __device__ __forceinline__ static char3 make(schar x, schar y, schar z) {return make_char3(x, y, z);}
  154. __host__ __device__ __forceinline__ static char3 make(const schar* v) {return make_char3(v[0], v[1], v[2]);}
  155. };
  156. template<> struct VecTraits<char4>
  157. {
  158. typedef schar elem_type;
  159. enum {cn=4};
  160. __host__ __device__ __forceinline__ static char4 all(schar v) {return make_char4(v, v, v, v);}
  161. __host__ __device__ __forceinline__ static char4 make(schar x, schar y, schar z, schar w) {return make_char4(x, y, z, w);}
  162. __host__ __device__ __forceinline__ static char4 make(const schar* v) {return make_char4(v[0], v[1], v[2], v[3]);}
  163. };
  164. //! @}
  165. }}
  166. // DataType
  167. namespace cv {
  168. template <> class DataType<uint>
  169. {
  170. public:
  171. typedef uint value_type;
  172. typedef value_type work_type;
  173. typedef value_type channel_type;
  174. typedef value_type vec_type;
  175. enum { generic_type = 0,
  176. depth = CV_32S,
  177. channels = 1,
  178. fmt = (int)'i',
  179. type = CV_MAKE_TYPE(depth, channels)
  180. };
  181. };
  182. #define CV_CUDEV_DATA_TYPE_INST(_depth_type, _channel_num) \
  183. template <> class DataType< _depth_type ## _channel_num > \
  184. { \
  185. public: \
  186. typedef _depth_type ## _channel_num value_type; \
  187. typedef value_type work_type; \
  188. typedef _depth_type channel_type; \
  189. typedef value_type vec_type; \
  190. enum { generic_type = 0, \
  191. depth = DataType<channel_type>::depth, \
  192. channels = _channel_num, \
  193. fmt = DataType<channel_type>::fmt + ((channels - 1) << 8), \
  194. type = CV_MAKE_TYPE(depth, channels) \
  195. }; \
  196. };
  197. CV_CUDEV_DATA_TYPE_INST(uchar, 1)
  198. CV_CUDEV_DATA_TYPE_INST(uchar, 2)
  199. CV_CUDEV_DATA_TYPE_INST(uchar, 3)
  200. CV_CUDEV_DATA_TYPE_INST(uchar, 4)
  201. CV_CUDEV_DATA_TYPE_INST(ushort, 1)
  202. CV_CUDEV_DATA_TYPE_INST(ushort, 2)
  203. CV_CUDEV_DATA_TYPE_INST(ushort, 3)
  204. CV_CUDEV_DATA_TYPE_INST(ushort, 4)
  205. CV_CUDEV_DATA_TYPE_INST(short, 1)
  206. CV_CUDEV_DATA_TYPE_INST(short, 2)
  207. CV_CUDEV_DATA_TYPE_INST(short, 3)
  208. CV_CUDEV_DATA_TYPE_INST(short, 4)
  209. CV_CUDEV_DATA_TYPE_INST(int, 1)
  210. CV_CUDEV_DATA_TYPE_INST(int, 2)
  211. CV_CUDEV_DATA_TYPE_INST(int, 3)
  212. CV_CUDEV_DATA_TYPE_INST(int, 4)
  213. CV_CUDEV_DATA_TYPE_INST(uint, 1)
  214. CV_CUDEV_DATA_TYPE_INST(uint, 2)
  215. CV_CUDEV_DATA_TYPE_INST(uint, 3)
  216. CV_CUDEV_DATA_TYPE_INST(uint, 4)
  217. CV_CUDEV_DATA_TYPE_INST(float, 1)
  218. CV_CUDEV_DATA_TYPE_INST(float, 2)
  219. CV_CUDEV_DATA_TYPE_INST(float, 3)
  220. CV_CUDEV_DATA_TYPE_INST(float, 4)
  221. CV_CUDEV_DATA_TYPE_INST(double, 1)
  222. CV_CUDEV_DATA_TYPE_INST(double, 2)
  223. CV_CUDEV_DATA_TYPE_INST(double, 3)
  224. CV_CUDEV_DATA_TYPE_INST(double, 4)
  225. #undef CV_CUDEV_DATA_TYPE_INST
  226. template<> class DataType<char1>
  227. {
  228. public:
  229. typedef char1 value_type;
  230. typedef value_type work_type;
  231. typedef schar channel_type;
  232. typedef value_type vec_type;
  233. enum { generic_type = 0,
  234. depth = DataType<channel_type>::depth,
  235. channels = 1,
  236. fmt = DataType<channel_type>::fmt + ((channels - 1) << 8),
  237. type = CV_MAKE_TYPE(depth, channels)
  238. };
  239. };
  240. template<> class DataType<char2>
  241. {
  242. public:
  243. typedef char2 value_type;
  244. typedef value_type work_type;
  245. typedef schar channel_type;
  246. typedef value_type vec_type;
  247. enum { generic_type = 0,
  248. depth = DataType<channel_type>::depth,
  249. channels = 2,
  250. fmt = DataType<channel_type>::fmt + ((channels - 1) << 8),
  251. type = CV_MAKE_TYPE(depth, channels)
  252. };
  253. };
  254. template<> class DataType<char3>
  255. {
  256. public:
  257. typedef char3 value_type;
  258. typedef value_type work_type;
  259. typedef schar channel_type;
  260. typedef value_type vec_type;
  261. enum { generic_type = 0,
  262. depth = DataType<channel_type>::depth,
  263. channels = 3,
  264. fmt = DataType<channel_type>::fmt + ((channels - 1) << 8),
  265. type = CV_MAKE_TYPE(depth, channels)
  266. };
  267. };
  268. template<> class DataType<char4>
  269. {
  270. public:
  271. typedef char4 value_type;
  272. typedef value_type work_type;
  273. typedef schar channel_type;
  274. typedef value_type vec_type;
  275. enum { generic_type = 0,
  276. depth = DataType<channel_type>::depth,
  277. channels = 4,
  278. fmt = DataType<channel_type>::fmt + ((channels - 1) << 8),
  279. type = CV_MAKE_TYPE(depth, channels)
  280. };
  281. };
  282. }
  283. #endif