1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #ifndef __OPENCV_PRECOMP_H__
44 #define __OPENCV_PRECOMP_H__
46 #if defined _MSC_VER && _MSC_VER >= 1200
47 // disable warnings related to inline functions
48 #pragma warning( disable: 4251 4711 4710 4514 )
55 #include "opencv2/core/core.hpp"
56 #include "opencv2/core/core_c.h"
57 #include "opencv2/core/internal.hpp"
68 #define CV_MEMCPY_CHAR( dst, src, len ) \
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); \
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_]; \
79 #define CV_MEMCPY_INT( dst, src, len ) \
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 ); \
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_];\
92 #define CV_MEMCPY_AUTO( dst, src, len ) \
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 ) \
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) ) \
104 *(int*)(_icv_memcpy_dst_+_icv_memcpy_i_) = \
105 *(const int*)(_icv_memcpy_src_+_icv_memcpy_i_); \
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_]; \
116 #define CV_ZERO_CHAR( dst, len ) \
118 size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
119 char* _icv_memcpy_dst_ = (char*)(dst); \
121 for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ ) \
122 _icv_memcpy_dst_[_icv_memcpy_i_] = '\0'; \
126 #define CV_ZERO_INT( dst, len ) \
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 ); \
132 for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++) \
133 _icv_memcpy_dst_[_icv_memcpy_i_] = 0; \
140 extern const float g_8x32fTab[];
141 #define CV_8TO32F(x) cv::g_8x32fTab[(x)+128]
143 extern const ushort g_8x16uSqrTab[];
144 #define CV_SQR_8U(x) cv::g_8x16uSqrTab[(x)+255]
146 extern const char* g_HersheyGlyphs[];
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)))
153 typedef void (*CopyMaskFunc)(const Mat& src, Mat& dst, const Mat& mask);
155 extern CopyMaskFunc g_copyMaskFuncTab[];
157 static inline CopyMaskFunc getCopyMaskFunc(int esz)
159 CV_Assert( (unsigned)esz <= 32U );
160 CopyMaskFunc func = g_copyMaskFuncTab[esz];
161 CV_Assert( func != 0 );
165 #if defined WIN32 || defined _WIN32
166 void deleteThreadAllocData();
167 void deleteThreadRNGData();
171 template<typename T1, typename T2=T1, typename T3=T1> struct OpAdd
176 T3 operator ()(T1 a, T2 b) const { return saturate_cast<T3>(a + b); }
179 template<typename T1, typename T2=T1, typename T3=T1> struct OpSub
184 T3 operator ()(T1 a, T2 b) const { return saturate_cast<T3>(a - b); }
187 template<typename T1, typename T2=T1, typename T3=T1> struct OpRSub
192 T3 operator ()(T1 a, T2 b) const { return saturate_cast<T3>(b - a); }
195 template<typename T1, typename T2=T1, typename T3=T1> struct OpMul
200 T3 operator ()(T1 a, T2 b) const { return saturate_cast<T3>(a * b); }
203 template<typename T1, typename T2=T1, typename T3=T1> struct OpDiv
208 T3 operator ()(T1 a, T2 b) const { return saturate_cast<T3>(a / b); }
211 template<typename T> struct OpMin
216 T operator ()(T a, T b) const { return std::min(a, b); }
219 template<typename T> struct OpMax
224 T operator ()(T a, T b) const { return std::max(a, b); }
227 inline Size getContinuousSize( const Mat& m1, int widthScale=1 )
229 return m1.isContinuous() ? Size(m1.cols*m1.rows*widthScale, 1) :
230 Size(m1.cols*widthScale, m1.rows);
233 inline Size getContinuousSize( const Mat& m1, const Mat& m2, int widthScale=1 )
235 return (m1.flags & m2.flags & Mat::CONTINUOUS_FLAG) != 0 ?
236 Size(m1.cols*m1.rows*widthScale, 1) : Size(m1.cols*widthScale, m1.rows);
239 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
240 const Mat& m3, int widthScale=1 )
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);
246 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
247 const Mat& m3, const Mat& m4,
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);
254 inline Size getContinuousSize( const Mat& m1, const Mat& m2,
255 const Mat& m3, const Mat& m4,
256 const Mat& m5, int widthScale=1 )
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);
264 int operator()(const void*, const void*, void*, int) const { return 0; }
268 template<class Op, class VecOp> static void
269 binaryOpC1_( const Mat& srcmat1, const Mat& srcmat2, Mat& dstmat )
272 typedef typename Op::type1 T1;
273 typedef typename Op::type2 T2;
274 typedef typename Op::rtype DT;
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() );
284 if( size.width == 1 )
286 for( ; size.height--; src1 += step1, src2 += step2, dst += step )
287 dst[0] = op( src1[0], src2[0] );
291 for( ; size.height--; src1 += step1, src2 += step2, dst += step )
293 int x = vecOp(src1, src2, dst, size.width);
294 for( ; x <= size.width - 4; x += 4 )
297 f0 = op( src1[x], src2[x] );
298 f1 = op( src1[x+1], src2[x+1] );
301 f0 = op(src1[x+2], src2[x+2]);
302 f1 = op(src1[x+3], src2[x+3]);
307 for( ; x < size.width; x++ )
308 dst[x] = op( src1[x], src2[x] );
312 typedef void (*BinaryFunc)(const Mat& src1, const Mat& src2, Mat& dst);
314 template<class Op> static void
315 binarySOpCn_( const Mat& srcmat, Mat& dstmat, const Scalar& _scalar )
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 );
328 scalarToRawData(_scalar, scalar, CV_MAKETYPE(DataType<WT>::depth,cn), 12);
330 for( ; size.height--; src0 += step1, dst0 += step )
332 int i, len = size.width;
336 for( ; (len -= 12) >= 0; dst += 12, src += 12 )
338 DT t0 = op(src[0], scalar[0]);
339 DT t1 = op(src[1], scalar[1]);
340 dst[0] = t0; dst[1] = t1;
342 t0 = op(src[2], scalar[2]);
343 t1 = op(src[3], scalar[3]);
344 dst[2] = t0; dst[3] = t1;
346 t0 = op(src[4], scalar[4]);
347 t1 = op(src[5], scalar[5]);
348 dst[4] = t0; dst[5] = t1;
350 t0 = op(src[6], scalar[6]);
351 t1 = op(src[7], scalar[7]);
352 dst[6] = t0; dst[7] = t1;
354 t0 = op(src[8], scalar[8]);
355 t1 = op(src[9], scalar[9]);
356 dst[8] = t0; dst[9] = t1;
358 t0 = op(src[10], scalar[10]);
359 t1 = op(src[11], scalar[11]);
360 dst[10] = t0; dst[11] = t1;
363 for( (len) += 12, i = 0; i < (len); i++ )
364 dst[i] = op((WT)src[i], scalar[i]);
368 template<class Op> static void
369 binarySOpC1_( const Mat& srcmat, Mat& dstmat, double _scalar )
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();
382 size.width *= srcmat.channels();
383 if( srcmat.isContinuous() && dstmat.isContinuous() )
385 size.width *= size.height;
389 for( ; size.height--; src += step1, dst += step )
392 for( x = 0; x <= size.width - 4; x += 4 )
394 DT f0 = op( src[x], scalar );
395 DT f1 = op( src[x+1], scalar );
398 f0 = op( src[x+2], scalar );
399 f1 = op( src[x+3], scalar );
404 for( ; x < size.width; x++ )
405 dst[x] = op( src[x], scalar );
409 typedef void (*BinarySFuncCn)(const Mat& src1, Mat& dst, const Scalar& scalar);
410 typedef void (*BinarySFuncC1)(const Mat& src1, Mat& dst, double scalar);
414 #endif /*_CXCORE_INTERNAL_H_*/