48 #ifndef __OPENCV_CORE_INTERNAL_HPP__
49 #define __OPENCV_CORE_INTERNAL_HPP__
53 #if defined WIN32 || defined _WIN32
62 #if defined WIN32 || defined WINCE
63 #ifndef _WIN32_WINNT // This is needed for the declaration of TryEnterCriticalSection in winbase.h with Visual Studio 2005 (and older?)
64 #define _WIN32_WINNT 0x0400 // http://msdn.microsoft.com/en-us/library/ms686857(VS.85).aspx
83 #undef _CV_ALWAYS_PROFILE_
84 #define _CV_ALWAYS_NO_PROFILE_
94 #define __BEGIN__ __CV_BEGIN__
95 #define __END__ __CV_END__
96 #define EXIT __CV_EXIT__
101 CV_INLINE IppiSize ippiSize(
int width,
int height)
103 IppiSize size = { width, height };
108 #if defined __SSE2__ || _MSC_VER >= 1300
109 #include "emmintrin.h"
112 #if defined __SSE3__ || _MSC_VER >= 1500
113 #include "pmmintrin.h"
123 #define IPPI_CALL(func) CV_Assert((func) >= 0)
127 #include "tbb/tbb_stddef.h"
128 #if TBB_VERSION_MAJOR*100 + TBB_VERSION_MINOR >= 202
138 #include <Eigen/Core>
147 typedef tbb::blocked_range<int> BlockedRange;
149 template<
typename Body>
static inline
150 void parallel_for(
const BlockedRange& range,
const Body& body )
152 tbb::parallel_for(range, body);
155 template<
typename Iterator,
typename Body>
static inline
156 void parallel_do( Iterator first, Iterator last,
const Body& body )
158 tbb::parallel_do(first, last, body);
163 template<
typename Body>
static inline
164 void parallel_reduce(
const BlockedRange& range, Body& body )
166 tbb::parallel_reduce(range, body);
187 template<
typename Body>
static inline
188 void parallel_for(
const BlockedRange& range,
const Body& body )
193 template<
typename Iterator,
typename Body>
static inline
194 void parallel_do( Iterator first, Iterator last,
const Body& body )
196 for( ; first != last; ++first )
202 template<
typename Body>
static inline
203 void parallel_reduce(
const BlockedRange& range, Body& body )
214 #define CV_MAX_INLINE_MAT_OP_SIZE 10
217 #define CV_MAX_LOCAL_MAT_SIZE 32
220 #define CV_MAX_LOCAL_SIZE \
221 (CV_MAX_LOCAL_MAT_SIZE*CV_MAX_LOCAL_MAT_SIZE*(int)sizeof(double))
224 #define CV_DEFAULT_IMAGE_ROW_ALIGN 4
227 #define CV_DEFAULT_MAT_ROW_ALIGN 1
231 #define CV_MAX_ALLOC_SIZE (((size_t)1 << (sizeof(size_t)*8-2)))
234 #define CV_MALLOC_ALIGN 16
237 #define CV_STRUCT_ALIGN ((int)sizeof(double))
240 #define CV_STORAGE_BLOCK_SIZE ((1<<16) - 128)
243 #define CV_SPARSE_MAT_BLOCK (1<<12)
246 #define CV_SPARSE_HASH_SIZE0 (1<<10)
249 #define CV_SPARSE_HASH_RATIO 3
252 #define CV_MAX_STRLEN 1024
255 #define CV_CHECK_NANS( arr ) cvCheckArray((arr))
257 #define CV_CHECK_NANS( arr )
267 #define alloca __builtin_alloca
268 #elif defined WIN32 || defined _WIN32 || \
269 defined WINCE || defined _MSC_VER || defined __BORLANDC__
271 #elif defined HAVE_ALLOCA_H
273 #elif defined HAVE_ALLOCA
280 #define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
281 #elif defined _MSC_VER
282 #define CV_DECL_ALIGNED(x) __declspec(align(x))
284 #define CV_DECL_ALIGNED(x)
288 #define cvStackAlloc(size) cvAlignPtr( alloca((size) + CV_MALLOC_ALIGN), CV_MALLOC_ALIGN )
290 #if defined _MSC_VER || defined __BORLANDC__
291 #define CV_BIG_INT(n) n##I64
292 #define CV_BIG_UINT(n) n##UI64
294 #define CV_BIG_INT(n) n##LL
295 #define CV_BIG_UINT(n) n##ULL
299 #define CV_IMPL CV_EXTERN_C
302 #define CV_DBG_BREAK() { volatile int* crashMe = 0; *crashMe = 0; }
306 #define CV_STUB_STEP (1 << 30)
308 #define CV_SIZEOF_FLOAT ((int)sizeof(float))
309 #define CV_SIZEOF_SHORT ((int)sizeof(short))
311 #define CV_ORIGIN_TL 0
312 #define CV_ORIGIN_BL 1
315 #define CV_POS_INF 0x7f800000
316 #define CV_NEG_INF 0x807fffff
317 #define CV_1F 0x3f800000
318 #define CV_TOGGLE_FLT(x) ((x)^((int)(x) < 0 ? 0x7fffffff : 0))
319 #define CV_TOGGLE_DBL(x) \
320 ((x)^((int64)(x) < 0 ? CV_BIG_INT(0x7fffffffffffffff) : 0))
322 #define CV_NOP(a) (a)
323 #define CV_ADD(a, b) ((a) + (b))
324 #define CV_SUB(a, b) ((a) - (b))
325 #define CV_MUL(a, b) ((a) * (b))
326 #define CV_AND(a, b) ((a) & (b))
327 #define CV_OR(a, b) ((a) | (b))
328 #define CV_XOR(a, b) ((a) ^ (b))
329 #define CV_ANDN(a, b) (~(a) & (b))
330 #define CV_ORN(a, b) (~(a) | (b))
331 #define CV_SQR(a) ((a) * (a))
333 #define CV_LT(a, b) ((a) < (b))
334 #define CV_LE(a, b) ((a) <= (b))
335 #define CV_EQ(a, b) ((a) == (b))
336 #define CV_NE(a, b) ((a) != (b))
337 #define CV_GT(a, b) ((a) > (b))
338 #define CV_GE(a, b) ((a) >= (b))
340 #define CV_NONZERO(a) ((a) != 0)
341 #define CV_NONZERO_FLT(a) (((a)+(a)) != 0)
344 #define CV_CAST_8U(t) (uchar)(!((t) & ~255) ? (t) : (t) > 0 ? 255 : 0)
345 #define CV_CAST_8S(t) (schar)(!(((t)+128) & ~255) ? (t) : (t) > 0 ? 127 : -128)
346 #define CV_CAST_16U(t) (ushort)(!((t) & ~65535) ? (t) : (t) > 0 ? 65535 : 0)
347 #define CV_CAST_16S(t) (short)(!(((t)+32768) & ~65535) ? (t) : (t) > 0 ? 32767 : -32768)
348 #define CV_CAST_32S(t) (int)(t)
349 #define CV_CAST_64S(t) (int64)(t)
350 #define CV_CAST_32F(t) (float)(t)
351 #define CV_CAST_64F(t) (double)(t)
353 #define CV_PASTE2(a,b) a##b
354 #define CV_PASTE(a,b) CV_PASTE2(a,b)
357 #define CV_MAKE_STR(a) #a
359 #define CV_ZERO_OBJ(x) memset((x), 0, sizeof(*(x)))
361 #define CV_DIM(static_array) ((int)(sizeof(static_array)/sizeof((static_array)[0])))
363 #define cvUnsupportedFormat "Unsupported format"
367 assert( (align & (align-1)) == 0 );
368 return (
void*)( ((size_t)ptr + align - 1) & ~(size_t)(align-1) );
373 assert( (align & (align-1)) == 0 && size < INT_MAX );
374 return (size + align - 1) & -align;
385 #define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n))
386 #define CV_FLT_TO_FIX(x,n) cvRound((x)*(1<<(n)))
450 #define CV_IMPLEMENT_QSORT_EX( func_name, T, LT, user_data_type ) \
451 void func_name( T *array, size_t total, user_data_type aux ) \
453 int isort_thresh = 7; \
469 stack[0].lb = array; \
470 stack[0].ub = array + (total - 1); \
474 T* left = stack[sp].lb; \
475 T* right = stack[sp--].ub; \
479 int i, n = (int)(right - left) + 1, m; \
483 if( n <= isort_thresh ) \
486 for( ptr = left + 1; ptr <= right; ptr++ ) \
488 for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--) \
489 CV_SWAP( ptr2[0], ptr2[-1], t ); \
507 pivot = left + (n/2); \
512 a = left, b = left + d, c = left + 2*d; \
513 left = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
514 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
516 a = pivot - d, b = pivot, c = pivot + d; \
517 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
518 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
520 a = right - 2*d, b = right - d, c = right; \
521 right = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
522 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
525 a = left, b = pivot, c = right; \
526 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
527 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
528 if( pivot != left0 ) \
530 CV_SWAP( *pivot, *left0, t ); \
533 left = left1 = left0 + 1; \
534 right = right1 = right0; \
538 while( left <= right && !LT(*pivot, *left) ) \
540 if( !LT(*left, *pivot) ) \
543 CV_SWAP( *left1, *left, t ); \
550 while( left <= right && !LT(*right, *pivot) ) \
552 if( !LT(*pivot, *right) ) \
554 if( right < right1 ) \
555 CV_SWAP( *right1, *right, t ); \
564 CV_SWAP( *left, *right, t ); \
570 if( swap_cnt == 0 ) \
572 left = left0, right = right0; \
576 n = MIN( (int)(left1 - left0), (int)(left - left1) ); \
577 for( i = 0; i < n; i++ ) \
578 CV_SWAP( left0[i], left[i-n], t ); \
580 n = MIN( (int)(right0 - right1), (int)(right1 - right) ); \
581 for( i = 0; i < n; i++ ) \
582 CV_SWAP( left[i], right0[i-n+1], t ); \
583 n = (int)(left - left1); \
584 m = (int)(right1 - right); \
591 stack[++sp].lb = left0; \
592 stack[sp].ub = left0 + n - 1; \
593 left = right0 - m + 1, right = right0; \
597 stack[++sp].lb = right0 - m + 1; \
598 stack[sp].ub = right0; \
599 left = left0, right = left0 + n - 1; \
603 left = left0, right = left0 + n - 1; \
606 left = right0 - m + 1, right = right0; \
614 #define CV_IMPLEMENT_QSORT( func_name, T, cmp ) \
615 CV_IMPLEMENT_QSORT_EX( func_name, T, cmp, int )
661 #define CV_NOTHROW throw()
675 #define CV_INIT_FUNC_TAB( tab, FUNCNAME, FLAG ) \
676 (tab).fn_2d[CV_8U] = (void*)FUNCNAME##_8u##FLAG; \
677 (tab).fn_2d[CV_8S] = 0; \
678 (tab).fn_2d[CV_16U] = (void*)FUNCNAME##_16u##FLAG; \
679 (tab).fn_2d[CV_16S] = (void*)FUNCNAME##_16s##FLAG; \
680 (tab).fn_2d[CV_32S] = (void*)FUNCNAME##_32s##FLAG; \
681 (tab).fn_2d[CV_32F] = (void*)FUNCNAME##_32f##FLAG; \
682 (tab).fn_2d[CV_64F] = (void*)FUNCNAME##_64f##FLAG