OpenCV 2.4.8 components for OpenCVgrabber.
[mmanager-3rdparty.git] / OpenCV2.4.8 / build / include / opencv2 / core / internal.hpp
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 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 /* The header is for internal use and it is likely to change.
44    It contains some macro definitions that are used in cxcore, cv, cvaux
45    and, probably, other libraries. If you need some of this functionality,
46    the safe way is to copy it into your code and rename the macros.
47 */
48 #ifndef __OPENCV_CORE_INTERNAL_HPP__
49 #define __OPENCV_CORE_INTERNAL_HPP__
50
51 #include <vector>
52
53 #include "opencv2/core/core.hpp"
54 #include "opencv2/core/types_c.h"
55
56 #if defined WIN32 || defined _WIN32
57 #  ifndef WIN32
58 #    define WIN32
59 #  endif
60 #  ifndef _WIN32
61 #    define _WIN32
62 #  endif
63 #endif
64
65 #if !defined WIN32 && !defined WINCE
66 #  include <pthread.h>
67 #endif
68
69 #ifdef __BORLANDC__
70 #  ifndef WIN32
71 #    define WIN32
72 #  endif
73 #  ifndef _WIN32
74 #    define _WIN32
75 #  endif
76 #  define CV_DLL
77 #  undef _CV_ALWAYS_PROFILE_
78 #  define _CV_ALWAYS_NO_PROFILE_
79 #endif
80
81 #ifndef FALSE
82 #  define FALSE 0
83 #endif
84 #ifndef TRUE
85 #  define TRUE 1
86 #endif
87
88 #define __BEGIN__ __CV_BEGIN__
89 #define __END__ __CV_END__
90 #define EXIT __CV_EXIT__
91
92 #ifdef HAVE_IPP
93 #  include "ipp.h"
94
95 CV_INLINE IppiSize ippiSize(int width, int height)
96 {
97     IppiSize size = { width, height };
98     return size;
99 }
100 #endif
101
102 #ifndef IPPI_CALL
103 #  define IPPI_CALL(func) CV_Assert((func) >= 0)
104 #endif
105
106 #if defined __SSE2__ || defined _M_X64  || (defined _M_IX86_FP && _M_IX86_FP >= 2)
107 #  include "emmintrin.h"
108 #  define CV_SSE 1
109 #  define CV_SSE2 1
110 #  if defined __SSE3__ || (defined _MSC_VER && _MSC_VER >= 1500)
111 #    include "pmmintrin.h"
112 #    define CV_SSE3 1
113 #  endif
114 #  if defined __SSSE3__  || (defined _MSC_VER && _MSC_VER >= 1500)
115 #    include "tmmintrin.h"
116 #    define CV_SSSE3 1
117 #  endif
118 #  if defined __SSE4_1__ || (defined _MSC_VER && _MSC_VER >= 1500)
119 #    include <smmintrin.h>
120 #    define CV_SSE4_1 1
121 #  endif
122 #  if defined __SSE4_2__ || (defined _MSC_VER && _MSC_VER >= 1500)
123 #    include <nmmintrin.h>
124 #    define CV_SSE4_2 1
125 #  endif
126 #  if defined __AVX__ || (defined _MSC_FULL_VER && _MSC_FULL_VER >= 160040219)
127 // MS Visual Studio 2010 (2012?) has no macro pre-defined to identify the use of /arch:AVX
128 // See: http://connect.microsoft.com/VisualStudio/feedback/details/605858/arch-avx-should-define-a-predefined-macro-in-x64-and-set-a-unique-value-for-m-ix86-fp-in-win32
129 #    include <immintrin.h>
130 #    define CV_AVX 1
131 #    if defined(_XCR_XFEATURE_ENABLED_MASK)
132 #      define __xgetbv() _xgetbv(_XCR_XFEATURE_ENABLED_MASK)
133 #    else
134 #      define __xgetbv() 0
135 #    endif
136 #  endif
137 #endif
138
139
140 #if (defined WIN32 || defined _WIN32) && defined(_M_ARM)
141 # include <Intrin.h>
142 # include "arm_neon.h"
143 # define CV_NEON 1
144 # define CPU_HAS_NEON_FEATURE (true)
145 #elif defined(__ARM_NEON__)
146 #  include <arm_neon.h>
147 #  define CV_NEON 1
148 #  define CPU_HAS_NEON_FEATURE (true)
149 #endif
150
151 #ifndef CV_SSE
152 #  define CV_SSE 0
153 #endif
154 #ifndef CV_SSE2
155 #  define CV_SSE2 0
156 #endif
157 #ifndef CV_SSE3
158 #  define CV_SSE3 0
159 #endif
160 #ifndef CV_SSSE3
161 #  define CV_SSSE3 0
162 #endif
163 #ifndef CV_SSE4_1
164 #  define CV_SSE4_1 0
165 #endif
166 #ifndef CV_SSE4_2
167 #  define CV_SSE4_2 0
168 #endif
169 #ifndef CV_AVX
170 #  define CV_AVX 0
171 #endif
172 #ifndef CV_NEON
173 #  define CV_NEON 0
174 #endif
175
176 #ifdef HAVE_TBB
177 #  include "tbb/tbb_stddef.h"
178 #  if TBB_VERSION_MAJOR*100 + TBB_VERSION_MINOR >= 202
179 #    include "tbb/tbb.h"
180 #    include "tbb/task.h"
181 #    undef min
182 #    undef max
183 #  else
184 #    undef HAVE_TBB
185 #  endif
186 #endif
187
188 #ifdef HAVE_EIGEN
189 #  if defined __GNUC__ && defined __APPLE__
190 #    pragma GCC diagnostic ignored "-Wshadow"
191 #  endif
192 #  include <Eigen/Core>
193 #  include "opencv2/core/eigen.hpp"
194 #endif
195
196 #ifdef __cplusplus
197
198 namespace cv
199 {
200 #ifdef HAVE_TBB
201
202     typedef tbb::blocked_range<int> BlockedRange;
203
204     template<typename Body> static inline
205     void parallel_for( const BlockedRange& range, const Body& body )
206     {
207         tbb::parallel_for(range, body);
208     }
209
210     template<typename Iterator, typename Body> static inline
211     void parallel_do( Iterator first, Iterator last, const Body& body )
212     {
213         tbb::parallel_do(first, last, body);
214     }
215
216     typedef tbb::split Split;
217
218     template<typename Body> static inline
219     void parallel_reduce( const BlockedRange& range, Body& body )
220     {
221         tbb::parallel_reduce(range, body);
222     }
223
224     typedef tbb::concurrent_vector<Rect> ConcurrentRectVector;
225     typedef tbb::concurrent_vector<double> ConcurrentDoubleVector;
226 #else
227     class BlockedRange
228     {
229     public:
230         BlockedRange() : _begin(0), _end(0), _grainsize(0) {}
231         BlockedRange(int b, int e, int g=1) : _begin(b), _end(e), _grainsize(g) {}
232         int begin() const { return _begin; }
233         int end() const { return _end; }
234         int grainsize() const { return _grainsize; }
235
236     protected:
237         int _begin, _end, _grainsize;
238     };
239
240     template<typename Body> static inline
241     void parallel_for( const BlockedRange& range, const Body& body )
242     {
243         body(range);
244     }
245     typedef std::vector<Rect> ConcurrentRectVector;
246     typedef std::vector<double> ConcurrentDoubleVector;
247
248     template<typename Iterator, typename Body> static inline
249     void parallel_do( Iterator first, Iterator last, const Body& body )
250     {
251         for( ; first != last; ++first )
252             body(*first);
253     }
254
255     class Split {};
256
257     template<typename Body> static inline
258     void parallel_reduce( const BlockedRange& range, Body& body )
259     {
260         body(range);
261     }
262 #endif
263
264     // Returns a static string if there is a parallel framework,
265     // NULL otherwise.
266     CV_EXPORTS const char* currentParallelFramework();
267 } //namespace cv
268
269 #define CV_INIT_ALGORITHM(classname, algname, memberinit) \
270     static ::cv::Algorithm* create##classname() \
271     { \
272         return new classname; \
273     } \
274     \
275     static ::cv::AlgorithmInfo& classname##_info() \
276     { \
277         static ::cv::AlgorithmInfo classname##_info_var(algname, create##classname); \
278         return classname##_info_var; \
279     } \
280     \
281     static ::cv::AlgorithmInfo& classname##_info_auto = classname##_info(); \
282     \
283     ::cv::AlgorithmInfo* classname::info() const \
284     { \
285         static volatile bool initialized = false; \
286         \
287         if( !initialized ) \
288         { \
289             initialized = true; \
290             classname obj; \
291             memberinit; \
292         } \
293         return &classname##_info(); \
294     }
295
296 #endif //__cplusplus
297
298 /* maximal size of vector to run matrix operations on it inline (i.e. w/o ipp calls) */
299 #define  CV_MAX_INLINE_MAT_OP_SIZE  10
300
301 /* maximal linear size of matrix to allocate it on stack. */
302 #define  CV_MAX_LOCAL_MAT_SIZE  32
303
304 /* maximal size of local memory storage */
305 #define  CV_MAX_LOCAL_SIZE  \
306     (CV_MAX_LOCAL_MAT_SIZE*CV_MAX_LOCAL_MAT_SIZE*(int)sizeof(double))
307
308 /* default image row align (in bytes) */
309 #define  CV_DEFAULT_IMAGE_ROW_ALIGN  4
310
311 /* matrices are continuous by default */
312 #define  CV_DEFAULT_MAT_ROW_ALIGN  1
313
314 /* maximum size of dynamic memory buffer.
315    cvAlloc reports an error if a larger block is requested. */
316 #define  CV_MAX_ALLOC_SIZE    (((size_t)1 << (sizeof(size_t)*8-2)))
317
318 /* the alignment of all the allocated buffers */
319 #define  CV_MALLOC_ALIGN    16
320
321 /* default alignment for dynamic data strucutures, resided in storages. */
322 #define  CV_STRUCT_ALIGN    ((int)sizeof(double))
323
324 /* default storage block size */
325 #define  CV_STORAGE_BLOCK_SIZE   ((1<<16) - 128)
326
327 /* default memory block for sparse array elements */
328 #define  CV_SPARSE_MAT_BLOCK    (1<<12)
329
330 /* initial hash table size */
331 #define  CV_SPARSE_HASH_SIZE0    (1<<10)
332
333 /* maximal average node_count/hash_size ratio beyond which hash table is resized */
334 #define  CV_SPARSE_HASH_RATIO    3
335
336 /* max length of strings */
337 #define  CV_MAX_STRLEN  1024
338
339 #if 0 /*def  CV_CHECK_FOR_NANS*/
340 #  define CV_CHECK_NANS( arr ) cvCheckArray((arr))
341 #else
342 #  define CV_CHECK_NANS( arr )
343 #endif
344
345 /****************************************************************************************\
346 *                                  Common declarations                                   *
347 \****************************************************************************************/
348
349 #ifdef __GNUC__
350 #  define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
351 #elif defined _MSC_VER
352 #  define CV_DECL_ALIGNED(x) __declspec(align(x))
353 #else
354 #  define CV_DECL_ALIGNED(x)
355 #endif
356
357 #ifndef CV_IMPL
358 #  define CV_IMPL CV_EXTERN_C
359 #endif
360
361 #define CV_DBG_BREAK() { volatile int* crashMe = 0; *crashMe = 0; }
362
363 /* default step, set in case of continuous data
364    to work around checks for valid step in some ipp functions */
365 #define  CV_STUB_STEP     (1 << 30)
366
367 #define  CV_SIZEOF_FLOAT ((int)sizeof(float))
368 #define  CV_SIZEOF_SHORT ((int)sizeof(short))
369
370 #define  CV_ORIGIN_TL  0
371 #define  CV_ORIGIN_BL  1
372
373 /* IEEE754 constants and macros */
374 #define  CV_POS_INF       0x7f800000
375 #define  CV_NEG_INF       0x807fffff /* CV_TOGGLE_FLT(0xff800000) */
376 #define  CV_1F            0x3f800000
377 #define  CV_TOGGLE_FLT(x) ((x)^((int)(x) < 0 ? 0x7fffffff : 0))
378 #define  CV_TOGGLE_DBL(x) \
379     ((x)^((int64)(x) < 0 ? CV_BIG_INT(0x7fffffffffffffff) : 0))
380
381 #define  CV_NOP(a)      (a)
382 #define  CV_ADD(a, b)   ((a) + (b))
383 #define  CV_SUB(a, b)   ((a) - (b))
384 #define  CV_MUL(a, b)   ((a) * (b))
385 #define  CV_AND(a, b)   ((a) & (b))
386 #define  CV_OR(a, b)    ((a) | (b))
387 #define  CV_XOR(a, b)   ((a) ^ (b))
388 #define  CV_ANDN(a, b)  (~(a) & (b))
389 #define  CV_ORN(a, b)   (~(a) | (b))
390 #define  CV_SQR(a)      ((a) * (a))
391
392 #define  CV_LT(a, b)    ((a) < (b))
393 #define  CV_LE(a, b)    ((a) <= (b))
394 #define  CV_EQ(a, b)    ((a) == (b))
395 #define  CV_NE(a, b)    ((a) != (b))
396 #define  CV_GT(a, b)    ((a) > (b))
397 #define  CV_GE(a, b)    ((a) >= (b))
398
399 #define  CV_NONZERO(a)      ((a) != 0)
400 #define  CV_NONZERO_FLT(a)  (((a)+(a)) != 0)
401
402 /* general-purpose saturation macros */
403 #define  CV_CAST_8U(t)  (uchar)(!((t) & ~255) ? (t) : (t) > 0 ? 255 : 0)
404 #define  CV_CAST_8S(t)  (schar)(!(((t)+128) & ~255) ? (t) : (t) > 0 ? 127 : -128)
405 #define  CV_CAST_16U(t) (ushort)(!((t) & ~65535) ? (t) : (t) > 0 ? 65535 : 0)
406 #define  CV_CAST_16S(t) (short)(!(((t)+32768) & ~65535) ? (t) : (t) > 0 ? 32767 : -32768)
407 #define  CV_CAST_32S(t) (int)(t)
408 #define  CV_CAST_64S(t) (int64)(t)
409 #define  CV_CAST_32F(t) (float)(t)
410 #define  CV_CAST_64F(t) (double)(t)
411
412 #define  CV_PASTE2(a,b) a##b
413 #define  CV_PASTE(a,b)  CV_PASTE2(a,b)
414
415 #define  CV_EMPTY
416 #define  CV_MAKE_STR(a) #a
417
418 #define  CV_ZERO_OBJ(x) memset((x), 0, sizeof(*(x)))
419
420 #define  CV_DIM(static_array) ((int)(sizeof(static_array)/sizeof((static_array)[0])))
421
422 #define  cvUnsupportedFormat "Unsupported format"
423
424 CV_INLINE void* cvAlignPtr( const void* ptr, int align CV_DEFAULT(32) )
425 {
426     assert( (align & (align-1)) == 0 );
427     return (void*)( ((size_t)ptr + align - 1) & ~(size_t)(align-1) );
428 }
429
430 CV_INLINE int cvAlign( int size, int align )
431 {
432     assert( (align & (align-1)) == 0 && size < INT_MAX );
433     return (size + align - 1) & -align;
434 }
435
436 CV_INLINE  CvSize  cvGetMatSize( const CvMat* mat )
437 {
438     CvSize size;
439     size.width = mat->cols;
440     size.height = mat->rows;
441     return size;
442 }
443
444 #define  CV_DESCALE(x,n)     (((x) + (1 << ((n)-1))) >> (n))
445 #define  CV_FLT_TO_FIX(x,n)  cvRound((x)*(1<<(n)))
446
447 /****************************************************************************************\
448
449   Generic implementation of QuickSort algorithm.
450   ----------------------------------------------
451   Using this macro user can declare customized sort function that can be much faster
452   than built-in qsort function because of lower overhead on elements
453   comparison and exchange. The macro takes less_than (or LT) argument - a macro or function
454   that takes 2 arguments returns non-zero if the first argument should be before the second
455   one in the sorted sequence and zero otherwise.
456
457   Example:
458
459     Suppose that the task is to sort points by ascending of y coordinates and if
460     y's are equal x's should ascend.
461
462     The code is:
463     ------------------------------------------------------------------------------
464            #define cmp_pts( pt1, pt2 ) \
465                ((pt1).y < (pt2).y || ((pt1).y < (pt2).y && (pt1).x < (pt2).x))
466
467            [static] CV_IMPLEMENT_QSORT( icvSortPoints, CvPoint, cmp_pts )
468     ------------------------------------------------------------------------------
469
470     After that the function "void icvSortPoints( CvPoint* array, size_t total, int aux );"
471     is available to user.
472
473   aux is an additional parameter, which can be used when comparing elements.
474   The current implementation was derived from *BSD system qsort():
475
476     * Copyright (c) 1992, 1993
477     *  The Regents of the University of California.  All rights reserved.
478     *
479     * Redistribution and use in source and binary forms, with or without
480     * modification, are permitted provided that the following conditions
481     * are met:
482     * 1. Redistributions of source code must retain the above copyright
483     *    notice, this list of conditions and the following disclaimer.
484     * 2. Redistributions in binary form must reproduce the above copyright
485     *    notice, this list of conditions and the following disclaimer in the
486     *    documentation and/or other materials provided with the distribution.
487     * 3. All advertising materials mentioning features or use of this software
488     *    must display the following acknowledgement:
489     *  This product includes software developed by the University of
490     *  California, Berkeley and its contributors.
491     * 4. Neither the name of the University nor the names of its contributors
492     *    may be used to endorse or promote products derived from this software
493     *    without specific prior written permission.
494     *
495     * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
496     * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
497     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
498     * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
499     * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
500     * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
501     * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
502     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
503     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
504     * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
505     * SUCH DAMAGE.
506
507 \****************************************************************************************/
508
509 #define CV_IMPLEMENT_QSORT_EX( func_name, T, LT, user_data_type )                   \
510 void func_name( T *array, size_t total, user_data_type aux )                        \
511 {                                                                                   \
512     int isort_thresh = 7;                                                           \
513     T t;                                                                            \
514     int sp = 0;                                                                     \
515                                                                                     \
516     struct                                                                          \
517     {                                                                               \
518         T *lb;                                                                      \
519         T *ub;                                                                      \
520     }                                                                               \
521     stack[48];                                                                      \
522                                                                                     \
523     aux = aux;                                                                      \
524                                                                                     \
525     if( total <= 1 )                                                                \
526         return;                                                                     \
527                                                                                     \
528     stack[0].lb = array;                                                            \
529     stack[0].ub = array + (total - 1);                                              \
530                                                                                     \
531     while( sp >= 0 )                                                                \
532     {                                                                               \
533         T* left = stack[sp].lb;                                                     \
534         T* right = stack[sp--].ub;                                                  \
535                                                                                     \
536         for(;;)                                                                     \
537         {                                                                           \
538             int i, n = (int)(right - left) + 1, m;                                  \
539             T* ptr;                                                                 \
540             T* ptr2;                                                                \
541                                                                                     \
542             if( n <= isort_thresh )                                                 \
543             {                                                                       \
544             insert_sort:                                                            \
545                 for( ptr = left + 1; ptr <= right; ptr++ )                          \
546                 {                                                                   \
547                     for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--)   \
548                         CV_SWAP( ptr2[0], ptr2[-1], t );                            \
549                 }                                                                   \
550                 break;                                                              \
551             }                                                                       \
552             else                                                                    \
553             {                                                                       \
554                 T* left0;                                                           \
555                 T* left1;                                                           \
556                 T* right0;                                                          \
557                 T* right1;                                                          \
558                 T* pivot;                                                           \
559                 T* a;                                                               \
560                 T* b;                                                               \
561                 T* c;                                                               \
562                 int swap_cnt = 0;                                                   \
563                                                                                     \
564                 left0 = left;                                                       \
565                 right0 = right;                                                     \
566                 pivot = left + (n/2);                                               \
567                                                                                     \
568                 if( n > 40 )                                                        \
569                 {                                                                   \
570                     int d = n / 8;                                                  \
571                     a = left, b = left + d, c = left + 2*d;                         \
572                     left = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))     \
573                                       : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));    \
574                                                                                     \
575                     a = pivot - d, b = pivot, c = pivot + d;                        \
576                     pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))    \
577                                       : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));    \
578                                                                                     \
579                     a = right - 2*d, b = right - d, c = right;                      \
580                     right = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))    \
581                                       : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));    \
582                 }                                                                   \
583                                                                                     \
584                 a = left, b = pivot, c = right;                                     \
585                 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))        \
586                                    : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));       \
587                 if( pivot != left0 )                                                \
588                 {                                                                   \
589                     CV_SWAP( *pivot, *left0, t );                                   \
590                     pivot = left0;                                                  \
591                 }                                                                   \
592                 left = left1 = left0 + 1;                                           \
593                 right = right1 = right0;                                            \
594                                                                                     \
595                 for(;;)                                                             \
596                 {                                                                   \
597                     while( left <= right && !LT(*pivot, *left) )                    \
598                     {                                                               \
599                         if( !LT(*left, *pivot) )                                    \
600                         {                                                           \
601                             if( left > left1 )                                      \
602                                 CV_SWAP( *left1, *left, t );                        \
603                             swap_cnt = 1;                                           \
604                             left1++;                                                \
605                         }                                                           \
606                         left++;                                                     \
607                     }                                                               \
608                                                                                     \
609                     while( left <= right && !LT(*right, *pivot) )                   \
610                     {                                                               \
611                         if( !LT(*pivot, *right) )                                   \
612                         {                                                           \
613                             if( right < right1 )                                    \
614                                 CV_SWAP( *right1, *right, t );                      \
615                             swap_cnt = 1;                                           \
616                             right1--;                                               \
617                         }                                                           \
618                         right--;                                                    \
619                     }                                                               \
620                                                                                     \
621                     if( left > right )                                              \
622                         break;                                                      \
623                     CV_SWAP( *left, *right, t );                                    \
624                     swap_cnt = 1;                                                   \
625                     left++;                                                         \
626                     right--;                                                        \
627                 }                                                                   \
628                                                                                     \
629                 if( swap_cnt == 0 )                                                 \
630                 {                                                                   \
631                     left = left0, right = right0;                                   \
632                     goto insert_sort;                                               \
633                 }                                                                   \
634                                                                                     \
635                 n = MIN( (int)(left1 - left0), (int)(left - left1) );               \
636                 for( i = 0; i < n; i++ )                                            \
637                     CV_SWAP( left0[i], left[i-n], t );                              \
638                                                                                     \
639                 n = MIN( (int)(right0 - right1), (int)(right1 - right) );           \
640                 for( i = 0; i < n; i++ )                                            \
641                     CV_SWAP( left[i], right0[i-n+1], t );                           \
642                 n = (int)(left - left1);                                            \
643                 m = (int)(right1 - right);                                          \
644                 if( n > 1 )                                                         \
645                 {                                                                   \
646                     if( m > 1 )                                                     \
647                     {                                                               \
648                         if( n > m )                                                 \
649                         {                                                           \
650                             stack[++sp].lb = left0;                                 \
651                             stack[sp].ub = left0 + n - 1;                           \
652                             left = right0 - m + 1, right = right0;                  \
653                         }                                                           \
654                         else                                                        \
655                         {                                                           \
656                             stack[++sp].lb = right0 - m + 1;                        \
657                             stack[sp].ub = right0;                                  \
658                             left = left0, right = left0 + n - 1;                    \
659                         }                                                           \
660                     }                                                               \
661                     else                                                            \
662                         left = left0, right = left0 + n - 1;                        \
663                 }                                                                   \
664                 else if( m > 1 )                                                    \
665                     left = right0 - m + 1, right = right0;                          \
666                 else                                                                \
667                     break;                                                          \
668             }                                                                       \
669         }                                                                           \
670     }                                                                               \
671 }
672
673 #define CV_IMPLEMENT_QSORT( func_name, T, cmp )  \
674     CV_IMPLEMENT_QSORT_EX( func_name, T, cmp, int )
675
676 /****************************************************************************************\
677 *                     Structures and macros for integration with IPP                     *
678 \****************************************************************************************/
679
680 /* IPP-compatible return codes */
681 typedef enum CvStatus
682 {
683     CV_BADMEMBLOCK_ERR          = -113,
684     CV_INPLACE_NOT_SUPPORTED_ERR= -112,
685     CV_UNMATCHED_ROI_ERR        = -111,
686     CV_NOTFOUND_ERR             = -110,
687     CV_BADCONVERGENCE_ERR       = -109,
688
689     CV_BADDEPTH_ERR             = -107,
690     CV_BADROI_ERR               = -106,
691     CV_BADHEADER_ERR            = -105,
692     CV_UNMATCHED_FORMATS_ERR    = -104,
693     CV_UNSUPPORTED_COI_ERR      = -103,
694     CV_UNSUPPORTED_CHANNELS_ERR = -102,
695     CV_UNSUPPORTED_DEPTH_ERR    = -101,
696     CV_UNSUPPORTED_FORMAT_ERR   = -100,
697
698     CV_BADARG_ERR               = -49,  //ipp comp
699     CV_NOTDEFINED_ERR           = -48,  //ipp comp
700
701     CV_BADCHANNELS_ERR          = -47,  //ipp comp
702     CV_BADRANGE_ERR             = -44,  //ipp comp
703     CV_BADSTEP_ERR              = -29,  //ipp comp
704
705     CV_BADFLAG_ERR              =  -12,
706     CV_DIV_BY_ZERO_ERR          =  -11, //ipp comp
707     CV_BADCOEF_ERR              =  -10,
708
709     CV_BADFACTOR_ERR            =  -7,
710     CV_BADPOINT_ERR             =  -6,
711     CV_BADSCALE_ERR             =  -4,
712     CV_OUTOFMEM_ERR             =  -3,
713     CV_NULLPTR_ERR              =  -2,
714     CV_BADSIZE_ERR              =  -1,
715     CV_NO_ERR                   =   0,
716     CV_OK                       =   CV_NO_ERR
717 }
718 CvStatus;
719
720 #define CV_NOTHROW throw()
721
722 typedef struct CvFuncTable
723 {
724     void*   fn_2d[CV_DEPTH_MAX];
725 }
726 CvFuncTable;
727
728 typedef struct CvBigFuncTable
729 {
730     void*   fn_2d[CV_DEPTH_MAX*4];
731 } CvBigFuncTable;
732
733 #define CV_INIT_FUNC_TAB( tab, FUNCNAME, FLAG )         \
734     (tab).fn_2d[CV_8U] = (void*)FUNCNAME##_8u##FLAG;    \
735     (tab).fn_2d[CV_8S] = 0;                             \
736     (tab).fn_2d[CV_16U] = (void*)FUNCNAME##_16u##FLAG;  \
737     (tab).fn_2d[CV_16S] = (void*)FUNCNAME##_16s##FLAG;  \
738     (tab).fn_2d[CV_32S] = (void*)FUNCNAME##_32s##FLAG;  \
739     (tab).fn_2d[CV_32F] = (void*)FUNCNAME##_32f##FLAG;  \
740     (tab).fn_2d[CV_64F] = (void*)FUNCNAME##_64f##FLAG
741
742 #ifdef __cplusplus
743
744 // < Deprecated
745
746 class CV_EXPORTS CvOpenGlFuncTab
747 {
748 public:
749     virtual ~CvOpenGlFuncTab();
750
751     virtual void genBuffers(int n, unsigned int* buffers) const = 0;
752     virtual void deleteBuffers(int n, const unsigned int* buffers) const = 0;
753
754     virtual void bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const = 0;
755     virtual void bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const = 0;
756
757     virtual void bindBuffer(unsigned int target, unsigned int buffer) const = 0;
758
759     virtual void* mapBuffer(unsigned int target, unsigned int access) const = 0;
760     virtual void unmapBuffer(unsigned int target) const = 0;
761
762     virtual void generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool underline, int start, int count, int base) const = 0;
763
764     virtual bool isGlContextInitialized() const = 0;
765 };
766
767 CV_EXPORTS void icvSetOpenGlFuncTab(const CvOpenGlFuncTab* tab);
768
769 CV_EXPORTS bool icvCheckGlError(const char* file, const int line, const char* func = "");
770
771 // >
772
773 namespace cv { namespace ogl {
774 CV_EXPORTS bool checkError(const char* file, const int line, const char* func = "");
775 }}
776
777 #define CV_CheckGlError() CV_DbgAssert( (cv::ogl::checkError(__FILE__, __LINE__, CV_Func)) )
778
779 #endif //__cplusplus
780
781 #endif // __OPENCV_CORE_INTERNAL_HPP__