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