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