cxmisc.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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. /* The header is mostly for internal use and it is likely to change.
  42. It contains some macro definitions that are used in cxcore, cv, cvaux
  43. and, probably, other libraries. If you need some of this functionality,
  44. the safe way is to copy it into your code and rename the macros.
  45. */
  46. #ifndef _CXCORE_MISC_H_
  47. #define _CXCORE_MISC_H_
  48. #ifdef HAVE_CONFIG_H
  49. #include "cvconfig.h"
  50. #endif
  51. #include <limits.h>
  52. #ifdef _OPENMP
  53. #include "omp.h"
  54. #endif
  55. /****************************************************************************************\
  56. * Compile-time tuning parameters *
  57. \****************************************************************************************/
  58. /* maximal size of vector to run matrix operations on it inline (i.e. w/o ipp calls) */
  59. #define CV_MAX_INLINE_MAT_OP_SIZE 10
  60. /* maximal linear size of matrix to allocate it on stack. */
  61. #define CV_MAX_LOCAL_MAT_SIZE 32
  62. /* maximal size of local memory storage */
  63. #define CV_MAX_LOCAL_SIZE \
  64. (CV_MAX_LOCAL_MAT_SIZE*CV_MAX_LOCAL_MAT_SIZE*(int)sizeof(double))
  65. /* default image row align (in bytes) */
  66. #define CV_DEFAULT_IMAGE_ROW_ALIGN 4
  67. /* matrices are continuous by default */
  68. #define CV_DEFAULT_MAT_ROW_ALIGN 1
  69. /* maximum size of dynamic memory buffer.
  70. cvAlloc reports an error if a larger block is requested. */
  71. #define CV_MAX_ALLOC_SIZE (((size_t)1 << (sizeof(size_t)*8-2)))
  72. /* the alignment of all the allocated buffers */
  73. #define CV_MALLOC_ALIGN 32
  74. /* default alignment for dynamic data strucutures, resided in storages. */
  75. #define CV_STRUCT_ALIGN ((int)sizeof(double))
  76. /* default storage block size */
  77. #define CV_STORAGE_BLOCK_SIZE ((1<<16) - 128)
  78. /* default memory block for sparse array elements */
  79. #define CV_SPARSE_MAT_BLOCK (1<<12)
  80. /* initial hash table size */
  81. #define CV_SPARSE_HASH_SIZE0 (1<<10)
  82. /* maximal average node_count/hash_size ratio beyond which hash table is resized */
  83. #define CV_SPARSE_HASH_RATIO 3
  84. /* max length of strings */
  85. #define CV_MAX_STRLEN 1024
  86. /* maximum possible number of threads in parallel implementations */
  87. #ifdef _OPENMP
  88. #define CV_MAX_THREADS 128
  89. #else
  90. #define CV_MAX_THREADS 1
  91. #endif
  92. #if 0 /*def CV_CHECK_FOR_NANS*/
  93. #define CV_CHECK_NANS( arr ) cvCheckArray((arr))
  94. #else
  95. #define CV_CHECK_NANS( arr )
  96. #endif
  97. /****************************************************************************************\
  98. * Common declarations *
  99. \****************************************************************************************/
  100. /* get alloca declaration */
  101. #ifdef __GNUC__
  102. #undef alloca
  103. #define alloca __builtin_alloca
  104. #elif defined WIN32 || defined WIN64
  105. #if defined _MSC_VER || defined __BORLANDC__
  106. #include <malloc.h>
  107. #endif
  108. #elif defined HAVE_ALLOCA_H
  109. #include <alloca.h>
  110. #elif defined HAVE_ALLOCA
  111. #include <stdlib.h>
  112. #elif
  113. #error
  114. #endif
  115. /* ! DO NOT make it an inline function */
  116. #define cvStackAlloc(size) cvAlignPtr( alloca((size) + CV_MALLOC_ALIGN), CV_MALLOC_ALIGN )
  117. #if defined _MSC_VER || defined __BORLANDC__
  118. #define CV_BIG_INT(n) n##I64
  119. #define CV_BIG_UINT(n) n##UI64
  120. #else
  121. #define CV_BIG_INT(n) n##LL
  122. #define CV_BIG_UINT(n) n##ULL
  123. #endif
  124. #define CV_IMPL CV_EXTERN_C
  125. #if defined WIN32 && !defined WIN64 && (_MSC_VER >= 1200 || defined CV_ICC)
  126. #define CV_DBG_BREAK() __asm int 3
  127. #else
  128. #define CV_DBG_BREAK() assert(0);
  129. #endif
  130. /* default step, set in case of continuous data
  131. to work around checks for valid step in some ipp functions */
  132. #define CV_STUB_STEP (1 << 30)
  133. #define CV_SIZEOF_FLOAT ((int)sizeof(float))
  134. #define CV_SIZEOF_SHORT ((int)sizeof(short))
  135. #define CV_ORIGIN_TL 0
  136. #define CV_ORIGIN_BL 1
  137. /* IEEE754 constants and macros */
  138. #define CV_POS_INF 0x7f800000
  139. #define CV_NEG_INF 0x807fffff /* CV_TOGGLE_FLT(0xff800000) */
  140. #define CV_1F 0x3f800000
  141. #define CV_TOGGLE_FLT(x) ((x)^((int)(x) < 0 ? 0x7fffffff : 0))
  142. #define CV_TOGGLE_DBL(x) \
  143. ((x)^((int64)(x) < 0 ? CV_BIG_INT(0x7fffffffffffffff) : 0))
  144. #define CV_NOP(a) (a)
  145. #define CV_ADD(a, b) ((a) + (b))
  146. #define CV_SUB(a, b) ((a) - (b))
  147. #define CV_MUL(a, b) ((a) * (b))
  148. #define CV_AND(a, b) ((a) & (b))
  149. #define CV_OR(a, b) ((a) | (b))
  150. #define CV_XOR(a, b) ((a) ^ (b))
  151. #define CV_ANDN(a, b) (~(a) & (b))
  152. #define CV_ORN(a, b) (~(a) | (b))
  153. #define CV_SQR(a) ((a) * (a))
  154. #define CV_LT(a, b) ((a) < (b))
  155. #define CV_LE(a, b) ((a) <= (b))
  156. #define CV_EQ(a, b) ((a) == (b))
  157. #define CV_NE(a, b) ((a) != (b))
  158. #define CV_GT(a, b) ((a) > (b))
  159. #define CV_GE(a, b) ((a) >= (b))
  160. #define CV_NONZERO(a) ((a) != 0)
  161. #define CV_NONZERO_FLT(a) (((a)+(a)) != 0)
  162. /* general-purpose saturation macros */
  163. #define CV_CAST_8U(t) (uchar)(!((t) & ~255) ? (t) : (t) > 0 ? 255 : 0)
  164. #define CV_CAST_8S(t) (char)(!(((t)+128) & ~255) ? (t) : (t) > 0 ? 127 : -128)
  165. #define CV_CAST_16U(t) (ushort)(!((t) & ~65535) ? (t) : (t) > 0 ? 65535 : 0)
  166. #define CV_CAST_16S(t) (short)(!(((t)+32768) & ~65535) ? (t) : (t) > 0 ? 32767 : -32768)
  167. #define CV_CAST_32S(t) (int)(t)
  168. #define CV_CAST_64S(t) (int64)(t)
  169. #define CV_CAST_32F(t) (float)(t)
  170. #define CV_CAST_64F(t) (double)(t)
  171. #define CV_PASTE2(a,b) a##b
  172. #define CV_PASTE(a,b) CV_PASTE2(a,b)
  173. #define CV_EMPTY
  174. #define CV_MAKE_STR(a) #a
  175. #define CV_DEFINE_MASK \
  176. float maskTab[2]; maskTab[0] = 0.f; maskTab[1] = 1.f;
  177. #define CV_ANDMASK( m, x ) ((x) & (((m) == 0) - 1))
  178. /* (x) * ((m) == 1 ? 1.f : (m) == 0 ? 0.f : <ERR> */
  179. #define CV_MULMASK( m, x ) (maskTab[(m) != 0]*(x))
  180. /* (x) * ((m) == -1 ? 1.f : (m) == 0 ? 0.f : <ERR> */
  181. #define CV_MULMASK1( m, x ) (maskTab[(m)+1]*(x))
  182. #define CV_ZERO_OBJ(x) memset((x), 0, sizeof(*(x)))
  183. #define CV_DIM(static_array) ((int)(sizeof(static_array)/sizeof((static_array)[0])))
  184. #define CV_UN_ENTRY_C1(worktype) \
  185. worktype s0 = scalar[0]
  186. #define CV_UN_ENTRY_C2(worktype) \
  187. worktype s0 = scalar[0], s1 = scalar[1]
  188. #define CV_UN_ENTRY_C3(worktype) \
  189. worktype s0 = scalar[0], s1 = scalar[1], s2 = scalar[2]
  190. #define CV_UN_ENTRY_C4(worktype) \
  191. worktype s0 = scalar[0], s1 = scalar[1], s2 = scalar[2], s3 = scalar[3]
  192. #define cvUnsupportedFormat "Unsupported format"
  193. CV_INLINE void* cvAlignPtr( const void* ptr, int align=32 )
  194. {
  195. assert( (align & (align-1)) == 0 );
  196. return (void*)( ((size_t)ptr + align - 1) & ~(size_t)(align-1) );
  197. }
  198. CV_INLINE int cvAlign( int size, int align )
  199. {
  200. assert( (align & (align-1)) == 0 && size < INT_MAX );
  201. return (size + align - 1) & -align;
  202. }
  203. CV_INLINE CvSize cvGetMatSize( const CvMat* mat )
  204. {
  205. CvSize size = { mat->width, mat->height };
  206. return size;
  207. }
  208. #define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n))
  209. #define CV_FLT_TO_FIX(x,n) cvRound((x)*(1<<(n)))
  210. #if 0
  211. /* This is a small engine for performing fast division of multiple numbers
  212. by the same constant. Most compilers do it too if they know the divisor value
  213. at compile-time. The algorithm was taken from Agner Fog's optimization guide
  214. at http://www.agner.org/assem */
  215. typedef struct CvFastDiv
  216. {
  217. unsigned delta, scale, divisor;
  218. }
  219. CvFastDiv;
  220. #define CV_FAST_DIV_SHIFT 32
  221. CV_INLINE CvFastDiv cvFastDiv( int divisor )
  222. {
  223. CvFastDiv fastdiv;
  224. assert( divisor >= 1 );
  225. uint64 temp = ((uint64)1 << CV_FAST_DIV_SHIFT)/divisor;
  226. fastdiv.divisor = divisor;
  227. fastdiv.delta = (unsigned)(((temp & 1) ^ 1) + divisor - 1);
  228. fastdiv.scale = (unsigned)((temp + 1) >> 1);
  229. return fastdiv;
  230. }
  231. #define CV_FAST_DIV( x, fastdiv ) \
  232. ((int)(((int64)((x)*2 + (int)(fastdiv).delta))*(int)(fastdiv).scale>>CV_FAST_DIV_SHIFT))
  233. #define CV_FAST_UDIV( x, fastdiv ) \
  234. ((int)(((uint64)((x)*2 + (fastdiv).delta))*(fastdiv).scale>>CV_FAST_DIV_SHIFT))
  235. #endif
  236. #define CV_MEMCPY_CHAR( dst, src, len ) \
  237. { \
  238. size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
  239. char* _icv_memcpy_dst_ = (char*)(dst); \
  240. const char* _icv_memcpy_src_ = (const char*)(src); \
  241. \
  242. for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ ) \
  243. _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_]; \
  244. }
  245. #define CV_MEMCPY_INT( dst, src, len ) \
  246. { \
  247. size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
  248. int* _icv_memcpy_dst_ = (int*)(dst); \
  249. const int* _icv_memcpy_src_ = (const int*)(src); \
  250. assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 && \
  251. ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 ); \
  252. \
  253. for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++) \
  254. _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_]; \
  255. }
  256. #define CV_MEMCPY_AUTO( dst, src, len ) \
  257. { \
  258. size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
  259. char* _icv_memcpy_dst_ = (char*)(dst); \
  260. const char* _icv_memcpy_src_ = (const char*)(src); \
  261. if( (_icv_memcpy_len_ & (sizeof(int)-1)) == 0 ) \
  262. { \
  263. assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 && \
  264. ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 ); \
  265. for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; \
  266. _icv_memcpy_i_+=sizeof(int) ) \
  267. { \
  268. *(int*)(_icv_memcpy_dst_+_icv_memcpy_i_) = \
  269. *(const int*)(_icv_memcpy_src_+_icv_memcpy_i_); \
  270. } \
  271. } \
  272. else \
  273. { \
  274. for(_icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++)\
  275. _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_]; \
  276. } \
  277. }
  278. #define CV_ZERO_CHAR( dst, len ) \
  279. { \
  280. size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
  281. char* _icv_memcpy_dst_ = (char*)(dst); \
  282. \
  283. for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ ) \
  284. _icv_memcpy_dst_[_icv_memcpy_i_] = '\0'; \
  285. }
  286. #define CV_ZERO_INT( dst, len ) \
  287. { \
  288. size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
  289. int* _icv_memcpy_dst_ = (int*)(dst); \
  290. assert( ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 ); \
  291. \
  292. for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++) \
  293. _icv_memcpy_dst_[_icv_memcpy_i_] = 0; \
  294. }
  295. /****************************************************************************************\
  296. Generic implementation of QuickSort algorithm.
  297. ----------------------------------------------
  298. Using this macro user can declare customized sort function that can be much faster
  299. than built-in qsort function because of lower overhead on elements
  300. comparison and exchange. The macro takes less_than (or LT) argument - a macro or function
  301. that takes 2 arguments returns non-zero if the first argument should be before the second
  302. one in the sorted sequence and zero otherwise.
  303. Example:
  304. Suppose that the task is to sort points by ascending of y coordinates and if
  305. y's are equal x's should ascend.
  306. The code is:
  307. ------------------------------------------------------------------------------
  308. #define cmp_pts( pt1, pt2 ) \
  309. ((pt1).y < (pt2).y || ((pt1).y < (pt2).y && (pt1).x < (pt2).x))
  310. [static] CV_IMPLEMENT_QSORT( icvSortPoints, CvPoint, cmp_pts )
  311. ------------------------------------------------------------------------------
  312. After that the function "void icvSortPoints( CvPoint* array, size_t total, int aux );"
  313. is available to user.
  314. aux is an additional parameter, which can be used when comparing elements.
  315. The current implementation was derived from *BSD system qsort():
  316. * Copyright (c) 1992, 1993
  317. * The Regents of the University of California. All rights reserved.
  318. *
  319. * Redistribution and use in source and binary forms, with or without
  320. * modification, are permitted provided that the following conditions
  321. * are met:
  322. * 1. Redistributions of source code must retain the above copyright
  323. * notice, this list of conditions and the following disclaimer.
  324. * 2. Redistributions in binary form must reproduce the above copyright
  325. * notice, this list of conditions and the following disclaimer in the
  326. * documentation and/or other materials provided with the distribution.
  327. * 3. All advertising materials mentioning features or use of this software
  328. * must display the following acknowledgement:
  329. * This product includes software developed by the University of
  330. * California, Berkeley and its contributors.
  331. * 4. Neither the name of the University nor the names of its contributors
  332. * may be used to endorse or promote products derived from this software
  333. * without specific prior written permission.
  334. *
  335. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  336. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  337. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  338. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  339. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  340. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  341. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  342. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  343. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  344. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  345. * SUCH DAMAGE.
  346. \****************************************************************************************/
  347. #define CV_IMPLEMENT_QSORT_EX( func_name, T, LT, user_data_type ) \
  348. void func_name( T *array, size_t total, user_data_type aux ) \
  349. { \
  350. int isort_thresh = 7; \
  351. T t; \
  352. int sp = 0; \
  353. \
  354. struct \
  355. { \
  356. T *lb; \
  357. T *ub; \
  358. } \
  359. stack[48]; \
  360. \
  361. aux = aux; \
  362. \
  363. if( total <= 1 ) \
  364. return; \
  365. \
  366. stack[0].lb = array; \
  367. stack[0].ub = array + (total - 1); \
  368. \
  369. while( sp >= 0 ) \
  370. { \
  371. T* left = stack[sp].lb; \
  372. T* right = stack[sp--].ub; \
  373. \
  374. for(;;) \
  375. { \
  376. int i, n = (int)(right - left) + 1, m; \
  377. T* ptr; \
  378. T* ptr2; \
  379. \
  380. if( n <= isort_thresh ) \
  381. { \
  382. insert_sort: \
  383. for( ptr = left + 1; ptr <= right; ptr++ ) \
  384. { \
  385. for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--) \
  386. CV_SWAP( ptr2[0], ptr2[-1], t ); \
  387. } \
  388. break; \
  389. } \
  390. else \
  391. { \
  392. T* left0; \
  393. T* left1; \
  394. T* right0; \
  395. T* right1; \
  396. T* pivot; \
  397. T* a; \
  398. T* b; \
  399. T* c; \
  400. int swap_cnt = 0; \
  401. \
  402. left0 = left; \
  403. right0 = right; \
  404. pivot = left + (n/2); \
  405. \
  406. if( n > 40 ) \
  407. { \
  408. int d = n / 8; \
  409. a = left, b = left + d, c = left + 2*d; \
  410. left = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
  411. : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
  412. \
  413. a = pivot - d, b = pivot, c = pivot + d; \
  414. pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
  415. : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
  416. \
  417. a = right - 2*d, b = right - d, c = right; \
  418. right = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
  419. : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
  420. } \
  421. \
  422. a = left, b = pivot, c = right; \
  423. pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a)) \
  424. : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c)); \
  425. if( pivot != left0 ) \
  426. { \
  427. CV_SWAP( *pivot, *left0, t ); \
  428. pivot = left0; \
  429. } \
  430. left = left1 = left0 + 1; \
  431. right = right1 = right0; \
  432. \
  433. for(;;) \
  434. { \
  435. while( left <= right && !LT(*pivot, *left) ) \
  436. { \
  437. if( !LT(*left, *pivot) ) \
  438. { \
  439. if( left > left1 ) \
  440. CV_SWAP( *left1, *left, t ); \
  441. swap_cnt = 1; \
  442. left1++; \
  443. } \
  444. left++; \
  445. } \
  446. \
  447. while( left <= right && !LT(*right, *pivot) ) \
  448. { \
  449. if( !LT(*pivot, *right) ) \
  450. { \
  451. if( right < right1 ) \
  452. CV_SWAP( *right1, *right, t ); \
  453. swap_cnt = 1; \
  454. right1--; \
  455. } \
  456. right--; \
  457. } \
  458. \
  459. if( left > right ) \
  460. break; \
  461. CV_SWAP( *left, *right, t ); \
  462. swap_cnt = 1; \
  463. left++; \
  464. right--; \
  465. } \
  466. \
  467. if( swap_cnt == 0 ) \
  468. { \
  469. left = left0, right = right0; \
  470. goto insert_sort; \
  471. } \
  472. \
  473. n = MIN( (int)(left1 - left0), (int)(left - left1) ); \
  474. for( i = 0; i < n; i++ ) \
  475. CV_SWAP( left0[i], left[i-n], t ); \
  476. \
  477. n = MIN( (int)(right0 - right1), (int)(right1 - right) ); \
  478. for( i = 0; i < n; i++ ) \
  479. CV_SWAP( left[i], right0[i-n+1], t ); \
  480. n = (int)(left - left1); \
  481. m = (int)(right1 - right); \
  482. if( n > 1 ) \
  483. { \
  484. if( m > 1 ) \
  485. { \
  486. if( n > m ) \
  487. { \
  488. stack[++sp].lb = left0; \
  489. stack[sp].ub = left0 + n - 1; \
  490. left = right0 - m + 1, right = right0; \
  491. } \
  492. else \
  493. { \
  494. stack[++sp].lb = right0 - m + 1; \
  495. stack[sp].ub = right0; \
  496. left = left0, right = left0 + n - 1; \
  497. } \
  498. } \
  499. else \
  500. left = left0, right = left0 + n - 1; \
  501. } \
  502. else if( m > 1 ) \
  503. left = right0 - m + 1, right = right0; \
  504. else \
  505. break; \
  506. } \
  507. } \
  508. } \
  509. }
  510. #define CV_IMPLEMENT_QSORT( func_name, T, cmp ) \
  511. CV_IMPLEMENT_QSORT_EX( func_name, T, cmp, int )
  512. /****************************************************************************************\
  513. * Structures and macros for integration with IPP *
  514. \****************************************************************************************/
  515. /* IPP-compatible return codes */
  516. typedef enum CvStatus
  517. {
  518. CV_BADMEMBLOCK_ERR = -113,
  519. CV_INPLACE_NOT_SUPPORTED_ERR= -112,
  520. CV_UNMATCHED_ROI_ERR = -111,
  521. CV_NOTFOUND_ERR = -110,
  522. CV_BADCONVERGENCE_ERR = -109,
  523. CV_BADDEPTH_ERR = -107,
  524. CV_BADROI_ERR = -106,
  525. CV_BADHEADER_ERR = -105,
  526. CV_UNMATCHED_FORMATS_ERR = -104,
  527. CV_UNSUPPORTED_COI_ERR = -103,
  528. CV_UNSUPPORTED_CHANNELS_ERR = -102,
  529. CV_UNSUPPORTED_DEPTH_ERR = -101,
  530. CV_UNSUPPORTED_FORMAT_ERR = -100,
  531. CV_BADARG_ERR = -49, //ipp comp
  532. CV_NOTDEFINED_ERR = -48, //ipp comp
  533. CV_BADCHANNELS_ERR = -47, //ipp comp
  534. CV_BADRANGE_ERR = -44, //ipp comp
  535. CV_BADSTEP_ERR = -29, //ipp comp
  536. CV_BADFLAG_ERR = -12,
  537. CV_DIV_BY_ZERO_ERR = -11, //ipp comp
  538. CV_BADCOEF_ERR = -10,
  539. CV_BADFACTOR_ERR = -7,
  540. CV_BADPOINT_ERR = -6,
  541. CV_BADSCALE_ERR = -4,
  542. CV_OUTOFMEM_ERR = -3,
  543. CV_NULLPTR_ERR = -2,
  544. CV_BADSIZE_ERR = -1,
  545. CV_NO_ERR = 0,
  546. CV_OK = CV_NO_ERR
  547. }
  548. CvStatus;
  549. #define CV_ERROR_FROM_STATUS( result ) \
  550. CV_ERROR( cvErrorFromIppStatus( result ), "OpenCV function failed" )
  551. #define IPPI_CALL( Func ) \
  552. { \
  553. CvStatus ippi_call_result; \
  554. ippi_call_result = Func; \
  555. \
  556. if( ippi_call_result < 0 ) \
  557. CV_ERROR_FROM_STATUS( (ippi_call_result)); \
  558. }
  559. #define CV_PLUGIN_NONE 0
  560. #define CV_PLUGIN_OPTCV 1 /* custom "emerged" ippopencv library */
  561. #define CV_PLUGIN_IPPCV 2 /* IPP: computer vision */
  562. #define CV_PLUGIN_IPPI 3 /* IPP: image processing */
  563. #define CV_PLUGIN_IPPS 4 /* IPP: signal processing */
  564. #define CV_PLUGIN_IPPVM 5 /* IPP: vector math functions */
  565. #define CV_PLUGIN_IPPCC 6 /* IPP: color space conversion */
  566. #define CV_PLUGIN_MKL 8 /* Intel Math Kernel Library */
  567. #define CV_PLUGIN_MAX 16
  568. #define CV_PLUGINS1(lib1) ((lib1)&15)
  569. #define CV_PLUGINS2(lib1,lib2) (((lib1)&15)|(((lib2)&15)<<4))
  570. #define CV_PLUGINS3(lib1,lib2,lib3) (((lib1)&15)|(((lib2)&15)<<4)|(((lib2)&15)<<8))
  571. #define CV_NOTHROW throw()
  572. #ifndef IPCVAPI
  573. #define IPCVAPI(type,declspec,name,args) \
  574. /* function pointer */ \
  575. typedef type (declspec* name##_t) args; \
  576. extern name##_t name##_p; \
  577. type declspec name args;
  578. #endif
  579. #define IPCVAPI_EX(type,name,ipp_name,ipp_search_modules,args) \
  580. IPCVAPI(type,CV_STDCALL,name,args)
  581. #define IPCVAPI_C_EX(type,name,ipp_name,ipp_search_modules,args)\
  582. IPCVAPI(type,CV_CDECL,name,args)
  583. #ifndef IPCVAPI_IMPL
  584. #define IPCVAPI_IMPL(type,name,args,arg_names) \
  585. static type CV_STDCALL name##_f args; \
  586. name##_t name##_p = name##_f; \
  587. type CV_STDCALL name args { return name##_p arg_names; } \
  588. static type CV_STDCALL name##_f args
  589. #endif
  590. /* IPP types' enumeration */
  591. typedef enum CvDataType {
  592. cv1u,
  593. cv8u, cv8s,
  594. cv16u, cv16s, cv16sc,
  595. cv32u, cv32s, cv32sc,
  596. cv32f, cv32fc,
  597. cv64u, cv64s, cv64sc,
  598. cv64f, cv64fc
  599. } CvDataType;
  600. typedef enum CvHintAlgorithm {
  601. cvAlgHintNone,
  602. cvAlgHintFast,
  603. cvAlgHintAccurate
  604. } CvHintAlgorithm;
  605. typedef enum CvCmpOp {
  606. cvCmpLess,
  607. cvCmpLessEq,
  608. cvCmpEq,
  609. cvCmpGreaterEq,
  610. cvCmpGreater
  611. } CvCmpOp;
  612. typedef struct CvFuncTable
  613. {
  614. void* fn_2d[CV_DEPTH_MAX];
  615. }
  616. CvFuncTable;
  617. typedef struct CvBigFuncTable
  618. {
  619. void* fn_2d[CV_DEPTH_MAX*CV_CN_MAX];
  620. }
  621. CvBigFuncTable;
  622. typedef struct CvBtFuncTable
  623. {
  624. void* fn_2d[33];
  625. }
  626. CvBtFuncTable;
  627. typedef CvStatus (CV_STDCALL *CvFunc2D_1A)(void* arr, int step, CvSize size);
  628. typedef CvStatus (CV_STDCALL *CvFunc2D_1A1P)(void* arr, int step, CvSize size, void* param);
  629. typedef CvStatus (CV_STDCALL *CvFunc2D_1A1P1I)(void* arr, int step, CvSize size,
  630. void* param, int flag);
  631. typedef CvStatus (CV_STDCALL *CvFunc2DnC_1A1P)( void* arr, int step, CvSize size,
  632. int cn, int coi, void* param );
  633. typedef CvStatus (CV_STDCALL *CvFunc2DnC_1A1P)( void* arr, int step, CvSize size,
  634. int cn, int coi, void* param );
  635. typedef CvStatus (CV_STDCALL *CvFunc2D_1A2P)( void* arr, int step, CvSize size,
  636. void* param1, void* param2 );
  637. typedef CvStatus (CV_STDCALL *CvFunc2DnC_1A2P)( void* arr, int step,
  638. CvSize size, int cn, int coi,
  639. void* param1, void* param2 );
  640. typedef CvStatus (CV_STDCALL *CvFunc2D_1A4P)( void* arr, int step, CvSize size,
  641. void* param1, void* param2,
  642. void* param3, void* param4 );
  643. typedef CvStatus (CV_STDCALL *CvFunc2DnC_1A4P)( void* arr, int step,
  644. CvSize size, int cn, int coi,
  645. void* param1, void* param2,
  646. void* param3, void* param4 );
  647. typedef CvStatus (CV_STDCALL *CvFunc2D_2A)( void* arr0, int step0,
  648. void* arr1, int step1, CvSize size );
  649. typedef CvStatus (CV_STDCALL *CvFunc2D_2A1P)( void* arr0, int step0,
  650. void* arr1, int step1,
  651. CvSize size, void* param );
  652. typedef CvStatus (CV_STDCALL *CvFunc2DnC_2A1P)( void* arr0, int step0,
  653. void* arr1, int step1,
  654. CvSize size, int cn,
  655. int coi, void* param );
  656. typedef CvStatus (CV_STDCALL *CvFunc2DnC_2A1P)( void* arr0, int step0,
  657. void* arr1, int step1,
  658. CvSize size, int cn,
  659. int coi, void* param );
  660. typedef CvStatus (CV_STDCALL *CvFunc2D_2A2P)( void* arr0, int step0,
  661. void* arr1, int step1, CvSize size,
  662. void* param1, void* param2 );
  663. typedef CvStatus (CV_STDCALL *CvFunc2DnC_2A2P)( void* arr0, int step0,
  664. void* arr1, int step1,
  665. CvSize size, int cn, int coi,
  666. void* param1, void* param2 );
  667. typedef CvStatus (CV_STDCALL *CvFunc2D_2A1P1I)( void* arr0, int step0,
  668. void* arr1, int step1, CvSize size,
  669. void* param, int flag );
  670. typedef CvStatus (CV_STDCALL *CvFunc2D_2A4P)( void* arr0, int step0,
  671. void* arr1, int step1, CvSize size,
  672. void* param1, void* param2,
  673. void* param3, void* param4 );
  674. typedef CvStatus (CV_STDCALL *CvFunc2DnC_2A4P)( void* arr0, int step0,
  675. void* arr1, int step1, CvSize size,
  676. int cn, int coi,
  677. void* param1, void* param2,
  678. void* param3, void* param4 );
  679. typedef CvStatus (CV_STDCALL *CvFunc2D_3A)( void* arr0, int step0,
  680. void* arr1, int step1,
  681. void* arr2, int step2, CvSize size );
  682. typedef CvStatus (CV_STDCALL *CvFunc2D_3A1P)( void* arr0, int step0,
  683. void* arr1, int step1,
  684. void* arr2, int step2,
  685. CvSize size, void* param );
  686. typedef CvStatus (CV_STDCALL *CvFunc2D_3A1I)( void* arr0, int step0,
  687. void* arr1, int step1,
  688. void* arr2, int step2,
  689. CvSize size, int flag );
  690. typedef CvStatus (CV_STDCALL *CvFunc2DnC_3A1P)( void* arr0, int step0,
  691. void* arr1, int step1,
  692. void* arr2, int step2,
  693. CvSize size, int cn,
  694. int coi, void* param );
  695. typedef CvStatus (CV_STDCALL *CvFunc2D_4A)( void* arr0, int step0,
  696. void* arr1, int step1,
  697. void* arr2, int step2,
  698. void* arr3, int step3,
  699. CvSize size );
  700. typedef CvStatus (CV_STDCALL *CvFunc0D)( const void* src, void* dst, int param );
  701. #define CV_DEF_INIT_FUNC_TAB_2D( FUNCNAME, FLAG ) \
  702. static void icvInit##FUNCNAME##FLAG##Table( CvFuncTable* tab ) \
  703. { \
  704. assert( tab ); \
  705. \
  706. tab->fn_2d[CV_8U] = (void*)icv##FUNCNAME##_8u_##FLAG; \
  707. tab->fn_2d[CV_8S] = (void*)icv##FUNCNAME##_8s_##FLAG; \
  708. tab->fn_2d[CV_16U] = (void*)icv##FUNCNAME##_16u_##FLAG; \
  709. tab->fn_2d[CV_16S] = (void*)icv##FUNCNAME##_16s_##FLAG; \
  710. tab->fn_2d[CV_32S] = (void*)icv##FUNCNAME##_32s_##FLAG; \
  711. tab->fn_2d[CV_32F] = (void*)icv##FUNCNAME##_32f_##FLAG; \
  712. tab->fn_2d[CV_64F] = (void*)icv##FUNCNAME##_64f_##FLAG; \
  713. }
  714. #define CV_DEF_INIT_BIG_FUNC_TAB_2D( FUNCNAME, FLAG ) \
  715. static void icvInit##FUNCNAME##FLAG##Table( CvBigFuncTable* tab ) \
  716. { \
  717. assert( tab ); \
  718. \
  719. tab->fn_2d[CV_8UC1] = (void*)icv##FUNCNAME##_8u_C1##FLAG; \
  720. tab->fn_2d[CV_8UC2] = (void*)icv##FUNCNAME##_8u_C2##FLAG; \
  721. tab->fn_2d[CV_8UC3] = (void*)icv##FUNCNAME##_8u_C3##FLAG; \
  722. tab->fn_2d[CV_8UC4] = (void*)icv##FUNCNAME##_8u_C4##FLAG; \
  723. \
  724. tab->fn_2d[CV_8SC1] = (void*)icv##FUNCNAME##_8s_C1##FLAG; \
  725. tab->fn_2d[CV_8SC2] = (void*)icv##FUNCNAME##_8s_C2##FLAG; \
  726. tab->fn_2d[CV_8SC3] = (void*)icv##FUNCNAME##_8s_C3##FLAG; \
  727. tab->fn_2d[CV_8SC4] = (void*)icv##FUNCNAME##_8s_C4##FLAG; \
  728. \
  729. tab->fn_2d[CV_16UC1] = (void*)icv##FUNCNAME##_16u_C1##FLAG; \
  730. tab->fn_2d[CV_16UC2] = (void*)icv##FUNCNAME##_16u_C2##FLAG; \
  731. tab->fn_2d[CV_16UC3] = (void*)icv##FUNCNAME##_16u_C3##FLAG; \
  732. tab->fn_2d[CV_16UC4] = (void*)icv##FUNCNAME##_16u_C4##FLAG; \
  733. \
  734. tab->fn_2d[CV_16SC1] = (void*)icv##FUNCNAME##_16s_C1##FLAG; \
  735. tab->fn_2d[CV_16SC2] = (void*)icv##FUNCNAME##_16s_C2##FLAG; \
  736. tab->fn_2d[CV_16SC3] = (void*)icv##FUNCNAME##_16s_C3##FLAG; \
  737. tab->fn_2d[CV_16SC4] = (void*)icv##FUNCNAME##_16s_C4##FLAG; \
  738. \
  739. tab->fn_2d[CV_32SC1] = (void*)icv##FUNCNAME##_32s_C1##FLAG; \
  740. tab->fn_2d[CV_32SC2] = (void*)icv##FUNCNAME##_32s_C2##FLAG; \
  741. tab->fn_2d[CV_32SC3] = (void*)icv##FUNCNAME##_32s_C3##FLAG; \
  742. tab->fn_2d[CV_32SC4] = (void*)icv##FUNCNAME##_32s_C4##FLAG; \
  743. \
  744. tab->fn_2d[CV_32FC1] = (void*)icv##FUNCNAME##_32f_C1##FLAG; \
  745. tab->fn_2d[CV_32FC2] = (void*)icv##FUNCNAME##_32f_C2##FLAG; \
  746. tab->fn_2d[CV_32FC3] = (void*)icv##FUNCNAME##_32f_C3##FLAG; \
  747. tab->fn_2d[CV_32FC4] = (void*)icv##FUNCNAME##_32f_C4##FLAG; \
  748. \
  749. tab->fn_2d[CV_64FC1] = (void*)icv##FUNCNAME##_64f_C1##FLAG; \
  750. tab->fn_2d[CV_64FC2] = (void*)icv##FUNCNAME##_64f_C2##FLAG; \
  751. tab->fn_2d[CV_64FC3] = (void*)icv##FUNCNAME##_64f_C3##FLAG; \
  752. tab->fn_2d[CV_64FC4] = (void*)icv##FUNCNAME##_64f_C4##FLAG; \
  753. }
  754. #define CV_DEF_INIT_FUNC_TAB_0D( FUNCNAME ) \
  755. static void icvInit##FUNCNAME##Table( CvFuncTable* tab ) \
  756. { \
  757. tab->fn_2d[CV_8U] = (void*)icv##FUNCNAME##_8u; \
  758. tab->fn_2d[CV_8S] = (void*)icv##FUNCNAME##_8s; \
  759. tab->fn_2d[CV_16U] = (void*)icv##FUNCNAME##_16u; \
  760. tab->fn_2d[CV_16S] = (void*)icv##FUNCNAME##_16s; \
  761. tab->fn_2d[CV_32S] = (void*)icv##FUNCNAME##_32s; \
  762. tab->fn_2d[CV_32F] = (void*)icv##FUNCNAME##_32f; \
  763. tab->fn_2d[CV_64F] = (void*)icv##FUNCNAME##_64f; \
  764. }
  765. #define CV_DEF_INIT_FUNC_TAB_1D CV_DEF_INIT_FUNC_TAB_0D
  766. #define CV_DEF_INIT_PIXSIZE_TAB_2D( FUNCNAME, FLAG ) \
  767. static void icvInit##FUNCNAME##FLAG##Table( CvBtFuncTable* table ) \
  768. { \
  769. table->fn_2d[1] = (void*)icv##FUNCNAME##_8u_C1##FLAG; \
  770. table->fn_2d[2] = (void*)icv##FUNCNAME##_8u_C2##FLAG; \
  771. table->fn_2d[3] = (void*)icv##FUNCNAME##_8u_C3##FLAG; \
  772. table->fn_2d[4] = (void*)icv##FUNCNAME##_16u_C2##FLAG; \
  773. table->fn_2d[6] = (void*)icv##FUNCNAME##_16u_C3##FLAG; \
  774. table->fn_2d[8] = (void*)icv##FUNCNAME##_32s_C2##FLAG; \
  775. table->fn_2d[12] = (void*)icv##FUNCNAME##_32s_C3##FLAG; \
  776. table->fn_2d[16] = (void*)icv##FUNCNAME##_64s_C2##FLAG; \
  777. table->fn_2d[24] = (void*)icv##FUNCNAME##_64s_C3##FLAG; \
  778. table->fn_2d[32] = (void*)icv##FUNCNAME##_64s_C4##FLAG; \
  779. }
  780. #define CV_GET_FUNC_PTR( func, table_entry ) \
  781. func = (table_entry); \
  782. \
  783. if( !func ) \
  784. CV_ERROR( CV_StsUnsupportedFormat, "" )
  785. #endif /*_CXCORE_MISC_H_*/