d85d41b4b1ae628f042b23eb0bf941769abecc86
[profile/ivi/opencv.git] / modules / core / src / precomp.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 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #ifndef __OPENCV_PRECOMP_H__
44 #define __OPENCV_PRECOMP_H__
45
46 #if defined _MSC_VER && _MSC_VER >= 1200
47     // disable warnings related to inline functions
48     #pragma warning( disable: 4251 4711 4710 4514 )
49 #endif
50
51 #ifdef HAVE_CONFIG_H 
52 #include <cvconfig.h> 
53 #endif
54
55 #include "opencv2/core/core.hpp"
56 #include "opencv2/core/core_c.h"
57 #include "opencv2/core/internal.hpp"
58
59 #include <assert.h>
60 #include <ctype.h>
61 #include <float.h>
62 #include <limits.h>
63 #include <math.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67
68 #define CV_MEMCPY_CHAR( dst, src, len )                 \
69 {                                                       \
70     size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);    \
71     char* _icv_memcpy_dst_ = (char*)(dst);              \
72     const char* _icv_memcpy_src_ = (const char*)(src);  \
73                                                         \
74     for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ )  \
75         _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_];        \
76 }
77
78
79 #define CV_MEMCPY_INT( dst, src, len )                  \
80 {                                                       \
81     size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);    \
82     int* _icv_memcpy_dst_ = (int*)(dst);                \
83     const int* _icv_memcpy_src_ = (const int*)(src);    \
84     assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 && \
85     ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 );  \
86                                                         \
87     for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++)  \
88         _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_];\
89 }
90
91
92 #define CV_MEMCPY_AUTO( dst, src, len )                                             \
93 {                                                                                   \
94     size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);                                \
95     char* _icv_memcpy_dst_ = (char*)(dst);                                          \
96     const char* _icv_memcpy_src_ = (const char*)(src);                              \
97     if( (_icv_memcpy_len_ & (sizeof(int)-1)) == 0 )                                 \
98     {                                                                               \
99         assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 &&                  \
100                 ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 );                  \
101         for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_;                 \
102             _icv_memcpy_i_+=sizeof(int) )                                           \
103         {                                                                           \
104             *(int*)(_icv_memcpy_dst_+_icv_memcpy_i_) =                              \
105             *(const int*)(_icv_memcpy_src_+_icv_memcpy_i_);                         \
106         }                                                                           \
107     }                                                                               \
108     else                                                                            \
109     {                                                                               \
110         for(_icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++)\
111             _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_];    \
112     }                                                                               \
113 }
114
115
116 #define CV_ZERO_CHAR( dst, len )                        \
117 {                                                       \
118     size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);    \
119     char* _icv_memcpy_dst_ = (char*)(dst);              \
120                                                         \
121     for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ )  \
122         _icv_memcpy_dst_[_icv_memcpy_i_] = '\0';        \
123 }
124
125
126 #define CV_ZERO_INT( dst, len )                                                     \
127 {                                                                                   \
128     size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);                                \
129     int* _icv_memcpy_dst_ = (int*)(dst);                                            \
130     assert( ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 );                      \
131                                                                                     \
132     for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++)          \
133         _icv_memcpy_dst_[_icv_memcpy_i_] = 0;                                       \
134 }
135
136 namespace cv
137 {
138
139 // -128.f ... 255.f
140 extern const float g_8x32fTab[];
141 #define CV_8TO32F(x)  cv::g_8x32fTab[(x)+128]
142
143 extern const ushort g_8x16uSqrTab[];
144 #define CV_SQR_8U(x)  cv::g_8x16uSqrTab[(x)+255]
145
146 extern const char* g_HersheyGlyphs[];
147
148 extern const uchar g_Saturate8u[];
149 #define CV_FAST_CAST_8U(t)   (assert(-256 <= (t) && (t) <= 512), cv::g_Saturate8u[(t)+256])
150 #define CV_MIN_8U(a,b)       ((a) - CV_FAST_CAST_8U((a) - (b)))
151 #define CV_MAX_8U(a,b)       ((a) + CV_FAST_CAST_8U((b) - (a)))
152
153 typedef void (*CopyMaskFunc)(const Mat& src, Mat& dst, const Mat& mask);
154
155 extern CopyMaskFunc g_copyMaskFuncTab[];
156
157 static inline CopyMaskFunc getCopyMaskFunc(int esz)
158 {
159     CV_Assert( (unsigned)esz <= 32U );
160     CopyMaskFunc func = g_copyMaskFuncTab[esz];
161     CV_Assert( func != 0 );
162     return func;
163 }
164
165 #if defined WIN32 || defined _WIN32
166 void deleteThreadAllocData();
167 void deleteThreadRNGData();
168 #endif
169
170     
171 template<typename T1, typename T2=T1, typename T3=T1> struct OpAdd
172 {
173     typedef T1 type1;
174     typedef T2 type2;
175     typedef T3 rtype;
176     T3 operator ()(T1 a, T2 b) const { return saturate_cast<T3>(a + b); }
177 };
178
179 template<typename T1, typename T2=T1, typename T3=T1> struct OpSub
180 {
181     typedef T1 type1;
182     typedef T2 type2;
183     typedef T3 rtype;
184     T3 operator ()(T1 a, T2 b) const { return saturate_cast<T3>(a - b); }
185 };
186
187 template<typename T1, typename T2=T1, typename T3=T1> struct OpRSub
188 {
189     typedef T1 type1;
190     typedef T2 type2;
191     typedef T3 rtype;
192     T3 operator ()(T1 a, T2 b) const { return saturate_cast<T3>(b - a); }
193 };
194
195 template<typename T1, typename T2=T1, typename T3=T1> struct OpMul
196 {
197     typedef T1 type1;
198     typedef T2 type2;
199     typedef T3 rtype;
200     T3 operator ()(T1 a, T2 b) const { return saturate_cast<T3>(a * b); }
201 };
202
203 template<typename T1, typename T2=T1, typename T3=T1> struct OpDiv
204 {
205     typedef T1 type1;
206     typedef T2 type2;
207     typedef T3 rtype;
208     T3 operator ()(T1 a, T2 b) const { return saturate_cast<T3>(a / b); }
209 };
210
211 template<typename T> struct OpMin
212 {
213     typedef T type1;
214     typedef T type2;
215     typedef T rtype;
216     T operator ()(T a, T b) const { return std::min(a, b); }
217 };
218
219 template<typename T> struct OpMax
220 {
221     typedef T type1;
222     typedef T type2;
223     typedef T rtype;
224     T operator ()(T a, T b) const { return std::max(a, b); }
225 };
226
227 inline Size getContinuousSize( const Mat& m1, int widthScale=1 )
228 {
229     return m1.isContinuous() ? Size(m1.cols*m1.rows*widthScale, 1) :
230         Size(m1.cols*widthScale, m1.rows);
231 }
232
233 inline Size getContinuousSize( const Mat& m1, const Mat& m2, int widthScale=1 )
234 {
235     return (m1.flags & m2.flags & Mat::CONTINUOUS_FLAG) != 0 ?
236         Size(m1.cols*m1.rows*widthScale, 1) : Size(m1.cols*widthScale, m1.rows);
237 }
238
239 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
240                                const Mat& m3, int widthScale=1 )
241 {
242     return (m1.flags & m2.flags & m3.flags & Mat::CONTINUOUS_FLAG) != 0 ?
243         Size(m1.cols*m1.rows*widthScale, 1) : Size(m1.cols*widthScale, m1.rows);
244 }
245
246 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
247                                const Mat& m3, const Mat& m4,
248                                int widthScale=1 )
249 {
250     return (m1.flags & m2.flags & m3.flags & m4.flags & Mat::CONTINUOUS_FLAG) != 0 ?
251         Size(m1.cols*m1.rows*widthScale, 1) : Size(m1.cols*widthScale, m1.rows);
252 }
253
254 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
255                                const Mat& m3, const Mat& m4,
256                                const Mat& m5, int widthScale=1 )
257 {
258     return (m1.flags & m2.flags & m3.flags & m4.flags & m5.flags & Mat::CONTINUOUS_FLAG) != 0 ?
259         Size(m1.cols*m1.rows*widthScale, 1) : Size(m1.cols*widthScale, m1.rows);
260 }
261
262 struct NoVec
263 {
264     int operator()(const void*, const void*, void*, int) const { return 0; }
265 };
266     
267     
268 template<class Op, class VecOp> static void
269 binaryOpC1_( const Mat& srcmat1, const Mat& srcmat2, Mat& dstmat )
270 {
271     Op op; VecOp vecOp;
272     typedef typename Op::type1 T1;
273     typedef typename Op::type2 T2;
274     typedef typename Op::rtype DT;
275
276     const T1* src1 = (const T1*)srcmat1.data;
277     const T2* src2 = (const T2*)srcmat2.data;
278     DT* dst = (DT*)dstmat.data;
279     size_t step1 = srcmat1.step/sizeof(src1[0]);
280     size_t step2 = srcmat2.step/sizeof(src2[0]);
281     size_t step = dstmat.step/sizeof(dst[0]);
282     Size size = getContinuousSize( srcmat1, srcmat2, dstmat, dstmat.channels() );
283
284     if( size.width == 1 )
285     {
286         for( ; size.height--; src1 += step1, src2 += step2, dst += step )
287             dst[0] = op( src1[0], src2[0] );
288         return;
289     }
290
291     for( ; size.height--; src1 += step1, src2 += step2, dst += step )
292     {
293         int x = vecOp(src1, src2, dst, size.width);
294         for( ; x <= size.width - 4; x += 4 )
295         {
296             DT f0, f1;
297             f0 = op( src1[x], src2[x] );
298             f1 = op( src1[x+1], src2[x+1] );
299             dst[x] = f0;
300             dst[x+1] = f1;
301             f0 = op(src1[x+2], src2[x+2]);
302             f1 = op(src1[x+3], src2[x+3]);
303             dst[x+2] = f0;
304             dst[x+3] = f1;
305         }
306
307         for( ; x < size.width; x++ )
308             dst[x] = op( src1[x], src2[x] );
309     }
310 }
311
312 typedef void (*BinaryFunc)(const Mat& src1, const Mat& src2, Mat& dst);
313
314 template<class Op> static void
315 binarySOpCn_( const Mat& srcmat, Mat& dstmat, const Scalar& _scalar )
316 {
317     Op op;
318     typedef typename Op::type1 T;
319     typedef typename Op::type2 WT;
320     typedef typename Op::rtype DT;
321     const T* src0 = (const T*)srcmat.data;
322     DT* dst0 = (DT*)dstmat.data;
323     size_t step1 = srcmat.step/sizeof(src0[0]);
324     size_t step = dstmat.step/sizeof(dst0[0]);
325     int cn = dstmat.channels();
326     Size size = getContinuousSize( srcmat, dstmat, cn );
327     WT scalar[12];
328     scalarToRawData(_scalar, scalar, CV_MAKETYPE(DataType<WT>::depth,cn), 12);
329
330     for( ; size.height--; src0 += step1, dst0 += step )
331     {
332         int i, len = size.width;
333         const T* src = src0;
334         T* dst = dst0;
335
336         for( ; (len -= 12) >= 0; dst += 12, src += 12 )
337         {
338             DT t0 = op(src[0], scalar[0]);
339             DT t1 = op(src[1], scalar[1]);
340             dst[0] = t0; dst[1] = t1;
341
342             t0 = op(src[2], scalar[2]);
343             t1 = op(src[3], scalar[3]);
344             dst[2] = t0; dst[3] = t1;
345
346             t0 = op(src[4], scalar[4]);
347             t1 = op(src[5], scalar[5]);
348             dst[4] = t0; dst[5] = t1;
349
350             t0 = op(src[6], scalar[6]);
351             t1 = op(src[7], scalar[7]);
352             dst[6] = t0; dst[7] = t1;
353
354             t0 = op(src[8], scalar[8]);
355             t1 = op(src[9], scalar[9]);
356             dst[8] = t0; dst[9] = t1;
357
358             t0 = op(src[10], scalar[10]);
359             t1 = op(src[11], scalar[11]);
360             dst[10] = t0; dst[11] = t1;
361         }
362
363         for( (len) += 12, i = 0; i < (len); i++ )
364             dst[i] = op((WT)src[i], scalar[i]);
365     }
366 }
367
368 template<class Op> static void
369 binarySOpC1_( const Mat& srcmat, Mat& dstmat, double _scalar )
370 {
371     Op op;
372     typedef typename Op::type1 T;
373     typedef typename Op::type2 WT;
374     typedef typename Op::rtype DT;
375     WT scalar = saturate_cast<WT>(_scalar);
376     const T* src = (const T*)srcmat.data;
377     DT* dst = (DT*)dstmat.data;
378     size_t step1 = srcmat.step/sizeof(src[0]);
379     size_t step = dstmat.step/sizeof(dst[0]);
380     Size size = srcmat.size();
381
382     size.width *= srcmat.channels();
383     if( srcmat.isContinuous() && dstmat.isContinuous() )
384     {
385         size.width *= size.height;
386         size.height = 1;
387     }
388
389     for( ; size.height--; src += step1, dst += step )
390     {
391         int x;
392         for( x = 0; x <= size.width - 4; x += 4 )
393         {
394             DT f0 = op( src[x], scalar );
395             DT f1 = op( src[x+1], scalar );
396             dst[x] = f0;
397             dst[x+1] = f1;
398             f0 = op( src[x+2], scalar );
399             f1 = op( src[x+3], scalar );
400             dst[x+2] = f0;
401             dst[x+3] = f1;
402         }
403
404         for( ; x < size.width; x++ )
405             dst[x] = op( src[x], scalar );
406     }
407 }
408
409 typedef void (*BinarySFuncCn)(const Mat& src1, Mat& dst, const Scalar& scalar);
410 typedef void (*BinarySFuncC1)(const Mat& src1, Mat& dst, double scalar);
411
412 }
413
414 #endif /*_CXCORE_INTERNAL_H_*/