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 // Intel License Agreement 11 // For Open Source Computer Vision Library 12 // 13 // Copyright (C) 2000, Intel Corporation, all rights reserved. 14 // Third party copyrights are property of their respective owners. 15 // 16 // Redistribution and use in source and binary forms, with or without modification, 17 // are permitted provided that the following conditions are met: 18 // 19 // * Redistribution's of source code must retain the above copyright notice, 20 // this list of conditions and the following disclaimer. 21 // 22 // * Redistribution's in binary form must reproduce the above copyright notice, 23 // this list of conditions and the following disclaimer in the documentation 24 // and/or other materials provided with the distribution. 25 // 26 // * The name of Intel Corporation may not be used to endorse or promote products 27 // derived from this software without specific prior written permission. 28 // 29 // This software is provided by the copyright holders and contributors "as is" and 30 // any express or implied warranties, including, but not limited to, the implied 31 // warranties of merchantability and fitness for a particular purpose are disclaimed. 32 // In no event shall the Intel Corporation or contributors be liable for any direct, 33 // indirect, incidental, special, exemplary, or consequential damages 34 // (including, but not limited to, procurement of substitute goods or services; 35 // loss of use, data, or profits; or business interruption) however caused 36 // and on any theory of liability, whether in contract, strict liability, 37 // or tort (including negligence or otherwise) arising in any way out of 38 // the use of this software, even if advised of the possibility of such damage. 39 // 40 //M*/ 41 42 /* The header is mostly for internal use and it is likely to change. 43 It contains some macro definitions that are used in cxcore, cv, cvaux 44 and, probably, other libraries. If you need some of this functionality, 45 the safe way is to copy it into your code and rename the macros. 46 */ 47 #ifndef _CXCORE_MISC_H_ 48 #define _CXCORE_MISC_H_ 49 50 #ifdef HAVE_CONFIG_H 51 #include "cvconfig.h" 52 #endif 53 54 #include <limits.h> 55 #ifdef _OPENMP 56 #include "omp.h" 57 #endif 58 59 /****************************************************************************************\ 60 * Compile-time tuning parameters * 61 \****************************************************************************************/ 62 63 /* maximal size of vector to run matrix operations on it inline (i.e. w/o ipp calls) */ 64 #define CV_MAX_INLINE_MAT_OP_SIZE 10 65 66 /* maximal linear size of matrix to allocate it on stack. */ 67 #define CV_MAX_LOCAL_MAT_SIZE 32 68 69 /* maximal size of local memory storage */ 70 #define CV_MAX_LOCAL_SIZE \ 71 (CV_MAX_LOCAL_MAT_SIZE*CV_MAX_LOCAL_MAT_SIZE*(int)sizeof(double)) 72 73 /* default image row align (in bytes) */ 74 #define CV_DEFAULT_IMAGE_ROW_ALIGN 4 75 76 /* matrices are continuous by default */ 77 #define CV_DEFAULT_MAT_ROW_ALIGN 1 78 79 /* maximum size of dynamic memory buffer. 80 cvAlloc reports an error if a larger block is requested. */ 81 #define CV_MAX_ALLOC_SIZE (((size_t)1 << (sizeof(size_t)*8-2))) 82 83 /* the alignment of all the allocated buffers */ 84 #define CV_MALLOC_ALIGN 32 85 86 /* default alignment for dynamic data strucutures, resided in storages. */ 87 #define CV_STRUCT_ALIGN ((int)sizeof(double)) 88 89 /* default storage block size */ 90 #define CV_STORAGE_BLOCK_SIZE ((1<<16) - 128) 91 92 /* default memory block for sparse array elements */ 93 #define CV_SPARSE_MAT_BLOCK (1<<12) 94 95 /* initial hash table size */ 96 #define CV_SPARSE_HASH_SIZE0 (1<<10) 97 98 /* maximal average node_count/hash_size ratio beyond which hash table is resized */ 99 #define CV_SPARSE_HASH_RATIO 3 100 101 /* max length of strings */ 102 #define CV_MAX_STRLEN 1024 103 104 /* maximum possible number of threads in parallel implementations */ 105 #ifdef _OPENMP 106 #define CV_MAX_THREADS 128 107 #else 108 #define CV_MAX_THREADS 1 109 #endif 110 111 #if 0 /*def CV_CHECK_FOR_NANS*/ 112 #define CV_CHECK_NANS( arr ) cvCheckArray((arr)) 113 #else 114 #define CV_CHECK_NANS( arr ) 115 #endif 116 117 /****************************************************************************************\ 118 * Common declarations * 119 \****************************************************************************************/ 120 121 /* get alloca declaration */ 122 #ifdef __GNUC__ 123 #undef alloca 124 #define alloca __builtin_alloca 125 #elif defined WIN32 || defined WIN64 126 #if defined _MSC_VER || defined __BORLANDC__ 127 #include <malloc.h> 128 #endif 129 #elif defined HAVE_ALLOCA_H 130 #include <alloca.h> 131 #elif defined HAVE_ALLOCA 132 #include <stdlib.h> 133 #else 134 #error 135 #endif 136 137 /* ! DO NOT make it an inline function */ 138 #define cvStackAlloc(size) cvAlignPtr( alloca((size) + CV_MALLOC_ALIGN), CV_MALLOC_ALIGN ) 139 140 #if defined _MSC_VER || defined __BORLANDC__ 141 #define CV_BIG_INT(n) n##I64 142 #define CV_BIG_UINT(n) n##UI64 143 #else 144 #define CV_BIG_INT(n) n##LL 145 #define CV_BIG_UINT(n) n##ULL 146 #endif 147 148 #define CV_IMPL CV_EXTERN_C 149 150 #define CV_DBG_BREAK() { volatile int* crashMe = 0; *crashMe = 0; } 151 152 /* default step, set in case of continuous data 153 to work around checks for valid step in some ipp functions */ 154 #define CV_STUB_STEP (1 << 30) 155 156 #define CV_SIZEOF_FLOAT ((int)sizeof(float)) 157 #define CV_SIZEOF_SHORT ((int)sizeof(short)) 158 159 #define CV_ORIGIN_TL 0 160 #define CV_ORIGIN_BL 1 161 162 /* IEEE754 constants and macros */ 163 #define CV_POS_INF 0x7f800000 164 #define CV_NEG_INF 0x807fffff /* CV_TOGGLE_FLT(0xff800000) */ 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)) 169 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)) 180 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)) 187 188 #define CV_NONZERO(a) ((a) != 0) 189 #define CV_NONZERO_FLT(a) (((a)+(a)) != 0) 190 191 /* general-purpose saturation macros */ 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) 200 201 #define CV_PASTE2(a,b) a##b 202 #define CV_PASTE(a,b) CV_PASTE2(a,b) 203 204 #define CV_EMPTY 205 #define CV_MAKE_STR(a) #a 206 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)) 210 211 /* (x) * ((m) == 1 ? 1.f : (m) == 0 ? 0.f : <ERR> */ 212 #define CV_MULMASK( m, x ) (maskTab[(m) != 0]*(x)) 213 214 /* (x) * ((m) == -1 ? 1.f : (m) == 0 ? 0.f : <ERR> */ 215 #define CV_MULMASK1( m, x ) (maskTab[(m)+1]*(x)) 216 217 #define CV_ZERO_OBJ(x) memset((x), 0, sizeof(*(x))) 218 219 #define CV_DIM(static_array) ((int)(sizeof(static_array)/sizeof((static_array)[0]))) 220 221 #define CV_UN_ENTRY_C1(worktype) \ 222 worktype s0 = scalar[0] 223 224 #define CV_UN_ENTRY_C2(worktype) \ 225 worktype s0 = scalar[0], s1 = scalar[1] 226 227 #define CV_UN_ENTRY_C3(worktype) \ 228 worktype s0 = scalar[0], s1 = scalar[1], s2 = scalar[2] 229 230 #define CV_UN_ENTRY_C4(worktype) \ 231 worktype s0 = scalar[0], s1 = scalar[1], s2 = scalar[2], s3 = scalar[3] 232 233 #define cvUnsupportedFormat "Unsupported format" 234 235 CV_INLINE void* cvAlignPtr( const void* ptr, int align=32 ) 236 { 237 assert( (align & (align-1)) == 0 ); 238 return (void*)( ((size_t)ptr + align - 1) & ~(size_t)(align-1) ); 239 } 240 241 CV_INLINE int cvAlign( int size, int align ) 242 { 243 assert( (align & (align-1)) == 0 && size < INT_MAX ); 244 return (size + align - 1) & -align; 245 } 246 247 CV_INLINE CvSize cvGetMatSize( const CvMat* mat ) 248 { 249 CvSize size = { mat->width, mat->height }; 250 return size; 251 } 252 253 #define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n)) 254 #define CV_FLT_TO_FIX(x,n) cvRound((x)*(1<<(n))) 255 256 #if 0 257 /* This is a small engine for performing fast division of multiple numbers 258 by the same constant. Most compilers do it too if they know the divisor value 259 at compile-time. The algorithm was taken from Agner Fog's optimization guide 260 at http://www.agner.org/assem */ 261 typedef struct CvFastDiv 262 { 263 unsigned delta, scale, divisor; 264 } 265 CvFastDiv; 266 267 #define CV_FAST_DIV_SHIFT 32 268 269 CV_INLINE CvFastDiv cvFastDiv( int divisor ) 270 { 271 CvFastDiv fastdiv; 272 273 assert( divisor >= 1 ); 274 uint64 temp = ((uint64)1 << CV_FAST_DIV_SHIFT)/divisor; 275 276 fastdiv.divisor = divisor; 277 fastdiv.delta = (unsigned)(((temp & 1) ^ 1) + divisor - 1); 278 fastdiv.scale = (unsigned)((temp + 1) >> 1); 279 280 return fastdiv; 281 } 282 283 #define CV_FAST_DIV( x, fastdiv ) \ 284 ((int)(((int64)((x)*2 + (int)(fastdiv).delta))*(int)(fastdiv).scale>>CV_FAST_DIV_SHIFT)) 285 286 #define CV_FAST_UDIV( x, fastdiv ) \ 287 ((int)(((uint64)((x)*2 + (fastdiv).delta))*(fastdiv).scale>>CV_FAST_DIV_SHIFT)) 288 #endif 289 290 #define CV_MEMCPY_CHAR( dst, src, len ) \ 291 { \ 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); \ 295 \ 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_]; \ 298 } 299 300 301 #define CV_MEMCPY_INT( dst, src, len ) \ 302 { \ 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 ); \ 308 \ 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_]; \ 311 } 312 313 314 #define CV_MEMCPY_AUTO( dst, src, len ) \ 315 { \ 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 ) \ 320 { \ 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) ) \ 325 { \ 326 *(int*)(_icv_memcpy_dst_+_icv_memcpy_i_) = \ 327 *(const int*)(_icv_memcpy_src_+_icv_memcpy_i_); \ 328 } \ 329 } \ 330 else \ 331 { \ 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_]; \ 334 } \ 335 } 336 337 338 #define CV_ZERO_CHAR( dst, len ) \ 339 { \ 340 size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \ 341 char* _icv_memcpy_dst_ = (char*)(dst); \ 342 \ 343 for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ ) \ 344 _icv_memcpy_dst_[_icv_memcpy_i_] = '\0'; \ 345 } 346 347 348 #define CV_ZERO_INT( dst, len ) \ 349 { \ 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 ); \ 353 \ 354 for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++) \ 355 _icv_memcpy_dst_[_icv_memcpy_i_] = 0; \ 356 } 357 358 359 /****************************************************************************************\ 360 361 Generic implementation of QuickSort algorithm. 362 ---------------------------------------------- 363 Using this macro user can declare customized sort function that can be much faster 364 than built-in qsort function because of lower overhead on elements 365 comparison and exchange. The macro takes less_than (or LT) argument - a macro or function 366 that takes 2 arguments returns non-zero if the first argument should be before the second 367 one in the sorted sequence and zero otherwise. 368 369 Example: 370 371 Suppose that the task is to sort points by ascending of y coordinates and if 372 y's are equal x's should ascend. 373 374 The code is: 375 ------------------------------------------------------------------------------ 376 #define cmp_pts( pt1, pt2 ) \ 377 ((pt1).y < (pt2).y || ((pt1).y < (pt2).y && (pt1).x < (pt2).x)) 378 379 [static] CV_IMPLEMENT_QSORT( icvSortPoints, CvPoint, cmp_pts ) 380 ------------------------------------------------------------------------------ 381 382 After that the function "void icvSortPoints( CvPoint* array, size_t total, int aux );" 383 is available to user. 384 385 aux is an additional parameter, which can be used when comparing elements. 386 The current implementation was derived from *BSD system qsort(): 387 388 * Copyright (c) 1992, 1993 389 * The Regents of the University of California. All rights reserved. 390 * 391 * Redistribution and use in source and binary forms, with or without 392 * modification, are permitted provided that the following conditions 393 * are met: 394 * 1. Redistributions of source code must retain the above copyright 395 * notice, this list of conditions and the following disclaimer. 396 * 2. Redistributions in binary form must reproduce the above copyright 397 * notice, this list of conditions and the following disclaimer in the 398 * documentation and/or other materials provided with the distribution. 399 * 3. All advertising materials mentioning features or use of this software 400 * must display the following acknowledgement: 401 * This product includes software developed by the University of 402 * California, Berkeley and its contributors. 403 * 4. Neither the name of the University nor the names of its contributors 404 * may be used to endorse or promote products derived from this software 405 * without specific prior written permission. 406 * 407 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 408 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 409 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 410 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 411 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 412 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 413 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 414 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 415 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 416 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 417 * SUCH DAMAGE. 418 419 \****************************************************************************************/ 420 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 ) \ 423 { \ 424 int isort_thresh = 7; \ 425 T t; \ 426 int sp = 0; \ 427 \ 428 struct \ 429 { \ 430 T *lb; \ 431 T *ub; \ 432 } \ 433 stack[48]; \ 434 \ 435 aux = aux; \ 436 \ 437 if( total <= 1 ) \ 438 return; \ 439 \ 440 stack[0].lb = array; \ 441 stack[0].ub = array + (total - 1); \ 442 \ 443 while( sp >= 0 ) \ 444 { \ 445 T* left = stack[sp].lb; \ 446 T* right = stack[sp--].ub; \ 447 \ 448 for(;;) \ 449 { \ 450 int i, n = (int)(right - left) + 1, m; \ 451 T* ptr; \ 452 T* ptr2; \ 453 \ 454 if( n <= isort_thresh ) \ 455 { \ 456 insert_sort: \ 457 for( ptr = left + 1; ptr <= right; ptr++ ) \ 458 { \ 459 for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--) \ 460 CV_SWAP( ptr2[0], ptr2[-1], t ); \ 461 } \ 462 break; \ 463 } \ 464 else \ 465 { \ 466 T* left0; \ 467 T* left1; \ 468 T* right0; \ 469 T* right1; \ 470 T* pivot; \ 471 T* a; \ 472 T* b; \ 473 T* c; \ 474 int swap_cnt = 0; \ 475 \ 476 left0 = left; \ 477 right0 = right; \ 478 pivot = left + (n/2); \ 479 \ 480 if( n > 40 ) \ 481 { \ 482 int d = n / 8; \ 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)); \ 486 \ 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)); \ 490 \ 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)); \ 494 } \ 495 \ 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 ) \ 500 { \ 501 CV_SWAP( *pivot, *left0, t ); \ 502 pivot = left0; \ 503 } \ 504 left = left1 = left0 + 1; \ 505 right = right1 = right0; \ 506 \ 507 for(;;) \ 508 { \ 509 while( left <= right && !LT(*pivot, *left) ) \ 510 { \ 511 if( !LT(*left, *pivot) ) \ 512 { \ 513 if( left > left1 ) \ 514 CV_SWAP( *left1, *left, t ); \ 515 swap_cnt = 1; \ 516 left1++; \ 517 } \ 518 left++; \ 519 } \ 520 \ 521 while( left <= right && !LT(*right, *pivot) ) \ 522 { \ 523 if( !LT(*pivot, *right) ) \ 524 { \ 525 if( right < right1 ) \ 526 CV_SWAP( *right1, *right, t ); \ 527 swap_cnt = 1; \ 528 right1--; \ 529 } \ 530 right--; \ 531 } \ 532 \ 533 if( left > right ) \ 534 break; \ 535 CV_SWAP( *left, *right, t ); \ 536 swap_cnt = 1; \ 537 left++; \ 538 right--; \ 539 } \ 540 \ 541 if( swap_cnt == 0 ) \ 542 { \ 543 left = left0, right = right0; \ 544 goto insert_sort; \ 545 } \ 546 \ 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 ); \ 550 \ 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); \ 556 if( n > 1 ) \ 557 { \ 558 if( m > 1 ) \ 559 { \ 560 if( n > m ) \ 561 { \ 562 stack[++sp].lb = left0; \ 563 stack[sp].ub = left0 + n - 1; \ 564 left = right0 - m + 1, right = right0; \ 565 } \ 566 else \ 567 { \ 568 stack[++sp].lb = right0 - m + 1; \ 569 stack[sp].ub = right0; \ 570 left = left0, right = left0 + n - 1; \ 571 } \ 572 } \ 573 else \ 574 left = left0, right = left0 + n - 1; \ 575 } \ 576 else if( m > 1 ) \ 577 left = right0 - m + 1, right = right0; \ 578 else \ 579 break; \ 580 } \ 581 } \ 582 } \ 583 } 584 585 #define CV_IMPLEMENT_QSORT( func_name, T, cmp ) \ 586 CV_IMPLEMENT_QSORT_EX( func_name, T, cmp, int ) 587 588 /****************************************************************************************\ 589 * Structures and macros for integration with IPP * 590 \****************************************************************************************/ 591 592 /* IPP-compatible return codes */ 593 typedef enum CvStatus 594 { 595 CV_BADMEMBLOCK_ERR = -113, 596 CV_INPLACE_NOT_SUPPORTED_ERR= -112, 597 CV_UNMATCHED_ROI_ERR = -111, 598 CV_NOTFOUND_ERR = -110, 599 CV_BADCONVERGENCE_ERR = -109, 600 601 CV_BADDEPTH_ERR = -107, 602 CV_BADROI_ERR = -106, 603 CV_BADHEADER_ERR = -105, 604 CV_UNMATCHED_FORMATS_ERR = -104, 605 CV_UNSUPPORTED_COI_ERR = -103, 606 CV_UNSUPPORTED_CHANNELS_ERR = -102, 607 CV_UNSUPPORTED_DEPTH_ERR = -101, 608 CV_UNSUPPORTED_FORMAT_ERR = -100, 609 610 CV_BADARG_ERR = -49, //ipp comp 611 CV_NOTDEFINED_ERR = -48, //ipp comp 612 613 CV_BADCHANNELS_ERR = -47, //ipp comp 614 CV_BADRANGE_ERR = -44, //ipp comp 615 CV_BADSTEP_ERR = -29, //ipp comp 616 617 CV_BADFLAG_ERR = -12, 618 CV_DIV_BY_ZERO_ERR = -11, //ipp comp 619 CV_BADCOEF_ERR = -10, 620 621 CV_BADFACTOR_ERR = -7, 622 CV_BADPOINT_ERR = -6, 623 CV_BADSCALE_ERR = -4, 624 CV_OUTOFMEM_ERR = -3, 625 CV_NULLPTR_ERR = -2, 626 CV_BADSIZE_ERR = -1, 627 CV_NO_ERR = 0, 628 CV_OK = CV_NO_ERR 629 } 630 CvStatus; 631 632 #define CV_ERROR_FROM_STATUS( result ) \ 633 CV_ERROR( cvErrorFromIppStatus( result ), "OpenCV function failed" ) 634 635 #define IPPI_CALL( Func ) \ 636 { \ 637 CvStatus ippi_call_result; \ 638 ippi_call_result = Func; \ 639 \ 640 if( ippi_call_result < 0 ) \ 641 CV_ERROR_FROM_STATUS( (ippi_call_result)); \ 642 } 643 644 #define CV_PLUGIN_NONE 0 645 #define CV_PLUGIN_OPTCV 1 /* custom "emerged" ippopencv library */ 646 #define CV_PLUGIN_IPPCV 2 /* IPP: computer vision */ 647 #define CV_PLUGIN_IPPI 3 /* IPP: image processing */ 648 #define CV_PLUGIN_IPPS 4 /* IPP: signal processing */ 649 #define CV_PLUGIN_IPPVM 5 /* IPP: vector math functions */ 650 #define CV_PLUGIN_IPPCC 6 /* IPP: color space conversion */ 651 #define CV_PLUGIN_MKL 8 /* Intel Math Kernel Library */ 652 653 #define CV_PLUGIN_MAX 16 654 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)) 658 659 #define CV_NOTHROW throw() 660 661 #ifndef IPCVAPI 662 #define IPCVAPI(type,declspec,name,args) \ 663 /* function pointer */ \ 664 typedef type (declspec* name##_t) args; \ 665 extern name##_t name##_p; \ 666 type declspec name args; 667 #endif 668 669 #define IPCVAPI_EX(type,name,ipp_name,ipp_search_modules,args) \ 670 IPCVAPI(type,CV_STDCALL,name,args) 671 672 #define IPCVAPI_C_EX(type,name,ipp_name,ipp_search_modules,args)\ 673 IPCVAPI(type,CV_CDECL,name,args) 674 675 #ifndef IPCVAPI_IMPL 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 681 #endif 682 683 /* IPP types' enumeration */ 684 typedef enum CvDataType { 685 cv1u, 686 cv8u, cv8s, 687 cv16u, cv16s, cv16sc, 688 cv32u, cv32s, cv32sc, 689 cv32f, cv32fc, 690 cv64u, cv64s, cv64sc, 691 cv64f, cv64fc 692 } CvDataType; 693 694 typedef enum CvHintAlgorithm { 695 cvAlgHintNone, 696 cvAlgHintFast, 697 cvAlgHintAccurate 698 } CvHintAlgorithm; 699 700 typedef enum CvCmpOp { 701 cvCmpLess, 702 cvCmpLessEq, 703 cvCmpEq, 704 cvCmpGreaterEq, 705 cvCmpGreater 706 } CvCmpOp; 707 708 typedef struct CvFuncTable 709 { 710 void* fn_2d[CV_DEPTH_MAX]; 711 } 712 CvFuncTable; 713 714 typedef struct CvBigFuncTable 715 { 716 void* fn_2d[CV_DEPTH_MAX*CV_CN_MAX]; 717 } 718 CvBigFuncTable; 719 720 721 typedef struct CvBtFuncTable 722 { 723 void* fn_2d[33]; 724 } 725 CvBtFuncTable; 726 727 typedef CvStatus (CV_STDCALL *CvFunc2D_1A)(void* arr, int step, CvSize size); 728 729 typedef CvStatus (CV_STDCALL *CvFunc2D_1A1P)(void* arr, int step, CvSize size, void* param); 730 731 typedef CvStatus (CV_STDCALL *CvFunc2D_1A1P1I)(void* arr, int step, CvSize size, 732 void* param, int flag); 733 734 typedef CvStatus (CV_STDCALL *CvFunc2DnC_1A1P)( void* arr, int step, CvSize size, 735 int cn, int coi, void* param ); 736 737 typedef CvStatus (CV_STDCALL *CvFunc2DnC_1A1P)( void* arr, int step, CvSize size, 738 int cn, int coi, void* param ); 739 740 typedef CvStatus (CV_STDCALL *CvFunc2D_1A2P)( void* arr, int step, CvSize size, 741 void* param1, void* param2 ); 742 743 typedef CvStatus (CV_STDCALL *CvFunc2DnC_1A2P)( void* arr, int step, 744 CvSize size, int cn, int coi, 745 void* param1, void* param2 ); 746 747 typedef CvStatus (CV_STDCALL *CvFunc2D_1A4P)( void* arr, int step, CvSize size, 748 void* param1, void* param2, 749 void* param3, void* param4 ); 750 751 typedef CvStatus (CV_STDCALL *CvFunc2DnC_1A4P)( void* arr, int step, 752 CvSize size, int cn, int coi, 753 void* param1, void* param2, 754 void* param3, void* param4 ); 755 756 typedef CvStatus (CV_STDCALL *CvFunc2D_2A)( void* arr0, int step0, 757 void* arr1, int step1, CvSize size ); 758 759 typedef CvStatus (CV_STDCALL *CvFunc2D_2A1P)( void* arr0, int step0, 760 void* arr1, int step1, 761 CvSize size, void* param ); 762 763 typedef CvStatus (CV_STDCALL *CvFunc2DnC_2A1P)( void* arr0, int step0, 764 void* arr1, int step1, 765 CvSize size, int cn, 766 int coi, void* param ); 767 768 typedef CvStatus (CV_STDCALL *CvFunc2DnC_2A1P)( void* arr0, int step0, 769 void* arr1, int step1, 770 CvSize size, int cn, 771 int coi, void* param ); 772 773 typedef CvStatus (CV_STDCALL *CvFunc2D_2A2P)( void* arr0, int step0, 774 void* arr1, int step1, CvSize size, 775 void* param1, void* param2 ); 776 777 typedef CvStatus (CV_STDCALL *CvFunc2DnC_2A2P)( void* arr0, int step0, 778 void* arr1, int step1, 779 CvSize size, int cn, int coi, 780 void* param1, void* param2 ); 781 782 typedef CvStatus (CV_STDCALL *CvFunc2D_2A1P1I)( void* arr0, int step0, 783 void* arr1, int step1, CvSize size, 784 void* param, int flag ); 785 786 typedef CvStatus (CV_STDCALL *CvFunc2D_2A4P)( void* arr0, int step0, 787 void* arr1, int step1, CvSize size, 788 void* param1, void* param2, 789 void* param3, void* param4 ); 790 791 typedef CvStatus (CV_STDCALL *CvFunc2DnC_2A4P)( void* arr0, int step0, 792 void* arr1, int step1, CvSize size, 793 int cn, int coi, 794 void* param1, void* param2, 795 void* param3, void* param4 ); 796 797 typedef CvStatus (CV_STDCALL *CvFunc2D_3A)( void* arr0, int step0, 798 void* arr1, int step1, 799 void* arr2, int step2, CvSize size ); 800 801 typedef CvStatus (CV_STDCALL *CvFunc2D_3A1P)( void* arr0, int step0, 802 void* arr1, int step1, 803 void* arr2, int step2, 804 CvSize size, void* param ); 805 806 typedef CvStatus (CV_STDCALL *CvFunc2D_3A1I)( void* arr0, int step0, 807 void* arr1, int step1, 808 void* arr2, int step2, 809 CvSize size, int flag ); 810 811 typedef CvStatus (CV_STDCALL *CvFunc2DnC_3A1P)( void* arr0, int step0, 812 void* arr1, int step1, 813 void* arr2, int step2, 814 CvSize size, int cn, 815 int coi, void* param ); 816 817 typedef CvStatus (CV_STDCALL *CvFunc2D_4A)( void* arr0, int step0, 818 void* arr1, int step1, 819 void* arr2, int step2, 820 void* arr3, int step3, 821 CvSize size ); 822 823 typedef CvStatus (CV_STDCALL *CvFunc0D)( const void* src, void* dst, int param ); 824 825 #define CV_DEF_INIT_FUNC_TAB_2D( FUNCNAME, FLAG ) \ 826 static void icvInit##FUNCNAME##FLAG##Table( CvFuncTable* tab ) \ 827 { \ 828 assert( tab ); \ 829 \ 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; \ 837 } 838 839 840 #define CV_DEF_INIT_BIG_FUNC_TAB_2D( FUNCNAME, FLAG ) \ 841 static void icvInit##FUNCNAME##FLAG##Table( CvBigFuncTable* tab ) \ 842 { \ 843 assert( tab ); \ 844 \ 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; \ 849 \ 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; \ 854 \ 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; \ 859 \ 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; \ 864 \ 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; \ 869 \ 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; \ 874 \ 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; \ 879 } 880 881 #define CV_DEF_INIT_FUNC_TAB_0D( FUNCNAME ) \ 882 static void icvInit##FUNCNAME##Table( CvFuncTable* tab ) \ 883 { \ 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; \ 891 } 892 893 #define CV_DEF_INIT_FUNC_TAB_1D CV_DEF_INIT_FUNC_TAB_0D 894 895 896 #define CV_DEF_INIT_PIXSIZE_TAB_2D( FUNCNAME, FLAG ) \ 897 static void icvInit##FUNCNAME##FLAG##Table( CvBtFuncTable* table ) \ 898 { \ 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; \ 909 } 910 911 #define CV_GET_FUNC_PTR( func, table_entry ) \ 912 func = (table_entry); \ 913 \ 914 if( !func ) \ 915 CV_ERROR( CV_StsUnsupportedFormat, "" ) 916 917 918 #endif /*_CXCORE_MISC_H_*/ 919