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