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