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