47 #ifndef _CXCORE_MISC_H_
48 #define _CXCORE_MISC_H_
64 #define CV_MAX_INLINE_MAT_OP_SIZE 10
67 #define CV_MAX_LOCAL_MAT_SIZE 32
70 #define CV_MAX_LOCAL_SIZE \
71 (CV_MAX_LOCAL_MAT_SIZE*CV_MAX_LOCAL_MAT_SIZE*(int)sizeof(double))
74 #define CV_DEFAULT_IMAGE_ROW_ALIGN 4
77 #define CV_DEFAULT_MAT_ROW_ALIGN 1
81 #define CV_MAX_ALLOC_SIZE (((size_t)1 << (sizeof(size_t)*8-2)))
84 #define CV_MALLOC_ALIGN 32
87 #define CV_STRUCT_ALIGN ((int)sizeof(double))
90 #define CV_STORAGE_BLOCK_SIZE ((1<<16) - 128)
93 #define CV_SPARSE_MAT_BLOCK (1<<12)
96 #define CV_SPARSE_HASH_SIZE0 (1<<10)
99 #define CV_SPARSE_HASH_RATIO 3
102 #define CV_MAX_STRLEN 1024
106 #define CV_MAX_THREADS 128
108 #define CV_MAX_THREADS 1
112 #define CV_CHECK_NANS( arr ) cvCheckArray((arr))
114 #define CV_CHECK_NANS( arr )
124 #define alloca __builtin_alloca
125 #elif defined WIN32 || defined WIN64
126 #if defined _MSC_VER || defined __BORLANDC__
129 #elif defined HAVE_ALLOCA_H
131 #elif defined HAVE_ALLOCA
138 #define cvStackAlloc(size) cvAlignPtr( alloca((size) + CV_MALLOC_ALIGN), CV_MALLOC_ALIGN )
140 #if defined _MSC_VER || defined __BORLANDC__
141 #define CV_BIG_INT(n) n##I64
142 #define CV_BIG_UINT(n) n##UI64
144 #define CV_BIG_INT(n) n##LL
145 #define CV_BIG_UINT(n) n##ULL
148 #define CV_IMPL CV_EXTERN_C
150 #define CV_DBG_BREAK() { volatile int* crashMe = 0; *crashMe = 0; }
154 #define CV_STUB_STEP (1 << 30)
156 #define CV_SIZEOF_FLOAT ((int)sizeof(float))
157 #define CV_SIZEOF_SHORT ((int)sizeof(short))
159 #define CV_ORIGIN_TL 0
160 #define CV_ORIGIN_BL 1
163 #define CV_POS_INF 0x7f800000
164 #define CV_NEG_INF 0x807fffff
165 #define CV_1F 0x3f800000
166 #define CV_TOGGLE_FLT(x) ((x)^((int)(x) < 0 ? 0x7fffffff : 0))
167 #define CV_TOGGLE_DBL(x) \
168 ((x)^((int64)(x) < 0 ? CV_BIG_INT(0x7fffffffffffffff) : 0))
170 #define CV_NOP(a) (a)
171 #define CV_ADD(a, b) ((a) + (b))
172 #define CV_SUB(a, b) ((a) - (b))
173 #define CV_MUL(a, b) ((a) * (b))
174 #define CV_AND(a, b) ((a) & (b))
175 #define CV_OR(a, b) ((a) | (b))
176 #define CV_XOR(a, b) ((a) ^ (b))
177 #define CV_ANDN(a, b) (~(a) & (b))
178 #define CV_ORN(a, b) (~(a) | (b))
179 #define CV_SQR(a) ((a) * (a))
181 #define CV_LT(a, b) ((a) < (b))
182 #define CV_LE(a, b) ((a) <= (b))
183 #define CV_EQ(a, b) ((a) == (b))
184 #define CV_NE(a, b) ((a) != (b))
185 #define CV_GT(a, b) ((a) > (b))
186 #define CV_GE(a, b) ((a) >= (b))
188 #define CV_NONZERO(a) ((a) != 0)
189 #define CV_NONZERO_FLT(a) (((a)+(a)) != 0)
192 #define CV_CAST_8U(t) (uchar)(!((t) & ~255) ? (t) : (t) > 0 ? 255 : 0)
193 #define CV_CAST_8S(t) (schar)(!(((t)+128) & ~255) ? (t) : (t) > 0 ? 127 : -128)
194 #define CV_CAST_16U(t) (ushort)(!((t) & ~65535) ? (t) : (t) > 0 ? 65535 : 0)
195 #define CV_CAST_16S(t) (short)(!(((t)+32768) & ~65535) ? (t) : (t) > 0 ? 32767 : -32768)
196 #define CV_CAST_32S(t) (int)(t)
197 #define CV_CAST_64S(t) (int64)(t)
198 #define CV_CAST_32F(t) (float)(t)
199 #define CV_CAST_64F(t) (double)(t)
201 #define CV_PASTE2(a,b) a##b
202 #define CV_PASTE(a,b) CV_PASTE2(a,b)
205 #define CV_MAKE_STR(a) #a
207 #define CV_DEFINE_MASK \
208 float maskTab[2]; maskTab[0] = 0.f; maskTab[1] = 1.f;
209 #define CV_ANDMASK( m, x ) ((x) & (((m) == 0) - 1))
212 #define CV_MULMASK( m, x ) (maskTab[(m) != 0]*(x))
215 #define CV_MULMASK1( m, x ) (maskTab[(m)+1]*(x))
217 #define CV_ZERO_OBJ(x) memset((x), 0, sizeof(*(x)))
219 #define CV_DIM(static_array) ((int)(sizeof(static_array)/sizeof((static_array)[0])))
221 #define CV_UN_ENTRY_C1(worktype) \
222 worktype s0 = scalar[0]
224 #define CV_UN_ENTRY_C2(worktype) \
225 worktype s0 = scalar[0], s1 = scalar[1]
227 #define CV_UN_ENTRY_C3(worktype) \
228 worktype s0 = scalar[0], s1 = scalar[1], s2 = scalar[2]
230 #define CV_UN_ENTRY_C4(worktype) \
231 worktype s0 = scalar[0], s1 = scalar[1], s2 = scalar[2], s3 = scalar[3]
233 #define cvUnsupportedFormat "Unsupported format"
238 return (
void*)( ((size_t)ptr +
align - 1) & ~(size_t)(
align-1) );
243 assert( (align & (align-1)) == 0 && size < INT_MAX );
244 return (size + align - 1) & -
align;
253 #define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n))
254 #define CV_FLT_TO_FIX(x,n) cvRound((x)*(1<<(n)))
261 typedef struct CvFastDiv
267 #define CV_FAST_DIV_SHIFT 32
269 CV_INLINE CvFastDiv cvFastDiv(
int divisor )
276 fastdiv.divisor = divisor;
277 fastdiv.delta = (
unsigned)(((temp & 1) ^ 1) + divisor - 1);
278 fastdiv.scale = (
unsigned)((temp + 1) >> 1);
283 #define CV_FAST_DIV( x, fastdiv ) \
284 ((int)(((int64)((x)*2 + (int)(fastdiv).delta))*(int)(fastdiv).scale>>CV_FAST_DIV_SHIFT))
286 #define CV_FAST_UDIV( x, fastdiv ) \
287 ((int)(((uint64)((x)*2 + (fastdiv).delta))*(fastdiv).scale>>CV_FAST_DIV_SHIFT))
290 #define CV_MEMCPY_CHAR( dst, src, len ) \
292 size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
293 char* _icv_memcpy_dst_ = (char*)(dst); \
294 const char* _icv_memcpy_src_ = (const char*)(src); \
296 for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ ) \
297 _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_]; \
301 #define CV_MEMCPY_INT( dst, src, len ) \
303 size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
304 int* _icv_memcpy_dst_ = (int*)(dst); \
305 const int* _icv_memcpy_src_ = (const int*)(src); \
306 assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 && \
307 ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 ); \
309 for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++) \
310 _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_]; \
314 #define CV_MEMCPY_AUTO( dst, src, len ) \
316 size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
317 char* _icv_memcpy_dst_ = (char*)(dst); \
318 const char* _icv_memcpy_src_ = (const char*)(src); \
319 if( (_icv_memcpy_len_ & (sizeof(int)-1)) == 0 ) \
321 assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 && \
322 ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 ); \
323 for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; \
324 _icv_memcpy_i_+=sizeof(int) ) \
326 *(int*)(_icv_memcpy_dst_+_icv_memcpy_i_) = \
327 *(const int*)(_icv_memcpy_src_+_icv_memcpy_i_); \
332 for(_icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++)\
333 _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_]; \
338 #define CV_ZERO_CHAR( dst, len ) \
340 size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
341 char* _icv_memcpy_dst_ = (char*)(dst); \
343 for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ ) \
344 _icv_memcpy_dst_[_icv_memcpy_i_] = '\0'; \
348 #define CV_ZERO_INT( dst, len ) \
350 size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
351 int* _icv_memcpy_dst_ = (int*)(dst); \
352 assert( ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 ); \
354 for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++) \
355 _icv_memcpy_dst_[_icv_memcpy_i_] = 0; \
421 #define CV_IMPLEMENT_QSORT_EX( func_name, T, LT, user_data_type ) \
422 void func_name( T *array, size_t total, user_data_type aux ) \
424 int isort_thresh = 7; \
440 stack[0].lb = array; \
441 stack[0].ub = array + (total - 1); \
445 T* left = stack[sp].lb; \
446 T* right = stack[sp--].ub; \
450 int i, n = (int)(right - left) + 1, m; \
454 if( n <= isort_thresh ) \
457 for( ptr = left + 1; ptr <= right; ptr++ ) \
459 for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--) \
460 CV_SWAP( ptr2[0], ptr2[-1], t ); \
478 pivot = left + (n/2); \
483 a = left, b = left + d, c = left + 2*d; \
484 left = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
485 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
487 a = pivot - d, b = pivot, c = pivot + d; \
488 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
489 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
491 a = right - 2*d, b = right - d, c = right; \
492 right = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
493 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
496 a = left, b = pivot, c = right; \
497 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
498 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
499 if( pivot != left0 ) \
501 CV_SWAP( *pivot, *left0, t ); \
504 left = left1 = left0 + 1; \
505 right = right1 = right0; \
509 while( left <= right && !LT(*pivot, *left) ) \
511 if( !LT(*left, *pivot) ) \
514 CV_SWAP( *left1, *left, t ); \
521 while( left <= right && !LT(*right, *pivot) ) \
523 if( !LT(*pivot, *right) ) \
525 if( right < right1 ) \
526 CV_SWAP( *right1, *right, t ); \
535 CV_SWAP( *left, *right, t ); \
541 if( swap_cnt == 0 ) \
543 left = left0, right = right0; \
547 n = MIN( (int)(left1 - left0), (int)(left - left1) ); \
548 for( i = 0; i < n; i++ ) \
549 CV_SWAP( left0[i], left[i-n], t ); \
551 n = MIN( (int)(right0 - right1), (int)(right1 - right) ); \
552 for( i = 0; i < n; i++ ) \
553 CV_SWAP( left[i], right0[i-n+1], t ); \
554 n = (int)(left - left1); \
555 m = (int)(right1 - right); \
562 stack[++sp].lb = left0; \
563 stack[sp].ub = left0 + n - 1; \
564 left = right0 - m + 1, right = right0; \
568 stack[++sp].lb = right0 - m + 1; \
569 stack[sp].ub = right0; \
570 left = left0, right = left0 + n - 1; \
574 left = left0, right = left0 + n - 1; \
577 left = right0 - m + 1, right = right0; \
585 #define CV_IMPLEMENT_QSORT( func_name, T, cmp ) \
586 CV_IMPLEMENT_QSORT_EX( func_name, T, cmp, int )
632 #define CV_ERROR_FROM_STATUS( result ) \
633 CV_ERROR( cvErrorFromIppStatus( result ), "OpenCV function failed" )
635 #define IPPI_CALL( Func ) \
637 CvStatus ippi_call_result; \
638 ippi_call_result = Func; \
640 if( ippi_call_result < 0 ) \
641 CV_ERROR_FROM_STATUS( (ippi_call_result)); \
644 #define CV_PLUGIN_NONE 0
645 #define CV_PLUGIN_OPTCV 1
646 #define CV_PLUGIN_IPPCV 2
647 #define CV_PLUGIN_IPPI 3
648 #define CV_PLUGIN_IPPS 4
649 #define CV_PLUGIN_IPPVM 5
650 #define CV_PLUGIN_IPPCC 6
651 #define CV_PLUGIN_MKL 8
653 #define CV_PLUGIN_MAX 16
655 #define CV_PLUGINS1(lib1) ((lib1)&15)
656 #define CV_PLUGINS2(lib1,lib2) (((lib1)&15)|(((lib2)&15)<<4))
657 #define CV_PLUGINS3(lib1,lib2,lib3) (((lib1)&15)|(((lib2)&15)<<4)|(((lib2)&15)<<8))
659 #define CV_NOTHROW throw()
662 #define IPCVAPI(type,declspec,name,args) \
664 typedef type (declspec* name##_t) args; \
665 extern name##_t name##_p; \
666 type declspec name args;
669 #define IPCVAPI_EX(type,name,ipp_name,ipp_search_modules,args) \
670 IPCVAPI(type,CV_STDCALL,name,args)
672 #define IPCVAPI_C_EX(type,name,ipp_name,ipp_search_modules,args)\
673 IPCVAPI(type,CV_CDECL,name,args)
676 #define IPCVAPI_IMPL(type,name,args,arg_names) \
677 static type CV_STDCALL name##_f args; \
678 name##_t name##_p = name##_f; \
679 type CV_STDCALL name args { return name##_p arg_names; } \
680 static type CV_STDCALL name##_f args
732 void*
param,
int flag);
749 void* param3,
void* param4 );
754 void* param3,
void* param4 );
760 void* arr1,
int step1,
764 void* arr1,
int step1,
769 void* arr1,
int step1,
778 void* arr1,
int step1,
784 void*
param,
int flag );
789 void* param3,
void* param4 );
795 void* param3,
void* param4 );
798 void* arr1,
int step1,
802 void* arr1,
int step1,
803 void* arr2,
int step2,
807 void* arr1,
int step1,
808 void* arr2,
int step2,
812 void* arr1,
int step1,
813 void* arr2,
int step2,
818 void* arr1,
int step1,
819 void* arr2,
int step2,
820 void* arr3,
int step3,
825 #define CV_DEF_INIT_FUNC_TAB_2D( FUNCNAME, FLAG ) \
826 static void icvInit##FUNCNAME##FLAG##Table( CvFuncTable* tab ) \
830 tab->fn_2d[CV_8U] = (void*)icv##FUNCNAME##_8u_##FLAG; \
831 tab->fn_2d[CV_8S] = (void*)icv##FUNCNAME##_8s_##FLAG; \
832 tab->fn_2d[CV_16U] = (void*)icv##FUNCNAME##_16u_##FLAG; \
833 tab->fn_2d[CV_16S] = (void*)icv##FUNCNAME##_16s_##FLAG; \
834 tab->fn_2d[CV_32S] = (void*)icv##FUNCNAME##_32s_##FLAG; \
835 tab->fn_2d[CV_32F] = (void*)icv##FUNCNAME##_32f_##FLAG; \
836 tab->fn_2d[CV_64F] = (void*)icv##FUNCNAME##_64f_##FLAG; \
840 #define CV_DEF_INIT_BIG_FUNC_TAB_2D( FUNCNAME, FLAG ) \
841 static void icvInit##FUNCNAME##FLAG##Table( CvBigFuncTable* tab ) \
845 tab->fn_2d[CV_8UC1] = (void*)icv##FUNCNAME##_8u_C1##FLAG; \
846 tab->fn_2d[CV_8UC2] = (void*)icv##FUNCNAME##_8u_C2##FLAG; \
847 tab->fn_2d[CV_8UC3] = (void*)icv##FUNCNAME##_8u_C3##FLAG; \
848 tab->fn_2d[CV_8UC4] = (void*)icv##FUNCNAME##_8u_C4##FLAG; \
850 tab->fn_2d[CV_8SC1] = (void*)icv##FUNCNAME##_8s_C1##FLAG; \
851 tab->fn_2d[CV_8SC2] = (void*)icv##FUNCNAME##_8s_C2##FLAG; \
852 tab->fn_2d[CV_8SC3] = (void*)icv##FUNCNAME##_8s_C3##FLAG; \
853 tab->fn_2d[CV_8SC4] = (void*)icv##FUNCNAME##_8s_C4##FLAG; \
855 tab->fn_2d[CV_16UC1] = (void*)icv##FUNCNAME##_16u_C1##FLAG; \
856 tab->fn_2d[CV_16UC2] = (void*)icv##FUNCNAME##_16u_C2##FLAG; \
857 tab->fn_2d[CV_16UC3] = (void*)icv##FUNCNAME##_16u_C3##FLAG; \
858 tab->fn_2d[CV_16UC4] = (void*)icv##FUNCNAME##_16u_C4##FLAG; \
860 tab->fn_2d[CV_16SC1] = (void*)icv##FUNCNAME##_16s_C1##FLAG; \
861 tab->fn_2d[CV_16SC2] = (void*)icv##FUNCNAME##_16s_C2##FLAG; \
862 tab->fn_2d[CV_16SC3] = (void*)icv##FUNCNAME##_16s_C3##FLAG; \
863 tab->fn_2d[CV_16SC4] = (void*)icv##FUNCNAME##_16s_C4##FLAG; \
865 tab->fn_2d[CV_32SC1] = (void*)icv##FUNCNAME##_32s_C1##FLAG; \
866 tab->fn_2d[CV_32SC2] = (void*)icv##FUNCNAME##_32s_C2##FLAG; \
867 tab->fn_2d[CV_32SC3] = (void*)icv##FUNCNAME##_32s_C3##FLAG; \
868 tab->fn_2d[CV_32SC4] = (void*)icv##FUNCNAME##_32s_C4##FLAG; \
870 tab->fn_2d[CV_32FC1] = (void*)icv##FUNCNAME##_32f_C1##FLAG; \
871 tab->fn_2d[CV_32FC2] = (void*)icv##FUNCNAME##_32f_C2##FLAG; \
872 tab->fn_2d[CV_32FC3] = (void*)icv##FUNCNAME##_32f_C3##FLAG; \
873 tab->fn_2d[CV_32FC4] = (void*)icv##FUNCNAME##_32f_C4##FLAG; \
875 tab->fn_2d[CV_64FC1] = (void*)icv##FUNCNAME##_64f_C1##FLAG; \
876 tab->fn_2d[CV_64FC2] = (void*)icv##FUNCNAME##_64f_C2##FLAG; \
877 tab->fn_2d[CV_64FC3] = (void*)icv##FUNCNAME##_64f_C3##FLAG; \
878 tab->fn_2d[CV_64FC4] = (void*)icv##FUNCNAME##_64f_C4##FLAG; \
881 #define CV_DEF_INIT_FUNC_TAB_0D( FUNCNAME ) \
882 static void icvInit##FUNCNAME##Table( CvFuncTable* tab ) \
884 tab->fn_2d[CV_8U] = (void*)icv##FUNCNAME##_8u; \
885 tab->fn_2d[CV_8S] = (void*)icv##FUNCNAME##_8s; \
886 tab->fn_2d[CV_16U] = (void*)icv##FUNCNAME##_16u; \
887 tab->fn_2d[CV_16S] = (void*)icv##FUNCNAME##_16s; \
888 tab->fn_2d[CV_32S] = (void*)icv##FUNCNAME##_32s; \
889 tab->fn_2d[CV_32F] = (void*)icv##FUNCNAME##_32f; \
890 tab->fn_2d[CV_64F] = (void*)icv##FUNCNAME##_64f; \
893 #define CV_DEF_INIT_FUNC_TAB_1D CV_DEF_INIT_FUNC_TAB_0D
896 #define CV_DEF_INIT_PIXSIZE_TAB_2D( FUNCNAME, FLAG ) \
897 static void icvInit##FUNCNAME##FLAG##Table( CvBtFuncTable* table ) \
899 table->fn_2d[1] = (void*)icv##FUNCNAME##_8u_C1##FLAG; \
900 table->fn_2d[2] = (void*)icv##FUNCNAME##_8u_C2##FLAG; \
901 table->fn_2d[3] = (void*)icv##FUNCNAME##_8u_C3##FLAG; \
902 table->fn_2d[4] = (void*)icv##FUNCNAME##_16u_C2##FLAG; \
903 table->fn_2d[6] = (void*)icv##FUNCNAME##_16u_C3##FLAG; \
904 table->fn_2d[8] = (void*)icv##FUNCNAME##_32s_C2##FLAG; \
905 table->fn_2d[12] = (void*)icv##FUNCNAME##_32s_C3##FLAG; \
906 table->fn_2d[16] = (void*)icv##FUNCNAME##_64s_C2##FLAG; \
907 table->fn_2d[24] = (void*)icv##FUNCNAME##_64s_C3##FLAG; \
908 table->fn_2d[32] = (void*)icv##FUNCNAME##_64s_C4##FLAG; \
911 #define CV_GET_FUNC_PTR( func, table_entry ) \
912 func = (table_entry); \
915 CV_ERROR( CV_StsUnsupportedFormat, "" )