]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/include/opencv/cxtypes.h
use the standard 64-bit integer definition (SF bug #2823918, thanks to Luca Bigliardi)
[opencv.git] / opencv / include / opencv / cxtypes.h
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 #ifndef _CXCORE_TYPES_H_
44 #define _CXCORE_TYPES_H_
45
46 #if !defined _CRT_SECURE_NO_DEPRECATE && _MSC_VER > 1300
47 #define _CRT_SECURE_NO_DEPRECATE /* to avoid multiple Visual Studio 2005 warnings */
48 #endif
49
50 #ifndef SKIP_INCLUDES
51   #include <assert.h>
52   #include <stdlib.h>
53   #include <string.h>
54   #include <float.h>
55
56 #if !defined _MSC_VER && !defined __BORLANDC__
57   #include <stdint.h>
58 #endif
59
60   #if defined __ICL
61     #define CV_ICC   __ICL
62   #elif defined __ICC
63     #define CV_ICC   __ICC
64   #elif defined __ECL
65     #define CV_ICC   __ECL
66   #elif defined __ECC
67     #define CV_ICC   __ECC
68   #endif
69
70   #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && \
71       (_MSC_VER >= 1400 || defined CV_ICC)) \
72       || (defined __SSE2__ && defined __GNUC__ && __GNUC__ >= 4)
73     #include <emmintrin.h>
74     #define CV_SSE2 1
75   #else
76     #define CV_SSE2 0
77   #endif
78
79   #if ((defined __SSE__ || defined __MMX__) && defined __GNUC__ && __GNUC__ >= 3)
80     #include <mmintrin.h>
81   #endif
82
83   #if defined __BORLANDC__
84     #include <fastmath.h>
85   #else
86     #include <math.h>
87   #endif
88
89   #ifdef HAVE_IPL
90       #ifndef __IPL_H__
91           #if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
92               #include <ipl.h>
93           #else
94               #include <ipl/ipl.h>
95           #endif
96       #endif
97   #elif defined __IPL_H__
98       #define HAVE_IPL
99   #endif
100 #endif // SKIP_INCLUDES
101
102 #if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
103     #define CV_CDECL __cdecl
104     #define CV_STDCALL __stdcall
105 #else
106     #define CV_CDECL
107     #define CV_STDCALL
108 #endif
109
110 #ifndef CV_EXTERN_C
111     #ifdef __cplusplus
112         #define CV_EXTERN_C extern "C"
113         #define CV_DEFAULT(val) = val
114     #else
115         #define CV_EXTERN_C
116         #define CV_DEFAULT(val)
117     #endif
118 #endif
119
120 #ifndef CV_EXTERN_C_FUNCPTR
121     #ifdef __cplusplus
122         #define CV_EXTERN_C_FUNCPTR(x) extern "C" { typedef x; }
123     #else
124         #define CV_EXTERN_C_FUNCPTR(x) typedef x
125     #endif
126 #endif
127
128 #ifndef CV_INLINE
129 #if defined __cplusplus
130     #define CV_INLINE inline
131 #elif (defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64 || defined WINCE) && !defined __GNUC__
132     #define CV_INLINE __inline
133 #else
134     #define CV_INLINE static
135 #endif
136 #endif /* CV_INLINE */
137
138 #if (defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64 || defined WINCE) && defined CVAPI_EXPORTS
139     #define CV_EXPORTS __declspec(dllexport)
140 #else
141     #define CV_EXPORTS
142 #endif
143
144 #ifndef CVAPI
145     #define CVAPI(rettype) CV_EXTERN_C CV_EXPORTS rettype CV_CDECL
146 #endif
147
148 #if defined _MSC_VER || defined __BORLANDC__
149 typedef __int64 int64;
150 typedef unsigned __int64 uint64;
151 #else
152 typedef int64_t int64;
153 typedef uint64_t uint64;
154 #endif
155
156 #ifndef HAVE_IPL
157 typedef unsigned char uchar;
158 typedef unsigned short ushort;
159 #endif
160
161 typedef signed char schar;
162
163 /* CvArr* is used to pass arbitrary
164  * array-like data structures
165  * into functions where the particular
166  * array type is recognized at runtime:
167  */
168 typedef void CvArr;
169
170 typedef union Cv32suf
171 {
172     int i;
173     unsigned u;
174     float f;
175 }
176 Cv32suf;
177
178 typedef union Cv64suf
179 {
180     int64 i;
181     uint64 u;
182     double f;
183 }
184 Cv64suf;
185
186 /****************************************************************************************\
187 *                             Common macros and inline functions                         *
188 \****************************************************************************************/
189
190 #define CV_PI   3.1415926535897932384626433832795
191 #define CV_LOG2 0.69314718055994530941723212145818
192
193 #define CV_SWAP(a,b,t) ((t) = (a), (a) = (b), (b) = (t))
194
195 #ifndef MIN
196 #define MIN(a,b)  ((a) > (b) ? (b) : (a))
197 #endif
198
199 #ifndef MAX
200 #define MAX(a,b)  ((a) < (b) ? (b) : (a))
201 #endif
202
203 /* min & max without jumps */
204 #define  CV_IMIN(a, b)  ((a) ^ (((a)^(b)) & (((a) < (b)) - 1)))
205
206 #define  CV_IMAX(a, b)  ((a) ^ (((a)^(b)) & (((a) > (b)) - 1)))
207
208 /* absolute value without jumps */
209 #ifndef __cplusplus
210 #define  CV_IABS(a)     (((a) ^ ((a) < 0 ? -1 : 0)) - ((a) < 0 ? -1 : 0))
211 #else
212 #define  CV_IABS(a)     abs(a)
213 #endif
214 #define  CV_CMP(a,b)    (((a) > (b)) - ((a) < (b)))
215 #define  CV_SIGN(a)     CV_CMP((a),0)
216
217 CV_INLINE  int  cvRound( double value )
218 {
219 #if CV_SSE2
220     __m128d t = _mm_set_sd( value );
221     return _mm_cvtsd_si32(t);
222 #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
223     int t;
224     __asm
225     {
226         fld value;
227         fistp t;
228     }
229     return t;
230 #elif defined HAVE_LRINT || defined CV_ICC || defined __GNUC__
231     return (int)lrint(value);
232 #else
233     // while this is not IEEE754-compliant rounding, it's usually a good enough approximation
234     return (int)(value + 0.5);
235 #endif
236 }
237
238
239 CV_INLINE  int  cvFloor( double value )
240 {
241 #ifdef __GNUC__
242     int i = (int)value;
243     return i - (i > value);
244 #elif CV_SSE2
245     __m128d t = _mm_set_sd( value );
246     int i = _mm_cvtsd_si32(t);
247     return i - _mm_movemask_pd(_mm_cmplt_sd(t, _mm_cvtsi32_sd(t,i)));
248 #else
249     int i = cvRound(value);
250     Cv32suf diff;
251     diff.f = (float)(value - i);
252     return i - (diff.i < 0);
253 #endif
254 }
255
256
257 CV_INLINE  int  cvCeil( double value )
258 {
259 #ifdef __GNUC__
260     int i = (int)value;
261     return i + (i < value);
262 #elif CV_SSE2
263     __m128d t = _mm_set_sd( value );
264     int i = _mm_cvtsd_si32(t);
265     return i + _mm_movemask_pd(_mm_cmplt_sd(_mm_cvtsi32_sd(t,i), t));
266 #else
267     int i = cvRound(value);
268     Cv32suf diff;
269     diff.f = (float)(i - value);
270     return i + (diff.i < 0);
271 #endif
272 }
273
274 #define cvInvSqrt(value) ((float)(1./sqrt(value)))
275 #define cvSqrt(value)  ((float)sqrt(value))
276
277 CV_INLINE int cvIsNaN( double value )
278 {
279 #if 1/*defined _MSC_VER || defined __BORLANDC__
280     return _isnan(value);
281 #elif defined __GNUC__
282     return isnan(value);
283 #else*/
284     Cv64suf ieee754;
285     ieee754.f = value;
286     return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) +
287            ((unsigned)ieee754.u != 0) > 0x7ff00000;
288 #endif
289 }
290
291
292 CV_INLINE int cvIsInf( double value )
293 {
294 #if 1/*defined _MSC_VER || defined __BORLANDC__
295     return !_finite(value);
296 #elif defined __GNUC__
297     return isinf(value);
298 #else*/
299     Cv64suf ieee754;
300     ieee754.f = value;
301     return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
302            (unsigned)ieee754.u == 0;
303 #endif
304 }
305
306
307 /*************** Random number generation *******************/
308
309 typedef uint64 CvRNG;
310
311 CV_INLINE CvRNG cvRNG( int64 seed CV_DEFAULT(-1))
312 {
313     CvRNG rng = seed ? (uint64)seed : (uint64)(int64)-1;
314     return rng;
315 }
316
317 /* Return random 32-bit unsigned integer: */
318 CV_INLINE unsigned cvRandInt( CvRNG* rng )
319 {
320     uint64 temp = *rng;
321     temp = (uint64)(unsigned)temp*4164903690U + (temp >> 32);
322     *rng = temp;
323     return (unsigned)temp;
324 }
325
326 /* Returns random floating-point number between 0 and 1: */
327 CV_INLINE double cvRandReal( CvRNG* rng )
328 {
329     return cvRandInt(rng)*2.3283064365386962890625e-10 /* 2^-32 */;
330 }
331
332 /****************************************************************************************\
333 *                                  Image type (IplImage)                                 *
334 \****************************************************************************************/
335
336 #ifndef HAVE_IPL
337
338 /*
339  * The following definitions (until #endif)
340  * is an extract from IPL headers.
341  * Copyright (c) 1995 Intel Corporation.
342  */
343 #define IPL_DEPTH_SIGN 0x80000000
344
345 #define IPL_DEPTH_1U     1
346 #define IPL_DEPTH_8U     8
347 #define IPL_DEPTH_16U   16
348 #define IPL_DEPTH_32F   32
349
350 #define IPL_DEPTH_8S  (IPL_DEPTH_SIGN| 8)
351 #define IPL_DEPTH_16S (IPL_DEPTH_SIGN|16)
352 #define IPL_DEPTH_32S (IPL_DEPTH_SIGN|32)
353
354 #define IPL_DATA_ORDER_PIXEL  0
355 #define IPL_DATA_ORDER_PLANE  1
356
357 #define IPL_ORIGIN_TL 0
358 #define IPL_ORIGIN_BL 1
359
360 #define IPL_ALIGN_4BYTES   4
361 #define IPL_ALIGN_8BYTES   8
362 #define IPL_ALIGN_16BYTES 16
363 #define IPL_ALIGN_32BYTES 32
364
365 #define IPL_ALIGN_DWORD   IPL_ALIGN_4BYTES
366 #define IPL_ALIGN_QWORD   IPL_ALIGN_8BYTES
367
368 #define IPL_BORDER_CONSTANT   0
369 #define IPL_BORDER_REPLICATE  1
370 #define IPL_BORDER_REFLECT    2
371 #define IPL_BORDER_WRAP       3
372
373 typedef struct _IplImage
374 {
375     int  nSize;             /* sizeof(IplImage) */
376     int  ID;                /* version (=0)*/
377     int  nChannels;         /* Most of OpenCV functions support 1,2,3 or 4 channels */
378     int  alphaChannel;      /* Ignored by OpenCV */
379     int  depth;             /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
380                                IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported.  */
381     char colorModel[4];     /* Ignored by OpenCV */
382     char channelSeq[4];     /* ditto */
383     int  dataOrder;         /* 0 - interleaved color channels, 1 - separate color channels.
384                                cvCreateImage can only create interleaved images */
385     int  origin;            /* 0 - top-left origin,
386                                1 - bottom-left origin (Windows bitmaps style).  */
387     int  align;             /* Alignment of image rows (4 or 8).
388                                OpenCV ignores it and uses widthStep instead.    */
389     int  width;             /* Image width in pixels.                           */
390     int  height;            /* Image height in pixels.                          */
391     struct _IplROI *roi;    /* Image ROI. If NULL, the whole image is selected. */
392     struct _IplImage *maskROI;      /* Must be NULL. */
393     void  *imageId;                 /* "           " */
394     struct _IplTileInfo *tileInfo;  /* "           " */
395     int  imageSize;         /* Image data size in bytes
396                                (==image->height*image->widthStep
397                                in case of interleaved data)*/
398     char *imageData;        /* Pointer to aligned image data.         */
399     int  widthStep;         /* Size of aligned image row in bytes.    */
400     int  BorderMode[4];     /* Ignored by OpenCV.                     */
401     int  BorderConst[4];    /* Ditto.                                 */
402     char *imageDataOrigin;  /* Pointer to very origin of image data
403                                (not necessarily aligned) -
404                                needed for correct deallocation */
405 }
406 IplImage;
407
408 typedef struct _IplTileInfo IplTileInfo;
409
410 typedef struct _IplROI
411 {
412     int  coi; /* 0 - no COI (all channels are selected), 1 - 0th channel is selected ...*/
413     int  xOffset;
414     int  yOffset;
415     int  width;
416     int  height;
417 }
418 IplROI;
419
420 typedef struct _IplConvKernel
421 {
422     int  nCols;
423     int  nRows;
424     int  anchorX;
425     int  anchorY;
426     int *values;
427     int  nShiftR;
428 }
429 IplConvKernel;
430
431 typedef struct _IplConvKernelFP
432 {
433     int  nCols;
434     int  nRows;
435     int  anchorX;
436     int  anchorY;
437     float *values;
438 }
439 IplConvKernelFP;
440
441 #define IPL_IMAGE_HEADER 1
442 #define IPL_IMAGE_DATA   2
443 #define IPL_IMAGE_ROI    4
444
445 #endif/*HAVE_IPL*/
446
447 /* extra border mode */
448 #define IPL_BORDER_REFLECT_101    4
449
450 #define IPL_IMAGE_MAGIC_VAL  ((int)sizeof(IplImage))
451 #define CV_TYPE_NAME_IMAGE "opencv-image"
452
453 #define CV_IS_IMAGE_HDR(img) \
454     ((img) != NULL && ((const IplImage*)(img))->nSize == sizeof(IplImage))
455
456 #define CV_IS_IMAGE(img) \
457     (CV_IS_IMAGE_HDR(img) && ((IplImage*)img)->imageData != NULL)
458
459 /* for storing double-precision
460    floating point data in IplImage's */
461 #define IPL_DEPTH_64F  64
462
463 /* get reference to pixel at (col,row),
464    for multi-channel images (col) should be multiplied by number of channels */
465 #define CV_IMAGE_ELEM( image, elemtype, row, col )       \
466     (((elemtype*)((image)->imageData + (image)->widthStep*(row)))[(col)])
467
468 /****************************************************************************************\
469 *                                  Matrix type (CvMat)                                   *
470 \****************************************************************************************/
471
472 #define CV_CN_MAX     64
473 #define CV_CN_SHIFT   3
474 #define CV_DEPTH_MAX  (1 << CV_CN_SHIFT)
475
476 #define CV_8U   0
477 #define CV_8S   1
478 #define CV_16U  2
479 #define CV_16S  3
480 #define CV_32S  4
481 #define CV_32F  5
482 #define CV_64F  6
483 #define CV_USRTYPE1 7
484
485 #define CV_MAT_DEPTH_MASK       (CV_DEPTH_MAX - 1)
486 #define CV_MAT_DEPTH(flags)     ((flags) & CV_MAT_DEPTH_MASK)
487
488 #define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT))
489 #define CV_MAKE_TYPE CV_MAKETYPE
490
491 #define CV_8UC1 CV_MAKETYPE(CV_8U,1)
492 #define CV_8UC2 CV_MAKETYPE(CV_8U,2)
493 #define CV_8UC3 CV_MAKETYPE(CV_8U,3)
494 #define CV_8UC4 CV_MAKETYPE(CV_8U,4)
495 #define CV_8UC(n) CV_MAKETYPE(CV_8U,(n))
496
497 #define CV_8SC1 CV_MAKETYPE(CV_8S,1)
498 #define CV_8SC2 CV_MAKETYPE(CV_8S,2)
499 #define CV_8SC3 CV_MAKETYPE(CV_8S,3)
500 #define CV_8SC4 CV_MAKETYPE(CV_8S,4)
501 #define CV_8SC(n) CV_MAKETYPE(CV_8S,(n))
502
503 #define CV_16UC1 CV_MAKETYPE(CV_16U,1)
504 #define CV_16UC2 CV_MAKETYPE(CV_16U,2)
505 #define CV_16UC3 CV_MAKETYPE(CV_16U,3)
506 #define CV_16UC4 CV_MAKETYPE(CV_16U,4)
507 #define CV_16UC(n) CV_MAKETYPE(CV_16U,(n))
508
509 #define CV_16SC1 CV_MAKETYPE(CV_16S,1)
510 #define CV_16SC2 CV_MAKETYPE(CV_16S,2)
511 #define CV_16SC3 CV_MAKETYPE(CV_16S,3)
512 #define CV_16SC4 CV_MAKETYPE(CV_16S,4)
513 #define CV_16SC(n) CV_MAKETYPE(CV_16S,(n))
514
515 #define CV_32SC1 CV_MAKETYPE(CV_32S,1)
516 #define CV_32SC2 CV_MAKETYPE(CV_32S,2)
517 #define CV_32SC3 CV_MAKETYPE(CV_32S,3)
518 #define CV_32SC4 CV_MAKETYPE(CV_32S,4)
519 #define CV_32SC(n) CV_MAKETYPE(CV_32S,(n))
520
521 #define CV_32FC1 CV_MAKETYPE(CV_32F,1)
522 #define CV_32FC2 CV_MAKETYPE(CV_32F,2)
523 #define CV_32FC3 CV_MAKETYPE(CV_32F,3)
524 #define CV_32FC4 CV_MAKETYPE(CV_32F,4)
525 #define CV_32FC(n) CV_MAKETYPE(CV_32F,(n))
526
527 #define CV_64FC1 CV_MAKETYPE(CV_64F,1)
528 #define CV_64FC2 CV_MAKETYPE(CV_64F,2)
529 #define CV_64FC3 CV_MAKETYPE(CV_64F,3)
530 #define CV_64FC4 CV_MAKETYPE(CV_64F,4)
531 #define CV_64FC(n) CV_MAKETYPE(CV_64F,(n))
532
533 #define CV_AUTO_STEP  0x7fffffff
534 #define CV_WHOLE_ARR  cvSlice( 0, 0x3fffffff )
535
536 #define CV_MAT_CN_MASK          ((CV_CN_MAX - 1) << CV_CN_SHIFT)
537 #define CV_MAT_CN(flags)        ((((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1)
538 #define CV_MAT_TYPE_MASK        (CV_DEPTH_MAX*CV_CN_MAX - 1)
539 #define CV_MAT_TYPE(flags)      ((flags) & CV_MAT_TYPE_MASK)
540 #define CV_MAT_CONT_FLAG_SHIFT  14
541 #define CV_MAT_CONT_FLAG        (1 << CV_MAT_CONT_FLAG_SHIFT)
542 #define CV_IS_MAT_CONT(flags)   ((flags) & CV_MAT_CONT_FLAG)
543 #define CV_IS_CONT_MAT          CV_IS_MAT_CONT
544 #define CV_MAT_TEMP_FLAG_SHIFT  15
545 #define CV_MAT_TEMP_FLAG        (1 << CV_MAT_TEMP_FLAG_SHIFT)
546 #define CV_IS_TEMP_MAT(flags)   ((flags) & CV_MAT_TEMP_FLAG)
547
548 #define CV_MAGIC_MASK       0xFFFF0000
549 #define CV_MAT_MAGIC_VAL    0x42420000
550 #define CV_TYPE_NAME_MAT    "opencv-matrix"
551
552 typedef struct CvMat
553 {
554     int type;
555     int step;
556
557     /* for internal use only */
558     int* refcount;
559     int hdr_refcount;
560
561     union
562     {
563         uchar* ptr;
564         short* s;
565         int* i;
566         float* fl;
567         double* db;
568     } data;
569
570 #ifdef __cplusplus
571     union
572     {
573         int rows;
574         int height;
575     };
576
577     union
578     {
579         int cols;
580         int width;
581     };
582 #else
583     int rows;
584     int cols;
585 #endif
586
587 }
588 CvMat;
589
590
591 #define CV_IS_MAT_HDR(mat) \
592     ((mat) != NULL && \
593     (((const CvMat*)(mat))->type & CV_MAGIC_MASK) == CV_MAT_MAGIC_VAL && \
594     ((const CvMat*)(mat))->cols > 0 && ((const CvMat*)(mat))->rows > 0)
595
596 #define CV_IS_MAT(mat) \
597     (CV_IS_MAT_HDR(mat) && ((const CvMat*)(mat))->data.ptr != NULL)
598
599 #define CV_IS_MASK_ARR(mat) \
600     (((mat)->type & (CV_MAT_TYPE_MASK & ~CV_8SC1)) == 0)
601
602 #define CV_ARE_TYPES_EQ(mat1, mat2) \
603     ((((mat1)->type ^ (mat2)->type) & CV_MAT_TYPE_MASK) == 0)
604
605 #define CV_ARE_CNS_EQ(mat1, mat2) \
606     ((((mat1)->type ^ (mat2)->type) & CV_MAT_CN_MASK) == 0)
607
608 #define CV_ARE_DEPTHS_EQ(mat1, mat2) \
609     ((((mat1)->type ^ (mat2)->type) & CV_MAT_DEPTH_MASK) == 0)
610
611 #define CV_ARE_SIZES_EQ(mat1, mat2) \
612     ((mat1)->rows == (mat2)->rows && (mat1)->cols == (mat2)->cols)
613
614 #define CV_IS_MAT_CONST(mat)  \
615     (((mat)->rows|(mat)->cols) == 1)
616
617 /* Size of each channel item,
618    0x124489 = 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */
619 #define CV_ELEM_SIZE1(type) \
620     ((((sizeof(size_t)<<28)|0x8442211) >> CV_MAT_DEPTH(type)*4) & 15)
621
622 /* 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */
623 #define CV_ELEM_SIZE(type) \
624     (CV_MAT_CN(type) << ((((sizeof(size_t)/4+1)*16384|0x3a50) >> CV_MAT_DEPTH(type)*2) & 3))
625
626 /* Inline constructor. No data is allocated internally!!!
627  * (Use together with cvCreateData, or use cvCreateMat instead to
628  * get a matrix with allocated data):
629  */
630 CV_INLINE CvMat cvMat( int rows, int cols, int type, void* data CV_DEFAULT(NULL))
631 {
632     CvMat m;
633
634     assert( (unsigned)CV_MAT_DEPTH(type) <= CV_64F );
635     type = CV_MAT_TYPE(type);
636     m.type = CV_MAT_MAGIC_VAL | CV_MAT_CONT_FLAG | type;
637     m.cols = cols;
638     m.rows = rows;
639     m.step = m.cols*CV_ELEM_SIZE(type);
640     m.data.ptr = (uchar*)data;
641     m.refcount = NULL;
642     m.hdr_refcount = 0;
643
644     return m;
645 }
646
647
648 #define CV_MAT_ELEM_PTR_FAST( mat, row, col, pix_size )  \
649     (assert( (unsigned)(row) < (unsigned)(mat).rows &&   \
650              (unsigned)(col) < (unsigned)(mat).cols ),   \
651      (mat).data.ptr + (size_t)(mat).step*(row) + (pix_size)*(col))
652
653 #define CV_MAT_ELEM_PTR( mat, row, col )                 \
654     CV_MAT_ELEM_PTR_FAST( mat, row, col, CV_ELEM_SIZE((mat).type) )
655
656 #define CV_MAT_ELEM( mat, elemtype, row, col )           \
657     (*(elemtype*)CV_MAT_ELEM_PTR_FAST( mat, row, col, sizeof(elemtype)))
658
659
660 CV_INLINE  double  cvmGet( const CvMat* mat, int row, int col )
661 {
662     int type;
663
664     type = CV_MAT_TYPE(mat->type);
665     assert( (unsigned)row < (unsigned)mat->rows &&
666             (unsigned)col < (unsigned)mat->cols );
667
668     if( type == CV_32FC1 )
669         return ((float*)(mat->data.ptr + (size_t)mat->step*row))[col];
670     else
671     {
672         assert( type == CV_64FC1 );
673         return ((double*)(mat->data.ptr + (size_t)mat->step*row))[col];
674     }
675 }
676
677
678 CV_INLINE  void  cvmSet( CvMat* mat, int row, int col, double value )
679 {
680     int type;
681     type = CV_MAT_TYPE(mat->type);
682     assert( (unsigned)row < (unsigned)mat->rows &&
683             (unsigned)col < (unsigned)mat->cols );
684
685     if( type == CV_32FC1 )
686         ((float*)(mat->data.ptr + (size_t)mat->step*row))[col] = (float)value;
687     else
688     {
689         assert( type == CV_64FC1 );
690         ((double*)(mat->data.ptr + (size_t)mat->step*row))[col] = (double)value;
691     }
692 }
693
694
695 CV_INLINE int cvIplDepth( int type )
696 {
697     int depth = CV_MAT_DEPTH(type);
698     return CV_ELEM_SIZE1(depth)*8 | (depth == CV_8S || depth == CV_16S ||
699            depth == CV_32S ? IPL_DEPTH_SIGN : 0);
700 }
701
702
703 /****************************************************************************************\
704 *                       Multi-dimensional dense array (CvMatND)                          *
705 \****************************************************************************************/
706
707 #define CV_MATND_MAGIC_VAL    0x42430000
708 #define CV_TYPE_NAME_MATND    "opencv-nd-matrix"
709
710 #define CV_MAX_DIM            32
711 #define CV_MAX_DIM_HEAP       (1 << 16)
712
713 typedef struct CvMatND
714 {
715     int type;
716     int dims;
717
718     int* refcount;
719     int hdr_refcount;
720
721     union
722     {
723         uchar* ptr;
724         float* fl;
725         double* db;
726         int* i;
727         short* s;
728     } data;
729
730     struct
731     {
732         int size;
733         int step;
734     }
735     dim[CV_MAX_DIM];
736 }
737 CvMatND;
738
739 #define CV_IS_MATND_HDR(mat) \
740     ((mat) != NULL && (((const CvMatND*)(mat))->type & CV_MAGIC_MASK) == CV_MATND_MAGIC_VAL)
741
742 #define CV_IS_MATND(mat) \
743     (CV_IS_MATND_HDR(mat) && ((const CvMatND*)(mat))->data.ptr != NULL)
744
745
746 /****************************************************************************************\
747 *                      Multi-dimensional sparse array (CvSparseMat)                      *
748 \****************************************************************************************/
749
750 #define CV_SPARSE_MAT_MAGIC_VAL    0x42440000
751 #define CV_TYPE_NAME_SPARSE_MAT    "opencv-sparse-matrix"
752
753 struct CvSet;
754
755 typedef struct CvSparseMat
756 {
757     int type;
758     int dims;
759     int* refcount;
760     int hdr_refcount;
761
762     struct CvSet* heap;
763     void** hashtable;
764     int hashsize;
765     int valoffset;
766     int idxoffset;
767     int size[CV_MAX_DIM];
768 }
769 CvSparseMat;
770
771 #define CV_IS_SPARSE_MAT_HDR(mat) \
772     ((mat) != NULL && \
773     (((const CvSparseMat*)(mat))->type & CV_MAGIC_MASK) == CV_SPARSE_MAT_MAGIC_VAL)
774
775 #define CV_IS_SPARSE_MAT(mat) \
776     CV_IS_SPARSE_MAT_HDR(mat)
777
778 /**************** iteration through a sparse array *****************/
779
780 typedef struct CvSparseNode
781 {
782     unsigned hashval;
783     struct CvSparseNode* next;
784 }
785 CvSparseNode;
786
787 typedef struct CvSparseMatIterator
788 {
789     CvSparseMat* mat;
790     CvSparseNode* node;
791     int curidx;
792 }
793 CvSparseMatIterator;
794
795 #define CV_NODE_VAL(mat,node)   ((void*)((uchar*)(node) + (mat)->valoffset))
796 #define CV_NODE_IDX(mat,node)   ((int*)((uchar*)(node) + (mat)->idxoffset))
797
798 /****************************************************************************************\
799 *                                         Histogram                                      *
800 \****************************************************************************************/
801
802 typedef int CvHistType;
803
804 #define CV_HIST_MAGIC_VAL     0x42450000
805 #define CV_HIST_UNIFORM_FLAG  (1 << 10)
806
807 /* indicates whether bin ranges are set already or not */
808 #define CV_HIST_RANGES_FLAG   (1 << 11)
809
810 #define CV_HIST_ARRAY         0
811 #define CV_HIST_SPARSE        1
812 #define CV_HIST_TREE          CV_HIST_SPARSE
813
814 /* should be used as a parameter only,
815    it turns to CV_HIST_UNIFORM_FLAG of hist->type */
816 #define CV_HIST_UNIFORM       1
817
818 typedef struct CvHistogram
819 {
820     int     type;
821     CvArr*  bins;
822     float   thresh[CV_MAX_DIM][2];  /* For uniform histograms.                      */
823     float** thresh2;                /* For non-uniform histograms.                  */
824     CvMatND mat;                    /* Embedded matrix header for array histograms. */
825 }
826 CvHistogram;
827
828 #define CV_IS_HIST( hist ) \
829     ((hist) != NULL  && \
830      (((CvHistogram*)(hist))->type & CV_MAGIC_MASK) == CV_HIST_MAGIC_VAL && \
831      (hist)->bins != NULL)
832
833 #define CV_IS_UNIFORM_HIST( hist ) \
834     (((hist)->type & CV_HIST_UNIFORM_FLAG) != 0)
835
836 #define CV_IS_SPARSE_HIST( hist ) \
837     CV_IS_SPARSE_MAT((hist)->bins)
838
839 #define CV_HIST_HAS_RANGES( hist ) \
840     (((hist)->type & CV_HIST_RANGES_FLAG) != 0)
841
842 /****************************************************************************************\
843 *                      Other supplementary data type definitions                         *
844 \****************************************************************************************/
845
846 /*************************************** CvRect *****************************************/
847
848 typedef struct CvRect
849 {
850     int x;
851     int y;
852     int width;
853     int height;
854 }
855 CvRect;
856
857 CV_INLINE  CvRect  cvRect( int x, int y, int width, int height )
858 {
859     CvRect r;
860
861     r.x = x;
862     r.y = y;
863     r.width = width;
864     r.height = height;
865
866     return r;
867 }
868
869
870 CV_INLINE  IplROI  cvRectToROI( CvRect rect, int coi )
871 {
872     IplROI roi;
873     roi.xOffset = rect.x;
874     roi.yOffset = rect.y;
875     roi.width = rect.width;
876     roi.height = rect.height;
877     roi.coi = coi;
878
879     return roi;
880 }
881
882
883 CV_INLINE  CvRect  cvROIToRect( IplROI roi )
884 {
885     return cvRect( roi.xOffset, roi.yOffset, roi.width, roi.height );
886 }
887
888 /*********************************** CvTermCriteria *************************************/
889
890 #define CV_TERMCRIT_ITER    1
891 #define CV_TERMCRIT_NUMBER  CV_TERMCRIT_ITER
892 #define CV_TERMCRIT_EPS     2
893
894 typedef struct CvTermCriteria
895 {
896     int    type;  /* may be combination of
897                      CV_TERMCRIT_ITER
898                      CV_TERMCRIT_EPS */
899     int    max_iter;
900     double epsilon;
901 }
902 CvTermCriteria;
903
904 CV_INLINE  CvTermCriteria  cvTermCriteria( int type, int max_iter, double epsilon )
905 {
906     CvTermCriteria t;
907
908     t.type = type;
909     t.max_iter = max_iter;
910     t.epsilon = (float)epsilon;
911
912     return t;
913 }
914
915
916 /******************************* CvPoint and variants ***********************************/
917
918 typedef struct CvPoint
919 {
920     int x;
921     int y;
922 }
923 CvPoint;
924
925
926 CV_INLINE  CvPoint  cvPoint( int x, int y )
927 {
928     CvPoint p;
929
930     p.x = x;
931     p.y = y;
932
933     return p;
934 }
935
936
937 typedef struct CvPoint2D32f
938 {
939     float x;
940     float y;
941 }
942 CvPoint2D32f;
943
944
945 CV_INLINE  CvPoint2D32f  cvPoint2D32f( double x, double y )
946 {
947     CvPoint2D32f p;
948
949     p.x = (float)x;
950     p.y = (float)y;
951
952     return p;
953 }
954
955
956 CV_INLINE  CvPoint2D32f  cvPointTo32f( CvPoint point )
957 {
958     return cvPoint2D32f( (float)point.x, (float)point.y );
959 }
960
961
962 CV_INLINE  CvPoint  cvPointFrom32f( CvPoint2D32f point )
963 {
964     CvPoint ipt;
965     ipt.x = cvRound(point.x);
966     ipt.y = cvRound(point.y);
967
968     return ipt;
969 }
970
971
972 typedef struct CvPoint3D32f
973 {
974     float x;
975     float y;
976     float z;
977 }
978 CvPoint3D32f;
979
980
981 CV_INLINE  CvPoint3D32f  cvPoint3D32f( double x, double y, double z )
982 {
983     CvPoint3D32f p;
984
985     p.x = (float)x;
986     p.y = (float)y;
987     p.z = (float)z;
988
989     return p;
990 }
991
992
993 typedef struct CvPoint2D64f
994 {
995     double x;
996     double y;
997 }
998 CvPoint2D64f;
999
1000
1001 CV_INLINE  CvPoint2D64f  cvPoint2D64f( double x, double y )
1002 {
1003     CvPoint2D64f p;
1004
1005     p.x = x;
1006     p.y = y;
1007
1008     return p;
1009 }
1010
1011
1012 typedef struct CvPoint3D64f
1013 {
1014     double x;
1015     double y;
1016     double z;
1017 }
1018 CvPoint3D64f;
1019
1020
1021 CV_INLINE  CvPoint3D64f  cvPoint3D64f( double x, double y, double z )
1022 {
1023     CvPoint3D64f p;
1024
1025     p.x = x;
1026     p.y = y;
1027     p.z = z;
1028
1029     return p;
1030 }
1031
1032
1033 /******************************** CvSize's & CvBox **************************************/
1034
1035 typedef struct
1036 {
1037     int width;
1038     int height;
1039 }
1040 CvSize;
1041
1042 CV_INLINE  CvSize  cvSize( int width, int height )
1043 {
1044     CvSize s;
1045
1046     s.width = width;
1047     s.height = height;
1048
1049     return s;
1050 }
1051
1052 typedef struct CvSize2D32f
1053 {
1054     float width;
1055     float height;
1056 }
1057 CvSize2D32f;
1058
1059
1060 CV_INLINE  CvSize2D32f  cvSize2D32f( double width, double height )
1061 {
1062     CvSize2D32f s;
1063
1064     s.width = (float)width;
1065     s.height = (float)height;
1066
1067     return s;
1068 }
1069
1070 typedef struct CvBox2D
1071 {
1072     CvPoint2D32f center;  /* Center of the box.                          */
1073     CvSize2D32f  size;    /* Box width and length.                       */
1074     float angle;          /* Angle between the horizontal axis           */
1075                           /* and the first side (i.e. length) in degrees */
1076 }
1077 CvBox2D;
1078
1079
1080 /* Line iterator state: */
1081 typedef struct CvLineIterator
1082 {
1083     /* Pointer to the current point: */
1084     uchar* ptr;
1085
1086     /* Bresenham algorithm state: */
1087     int  err;
1088     int  plus_delta;
1089     int  minus_delta;
1090     int  plus_step;
1091     int  minus_step;
1092 }
1093 CvLineIterator;
1094
1095
1096
1097 /************************************* CvSlice ******************************************/
1098
1099 typedef struct CvSlice
1100 {
1101     int  start_index, end_index;
1102 }
1103 CvSlice;
1104
1105 CV_INLINE  CvSlice  cvSlice( int start, int end )
1106 {
1107     CvSlice slice;
1108     slice.start_index = start;
1109     slice.end_index = end;
1110
1111     return slice;
1112 }
1113
1114 #define CV_WHOLE_SEQ_END_INDEX 0x3fffffff
1115 #define CV_WHOLE_SEQ  cvSlice(0, CV_WHOLE_SEQ_END_INDEX)
1116
1117
1118 /************************************* CvScalar *****************************************/
1119
1120 typedef struct CvScalar
1121 {
1122     double val[4];
1123 }
1124 CvScalar;
1125
1126 CV_INLINE  CvScalar  cvScalar( double val0, double val1 CV_DEFAULT(0),
1127                                double val2 CV_DEFAULT(0), double val3 CV_DEFAULT(0))
1128 {
1129     CvScalar scalar;
1130     scalar.val[0] = val0; scalar.val[1] = val1;
1131     scalar.val[2] = val2; scalar.val[3] = val3;
1132     return scalar;
1133 }
1134
1135
1136 CV_INLINE  CvScalar  cvRealScalar( double val0 )
1137 {
1138     CvScalar scalar;
1139     scalar.val[0] = val0;
1140     scalar.val[1] = scalar.val[2] = scalar.val[3] = 0;
1141     return scalar;
1142 }
1143
1144 CV_INLINE  CvScalar  cvScalarAll( double val0123 )
1145 {
1146     CvScalar scalar;
1147     scalar.val[0] = val0123;
1148     scalar.val[1] = val0123;
1149     scalar.val[2] = val0123;
1150     scalar.val[3] = val0123;
1151     return scalar;
1152 }
1153
1154 /****************************************************************************************\
1155 *                                   Dynamic Data structures                              *
1156 \****************************************************************************************/
1157
1158 /******************************** Memory storage ****************************************/
1159
1160 typedef struct CvMemBlock
1161 {
1162     struct CvMemBlock*  prev;
1163     struct CvMemBlock*  next;
1164 }
1165 CvMemBlock;
1166
1167 #define CV_STORAGE_MAGIC_VAL    0x42890000
1168
1169 typedef struct CvMemStorage
1170 {
1171     int signature;
1172     CvMemBlock* bottom;           /* First allocated block.                   */
1173     CvMemBlock* top;              /* Current memory block - top of the stack. */
1174     struct  CvMemStorage* parent; /* We get new blocks from parent as needed. */
1175     int block_size;               /* Block size.                              */
1176     int free_space;               /* Remaining free space in current block.   */
1177 }
1178 CvMemStorage;
1179
1180 #define CV_IS_STORAGE(storage)  \
1181     ((storage) != NULL &&       \
1182     (((CvMemStorage*)(storage))->signature & CV_MAGIC_MASK) == CV_STORAGE_MAGIC_VAL)
1183
1184
1185 typedef struct CvMemStoragePos
1186 {
1187     CvMemBlock* top;
1188     int free_space;
1189 }
1190 CvMemStoragePos;
1191
1192
1193 /*********************************** Sequence *******************************************/
1194
1195 typedef struct CvSeqBlock
1196 {
1197     struct CvSeqBlock*  prev; /* Previous sequence block.                   */
1198     struct CvSeqBlock*  next; /* Next sequence block.                       */
1199   int    start_index;         /* Index of the first element in the block +  */
1200                               /* sequence->first->start_index.              */
1201     int    count;             /* Number of elements in the block.           */
1202     schar* data;              /* Pointer to the first element of the block. */
1203 }
1204 CvSeqBlock;
1205
1206
1207 #define CV_TREE_NODE_FIELDS(node_type)                               \
1208     int       flags;             /* Miscellaneous flags.     */      \
1209     int       header_size;       /* Size of sequence header. */      \
1210     struct    node_type* h_prev; /* Previous sequence.       */      \
1211     struct    node_type* h_next; /* Next sequence.           */      \
1212     struct    node_type* v_prev; /* 2nd previous sequence.   */      \
1213     struct    node_type* v_next  /* 2nd next sequence.       */
1214
1215 /*
1216    Read/Write sequence.
1217    Elements can be dynamically inserted to or deleted from the sequence.
1218 */
1219 #define CV_SEQUENCE_FIELDS()                                              \
1220     CV_TREE_NODE_FIELDS(CvSeq);                                           \
1221     int       total;          /* Total number of elements.            */  \
1222     int       elem_size;      /* Size of sequence element in bytes.   */  \
1223     schar*    block_max;      /* Maximal bound of the last block.     */  \
1224     schar*    ptr;            /* Current write pointer.               */  \
1225     int       delta_elems;    /* Grow seq this many at a time.        */  \
1226     CvMemStorage* storage;    /* Where the seq is stored.             */  \
1227     CvSeqBlock* free_blocks;  /* Free blocks list.                    */  \
1228     CvSeqBlock* first;        /* Pointer to the first sequence block. */
1229
1230 typedef struct CvSeq
1231 {
1232     CV_SEQUENCE_FIELDS()
1233 }
1234 CvSeq;
1235
1236 #define CV_TYPE_NAME_SEQ             "opencv-sequence"
1237 #define CV_TYPE_NAME_SEQ_TREE        "opencv-sequence-tree"
1238
1239 /*************************************** Set ********************************************/
1240 /*
1241   Set.
1242   Order is not preserved. There can be gaps between sequence elements.
1243   After the element has been inserted it stays in the same place all the time.
1244   The MSB(most-significant or sign bit) of the first field (flags) is 0 iff the element exists.
1245 */
1246 #define CV_SET_ELEM_FIELDS(elem_type)   \
1247     int  flags;                         \
1248     struct elem_type* next_free;
1249
1250 typedef struct CvSetElem
1251 {
1252     CV_SET_ELEM_FIELDS(CvSetElem)
1253 }
1254 CvSetElem;
1255
1256 #define CV_SET_FIELDS()      \
1257     CV_SEQUENCE_FIELDS()     \
1258     CvSetElem* free_elems;   \
1259     int active_count;
1260
1261 typedef struct CvSet
1262 {
1263     CV_SET_FIELDS()
1264 }
1265 CvSet;
1266
1267
1268 #define CV_SET_ELEM_IDX_MASK   ((1 << 26) - 1)
1269 #define CV_SET_ELEM_FREE_FLAG  (1 << (sizeof(int)*8-1))
1270
1271 /* Checks whether the element pointed by ptr belongs to a set or not */
1272 #define CV_IS_SET_ELEM( ptr )  (((CvSetElem*)(ptr))->flags >= 0)
1273
1274 /************************************* Graph ********************************************/
1275
1276 /*
1277   We represent a graph as a set of vertices.
1278   Vertices contain their adjacency lists (more exactly, pointers to first incoming or
1279   outcoming edge (or 0 if isolated vertex)). Edges are stored in another set.
1280   There is a singly-linked list of incoming/outcoming edges for each vertex.
1281
1282   Each edge consists of
1283
1284      o   Two pointers to the starting and ending vertices
1285          (vtx[0] and vtx[1] respectively).
1286
1287          A graph may be oriented or not. In the latter case, edges between
1288          vertex i to vertex j are not distinguished during search operations.
1289
1290      o   Two pointers to next edges for the starting and ending vertices, where
1291          next[0] points to the next edge in the vtx[0] adjacency list and
1292          next[1] points to the next edge in the vtx[1] adjacency list.
1293 */
1294 #define CV_GRAPH_EDGE_FIELDS()      \
1295     int flags;                      \
1296     float weight;                   \
1297     struct CvGraphEdge* next[2];    \
1298     struct CvGraphVtx* vtx[2];
1299
1300
1301 #define CV_GRAPH_VERTEX_FIELDS()    \
1302     int flags;                      \
1303     struct CvGraphEdge* first;
1304
1305
1306 typedef struct CvGraphEdge
1307 {
1308     CV_GRAPH_EDGE_FIELDS()
1309 }
1310 CvGraphEdge;
1311
1312 typedef struct CvGraphVtx
1313 {
1314     CV_GRAPH_VERTEX_FIELDS()
1315 }
1316 CvGraphVtx;
1317
1318 typedef struct CvGraphVtx2D
1319 {
1320     CV_GRAPH_VERTEX_FIELDS()
1321     CvPoint2D32f* ptr;
1322 }
1323 CvGraphVtx2D;
1324
1325 /*
1326    Graph is "derived" from the set (this is set a of vertices)
1327    and includes another set (edges)
1328 */
1329 #define  CV_GRAPH_FIELDS()   \
1330     CV_SET_FIELDS()          \
1331     CvSet* edges;
1332
1333 typedef struct CvGraph
1334 {
1335     CV_GRAPH_FIELDS()
1336 }
1337 CvGraph;
1338
1339 #define CV_TYPE_NAME_GRAPH "opencv-graph"
1340
1341 /*********************************** Chain/Countour *************************************/
1342
1343 typedef struct CvChain
1344 {
1345     CV_SEQUENCE_FIELDS()
1346     CvPoint  origin;
1347 }
1348 CvChain;
1349
1350 #define CV_CONTOUR_FIELDS()  \
1351     CV_SEQUENCE_FIELDS()     \
1352     CvRect rect;             \
1353     int color;               \
1354     int reserved[3];
1355
1356 typedef struct CvContour
1357 {
1358     CV_CONTOUR_FIELDS()
1359 }
1360 CvContour;
1361
1362 typedef CvContour CvPoint2DSeq;
1363
1364 /****************************************************************************************\
1365 *                                    Sequence types                                      *
1366 \****************************************************************************************/
1367
1368 #define CV_SEQ_MAGIC_VAL             0x42990000
1369
1370 #define CV_IS_SEQ(seq) \
1371     ((seq) != NULL && (((CvSeq*)(seq))->flags & CV_MAGIC_MASK) == CV_SEQ_MAGIC_VAL)
1372
1373 #define CV_SET_MAGIC_VAL             0x42980000
1374 #define CV_IS_SET(set) \
1375     ((set) != NULL && (((CvSeq*)(set))->flags & CV_MAGIC_MASK) == CV_SET_MAGIC_VAL)
1376
1377 #define CV_SEQ_ELTYPE_BITS           9
1378 #define CV_SEQ_ELTYPE_MASK           ((1 << CV_SEQ_ELTYPE_BITS) - 1)
1379
1380 #define CV_SEQ_ELTYPE_POINT          CV_32SC2  /* (x,y) */
1381 #define CV_SEQ_ELTYPE_CODE           CV_8UC1   /* freeman code: 0..7 */
1382 #define CV_SEQ_ELTYPE_GENERIC        0
1383 #define CV_SEQ_ELTYPE_PTR            CV_USRTYPE1
1384 #define CV_SEQ_ELTYPE_PPOINT         CV_SEQ_ELTYPE_PTR  /* &(x,y) */
1385 #define CV_SEQ_ELTYPE_INDEX          CV_32SC1  /* #(x,y) */
1386 #define CV_SEQ_ELTYPE_GRAPH_EDGE     0  /* &next_o, &next_d, &vtx_o, &vtx_d */
1387 #define CV_SEQ_ELTYPE_GRAPH_VERTEX   0  /* first_edge, &(x,y) */
1388 #define CV_SEQ_ELTYPE_TRIAN_ATR      0  /* vertex of the binary tree   */
1389 #define CV_SEQ_ELTYPE_CONNECTED_COMP 0  /* connected component  */
1390 #define CV_SEQ_ELTYPE_POINT3D        CV_32FC3  /* (x,y,z)  */
1391
1392 #define CV_SEQ_KIND_BITS        3
1393 #define CV_SEQ_KIND_MASK        (((1 << CV_SEQ_KIND_BITS) - 1)<<CV_SEQ_ELTYPE_BITS)
1394
1395 /* types of sequences */
1396 #define CV_SEQ_KIND_GENERIC     (0 << CV_SEQ_ELTYPE_BITS)
1397 #define CV_SEQ_KIND_CURVE       (1 << CV_SEQ_ELTYPE_BITS)
1398 #define CV_SEQ_KIND_BIN_TREE    (2 << CV_SEQ_ELTYPE_BITS)
1399
1400 /* types of sparse sequences (sets) */
1401 #define CV_SEQ_KIND_GRAPH       (3 << CV_SEQ_ELTYPE_BITS)
1402 #define CV_SEQ_KIND_SUBDIV2D    (4 << CV_SEQ_ELTYPE_BITS)
1403
1404 #define CV_SEQ_FLAG_SHIFT       (CV_SEQ_KIND_BITS + CV_SEQ_ELTYPE_BITS)
1405
1406 /* flags for curves */
1407 #define CV_SEQ_FLAG_CLOSED     (1 << CV_SEQ_FLAG_SHIFT)
1408 #define CV_SEQ_FLAG_SIMPLE     (2 << CV_SEQ_FLAG_SHIFT)
1409 #define CV_SEQ_FLAG_CONVEX     (4 << CV_SEQ_FLAG_SHIFT)
1410 #define CV_SEQ_FLAG_HOLE       (8 << CV_SEQ_FLAG_SHIFT)
1411
1412 /* flags for graphs */
1413 #define CV_GRAPH_FLAG_ORIENTED (1 << CV_SEQ_FLAG_SHIFT)
1414
1415 #define CV_GRAPH               CV_SEQ_KIND_GRAPH
1416 #define CV_ORIENTED_GRAPH      (CV_SEQ_KIND_GRAPH|CV_GRAPH_FLAG_ORIENTED)
1417
1418 /* point sets */
1419 #define CV_SEQ_POINT_SET       (CV_SEQ_KIND_GENERIC| CV_SEQ_ELTYPE_POINT)
1420 #define CV_SEQ_POINT3D_SET     (CV_SEQ_KIND_GENERIC| CV_SEQ_ELTYPE_POINT3D)
1421 #define CV_SEQ_POLYLINE        (CV_SEQ_KIND_CURVE  | CV_SEQ_ELTYPE_POINT)
1422 #define CV_SEQ_POLYGON         (CV_SEQ_FLAG_CLOSED | CV_SEQ_POLYLINE )
1423 #define CV_SEQ_CONTOUR         CV_SEQ_POLYGON
1424 #define CV_SEQ_SIMPLE_POLYGON  (CV_SEQ_FLAG_SIMPLE | CV_SEQ_POLYGON  )
1425
1426 /* chain-coded curves */
1427 #define CV_SEQ_CHAIN           (CV_SEQ_KIND_CURVE  | CV_SEQ_ELTYPE_CODE)
1428 #define CV_SEQ_CHAIN_CONTOUR   (CV_SEQ_FLAG_CLOSED | CV_SEQ_CHAIN)
1429
1430 /* binary tree for the contour */
1431 #define CV_SEQ_POLYGON_TREE    (CV_SEQ_KIND_BIN_TREE  | CV_SEQ_ELTYPE_TRIAN_ATR)
1432
1433 /* sequence of the connected components */
1434 #define CV_SEQ_CONNECTED_COMP  (CV_SEQ_KIND_GENERIC  | CV_SEQ_ELTYPE_CONNECTED_COMP)
1435
1436 /* sequence of the integer numbers */
1437 #define CV_SEQ_INDEX           (CV_SEQ_KIND_GENERIC  | CV_SEQ_ELTYPE_INDEX)
1438
1439 #define CV_SEQ_ELTYPE( seq )   ((seq)->flags & CV_SEQ_ELTYPE_MASK)
1440 #define CV_SEQ_KIND( seq )     ((seq)->flags & CV_SEQ_KIND_MASK )
1441
1442 /* flag checking */
1443 #define CV_IS_SEQ_INDEX( seq )      ((CV_SEQ_ELTYPE(seq) == CV_SEQ_ELTYPE_INDEX) && \
1444                                      (CV_SEQ_KIND(seq) == CV_SEQ_KIND_GENERIC))
1445
1446 #define CV_IS_SEQ_CURVE( seq )      (CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE)
1447 #define CV_IS_SEQ_CLOSED( seq )     (((seq)->flags & CV_SEQ_FLAG_CLOSED) != 0)
1448 #define CV_IS_SEQ_CONVEX( seq )     (((seq)->flags & CV_SEQ_FLAG_CONVEX) != 0)
1449 #define CV_IS_SEQ_HOLE( seq )       (((seq)->flags & CV_SEQ_FLAG_HOLE) != 0)
1450 #define CV_IS_SEQ_SIMPLE( seq )     ((((seq)->flags & CV_SEQ_FLAG_SIMPLE) != 0) || \
1451                                     CV_IS_SEQ_CONVEX(seq))
1452
1453 /* type checking macros */
1454 #define CV_IS_SEQ_POINT_SET( seq ) \
1455     ((CV_SEQ_ELTYPE(seq) == CV_32SC2 || CV_SEQ_ELTYPE(seq) == CV_32FC2))
1456
1457 #define CV_IS_SEQ_POINT_SUBSET( seq ) \
1458     (CV_IS_SEQ_INDEX( seq ) || CV_SEQ_ELTYPE(seq) == CV_SEQ_ELTYPE_PPOINT)
1459
1460 #define CV_IS_SEQ_POLYLINE( seq )   \
1461     (CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE && CV_IS_SEQ_POINT_SET(seq))
1462
1463 #define CV_IS_SEQ_POLYGON( seq )   \
1464     (CV_IS_SEQ_POLYLINE(seq) && CV_IS_SEQ_CLOSED(seq))
1465
1466 #define CV_IS_SEQ_CHAIN( seq )   \
1467     (CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE && (seq)->elem_size == 1)
1468
1469 #define CV_IS_SEQ_CONTOUR( seq )   \
1470     (CV_IS_SEQ_CLOSED(seq) && (CV_IS_SEQ_POLYLINE(seq) || CV_IS_SEQ_CHAIN(seq)))
1471
1472 #define CV_IS_SEQ_CHAIN_CONTOUR( seq ) \
1473     (CV_IS_SEQ_CHAIN( seq ) && CV_IS_SEQ_CLOSED( seq ))
1474
1475 #define CV_IS_SEQ_POLYGON_TREE( seq ) \
1476     (CV_SEQ_ELTYPE (seq) ==  CV_SEQ_ELTYPE_TRIAN_ATR &&    \
1477     CV_SEQ_KIND( seq ) ==  CV_SEQ_KIND_BIN_TREE )
1478
1479 #define CV_IS_GRAPH( seq )    \
1480     (CV_IS_SET(seq) && CV_SEQ_KIND((CvSet*)(seq)) == CV_SEQ_KIND_GRAPH)
1481
1482 #define CV_IS_GRAPH_ORIENTED( seq )   \
1483     (((seq)->flags & CV_GRAPH_FLAG_ORIENTED) != 0)
1484
1485 #define CV_IS_SUBDIV2D( seq )  \
1486     (CV_IS_SET(seq) && CV_SEQ_KIND((CvSet*)(seq)) == CV_SEQ_KIND_SUBDIV2D)
1487
1488 /****************************************************************************************/
1489 /*                            Sequence writer & reader                                  */
1490 /****************************************************************************************/
1491
1492 #define CV_SEQ_WRITER_FIELDS()                                     \
1493     int          header_size;                                      \
1494     CvSeq*       seq;        /* the sequence written */            \
1495     CvSeqBlock*  block;      /* current block */                   \
1496     schar*       ptr;        /* pointer to free space */           \
1497     schar*       block_min;  /* pointer to the beginning of block*/\
1498     schar*       block_max;  /* pointer to the end of block */
1499
1500 typedef struct CvSeqWriter
1501 {
1502     CV_SEQ_WRITER_FIELDS()
1503 }
1504 CvSeqWriter;
1505
1506
1507 #define CV_SEQ_READER_FIELDS()                                      \
1508     int          header_size;                                       \
1509     CvSeq*       seq;        /* sequence, beign read */             \
1510     CvSeqBlock*  block;      /* current block */                    \
1511     schar*       ptr;        /* pointer to element be read next */  \
1512     schar*       block_min;  /* pointer to the beginning of block */\
1513     schar*       block_max;  /* pointer to the end of block */      \
1514     int          delta_index;/* = seq->first->start_index   */      \
1515     schar*       prev_elem;  /* pointer to previous element */
1516
1517
1518 typedef struct CvSeqReader
1519 {
1520     CV_SEQ_READER_FIELDS()
1521 }
1522 CvSeqReader;
1523
1524 /****************************************************************************************/
1525 /*                                Operations on sequences                               */
1526 /****************************************************************************************/
1527
1528 #define  CV_SEQ_ELEM( seq, elem_type, index )                    \
1529 /* assert gives some guarantee that <seq> parameter is valid */  \
1530 (   assert(sizeof((seq)->first[0]) == sizeof(CvSeqBlock) &&      \
1531     (seq)->elem_size == sizeof(elem_type)),                      \
1532     (elem_type*)((seq)->first && (unsigned)index <               \
1533     (unsigned)((seq)->first->count) ?                            \
1534     (seq)->first->data + (index) * sizeof(elem_type) :           \
1535     cvGetSeqElem( (CvSeq*)(seq), (index) )))
1536 #define CV_GET_SEQ_ELEM( elem_type, seq, index ) CV_SEQ_ELEM( (seq), elem_type, (index) )
1537
1538 /* Add element to sequence: */
1539 #define CV_WRITE_SEQ_ELEM_VAR( elem_ptr, writer )     \
1540 {                                                     \
1541     if( (writer).ptr >= (writer).block_max )          \
1542     {                                                 \
1543         cvCreateSeqBlock( &writer);                   \
1544     }                                                 \
1545     memcpy((writer).ptr, elem_ptr, (writer).seq->elem_size);\
1546     (writer).ptr += (writer).seq->elem_size;          \
1547 }
1548
1549 #define CV_WRITE_SEQ_ELEM( elem, writer )             \
1550 {                                                     \
1551     assert( (writer).seq->elem_size == sizeof(elem)); \
1552     if( (writer).ptr >= (writer).block_max )          \
1553     {                                                 \
1554         cvCreateSeqBlock( &writer);                   \
1555     }                                                 \
1556     assert( (writer).ptr <= (writer).block_max - sizeof(elem));\
1557     memcpy((writer).ptr, &(elem), sizeof(elem));      \
1558     (writer).ptr += sizeof(elem);                     \
1559 }
1560
1561
1562 /* Move reader position forward: */
1563 #define CV_NEXT_SEQ_ELEM( elem_size, reader )                 \
1564 {                                                             \
1565     if( ((reader).ptr += (elem_size)) >= (reader).block_max ) \
1566     {                                                         \
1567         cvChangeSeqBlock( &(reader), 1 );                     \
1568     }                                                         \
1569 }
1570
1571
1572 /* Move reader position backward: */
1573 #define CV_PREV_SEQ_ELEM( elem_size, reader )                \
1574 {                                                            \
1575     if( ((reader).ptr -= (elem_size)) < (reader).block_min ) \
1576     {                                                        \
1577         cvChangeSeqBlock( &(reader), -1 );                   \
1578     }                                                        \
1579 }
1580
1581 /* Read element and move read position forward: */
1582 #define CV_READ_SEQ_ELEM( elem, reader )                       \
1583 {                                                              \
1584     assert( (reader).seq->elem_size == sizeof(elem));          \
1585     memcpy( &(elem), (reader).ptr, sizeof((elem)));            \
1586     CV_NEXT_SEQ_ELEM( sizeof(elem), reader )                   \
1587 }
1588
1589 /* Read element and move read position backward: */
1590 #define CV_REV_READ_SEQ_ELEM( elem, reader )                     \
1591 {                                                                \
1592     assert( (reader).seq->elem_size == sizeof(elem));            \
1593     memcpy(&(elem), (reader).ptr, sizeof((elem)));               \
1594     CV_PREV_SEQ_ELEM( sizeof(elem), reader )                     \
1595 }
1596
1597
1598 #define CV_READ_CHAIN_POINT( _pt, reader )                              \
1599 {                                                                       \
1600     (_pt) = (reader).pt;                                                \
1601     if( (reader).ptr )                                                  \
1602     {                                                                   \
1603         CV_READ_SEQ_ELEM( (reader).code, (reader));                     \
1604         assert( ((reader).code & ~7) == 0 );                            \
1605         (reader).pt.x += (reader).deltas[(int)(reader).code][0];        \
1606         (reader).pt.y += (reader).deltas[(int)(reader).code][1];        \
1607     }                                                                   \
1608 }
1609
1610 #define CV_CURRENT_POINT( reader )  (*((CvPoint*)((reader).ptr)))
1611 #define CV_PREV_POINT( reader )     (*((CvPoint*)((reader).prev_elem)))
1612
1613 #define CV_READ_EDGE( pt1, pt2, reader )               \
1614 {                                                      \
1615     assert( sizeof(pt1) == sizeof(CvPoint) &&          \
1616             sizeof(pt2) == sizeof(CvPoint) &&          \
1617             reader.seq->elem_size == sizeof(CvPoint)); \
1618     (pt1) = CV_PREV_POINT( reader );                   \
1619     (pt2) = CV_CURRENT_POINT( reader );                \
1620     (reader).prev_elem = (reader).ptr;                 \
1621     CV_NEXT_SEQ_ELEM( sizeof(CvPoint), (reader));      \
1622 }
1623
1624 /************ Graph macros ************/
1625
1626 /* Return next graph edge for given vertex: */
1627 #define  CV_NEXT_GRAPH_EDGE( edge, vertex )                              \
1628      (assert((edge)->vtx[0] == (vertex) || (edge)->vtx[1] == (vertex)),  \
1629       (edge)->next[(edge)->vtx[1] == (vertex)])
1630
1631
1632
1633 /****************************************************************************************\
1634 *             Data structures for persistence (a.k.a serialization) functionality        *
1635 \****************************************************************************************/
1636
1637 /* "black box" file storage */
1638 typedef struct CvFileStorage CvFileStorage;
1639
1640 /* Storage flags: */
1641 #define CV_STORAGE_READ          0
1642 #define CV_STORAGE_WRITE         1
1643 #define CV_STORAGE_WRITE_TEXT    CV_STORAGE_WRITE
1644 #define CV_STORAGE_WRITE_BINARY  CV_STORAGE_WRITE
1645 #define CV_STORAGE_APPEND        2
1646
1647 /* List of attributes: */
1648 typedef struct CvAttrList
1649 {
1650     const char** attr;         /* NULL-terminated array of (attribute_name,attribute_value) pairs. */
1651     struct CvAttrList* next;   /* Pointer to next chunk of the attributes list.                    */
1652 }
1653 CvAttrList;
1654
1655 CV_INLINE CvAttrList cvAttrList( const char** attr CV_DEFAULT(NULL),
1656                                  CvAttrList* next CV_DEFAULT(NULL) )
1657 {
1658     CvAttrList l;
1659     l.attr = attr;
1660     l.next = next;
1661
1662     return l;
1663 }
1664
1665 struct CvTypeInfo;
1666
1667 #define CV_NODE_NONE        0
1668 #define CV_NODE_INT         1
1669 #define CV_NODE_INTEGER     CV_NODE_INT
1670 #define CV_NODE_REAL        2
1671 #define CV_NODE_FLOAT       CV_NODE_REAL
1672 #define CV_NODE_STR         3
1673 #define CV_NODE_STRING      CV_NODE_STR
1674 #define CV_NODE_REF         4 /* not used */
1675 #define CV_NODE_SEQ         5
1676 #define CV_NODE_MAP         6
1677 #define CV_NODE_TYPE_MASK   7
1678
1679 #define CV_NODE_TYPE(flags)  ((flags) & CV_NODE_TYPE_MASK)
1680
1681 /* file node flags */
1682 #define CV_NODE_FLOW        8 /* Used only for writing structures in YAML format. */
1683 #define CV_NODE_USER        16
1684 #define CV_NODE_EMPTY       32
1685 #define CV_NODE_NAMED       64
1686
1687 #define CV_NODE_IS_INT(flags)        (CV_NODE_TYPE(flags) == CV_NODE_INT)
1688 #define CV_NODE_IS_REAL(flags)       (CV_NODE_TYPE(flags) == CV_NODE_REAL)
1689 #define CV_NODE_IS_STRING(flags)     (CV_NODE_TYPE(flags) == CV_NODE_STRING)
1690 #define CV_NODE_IS_SEQ(flags)        (CV_NODE_TYPE(flags) == CV_NODE_SEQ)
1691 #define CV_NODE_IS_MAP(flags)        (CV_NODE_TYPE(flags) == CV_NODE_MAP)
1692 #define CV_NODE_IS_COLLECTION(flags) (CV_NODE_TYPE(flags) >= CV_NODE_SEQ)
1693 #define CV_NODE_IS_FLOW(flags)       (((flags) & CV_NODE_FLOW) != 0)
1694 #define CV_NODE_IS_EMPTY(flags)      (((flags) & CV_NODE_EMPTY) != 0)
1695 #define CV_NODE_IS_USER(flags)       (((flags) & CV_NODE_USER) != 0)
1696 #define CV_NODE_HAS_NAME(flags)      (((flags) & CV_NODE_NAMED) != 0)
1697
1698 #define CV_NODE_SEQ_SIMPLE 256
1699 #define CV_NODE_SEQ_IS_SIMPLE(seq) (((seq)->flags & CV_NODE_SEQ_SIMPLE) != 0)
1700
1701 typedef struct CvString
1702 {
1703     int len;
1704     char* ptr;
1705 }
1706 CvString;
1707
1708 /* All the keys (names) of elements in the readed file storage
1709    are stored in the hash to speed up the lookup operations: */
1710 typedef struct CvStringHashNode
1711 {
1712     unsigned hashval;
1713     CvString str;
1714     struct CvStringHashNode* next;
1715 }
1716 CvStringHashNode;
1717
1718 typedef struct CvGenericHash CvFileNodeHash;
1719
1720 /* Basic element of the file storage - scalar or collection: */
1721 typedef struct CvFileNode
1722 {
1723     int tag;
1724     struct CvTypeInfo* info; /* type information
1725             (only for user-defined object, for others it is 0) */
1726     union
1727     {
1728         double f; /* scalar floating-point number */
1729         int i;    /* scalar integer number */
1730         CvString str; /* text string */
1731         CvSeq* seq; /* sequence (ordered collection of file nodes) */
1732         CvFileNodeHash* map; /* map (collection of named file nodes) */
1733     } data;
1734 }
1735 CvFileNode;
1736
1737 #ifdef __cplusplus
1738 extern "C" {
1739 #endif
1740 typedef int (CV_CDECL *CvIsInstanceFunc)( const void* struct_ptr );
1741 typedef void (CV_CDECL *CvReleaseFunc)( void** struct_dblptr );
1742 typedef void* (CV_CDECL *CvReadFunc)( CvFileStorage* storage, CvFileNode* node );
1743 typedef void (CV_CDECL *CvWriteFunc)( CvFileStorage* storage, const char* name,
1744                                       const void* struct_ptr, CvAttrList attributes );
1745 typedef void* (CV_CDECL *CvCloneFunc)( const void* struct_ptr );
1746 #ifdef __cplusplus
1747 }
1748 #endif
1749
1750 typedef struct CvTypeInfo
1751 {
1752     int flags;
1753     int header_size;
1754     struct CvTypeInfo* prev;
1755     struct CvTypeInfo* next;
1756     const char* type_name;
1757     CvIsInstanceFunc is_instance;
1758     CvReleaseFunc release;
1759     CvReadFunc read;
1760     CvWriteFunc write;
1761     CvCloneFunc clone;
1762 }
1763 CvTypeInfo;
1764
1765
1766 /**** System data types ******/
1767
1768 typedef struct CvPluginFuncInfo
1769 {
1770     void** func_addr;
1771     void* default_func_addr;
1772     const char* func_names;
1773     int search_modules;
1774     int loaded_from;
1775 }
1776 CvPluginFuncInfo;
1777
1778 typedef struct CvModuleInfo
1779 {
1780     struct CvModuleInfo* next;
1781     const char* name;
1782     const char* version;
1783     CvPluginFuncInfo* func_tab;
1784 }
1785 CvModuleInfo;
1786
1787 #endif /*_CXCORE_TYPES_H_*/
1788
1789 /* End of file. */