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 //! @addtogroup core_basic
53 //! @{
54 
55 /** @brief Template "trait" class for OpenCV primitive data types.
56 
57 A primitive OpenCV data type is one of unsigned char, bool, signed char, unsigned short, signed
58 short, int, float, double, or a tuple of values of one of these types, where all the values in the
59 tuple have the same type. Any primitive type from the list can be defined by an identifier in the
60 form CV_\<bit-depth\>{U|S|F}C(\<number_of_channels\>), for example: uchar \~ CV_8UC1, 3-element
61 floating-point tuple \~ CV_32FC3, and so on. A universal OpenCV structure that is able to store a
62 single instance of such a primitive data type is Vec. Multiple instances of such a type can be
63 stored in a std::vector, Mat, Mat_, SparseMat, SparseMat_, or any other container that is able to
64 store Vec instances.
65 
66 The DataType class is basically used to provide a description of such primitive data types without
67 adding any fields or methods to the corresponding classes (and it is actually impossible to add
68 anything to primitive C/C++ data types). This technique is known in C++ as class traits. It is not
69 DataType itself that is used but its specialized versions, such as:
70 @code
71     template<> class DataType<uchar>
72     {
73         typedef uchar value_type;
74         typedef int work_type;
75         typedef uchar channel_type;
76         enum { channel_type = CV_8U, channels = 1, fmt='u', type = CV_8U };
77     };
78     ...
79     template<typename _Tp> DataType<std::complex<_Tp> >
80     {
81         typedef std::complex<_Tp> value_type;
82         typedef std::complex<_Tp> work_type;
83         typedef _Tp channel_type;
84         // DataDepth is another helper trait class
85         enum { depth = DataDepth<_Tp>::value, channels=2,
86             fmt=(channels-1)*256+DataDepth<_Tp>::fmt,
87             type=CV_MAKETYPE(depth, channels) };
88     };
89     ...
90 @endcode
91 The main purpose of this class is to convert compilation-time type information to an
92 OpenCV-compatible data type identifier, for example:
93 @code
94     // allocates a 30x40 floating-point matrix
95     Mat A(30, 40, DataType<float>::type);
96 
97     Mat B = Mat_<std::complex<double> >(3, 3);
98     // the statement below will print 6, 2 , that is depth == CV_64F, channels == 2
99     cout << B.depth() << ", " << B.channels() << endl;
100 @endcode
101 So, such traits are used to tell OpenCV which data type you are working with, even if such a type is
102 not native to OpenCV. For example, the matrix B initialization above is compiled because OpenCV
103 defines the proper specialized template class DataType\<complex\<_Tp\> \> . This mechanism is also
104 useful (and used in OpenCV this way) for generic algorithms implementations.
105 */
106 template<typename _Tp> class DataType
107 {
108 public:
109     typedef _Tp         value_type;
110     typedef value_type  work_type;
111     typedef value_type  channel_type;
112     typedef value_type  vec_type;
113     enum { generic_type = 1,
114            depth        = -1,
115            channels     = 1,
116            fmt          = 0,
117            type = CV_MAKETYPE(depth, channels)
118          };
119 };
120 
121 template<> class DataType<bool>
122 {
123 public:
124     typedef bool        value_type;
125     typedef int         work_type;
126     typedef value_type  channel_type;
127     typedef value_type  vec_type;
128     enum { generic_type = 0,
129            depth        = CV_8U,
130            channels     = 1,
131            fmt          = (int)'u',
132            type         = CV_MAKETYPE(depth, channels)
133          };
134 };
135 
136 template<> class DataType<uchar>
137 {
138 public:
139     typedef uchar       value_type;
140     typedef int         work_type;
141     typedef value_type  channel_type;
142     typedef value_type  vec_type;
143     enum { generic_type = 0,
144            depth        = CV_8U,
145            channels     = 1,
146            fmt          = (int)'u',
147            type         = CV_MAKETYPE(depth, channels)
148          };
149 };
150 
151 template<> class DataType<schar>
152 {
153 public:
154     typedef schar       value_type;
155     typedef int         work_type;
156     typedef value_type  channel_type;
157     typedef value_type  vec_type;
158     enum { generic_type = 0,
159            depth        = CV_8S,
160            channels     = 1,
161            fmt          = (int)'c',
162            type         = CV_MAKETYPE(depth, channels)
163          };
164 };
165 
166 template<> class DataType<char>
167 {
168 public:
169     typedef schar       value_type;
170     typedef int         work_type;
171     typedef value_type  channel_type;
172     typedef value_type  vec_type;
173     enum { generic_type = 0,
174            depth        = CV_8S,
175            channels     = 1,
176            fmt          = (int)'c',
177            type         = CV_MAKETYPE(depth, channels)
178          };
179 };
180 
181 template<> class DataType<ushort>
182 {
183 public:
184     typedef ushort      value_type;
185     typedef int         work_type;
186     typedef value_type  channel_type;
187     typedef value_type  vec_type;
188     enum { generic_type = 0,
189            depth        = CV_16U,
190            channels     = 1,
191            fmt          = (int)'w',
192            type         = CV_MAKETYPE(depth, channels)
193          };
194 };
195 
196 template<> class DataType<short>
197 {
198 public:
199     typedef short       value_type;
200     typedef int         work_type;
201     typedef value_type  channel_type;
202     typedef value_type  vec_type;
203     enum { generic_type = 0,
204            depth        = CV_16S,
205            channels     = 1,
206            fmt          = (int)'s',
207            type         = CV_MAKETYPE(depth, channels)
208          };
209 };
210 
211 template<> class DataType<int>
212 {
213 public:
214     typedef int         value_type;
215     typedef value_type  work_type;
216     typedef value_type  channel_type;
217     typedef value_type  vec_type;
218     enum { generic_type = 0,
219            depth        = CV_32S,
220            channels     = 1,
221            fmt          = (int)'i',
222            type         = CV_MAKETYPE(depth, channels)
223          };
224 };
225 
226 template<> class DataType<float>
227 {
228 public:
229     typedef float       value_type;
230     typedef value_type  work_type;
231     typedef value_type  channel_type;
232     typedef value_type  vec_type;
233     enum { generic_type = 0,
234            depth        = CV_32F,
235            channels     = 1,
236            fmt          = (int)'f',
237            type         = CV_MAKETYPE(depth, channels)
238          };
239 };
240 
241 template<> class DataType<double>
242 {
243 public:
244     typedef double      value_type;
245     typedef value_type  work_type;
246     typedef value_type  channel_type;
247     typedef value_type  vec_type;
248     enum { generic_type = 0,
249            depth        = CV_64F,
250            channels     = 1,
251            fmt          = (int)'d',
252            type         = CV_MAKETYPE(depth, channels)
253          };
254 };
255 
256 
257 /** @brief A helper class for cv::DataType
258 
259 The class is specialized for each fundamental numerical data type supported by OpenCV. It provides
260 DataDepth<T>::value constant.
261 */
262 template<typename _Tp> class DataDepth
263 {
264 public:
265     enum
266     {
267         value = DataType<_Tp>::depth,
268         fmt   = DataType<_Tp>::fmt
269     };
270 };
271 
272 
273 
274 template<int _depth> class TypeDepth
275 {
276     enum { depth = CV_USRTYPE1 };
277     typedef void value_type;
278 };
279 
280 template<> class TypeDepth<CV_8U>
281 {
282     enum { depth = CV_8U };
283     typedef uchar value_type;
284 };
285 
286 template<> class TypeDepth<CV_8S>
287 {
288     enum { depth = CV_8S };
289     typedef schar value_type;
290 };
291 
292 template<> class TypeDepth<CV_16U>
293 {
294     enum { depth = CV_16U };
295     typedef ushort value_type;
296 };
297 
298 template<> class TypeDepth<CV_16S>
299 {
300     enum { depth = CV_16S };
301     typedef short value_type;
302 };
303 
304 template<> class TypeDepth<CV_32S>
305 {
306     enum { depth = CV_32S };
307     typedef int value_type;
308 };
309 
310 template<> class TypeDepth<CV_32F>
311 {
312     enum { depth = CV_32F };
313     typedef float value_type;
314 };
315 
316 template<> class TypeDepth<CV_64F>
317 {
318     enum { depth = CV_64F };
319     typedef double value_type;
320 };
321 
322 //! @}
323 
324 } // cv
325 
326 #endif // __OPENCV_CORE_TRAITS_HPP__
327