Merge pull request #2478 from clemenscorny:master
[profile/ivi/opencv.git] / modules / core / include / opencv2 / core / traits.hpp
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 #ifndef __OPENCV_CORE_TRAITS_HPP__
45 #define __OPENCV_CORE_TRAITS_HPP__
46
47 #include "opencv2/core/cvdef.h"
48
49 namespace cv
50 {
51
52 /*!
53    Informative template class for OpenCV "scalars".
54
55    The class is specialized for each primitive numerical type supported by OpenCV (such as unsigned char or float),
56    as well as for more complex types, like cv::Complex<>, std::complex<>, cv::Vec<> etc.
57    The common property of all such types (called "scalars", do not confuse it with cv::Scalar_)
58    is that each of them is basically a tuple of numbers of the same type. Each "scalar" can be represented
59    by the depth id (CV_8U ... CV_64F) and the number of channels.
60    OpenCV matrices, 2D or nD, dense or sparse, can store "scalars",
61    as long as the number of channels does not exceed CV_CN_MAX.
62 */
63 template<typename _Tp> class DataType
64 {
65 public:
66     typedef _Tp         value_type;
67     typedef value_type  work_type;
68     typedef value_type  channel_type;
69     typedef value_type  vec_type;
70     enum { generic_type = 1,
71            depth        = -1,
72            channels     = 1,
73            fmt          = 0,
74            type = CV_MAKETYPE(depth, channels)
75          };
76 };
77
78 template<> class DataType<bool>
79 {
80 public:
81     typedef bool        value_type;
82     typedef int         work_type;
83     typedef value_type  channel_type;
84     typedef value_type  vec_type;
85     enum { generic_type = 0,
86            depth        = CV_8U,
87            channels     = 1,
88            fmt          = (int)'u',
89            type         = CV_MAKETYPE(depth, channels)
90          };
91 };
92
93 template<> class DataType<uchar>
94 {
95 public:
96     typedef uchar       value_type;
97     typedef int         work_type;
98     typedef value_type  channel_type;
99     typedef value_type  vec_type;
100     enum { generic_type = 0,
101            depth        = CV_8U,
102            channels     = 1,
103            fmt          = (int)'u',
104            type         = CV_MAKETYPE(depth, channels)
105          };
106 };
107
108 template<> class DataType<schar>
109 {
110 public:
111     typedef schar       value_type;
112     typedef int         work_type;
113     typedef value_type  channel_type;
114     typedef value_type  vec_type;
115     enum { generic_type = 0,
116            depth        = CV_8S,
117            channels     = 1,
118            fmt          = (int)'c',
119            type         = CV_MAKETYPE(depth, channels)
120          };
121 };
122
123 template<> class DataType<char>
124 {
125 public:
126     typedef schar       value_type;
127     typedef int         work_type;
128     typedef value_type  channel_type;
129     typedef value_type  vec_type;
130     enum { generic_type = 0,
131            depth        = CV_8S,
132            channels     = 1,
133            fmt          = (int)'c',
134            type         = CV_MAKETYPE(depth, channels)
135          };
136 };
137
138 template<> class DataType<ushort>
139 {
140 public:
141     typedef ushort      value_type;
142     typedef int         work_type;
143     typedef value_type  channel_type;
144     typedef value_type  vec_type;
145     enum { generic_type = 0,
146            depth        = CV_16U,
147            channels     = 1,
148            fmt          = (int)'w',
149            type         = CV_MAKETYPE(depth, channels)
150          };
151 };
152
153 template<> class DataType<short>
154 {
155 public:
156     typedef short       value_type;
157     typedef int         work_type;
158     typedef value_type  channel_type;
159     typedef value_type  vec_type;
160     enum { generic_type = 0,
161            depth        = CV_16S,
162            channels     = 1,
163            fmt          = (int)'s',
164            type         = CV_MAKETYPE(depth, channels)
165          };
166 };
167
168 template<> class DataType<int>
169 {
170 public:
171     typedef int         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_MAKETYPE(depth, channels)
180          };
181 };
182
183 template<> class DataType<float>
184 {
185 public:
186     typedef float       value_type;
187     typedef value_type  work_type;
188     typedef value_type  channel_type;
189     typedef value_type  vec_type;
190     enum { generic_type = 0,
191            depth        = CV_32F,
192            channels     = 1,
193            fmt          = (int)'f',
194            type         = CV_MAKETYPE(depth, channels)
195          };
196 };
197
198 template<> class DataType<double>
199 {
200 public:
201     typedef double      value_type;
202     typedef value_type  work_type;
203     typedef value_type  channel_type;
204     typedef value_type  vec_type;
205     enum { generic_type = 0,
206            depth        = CV_64F,
207            channels     = 1,
208            fmt          = (int)'d',
209            type         = CV_MAKETYPE(depth, channels)
210          };
211 };
212
213
214 /*!
215   A helper class for cv::DataType
216
217   The class is specialized for each fundamental numerical data type supported by OpenCV.
218   It provides DataDepth<T>::value constant.
219 */
220 template<typename _Tp> class DataDepth
221 {
222 public:
223     enum
224     {
225         value = DataType<_Tp>::depth,
226         fmt   = DataType<_Tp>::fmt
227     };
228 };
229
230
231
232 template<int _depth> class TypeDepth
233 {
234     enum { depth = CV_USRTYPE1 };
235     typedef void value_type;
236 };
237
238 template<> class TypeDepth<CV_8U>
239 {
240     enum { depth = CV_8U };
241     typedef uchar value_type;
242 };
243
244 template<> class TypeDepth<CV_8S>
245 {
246     enum { depth = CV_8S };
247     typedef schar value_type;
248 };
249
250 template<> class TypeDepth<CV_16U>
251 {
252     enum { depth = CV_16U };
253     typedef ushort value_type;
254 };
255
256 template<> class TypeDepth<CV_16S>
257 {
258     enum { depth = CV_16S };
259     typedef short value_type;
260 };
261
262 template<> class TypeDepth<CV_32S>
263 {
264     enum { depth = CV_32S };
265     typedef int value_type;
266 };
267
268 template<> class TypeDepth<CV_32F>
269 {
270     enum { depth = CV_32F };
271     typedef float value_type;
272 };
273
274 template<> class TypeDepth<CV_64F>
275 {
276     enum { depth = CV_64F };
277     typedef double value_type;
278 };
279
280 } // cv
281
282 #endif // __OPENCV_CORE_TRAITS_HPP__