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