00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #ifndef __OPENCV_CORE_INTERNAL_HPP__
00049 #define __OPENCV_CORE_INTERNAL_HPP__
00050
00051 #include <vector>
00052
00053 #if defined WIN32 || defined _WIN32
00054 # ifndef WIN32
00055 # define WIN32
00056 # endif
00057 # ifndef _WIN32
00058 # define _WIN32
00059 # endif
00060 #endif
00061
00062 #if defined WIN32 || defined WINCE
00063 #ifndef _WIN32_WINNT // This is needed for the declaration of TryEnterCriticalSection in winbase.h with Visual Studio 2005 (and older?)
00064 #define _WIN32_WINNT 0x0400 // http://msdn.microsoft.com/en-us/library/ms686857(VS.85).aspx
00065 #endif
00066 #include <windows.h>
00067 #undef small
00068 #undef min
00069 #undef max
00070 #else
00071 #include <pthread.h>
00072 #include <sys/mman.h>
00073 #endif
00074
00075 #ifdef __BORLANDC__
00076 #ifndef WIN32
00077 #define WIN32
00078 #endif
00079 #ifndef _WIN32
00080 #define _WIN32
00081 #endif
00082 #define CV_DLL
00083 #undef _CV_ALWAYS_PROFILE_
00084 #define _CV_ALWAYS_NO_PROFILE_
00085 #endif
00086
00087 #ifndef FALSE
00088 #define FALSE 0
00089 #endif
00090 #ifndef TRUE
00091 #define TRUE 1
00092 #endif
00093
00094 #define __BEGIN__ __CV_BEGIN__
00095 #define __END__ __CV_END__
00096 #define EXIT __CV_EXIT__
00097
00098 #ifdef HAVE_IPP
00099 #include "ipp.h"
00100
00101 CV_INLINE IppiSize ippiSize(int width, int height)
00102 {
00103 IppiSize size = { width, height };
00104 return size;
00105 }
00106 #endif
00107
00108 #if defined __SSE2__ || _MSC_VER >= 1300
00109 #include "emmintrin.h"
00110 #define CV_SSE 1
00111 #define CV_SSE2 1
00112 #if defined __SSE3__ || _MSC_VER >= 1500
00113 #include "pmmintrin.h"
00114 #define CV_SSE3 1
00115 #endif
00116 #else
00117 #define CV_SSE 0
00118 #define CV_SSE2 0
00119 #define CV_SSE3 0
00120 #endif
00121
00122 #ifndef IPPI_CALL
00123 #define IPPI_CALL(func) CV_Assert((func) >= 0)
00124 #endif
00125
00126 #ifdef HAVE_TBB
00127 #include "tbb/tbb_stddef.h"
00128 #if TBB_VERSION_MAJOR*100 + TBB_VERSION_MINOR >= 202
00129 #include "tbb/tbb.h"
00130 #undef min
00131 #undef max
00132 #else
00133 #undef HAVE_TBB
00134 #endif
00135 #endif
00136
00137 #ifdef HAVE_EIGEN2
00138 #include <Eigen/Core>
00139 #include "opencv2/core/eigen.hpp"
00140 #endif
00141
00142 #ifdef __cplusplus
00143
00144 #ifdef HAVE_TBB
00145 namespace cv
00146 {
00147 typedef tbb::blocked_range<int> BlockedRange;
00148
00149 template<typename Body> static inline
00150 void parallel_for( const BlockedRange& range, const Body& body )
00151 {
00152 tbb::parallel_for(range, body);
00153 }
00154
00155 template<typename Iterator, typename Body> static inline
00156 void parallel_do( Iterator first, Iterator last, const Body& body )
00157 {
00158 tbb::parallel_do(first, last, body);
00159 }
00160
00161 typedef tbb::split Split;
00162
00163 template<typename Body> static inline
00164 void parallel_reduce( const BlockedRange& range, Body& body )
00165 {
00166 tbb::parallel_reduce(range, body);
00167 }
00168
00169 typedef tbb::concurrent_vector<Rect> ConcurrentRectVector;
00170 }
00171 #else
00172 namespace cv
00173 {
00174 class BlockedRange
00175 {
00176 public:
00177 BlockedRange() : _begin(0), _end(0), _grainsize(0) {}
00178 BlockedRange(int b, int e, int g=1) : _begin(b), _end(e), _grainsize(g) {}
00179 int begin() const { return _begin; }
00180 int end() const { return _end; }
00181 int grainsize() const { return _grainsize; }
00182
00183 protected:
00184 int _begin, _end, _grainsize;
00185 };
00186
00187 template<typename Body> static inline
00188 void parallel_for( const BlockedRange& range, const Body& body )
00189 {
00190 body(range);
00191 }
00192
00193 template<typename Iterator, typename Body> static inline
00194 void parallel_do( Iterator first, Iterator last, const Body& body )
00195 {
00196 for( ; first != last; ++first )
00197 body(*first);
00198 }
00199
00200 class Split {};
00201
00202 template<typename Body> static inline
00203 void parallel_reduce( const BlockedRange& range, Body& body )
00204 {
00205 body(range);
00206 }
00207
00208 typedef std::vector<Rect> ConcurrentRectVector;
00209 }
00210 #endif
00211 #endif
00212
00213
00214 #define CV_MAX_INLINE_MAT_OP_SIZE 10
00215
00216
00217 #define CV_MAX_LOCAL_MAT_SIZE 32
00218
00219
00220 #define CV_MAX_LOCAL_SIZE \
00221 (CV_MAX_LOCAL_MAT_SIZE*CV_MAX_LOCAL_MAT_SIZE*(int)sizeof(double))
00222
00223
00224 #define CV_DEFAULT_IMAGE_ROW_ALIGN 4
00225
00226
00227 #define CV_DEFAULT_MAT_ROW_ALIGN 1
00228
00229
00230
00231 #define CV_MAX_ALLOC_SIZE (((size_t)1 << (sizeof(size_t)*8-2)))
00232
00233
00234 #define CV_MALLOC_ALIGN 16
00235
00236
00237 #define CV_STRUCT_ALIGN ((int)sizeof(double))
00238
00239
00240 #define CV_STORAGE_BLOCK_SIZE ((1<<16) - 128)
00241
00242
00243 #define CV_SPARSE_MAT_BLOCK (1<<12)
00244
00245
00246 #define CV_SPARSE_HASH_SIZE0 (1<<10)
00247
00248
00249 #define CV_SPARSE_HASH_RATIO 3
00250
00251
00252 #define CV_MAX_STRLEN 1024
00253
00254 #if 0
00255 #define CV_CHECK_NANS( arr ) cvCheckArray((arr))
00256 #else
00257 #define CV_CHECK_NANS( arr )
00258 #endif
00259
00260
00261
00262
00263
00264
00265 #ifdef __GNUC__
00266 #undef alloca
00267 #define alloca __builtin_alloca
00268 #elif defined WIN32 || defined _WIN32 || \
00269 defined WINCE || defined _MSC_VER || defined __BORLANDC__
00270 #include <malloc.h>
00271 #elif defined HAVE_ALLOCA_H
00272 #include <alloca.h>
00273 #elif defined HAVE_ALLOCA
00274 #include <stdlib.h>
00275 #else
00276 #error "No alloca!"
00277 #endif
00278
00279 #ifdef __GNUC__
00280 #define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
00281 #elif defined _MSC_VER
00282 #define CV_DECL_ALIGNED(x) __declspec(align(x))
00283 #else
00284 #define CV_DECL_ALIGNED(x)
00285 #endif
00286
00287
00288 #define cvStackAlloc(size) cvAlignPtr( alloca((size) + CV_MALLOC_ALIGN), CV_MALLOC_ALIGN )
00289
00290 #if defined _MSC_VER || defined __BORLANDC__
00291 #define CV_BIG_INT(n) n##I64
00292 #define CV_BIG_UINT(n) n##UI64
00293 #else
00294 #define CV_BIG_INT(n) n##LL
00295 #define CV_BIG_UINT(n) n##ULL
00296 #endif
00297
00298 #ifndef CV_IMPL
00299 #define CV_IMPL CV_EXTERN_C
00300 #endif
00301
00302 #define CV_DBG_BREAK() { volatile int* crashMe = 0; *crashMe = 0; }
00303
00304
00305
00306 #define CV_STUB_STEP (1 << 30)
00307
00308 #define CV_SIZEOF_FLOAT ((int)sizeof(float))
00309 #define CV_SIZEOF_SHORT ((int)sizeof(short))
00310
00311 #define CV_ORIGIN_TL 0
00312 #define CV_ORIGIN_BL 1
00313
00314
00315 #define CV_POS_INF 0x7f800000
00316 #define CV_NEG_INF 0x807fffff
00317 #define CV_1F 0x3f800000
00318 #define CV_TOGGLE_FLT(x) ((x)^((int)(x) < 0 ? 0x7fffffff : 0))
00319 #define CV_TOGGLE_DBL(x) \
00320 ((x)^((int64)(x) < 0 ? CV_BIG_INT(0x7fffffffffffffff) : 0))
00321
00322 #define CV_NOP(a) (a)
00323 #define CV_ADD(a, b) ((a) + (b))
00324 #define CV_SUB(a, b) ((a) - (b))
00325 #define CV_MUL(a, b) ((a) * (b))
00326 #define CV_AND(a, b) ((a) & (b))
00327 #define CV_OR(a, b) ((a) | (b))
00328 #define CV_XOR(a, b) ((a) ^ (b))
00329 #define CV_ANDN(a, b) (~(a) & (b))
00330 #define CV_ORN(a, b) (~(a) | (b))
00331 #define CV_SQR(a) ((a) * (a))
00332
00333 #define CV_LT(a, b) ((a) < (b))
00334 #define CV_LE(a, b) ((a) <= (b))
00335 #define CV_EQ(a, b) ((a) == (b))
00336 #define CV_NE(a, b) ((a) != (b))
00337 #define CV_GT(a, b) ((a) > (b))
00338 #define CV_GE(a, b) ((a) >= (b))
00339
00340 #define CV_NONZERO(a) ((a) != 0)
00341 #define CV_NONZERO_FLT(a) (((a)+(a)) != 0)
00342
00343
00344 #define CV_CAST_8U(t) (uchar)(!((t) & ~255) ? (t) : (t) > 0 ? 255 : 0)
00345 #define CV_CAST_8S(t) (schar)(!(((t)+128) & ~255) ? (t) : (t) > 0 ? 127 : -128)
00346 #define CV_CAST_16U(t) (ushort)(!((t) & ~65535) ? (t) : (t) > 0 ? 65535 : 0)
00347 #define CV_CAST_16S(t) (short)(!(((t)+32768) & ~65535) ? (t) : (t) > 0 ? 32767 : -32768)
00348 #define CV_CAST_32S(t) (int)(t)
00349 #define CV_CAST_64S(t) (int64)(t)
00350 #define CV_CAST_32F(t) (float)(t)
00351 #define CV_CAST_64F(t) (double)(t)
00352
00353 #define CV_PASTE2(a,b) a##b
00354 #define CV_PASTE(a,b) CV_PASTE2(a,b)
00355
00356 #define CV_EMPTY
00357 #define CV_MAKE_STR(a) #a
00358
00359 #define CV_ZERO_OBJ(x) memset((x), 0, sizeof(*(x)))
00360
00361 #define CV_DIM(static_array) ((int)(sizeof(static_array)/sizeof((static_array)[0])))
00362
00363 #define cvUnsupportedFormat "Unsupported format"
00364
00365 CV_INLINE void* cvAlignPtr( const void* ptr, int align CV_DEFAULT(32) )
00366 {
00367 assert( (align & (align-1)) == 0 );
00368 return (void*)( ((size_t)ptr + align - 1) & ~(size_t)(align-1) );
00369 }
00370
00371 CV_INLINE int cvAlign( int size, int align )
00372 {
00373 assert( (align & (align-1)) == 0 && size < INT_MAX );
00374 return (size + align - 1) & -align;
00375 }
00376
00377 CV_INLINE CvSize cvGetMatSize( const CvMat* mat )
00378 {
00379 CvSize size;
00380 size.width = mat->cols;
00381 size.height = mat->rows;
00382 return size;
00383 }
00384
00385 #define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n))
00386 #define CV_FLT_TO_FIX(x,n) cvRound((x)*(1<<(n)))
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450 #define CV_IMPLEMENT_QSORT_EX( func_name, T, LT, user_data_type ) \
00451 void func_name( T *array, size_t total, user_data_type aux ) \
00452 { \
00453 int isort_thresh = 7; \
00454 T t; \
00455 int sp = 0; \
00456 \
00457 struct \
00458 { \
00459 T *lb; \
00460 T *ub; \
00461 } \
00462 stack[48]; \
00463 \
00464 aux = aux; \
00465 \
00466 if( total <= 1 ) \
00467 return; \
00468 \
00469 stack[0].lb = array; \
00470 stack[0].ub = array + (total - 1); \
00471 \
00472 while( sp >= 0 ) \
00473 { \
00474 T* left = stack[sp].lb; \
00475 T* right = stack[sp--].ub; \
00476 \
00477 for(;;) \
00478 { \
00479 int i, n = (int)(right - left) + 1, m; \
00480 T* ptr; \
00481 T* ptr2; \
00482 \
00483 if( n <= isort_thresh ) \
00484 { \
00485 insert_sort: \
00486 for( ptr = left + 1; ptr <= right; ptr++ ) \
00487 { \
00488 for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--) \
00489 CV_SWAP( ptr2[0], ptr2[-1], t ); \
00490 } \
00491 break; \
00492 } \
00493 else \
00494 { \
00495 T* left0; \
00496 T* left1; \
00497 T* right0; \
00498 T* right1; \
00499 T* pivot; \
00500 T* a; \
00501 T* b; \
00502 T* c; \
00503 int swap_cnt = 0; \
00504 \
00505 left0 = left; \
00506 right0 = right; \
00507 pivot = left + (n/2); \
00508 \
00509 if( n > 40 ) \
00510 { \
00511 int d = n / 8; \
00512 a = left, b = left + d, c = left + 2*d; \
00513 left = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
00514 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
00515 \
00516 a = pivot - d, b = pivot, c = pivot + d; \
00517 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
00518 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
00519 \
00520 a = right - 2*d, b = right - d, c = right; \
00521 right = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
00522 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
00523 } \
00524 \
00525 a = left, b = pivot, c = right; \
00526 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
00527 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
00528 if( pivot != left0 ) \
00529 { \
00530 CV_SWAP( *pivot, *left0, t ); \
00531 pivot = left0; \
00532 } \
00533 left = left1 = left0 + 1; \
00534 right = right1 = right0; \
00535 \
00536 for(;;) \
00537 { \
00538 while( left <= right && !LT(*pivot, *left) ) \
00539 { \
00540 if( !LT(*left, *pivot) ) \
00541 { \
00542 if( left > left1 ) \
00543 CV_SWAP( *left1, *left, t ); \
00544 swap_cnt = 1; \
00545 left1++; \
00546 } \
00547 left++; \
00548 } \
00549 \
00550 while( left <= right && !LT(*right, *pivot) ) \
00551 { \
00552 if( !LT(*pivot, *right) ) \
00553 { \
00554 if( right < right1 ) \
00555 CV_SWAP( *right1, *right, t ); \
00556 swap_cnt = 1; \
00557 right1--; \
00558 } \
00559 right--; \
00560 } \
00561 \
00562 if( left > right ) \
00563 break; \
00564 CV_SWAP( *left, *right, t ); \
00565 swap_cnt = 1; \
00566 left++; \
00567 right--; \
00568 } \
00569 \
00570 if( swap_cnt == 0 ) \
00571 { \
00572 left = left0, right = right0; \
00573 goto insert_sort; \
00574 } \
00575 \
00576 n = MIN( (int)(left1 - left0), (int)(left - left1) ); \
00577 for( i = 0; i < n; i++ ) \
00578 CV_SWAP( left0[i], left[i-n], t ); \
00579 \
00580 n = MIN( (int)(right0 - right1), (int)(right1 - right) ); \
00581 for( i = 0; i < n; i++ ) \
00582 CV_SWAP( left[i], right0[i-n+1], t ); \
00583 n = (int)(left - left1); \
00584 m = (int)(right1 - right); \
00585 if( n > 1 ) \
00586 { \
00587 if( m > 1 ) \
00588 { \
00589 if( n > m ) \
00590 { \
00591 stack[++sp].lb = left0; \
00592 stack[sp].ub = left0 + n - 1; \
00593 left = right0 - m + 1, right = right0; \
00594 } \
00595 else \
00596 { \
00597 stack[++sp].lb = right0 - m + 1; \
00598 stack[sp].ub = right0; \
00599 left = left0, right = left0 + n - 1; \
00600 } \
00601 } \
00602 else \
00603 left = left0, right = left0 + n - 1; \
00604 } \
00605 else if( m > 1 ) \
00606 left = right0 - m + 1, right = right0; \
00607 else \
00608 break; \
00609 } \
00610 } \
00611 } \
00612 }
00613
00614 #define CV_IMPLEMENT_QSORT( func_name, T, cmp ) \
00615 CV_IMPLEMENT_QSORT_EX( func_name, T, cmp, int )
00616
00617
00618
00619
00620
00621
00622 typedef enum CvStatus
00623 {
00624 CV_BADMEMBLOCK_ERR = -113,
00625 CV_INPLACE_NOT_SUPPORTED_ERR= -112,
00626 CV_UNMATCHED_ROI_ERR = -111,
00627 CV_NOTFOUND_ERR = -110,
00628 CV_BADCONVERGENCE_ERR = -109,
00629
00630 CV_BADDEPTH_ERR = -107,
00631 CV_BADROI_ERR = -106,
00632 CV_BADHEADER_ERR = -105,
00633 CV_UNMATCHED_FORMATS_ERR = -104,
00634 CV_UNSUPPORTED_COI_ERR = -103,
00635 CV_UNSUPPORTED_CHANNELS_ERR = -102,
00636 CV_UNSUPPORTED_DEPTH_ERR = -101,
00637 CV_UNSUPPORTED_FORMAT_ERR = -100,
00638
00639 CV_BADARG_ERR = -49,
00640 CV_NOTDEFINED_ERR = -48,
00641
00642 CV_BADCHANNELS_ERR = -47,
00643 CV_BADRANGE_ERR = -44,
00644 CV_BADSTEP_ERR = -29,
00645
00646 CV_BADFLAG_ERR = -12,
00647 CV_DIV_BY_ZERO_ERR = -11,
00648 CV_BADCOEF_ERR = -10,
00649
00650 CV_BADFACTOR_ERR = -7,
00651 CV_BADPOINT_ERR = -6,
00652 CV_BADSCALE_ERR = -4,
00653 CV_OUTOFMEM_ERR = -3,
00654 CV_NULLPTR_ERR = -2,
00655 CV_BADSIZE_ERR = -1,
00656 CV_NO_ERR = 0,
00657 CV_OK = CV_NO_ERR
00658 }
00659 CvStatus;
00660
00661 #define CV_NOTHROW throw()
00662
00663 typedef struct CvFuncTable
00664 {
00665 void* fn_2d[CV_DEPTH_MAX];
00666 }
00667 CvFuncTable;
00668
00669 typedef struct CvBigFuncTable
00670 {
00671 void* fn_2d[CV_DEPTH_MAX*4];
00672 }
00673 CvBigFuncTable;
00674
00675 #define CV_INIT_FUNC_TAB( tab, FUNCNAME, FLAG ) \
00676 (tab).fn_2d[CV_8U] = (void*)FUNCNAME##_8u##FLAG; \
00677 (tab).fn_2d[CV_8S] = 0; \
00678 (tab).fn_2d[CV_16U] = (void*)FUNCNAME##_16u##FLAG; \
00679 (tab).fn_2d[CV_16S] = (void*)FUNCNAME##_16s##FLAG; \
00680 (tab).fn_2d[CV_32S] = (void*)FUNCNAME##_32s##FLAG; \
00681 (tab).fn_2d[CV_32F] = (void*)FUNCNAME##_32f##FLAG; \
00682 (tab).fn_2d[CV_64F] = (void*)FUNCNAME##_64f##FLAG
00683
00684 #endif