1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #ifndef _CXCORE_TYPES_H_
44 #define _CXCORE_TYPES_H_
46 #if !defined _CRT_SECURE_NO_DEPRECATE && _MSC_VER > 1300
47 #define _CRT_SECURE_NO_DEPRECATE /* to avoid multiple Visual Studio 2005 warnings */
51 #ifndef _BIND_TO_CURRENT_CRT_VERSION
52 #define _BIND_TO_CURRENT_CRT_VERSION 1
54 #ifndef _BIND_TO_CURRENT_VCLIBS_VERSION
55 #define _BIND_TO_CURRENT_VCLIBS_VERSION 1
65 #if !defined _MSC_VER && !defined __BORLANDC__
79 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && \
80 (_MSC_VER >= 1400 || defined CV_ICC)) \
81 || (defined __SSE2__ && defined __GNUC__ && __GNUC__ >= 4)
82 #include <emmintrin.h>
84 #if (_MSC_VER >= 1500 || defined CV_ICC) || \
85 (defined __SSE3__ && defined __GNUC__ && __GNUC__ >= 4)
86 #include <pmmintrin.h>
88 #define _cv_loadu_si128 _mm_lddqu_si128
91 #define _cv_loadu_si128 _mm_loadu_si128
97 #if ((defined __SSE__ || defined __MMX__) && defined __GNUC__ && __GNUC__ >= 3)
101 #if defined __BORLANDC__
102 #include <fastmath.h>
109 #if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
115 #elif defined __IPL_H__
118 #endif // SKIP_INCLUDES
120 #if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
121 #define CV_CDECL __cdecl
122 #define CV_STDCALL __stdcall
130 #define CV_EXTERN_C extern "C"
131 #define CV_DEFAULT(val) = val
134 #define CV_DEFAULT(val)
138 #ifndef CV_EXTERN_C_FUNCPTR
140 #define CV_EXTERN_C_FUNCPTR(x) extern "C" { typedef x; }
142 #define CV_EXTERN_C_FUNCPTR(x) typedef x
147 #if defined __cplusplus
148 #define CV_INLINE inline
149 #elif (defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64 || defined WINCE) && !defined __GNUC__
150 #define CV_INLINE __inline
152 #define CV_INLINE static
154 #endif /* CV_INLINE */
156 #if (defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64 || defined WINCE) && defined CVAPI_EXPORTS
157 #define CV_EXPORTS __declspec(dllexport)
163 #define CVAPI(rettype) CV_EXTERN_C CV_EXPORTS rettype CV_CDECL
166 #if defined _MSC_VER || defined __BORLANDC__
167 typedef __int64 int64;
168 typedef unsigned __int64 uint64;
170 typedef int64_t int64;
171 typedef uint64_t uint64;
175 typedef unsigned char uchar;
176 typedef unsigned short ushort;
179 typedef signed char schar;
181 /* CvArr* is used to pass arbitrary
182 * array-like data structures
183 * into functions where the particular
184 * array type is recognized at runtime:
188 typedef union Cv32suf
196 typedef union Cv64suf
204 /****************************************************************************************\
205 * Common macros and inline functions *
206 \****************************************************************************************/
208 #define CV_PI 3.1415926535897932384626433832795
209 #define CV_LOG2 0.69314718055994530941723212145818
211 #define CV_SWAP(a,b,t) ((t) = (a), (a) = (b), (b) = (t))
214 #define MIN(a,b) ((a) > (b) ? (b) : (a))
218 #define MAX(a,b) ((a) < (b) ? (b) : (a))
221 /* min & max without jumps */
222 #define CV_IMIN(a, b) ((a) ^ (((a)^(b)) & (((a) < (b)) - 1)))
224 #define CV_IMAX(a, b) ((a) ^ (((a)^(b)) & (((a) > (b)) - 1)))
226 /* absolute value without jumps */
228 #define CV_IABS(a) (((a) ^ ((a) < 0 ? -1 : 0)) - ((a) < 0 ? -1 : 0))
230 #define CV_IABS(a) abs(a)
232 #define CV_CMP(a,b) (((a) > (b)) - ((a) < (b)))
233 #define CV_SIGN(a) CV_CMP((a),0)
235 CV_INLINE int cvRound( double value )
237 #if CV_SSE2 && !defined __APPLE__
238 __m128d t = _mm_set_sd( value );
239 return _mm_cvtsd_si32(t);
240 #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
248 #elif defined HAVE_LRINT || defined CV_ICC || defined __GNUC__
249 return (int)lrint(value);
251 // while this is not IEEE754-compliant rounding, it's usually a good enough approximation
252 return (int)(value + 0.5);
257 CV_INLINE int cvFloor( double value )
261 return i - (i > value);
263 __m128d t = _mm_set_sd( value );
264 int i = _mm_cvtsd_si32(t);
265 return i - _mm_movemask_pd(_mm_cmplt_sd(t, _mm_cvtsi32_sd(t,i)));
267 int i = cvRound(value);
269 diff.f = (float)(value - i);
270 return i - (diff.i < 0);
275 CV_INLINE int cvCeil( double value )
279 return i + (i < value);
281 __m128d t = _mm_set_sd( value );
282 int i = _mm_cvtsd_si32(t);
283 return i + _mm_movemask_pd(_mm_cmplt_sd(_mm_cvtsi32_sd(t,i), t));
285 int i = cvRound(value);
287 diff.f = (float)(i - value);
288 return i + (diff.i < 0);
292 #define cvInvSqrt(value) ((float)(1./sqrt(value)))
293 #define cvSqrt(value) ((float)sqrt(value))
295 CV_INLINE int cvIsNaN( double value )
297 #if 1/*defined _MSC_VER || defined __BORLANDC__
298 return _isnan(value);
299 #elif defined __GNUC__
304 return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) +
305 ((unsigned)ieee754.u != 0) > 0x7ff00000;
310 CV_INLINE int cvIsInf( double value )
312 #if 1/*defined _MSC_VER || defined __BORLANDC__
313 return !_finite(value);
314 #elif defined __GNUC__
319 return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
320 (unsigned)ieee754.u == 0;
325 /*************** Random number generation *******************/
327 typedef uint64 CvRNG;
329 CV_INLINE CvRNG cvRNG( int64 seed CV_DEFAULT(-1))
331 CvRNG rng = seed ? (uint64)seed : (uint64)(int64)-1;
335 /* Return random 32-bit unsigned integer: */
336 CV_INLINE unsigned cvRandInt( CvRNG* rng )
339 temp = (uint64)(unsigned)temp*4164903690U + (temp >> 32);
341 return (unsigned)temp;
344 /* Returns random floating-point number between 0 and 1: */
345 CV_INLINE double cvRandReal( CvRNG* rng )
347 return cvRandInt(rng)*2.3283064365386962890625e-10 /* 2^-32 */;
350 /****************************************************************************************\
351 * Image type (IplImage) *
352 \****************************************************************************************/
357 * The following definitions (until #endif)
358 * is an extract from IPL headers.
359 * Copyright (c) 1995 Intel Corporation.
361 #define IPL_DEPTH_SIGN 0x80000000
363 #define IPL_DEPTH_1U 1
364 #define IPL_DEPTH_8U 8
365 #define IPL_DEPTH_16U 16
366 #define IPL_DEPTH_32F 32
368 #define IPL_DEPTH_8S (IPL_DEPTH_SIGN| 8)
369 #define IPL_DEPTH_16S (IPL_DEPTH_SIGN|16)
370 #define IPL_DEPTH_32S (IPL_DEPTH_SIGN|32)
372 #define IPL_DATA_ORDER_PIXEL 0
373 #define IPL_DATA_ORDER_PLANE 1
375 #define IPL_ORIGIN_TL 0
376 #define IPL_ORIGIN_BL 1
378 #define IPL_ALIGN_4BYTES 4
379 #define IPL_ALIGN_8BYTES 8
380 #define IPL_ALIGN_16BYTES 16
381 #define IPL_ALIGN_32BYTES 32
383 #define IPL_ALIGN_DWORD IPL_ALIGN_4BYTES
384 #define IPL_ALIGN_QWORD IPL_ALIGN_8BYTES
386 #define IPL_BORDER_CONSTANT 0
387 #define IPL_BORDER_REPLICATE 1
388 #define IPL_BORDER_REFLECT 2
389 #define IPL_BORDER_WRAP 3
391 typedef struct _IplImage
393 int nSize; /* sizeof(IplImage) */
394 int ID; /* version (=0)*/
395 int nChannels; /* Most of OpenCV functions support 1,2,3 or 4 channels */
396 int alphaChannel; /* Ignored by OpenCV */
397 int depth; /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
398 IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported. */
399 char colorModel[4]; /* Ignored by OpenCV */
400 char channelSeq[4]; /* ditto */
401 int dataOrder; /* 0 - interleaved color channels, 1 - separate color channels.
402 cvCreateImage can only create interleaved images */
403 int origin; /* 0 - top-left origin,
404 1 - bottom-left origin (Windows bitmaps style). */
405 int align; /* Alignment of image rows (4 or 8).
406 OpenCV ignores it and uses widthStep instead. */
407 int width; /* Image width in pixels. */
408 int height; /* Image height in pixels. */
409 struct _IplROI *roi; /* Image ROI. If NULL, the whole image is selected. */
410 struct _IplImage *maskROI; /* Must be NULL. */
411 void *imageId; /* " " */
412 struct _IplTileInfo *tileInfo; /* " " */
413 int imageSize; /* Image data size in bytes
414 (==image->height*image->widthStep
415 in case of interleaved data)*/
416 char *imageData; /* Pointer to aligned image data. */
417 int widthStep; /* Size of aligned image row in bytes. */
418 int BorderMode[4]; /* Ignored by OpenCV. */
419 int BorderConst[4]; /* Ditto. */
420 char *imageDataOrigin; /* Pointer to very origin of image data
421 (not necessarily aligned) -
422 needed for correct deallocation */
426 typedef struct _IplTileInfo IplTileInfo;
428 typedef struct _IplROI
430 int coi; /* 0 - no COI (all channels are selected), 1 - 0th channel is selected ...*/
438 typedef struct _IplConvKernel
449 typedef struct _IplConvKernelFP
459 #define IPL_IMAGE_HEADER 1
460 #define IPL_IMAGE_DATA 2
461 #define IPL_IMAGE_ROI 4
465 /* extra border mode */
466 #define IPL_BORDER_REFLECT_101 4
468 #define IPL_IMAGE_MAGIC_VAL ((int)sizeof(IplImage))
469 #define CV_TYPE_NAME_IMAGE "opencv-image"
471 #define CV_IS_IMAGE_HDR(img) \
472 ((img) != NULL && ((const IplImage*)(img))->nSize == sizeof(IplImage))
474 #define CV_IS_IMAGE(img) \
475 (CV_IS_IMAGE_HDR(img) && ((IplImage*)img)->imageData != NULL)
477 /* for storing double-precision
478 floating point data in IplImage's */
479 #define IPL_DEPTH_64F 64
481 /* get reference to pixel at (col,row),
482 for multi-channel images (col) should be multiplied by number of channels */
483 #define CV_IMAGE_ELEM( image, elemtype, row, col ) \
484 (((elemtype*)((image)->imageData + (image)->widthStep*(row)))[(col)])
486 /****************************************************************************************\
487 * Matrix type (CvMat) *
488 \****************************************************************************************/
491 #define CV_CN_SHIFT 3
492 #define CV_DEPTH_MAX (1 << CV_CN_SHIFT)
501 #define CV_USRTYPE1 7
503 #define CV_MAT_DEPTH_MASK (CV_DEPTH_MAX - 1)
504 #define CV_MAT_DEPTH(flags) ((flags) & CV_MAT_DEPTH_MASK)
506 #define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT))
507 #define CV_MAKE_TYPE CV_MAKETYPE
509 #define CV_8UC1 CV_MAKETYPE(CV_8U,1)
510 #define CV_8UC2 CV_MAKETYPE(CV_8U,2)
511 #define CV_8UC3 CV_MAKETYPE(CV_8U,3)
512 #define CV_8UC4 CV_MAKETYPE(CV_8U,4)
513 #define CV_8UC(n) CV_MAKETYPE(CV_8U,(n))
515 #define CV_8SC1 CV_MAKETYPE(CV_8S,1)
516 #define CV_8SC2 CV_MAKETYPE(CV_8S,2)
517 #define CV_8SC3 CV_MAKETYPE(CV_8S,3)
518 #define CV_8SC4 CV_MAKETYPE(CV_8S,4)
519 #define CV_8SC(n) CV_MAKETYPE(CV_8S,(n))
521 #define CV_16UC1 CV_MAKETYPE(CV_16U,1)
522 #define CV_16UC2 CV_MAKETYPE(CV_16U,2)
523 #define CV_16UC3 CV_MAKETYPE(CV_16U,3)
524 #define CV_16UC4 CV_MAKETYPE(CV_16U,4)
525 #define CV_16UC(n) CV_MAKETYPE(CV_16U,(n))
527 #define CV_16SC1 CV_MAKETYPE(CV_16S,1)
528 #define CV_16SC2 CV_MAKETYPE(CV_16S,2)
529 #define CV_16SC3 CV_MAKETYPE(CV_16S,3)
530 #define CV_16SC4 CV_MAKETYPE(CV_16S,4)
531 #define CV_16SC(n) CV_MAKETYPE(CV_16S,(n))
533 #define CV_32SC1 CV_MAKETYPE(CV_32S,1)
534 #define CV_32SC2 CV_MAKETYPE(CV_32S,2)
535 #define CV_32SC3 CV_MAKETYPE(CV_32S,3)
536 #define CV_32SC4 CV_MAKETYPE(CV_32S,4)
537 #define CV_32SC(n) CV_MAKETYPE(CV_32S,(n))
539 #define CV_32FC1 CV_MAKETYPE(CV_32F,1)
540 #define CV_32FC2 CV_MAKETYPE(CV_32F,2)
541 #define CV_32FC3 CV_MAKETYPE(CV_32F,3)
542 #define CV_32FC4 CV_MAKETYPE(CV_32F,4)
543 #define CV_32FC(n) CV_MAKETYPE(CV_32F,(n))
545 #define CV_64FC1 CV_MAKETYPE(CV_64F,1)
546 #define CV_64FC2 CV_MAKETYPE(CV_64F,2)
547 #define CV_64FC3 CV_MAKETYPE(CV_64F,3)
548 #define CV_64FC4 CV_MAKETYPE(CV_64F,4)
549 #define CV_64FC(n) CV_MAKETYPE(CV_64F,(n))
551 #define CV_AUTO_STEP 0x7fffffff
552 #define CV_WHOLE_ARR cvSlice( 0, 0x3fffffff )
554 #define CV_MAT_CN_MASK ((CV_CN_MAX - 1) << CV_CN_SHIFT)
555 #define CV_MAT_CN(flags) ((((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1)
556 #define CV_MAT_TYPE_MASK (CV_DEPTH_MAX*CV_CN_MAX - 1)
557 #define CV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
558 #define CV_MAT_CONT_FLAG_SHIFT 14
559 #define CV_MAT_CONT_FLAG (1 << CV_MAT_CONT_FLAG_SHIFT)
560 #define CV_IS_MAT_CONT(flags) ((flags) & CV_MAT_CONT_FLAG)
561 #define CV_IS_CONT_MAT CV_IS_MAT_CONT
562 #define CV_MAT_TEMP_FLAG_SHIFT 15
563 #define CV_MAT_TEMP_FLAG (1 << CV_MAT_TEMP_FLAG_SHIFT)
564 #define CV_IS_TEMP_MAT(flags) ((flags) & CV_MAT_TEMP_FLAG)
566 #define CV_MAGIC_MASK 0xFFFF0000
567 #define CV_MAT_MAGIC_VAL 0x42420000
568 #define CV_TYPE_NAME_MAT "opencv-matrix"
575 /* for internal use only */
609 #define CV_IS_MAT_HDR(mat) \
611 (((const CvMat*)(mat))->type & CV_MAGIC_MASK) == CV_MAT_MAGIC_VAL && \
612 ((const CvMat*)(mat))->cols > 0 && ((const CvMat*)(mat))->rows > 0)
614 #define CV_IS_MAT(mat) \
615 (CV_IS_MAT_HDR(mat) && ((const CvMat*)(mat))->data.ptr != NULL)
617 #define CV_IS_MASK_ARR(mat) \
618 (((mat)->type & (CV_MAT_TYPE_MASK & ~CV_8SC1)) == 0)
620 #define CV_ARE_TYPES_EQ(mat1, mat2) \
621 ((((mat1)->type ^ (mat2)->type) & CV_MAT_TYPE_MASK) == 0)
623 #define CV_ARE_CNS_EQ(mat1, mat2) \
624 ((((mat1)->type ^ (mat2)->type) & CV_MAT_CN_MASK) == 0)
626 #define CV_ARE_DEPTHS_EQ(mat1, mat2) \
627 ((((mat1)->type ^ (mat2)->type) & CV_MAT_DEPTH_MASK) == 0)
629 #define CV_ARE_SIZES_EQ(mat1, mat2) \
630 ((mat1)->rows == (mat2)->rows && (mat1)->cols == (mat2)->cols)
632 #define CV_IS_MAT_CONST(mat) \
633 (((mat)->rows|(mat)->cols) == 1)
635 /* Size of each channel item,
636 0x124489 = 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */
637 #define CV_ELEM_SIZE1(type) \
638 ((((sizeof(size_t)<<28)|0x8442211) >> CV_MAT_DEPTH(type)*4) & 15)
640 /* 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */
641 #define CV_ELEM_SIZE(type) \
642 (CV_MAT_CN(type) << ((((sizeof(size_t)/4+1)*16384|0x3a50) >> CV_MAT_DEPTH(type)*2) & 3))
644 #define IPL2CV_DEPTH(depth) \
645 (((CV_8U)+(CV_16U<<4)+(CV_32F<<8)+(CV_64F<<16)+(CV_8S<<20)+ \
646 (CV_16S<<24)+(CV_32S<<28)) >> (((depth & 0xF0) >> 2) + (depth<0 ? 20 : 0)))
648 /* Inline constructor. No data is allocated internally!!!
649 * (Use together with cvCreateData, or use cvCreateMat instead to
650 * get a matrix with allocated data):
652 CV_INLINE CvMat cvMat( int rows, int cols, int type, void* data CV_DEFAULT(NULL))
656 assert( (unsigned)CV_MAT_DEPTH(type) <= CV_64F );
657 type = CV_MAT_TYPE(type);
658 m.type = CV_MAT_MAGIC_VAL | CV_MAT_CONT_FLAG | type;
661 m.step = m.cols*CV_ELEM_SIZE(type);
662 m.data.ptr = (uchar*)data;
670 #define CV_MAT_ELEM_PTR_FAST( mat, row, col, pix_size ) \
671 (assert( (unsigned)(row) < (unsigned)(mat).rows && \
672 (unsigned)(col) < (unsigned)(mat).cols ), \
673 (mat).data.ptr + (size_t)(mat).step*(row) + (pix_size)*(col))
675 #define CV_MAT_ELEM_PTR( mat, row, col ) \
676 CV_MAT_ELEM_PTR_FAST( mat, row, col, CV_ELEM_SIZE((mat).type) )
678 #define CV_MAT_ELEM( mat, elemtype, row, col ) \
679 (*(elemtype*)CV_MAT_ELEM_PTR_FAST( mat, row, col, sizeof(elemtype)))
682 CV_INLINE double cvmGet( const CvMat* mat, int row, int col )
686 type = CV_MAT_TYPE(mat->type);
687 assert( (unsigned)row < (unsigned)mat->rows &&
688 (unsigned)col < (unsigned)mat->cols );
690 if( type == CV_32FC1 )
691 return ((float*)(mat->data.ptr + (size_t)mat->step*row))[col];
694 assert( type == CV_64FC1 );
695 return ((double*)(mat->data.ptr + (size_t)mat->step*row))[col];
700 CV_INLINE void cvmSet( CvMat* mat, int row, int col, double value )
703 type = CV_MAT_TYPE(mat->type);
704 assert( (unsigned)row < (unsigned)mat->rows &&
705 (unsigned)col < (unsigned)mat->cols );
707 if( type == CV_32FC1 )
708 ((float*)(mat->data.ptr + (size_t)mat->step*row))[col] = (float)value;
711 assert( type == CV_64FC1 );
712 ((double*)(mat->data.ptr + (size_t)mat->step*row))[col] = (double)value;
717 CV_INLINE int cvIplDepth( int type )
719 int depth = CV_MAT_DEPTH(type);
720 return CV_ELEM_SIZE1(depth)*8 | (depth == CV_8S || depth == CV_16S ||
721 depth == CV_32S ? IPL_DEPTH_SIGN : 0);
725 /****************************************************************************************\
726 * Multi-dimensional dense array (CvMatND) *
727 \****************************************************************************************/
729 #define CV_MATND_MAGIC_VAL 0x42430000
730 #define CV_TYPE_NAME_MATND "opencv-nd-matrix"
732 #define CV_MAX_DIM 32
733 #define CV_MAX_DIM_HEAP (1 << 16)
735 typedef struct CvMatND
761 #define CV_IS_MATND_HDR(mat) \
762 ((mat) != NULL && (((const CvMatND*)(mat))->type & CV_MAGIC_MASK) == CV_MATND_MAGIC_VAL)
764 #define CV_IS_MATND(mat) \
765 (CV_IS_MATND_HDR(mat) && ((const CvMatND*)(mat))->data.ptr != NULL)
768 /****************************************************************************************\
769 * Multi-dimensional sparse array (CvSparseMat) *
770 \****************************************************************************************/
772 #define CV_SPARSE_MAT_MAGIC_VAL 0x42440000
773 #define CV_TYPE_NAME_SPARSE_MAT "opencv-sparse-matrix"
777 typedef struct CvSparseMat
789 int size[CV_MAX_DIM];
793 #define CV_IS_SPARSE_MAT_HDR(mat) \
795 (((const CvSparseMat*)(mat))->type & CV_MAGIC_MASK) == CV_SPARSE_MAT_MAGIC_VAL)
797 #define CV_IS_SPARSE_MAT(mat) \
798 CV_IS_SPARSE_MAT_HDR(mat)
800 /**************** iteration through a sparse array *****************/
802 typedef struct CvSparseNode
805 struct CvSparseNode* next;
809 typedef struct CvSparseMatIterator
817 #define CV_NODE_VAL(mat,node) ((void*)((uchar*)(node) + (mat)->valoffset))
818 #define CV_NODE_IDX(mat,node) ((int*)((uchar*)(node) + (mat)->idxoffset))
820 /****************************************************************************************\
822 \****************************************************************************************/
824 typedef int CvHistType;
826 #define CV_HIST_MAGIC_VAL 0x42450000
827 #define CV_HIST_UNIFORM_FLAG (1 << 10)
829 /* indicates whether bin ranges are set already or not */
830 #define CV_HIST_RANGES_FLAG (1 << 11)
832 #define CV_HIST_ARRAY 0
833 #define CV_HIST_SPARSE 1
834 #define CV_HIST_TREE CV_HIST_SPARSE
836 /* should be used as a parameter only,
837 it turns to CV_HIST_UNIFORM_FLAG of hist->type */
838 #define CV_HIST_UNIFORM 1
840 typedef struct CvHistogram
844 float thresh[CV_MAX_DIM][2]; /* For uniform histograms. */
845 float** thresh2; /* For non-uniform histograms. */
846 CvMatND mat; /* Embedded matrix header for array histograms. */
850 #define CV_IS_HIST( hist ) \
852 (((CvHistogram*)(hist))->type & CV_MAGIC_MASK) == CV_HIST_MAGIC_VAL && \
853 (hist)->bins != NULL)
855 #define CV_IS_UNIFORM_HIST( hist ) \
856 (((hist)->type & CV_HIST_UNIFORM_FLAG) != 0)
858 #define CV_IS_SPARSE_HIST( hist ) \
859 CV_IS_SPARSE_MAT((hist)->bins)
861 #define CV_HIST_HAS_RANGES( hist ) \
862 (((hist)->type & CV_HIST_RANGES_FLAG) != 0)
864 /****************************************************************************************\
865 * Other supplementary data type definitions *
866 \****************************************************************************************/
868 /*************************************** CvRect *****************************************/
870 typedef struct CvRect
879 CV_INLINE CvRect cvRect( int x, int y, int width, int height )
892 CV_INLINE IplROI cvRectToROI( CvRect rect, int coi )
895 roi.xOffset = rect.x;
896 roi.yOffset = rect.y;
897 roi.width = rect.width;
898 roi.height = rect.height;
905 CV_INLINE CvRect cvROIToRect( IplROI roi )
907 return cvRect( roi.xOffset, roi.yOffset, roi.width, roi.height );
910 /*********************************** CvTermCriteria *************************************/
912 #define CV_TERMCRIT_ITER 1
913 #define CV_TERMCRIT_NUMBER CV_TERMCRIT_ITER
914 #define CV_TERMCRIT_EPS 2
916 typedef struct CvTermCriteria
918 int type; /* may be combination of
926 CV_INLINE CvTermCriteria cvTermCriteria( int type, int max_iter, double epsilon )
931 t.max_iter = max_iter;
932 t.epsilon = (float)epsilon;
938 /******************************* CvPoint and variants ***********************************/
940 typedef struct CvPoint
948 CV_INLINE CvPoint cvPoint( int x, int y )
959 typedef struct CvPoint2D32f
967 CV_INLINE CvPoint2D32f cvPoint2D32f( double x, double y )
978 CV_INLINE CvPoint2D32f cvPointTo32f( CvPoint point )
980 return cvPoint2D32f( (float)point.x, (float)point.y );
984 CV_INLINE CvPoint cvPointFrom32f( CvPoint2D32f point )
987 ipt.x = cvRound(point.x);
988 ipt.y = cvRound(point.y);
994 typedef struct CvPoint3D32f
1003 CV_INLINE CvPoint3D32f cvPoint3D32f( double x, double y, double z )
1015 typedef struct CvPoint2D64f
1023 CV_INLINE CvPoint2D64f cvPoint2D64f( double x, double y )
1034 typedef struct CvPoint3D64f
1043 CV_INLINE CvPoint3D64f cvPoint3D64f( double x, double y, double z )
1055 /******************************** CvSize's & CvBox **************************************/
1064 CV_INLINE CvSize cvSize( int width, int height )
1074 typedef struct CvSize2D32f
1082 CV_INLINE CvSize2D32f cvSize2D32f( double width, double height )
1086 s.width = (float)width;
1087 s.height = (float)height;
1092 typedef struct CvBox2D
1094 CvPoint2D32f center; /* Center of the box. */
1095 CvSize2D32f size; /* Box width and length. */
1096 float angle; /* Angle between the horizontal axis */
1097 /* and the first side (i.e. length) in degrees */
1102 /* Line iterator state: */
1103 typedef struct CvLineIterator
1105 /* Pointer to the current point: */
1108 /* Bresenham algorithm state: */
1119 /************************************* CvSlice ******************************************/
1121 typedef struct CvSlice
1123 int start_index, end_index;
1127 CV_INLINE CvSlice cvSlice( int start, int end )
1130 slice.start_index = start;
1131 slice.end_index = end;
1136 #define CV_WHOLE_SEQ_END_INDEX 0x3fffffff
1137 #define CV_WHOLE_SEQ cvSlice(0, CV_WHOLE_SEQ_END_INDEX)
1140 /************************************* CvScalar *****************************************/
1142 typedef struct CvScalar
1148 CV_INLINE CvScalar cvScalar( double val0, double val1 CV_DEFAULT(0),
1149 double val2 CV_DEFAULT(0), double val3 CV_DEFAULT(0))
1152 scalar.val[0] = val0; scalar.val[1] = val1;
1153 scalar.val[2] = val2; scalar.val[3] = val3;
1158 CV_INLINE CvScalar cvRealScalar( double val0 )
1161 scalar.val[0] = val0;
1162 scalar.val[1] = scalar.val[2] = scalar.val[3] = 0;
1166 CV_INLINE CvScalar cvScalarAll( double val0123 )
1169 scalar.val[0] = val0123;
1170 scalar.val[1] = val0123;
1171 scalar.val[2] = val0123;
1172 scalar.val[3] = val0123;
1176 /****************************************************************************************\
1177 * Dynamic Data structures *
1178 \****************************************************************************************/
1180 /******************************** Memory storage ****************************************/
1182 typedef struct CvMemBlock
1184 struct CvMemBlock* prev;
1185 struct CvMemBlock* next;
1189 #define CV_STORAGE_MAGIC_VAL 0x42890000
1191 typedef struct CvMemStorage
1194 CvMemBlock* bottom; /* First allocated block. */
1195 CvMemBlock* top; /* Current memory block - top of the stack. */
1196 struct CvMemStorage* parent; /* We get new blocks from parent as needed. */
1197 int block_size; /* Block size. */
1198 int free_space; /* Remaining free space in current block. */
1202 #define CV_IS_STORAGE(storage) \
1203 ((storage) != NULL && \
1204 (((CvMemStorage*)(storage))->signature & CV_MAGIC_MASK) == CV_STORAGE_MAGIC_VAL)
1207 typedef struct CvMemStoragePos
1215 /*********************************** Sequence *******************************************/
1217 typedef struct CvSeqBlock
1219 struct CvSeqBlock* prev; /* Previous sequence block. */
1220 struct CvSeqBlock* next; /* Next sequence block. */
1221 int start_index; /* Index of the first element in the block + */
1222 /* sequence->first->start_index. */
1223 int count; /* Number of elements in the block. */
1224 schar* data; /* Pointer to the first element of the block. */
1229 #define CV_TREE_NODE_FIELDS(node_type) \
1230 int flags; /* Miscellaneous flags. */ \
1231 int header_size; /* Size of sequence header. */ \
1232 struct node_type* h_prev; /* Previous sequence. */ \
1233 struct node_type* h_next; /* Next sequence. */ \
1234 struct node_type* v_prev; /* 2nd previous sequence. */ \
1235 struct node_type* v_next /* 2nd next sequence. */
1238 Read/Write sequence.
1239 Elements can be dynamically inserted to or deleted from the sequence.
1241 #define CV_SEQUENCE_FIELDS() \
1242 CV_TREE_NODE_FIELDS(CvSeq); \
1243 int total; /* Total number of elements. */ \
1244 int elem_size; /* Size of sequence element in bytes. */ \
1245 schar* block_max; /* Maximal bound of the last block. */ \
1246 schar* ptr; /* Current write pointer. */ \
1247 int delta_elems; /* Grow seq this many at a time. */ \
1248 CvMemStorage* storage; /* Where the seq is stored. */ \
1249 CvSeqBlock* free_blocks; /* Free blocks list. */ \
1250 CvSeqBlock* first; /* Pointer to the first sequence block. */
1252 typedef struct CvSeq
1254 CV_SEQUENCE_FIELDS()
1258 #define CV_TYPE_NAME_SEQ "opencv-sequence"
1259 #define CV_TYPE_NAME_SEQ_TREE "opencv-sequence-tree"
1261 /*************************************** Set ********************************************/
1264 Order is not preserved. There can be gaps between sequence elements.
1265 After the element has been inserted it stays in the same place all the time.
1266 The MSB(most-significant or sign bit) of the first field (flags) is 0 iff the element exists.
1268 #define CV_SET_ELEM_FIELDS(elem_type) \
1270 struct elem_type* next_free;
1272 typedef struct CvSetElem
1274 CV_SET_ELEM_FIELDS(CvSetElem)
1278 #define CV_SET_FIELDS() \
1279 CV_SEQUENCE_FIELDS() \
1280 CvSetElem* free_elems; \
1283 typedef struct CvSet
1290 #define CV_SET_ELEM_IDX_MASK ((1 << 26) - 1)
1291 #define CV_SET_ELEM_FREE_FLAG (1 << (sizeof(int)*8-1))
1293 /* Checks whether the element pointed by ptr belongs to a set or not */
1294 #define CV_IS_SET_ELEM( ptr ) (((CvSetElem*)(ptr))->flags >= 0)
1296 /************************************* Graph ********************************************/
1299 We represent a graph as a set of vertices.
1300 Vertices contain their adjacency lists (more exactly, pointers to first incoming or
1301 outcoming edge (or 0 if isolated vertex)). Edges are stored in another set.
1302 There is a singly-linked list of incoming/outcoming edges for each vertex.
1304 Each edge consists of
1306 o Two pointers to the starting and ending vertices
1307 (vtx[0] and vtx[1] respectively).
1309 A graph may be oriented or not. In the latter case, edges between
1310 vertex i to vertex j are not distinguished during search operations.
1312 o Two pointers to next edges for the starting and ending vertices, where
1313 next[0] points to the next edge in the vtx[0] adjacency list and
1314 next[1] points to the next edge in the vtx[1] adjacency list.
1316 #define CV_GRAPH_EDGE_FIELDS() \
1319 struct CvGraphEdge* next[2]; \
1320 struct CvGraphVtx* vtx[2];
1323 #define CV_GRAPH_VERTEX_FIELDS() \
1325 struct CvGraphEdge* first;
1328 typedef struct CvGraphEdge
1330 CV_GRAPH_EDGE_FIELDS()
1334 typedef struct CvGraphVtx
1336 CV_GRAPH_VERTEX_FIELDS()
1340 typedef struct CvGraphVtx2D
1342 CV_GRAPH_VERTEX_FIELDS()
1348 Graph is "derived" from the set (this is set a of vertices)
1349 and includes another set (edges)
1351 #define CV_GRAPH_FIELDS() \
1355 typedef struct CvGraph
1361 #define CV_TYPE_NAME_GRAPH "opencv-graph"
1363 /*********************************** Chain/Countour *************************************/
1365 typedef struct CvChain
1367 CV_SEQUENCE_FIELDS()
1372 #define CV_CONTOUR_FIELDS() \
1373 CV_SEQUENCE_FIELDS() \
1378 typedef struct CvContour
1384 typedef CvContour CvPoint2DSeq;
1386 /****************************************************************************************\
1388 \****************************************************************************************/
1390 #define CV_SEQ_MAGIC_VAL 0x42990000
1392 #define CV_IS_SEQ(seq) \
1393 ((seq) != NULL && (((CvSeq*)(seq))->flags & CV_MAGIC_MASK) == CV_SEQ_MAGIC_VAL)
1395 #define CV_SET_MAGIC_VAL 0x42980000
1396 #define CV_IS_SET(set) \
1397 ((set) != NULL && (((CvSeq*)(set))->flags & CV_MAGIC_MASK) == CV_SET_MAGIC_VAL)
1399 #define CV_SEQ_ELTYPE_BITS 9
1400 #define CV_SEQ_ELTYPE_MASK ((1 << CV_SEQ_ELTYPE_BITS) - 1)
1402 #define CV_SEQ_ELTYPE_POINT CV_32SC2 /* (x,y) */
1403 #define CV_SEQ_ELTYPE_CODE CV_8UC1 /* freeman code: 0..7 */
1404 #define CV_SEQ_ELTYPE_GENERIC 0
1405 #define CV_SEQ_ELTYPE_PTR CV_USRTYPE1
1406 #define CV_SEQ_ELTYPE_PPOINT CV_SEQ_ELTYPE_PTR /* &(x,y) */
1407 #define CV_SEQ_ELTYPE_INDEX CV_32SC1 /* #(x,y) */
1408 #define CV_SEQ_ELTYPE_GRAPH_EDGE 0 /* &next_o, &next_d, &vtx_o, &vtx_d */
1409 #define CV_SEQ_ELTYPE_GRAPH_VERTEX 0 /* first_edge, &(x,y) */
1410 #define CV_SEQ_ELTYPE_TRIAN_ATR 0 /* vertex of the binary tree */
1411 #define CV_SEQ_ELTYPE_CONNECTED_COMP 0 /* connected component */
1412 #define CV_SEQ_ELTYPE_POINT3D CV_32FC3 /* (x,y,z) */
1414 #define CV_SEQ_KIND_BITS 3
1415 #define CV_SEQ_KIND_MASK (((1 << CV_SEQ_KIND_BITS) - 1)<<CV_SEQ_ELTYPE_BITS)
1417 /* types of sequences */
1418 #define CV_SEQ_KIND_GENERIC (0 << CV_SEQ_ELTYPE_BITS)
1419 #define CV_SEQ_KIND_CURVE (1 << CV_SEQ_ELTYPE_BITS)
1420 #define CV_SEQ_KIND_BIN_TREE (2 << CV_SEQ_ELTYPE_BITS)
1422 /* types of sparse sequences (sets) */
1423 #define CV_SEQ_KIND_GRAPH (3 << CV_SEQ_ELTYPE_BITS)
1424 #define CV_SEQ_KIND_SUBDIV2D (4 << CV_SEQ_ELTYPE_BITS)
1426 #define CV_SEQ_FLAG_SHIFT (CV_SEQ_KIND_BITS + CV_SEQ_ELTYPE_BITS)
1428 /* flags for curves */
1429 #define CV_SEQ_FLAG_CLOSED (1 << CV_SEQ_FLAG_SHIFT)
1430 #define CV_SEQ_FLAG_SIMPLE (2 << CV_SEQ_FLAG_SHIFT)
1431 #define CV_SEQ_FLAG_CONVEX (4 << CV_SEQ_FLAG_SHIFT)
1432 #define CV_SEQ_FLAG_HOLE (8 << CV_SEQ_FLAG_SHIFT)
1434 /* flags for graphs */
1435 #define CV_GRAPH_FLAG_ORIENTED (1 << CV_SEQ_FLAG_SHIFT)
1437 #define CV_GRAPH CV_SEQ_KIND_GRAPH
1438 #define CV_ORIENTED_GRAPH (CV_SEQ_KIND_GRAPH|CV_GRAPH_FLAG_ORIENTED)
1441 #define CV_SEQ_POINT_SET (CV_SEQ_KIND_GENERIC| CV_SEQ_ELTYPE_POINT)
1442 #define CV_SEQ_POINT3D_SET (CV_SEQ_KIND_GENERIC| CV_SEQ_ELTYPE_POINT3D)
1443 #define CV_SEQ_POLYLINE (CV_SEQ_KIND_CURVE | CV_SEQ_ELTYPE_POINT)
1444 #define CV_SEQ_POLYGON (CV_SEQ_FLAG_CLOSED | CV_SEQ_POLYLINE )
1445 #define CV_SEQ_CONTOUR CV_SEQ_POLYGON
1446 #define CV_SEQ_SIMPLE_POLYGON (CV_SEQ_FLAG_SIMPLE | CV_SEQ_POLYGON )
1448 /* chain-coded curves */
1449 #define CV_SEQ_CHAIN (CV_SEQ_KIND_CURVE | CV_SEQ_ELTYPE_CODE)
1450 #define CV_SEQ_CHAIN_CONTOUR (CV_SEQ_FLAG_CLOSED | CV_SEQ_CHAIN)
1452 /* binary tree for the contour */
1453 #define CV_SEQ_POLYGON_TREE (CV_SEQ_KIND_BIN_TREE | CV_SEQ_ELTYPE_TRIAN_ATR)
1455 /* sequence of the connected components */
1456 #define CV_SEQ_CONNECTED_COMP (CV_SEQ_KIND_GENERIC | CV_SEQ_ELTYPE_CONNECTED_COMP)
1458 /* sequence of the integer numbers */
1459 #define CV_SEQ_INDEX (CV_SEQ_KIND_GENERIC | CV_SEQ_ELTYPE_INDEX)
1461 #define CV_SEQ_ELTYPE( seq ) ((seq)->flags & CV_SEQ_ELTYPE_MASK)
1462 #define CV_SEQ_KIND( seq ) ((seq)->flags & CV_SEQ_KIND_MASK )
1465 #define CV_IS_SEQ_INDEX( seq ) ((CV_SEQ_ELTYPE(seq) == CV_SEQ_ELTYPE_INDEX) && \
1466 (CV_SEQ_KIND(seq) == CV_SEQ_KIND_GENERIC))
1468 #define CV_IS_SEQ_CURVE( seq ) (CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE)
1469 #define CV_IS_SEQ_CLOSED( seq ) (((seq)->flags & CV_SEQ_FLAG_CLOSED) != 0)
1470 #define CV_IS_SEQ_CONVEX( seq ) (((seq)->flags & CV_SEQ_FLAG_CONVEX) != 0)
1471 #define CV_IS_SEQ_HOLE( seq ) (((seq)->flags & CV_SEQ_FLAG_HOLE) != 0)
1472 #define CV_IS_SEQ_SIMPLE( seq ) ((((seq)->flags & CV_SEQ_FLAG_SIMPLE) != 0) || \
1473 CV_IS_SEQ_CONVEX(seq))
1475 /* type checking macros */
1476 #define CV_IS_SEQ_POINT_SET( seq ) \
1477 ((CV_SEQ_ELTYPE(seq) == CV_32SC2 || CV_SEQ_ELTYPE(seq) == CV_32FC2))
1479 #define CV_IS_SEQ_POINT_SUBSET( seq ) \
1480 (CV_IS_SEQ_INDEX( seq ) || CV_SEQ_ELTYPE(seq) == CV_SEQ_ELTYPE_PPOINT)
1482 #define CV_IS_SEQ_POLYLINE( seq ) \
1483 (CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE && CV_IS_SEQ_POINT_SET(seq))
1485 #define CV_IS_SEQ_POLYGON( seq ) \
1486 (CV_IS_SEQ_POLYLINE(seq) && CV_IS_SEQ_CLOSED(seq))
1488 #define CV_IS_SEQ_CHAIN( seq ) \
1489 (CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE && (seq)->elem_size == 1)
1491 #define CV_IS_SEQ_CONTOUR( seq ) \
1492 (CV_IS_SEQ_CLOSED(seq) && (CV_IS_SEQ_POLYLINE(seq) || CV_IS_SEQ_CHAIN(seq)))
1494 #define CV_IS_SEQ_CHAIN_CONTOUR( seq ) \
1495 (CV_IS_SEQ_CHAIN( seq ) && CV_IS_SEQ_CLOSED( seq ))
1497 #define CV_IS_SEQ_POLYGON_TREE( seq ) \
1498 (CV_SEQ_ELTYPE (seq) == CV_SEQ_ELTYPE_TRIAN_ATR && \
1499 CV_SEQ_KIND( seq ) == CV_SEQ_KIND_BIN_TREE )
1501 #define CV_IS_GRAPH( seq ) \
1502 (CV_IS_SET(seq) && CV_SEQ_KIND((CvSet*)(seq)) == CV_SEQ_KIND_GRAPH)
1504 #define CV_IS_GRAPH_ORIENTED( seq ) \
1505 (((seq)->flags & CV_GRAPH_FLAG_ORIENTED) != 0)
1507 #define CV_IS_SUBDIV2D( seq ) \
1508 (CV_IS_SET(seq) && CV_SEQ_KIND((CvSet*)(seq)) == CV_SEQ_KIND_SUBDIV2D)
1510 /****************************************************************************************/
1511 /* Sequence writer & reader */
1512 /****************************************************************************************/
1514 #define CV_SEQ_WRITER_FIELDS() \
1516 CvSeq* seq; /* the sequence written */ \
1517 CvSeqBlock* block; /* current block */ \
1518 schar* ptr; /* pointer to free space */ \
1519 schar* block_min; /* pointer to the beginning of block*/\
1520 schar* block_max; /* pointer to the end of block */
1522 typedef struct CvSeqWriter
1524 CV_SEQ_WRITER_FIELDS()
1529 #define CV_SEQ_READER_FIELDS() \
1531 CvSeq* seq; /* sequence, beign read */ \
1532 CvSeqBlock* block; /* current block */ \
1533 schar* ptr; /* pointer to element be read next */ \
1534 schar* block_min; /* pointer to the beginning of block */\
1535 schar* block_max; /* pointer to the end of block */ \
1536 int delta_index;/* = seq->first->start_index */ \
1537 schar* prev_elem; /* pointer to previous element */
1540 typedef struct CvSeqReader
1542 CV_SEQ_READER_FIELDS()
1546 /****************************************************************************************/
1547 /* Operations on sequences */
1548 /****************************************************************************************/
1550 #define CV_SEQ_ELEM( seq, elem_type, index ) \
1551 /* assert gives some guarantee that <seq> parameter is valid */ \
1552 ( assert(sizeof((seq)->first[0]) == sizeof(CvSeqBlock) && \
1553 (seq)->elem_size == sizeof(elem_type)), \
1554 (elem_type*)((seq)->first && (unsigned)index < \
1555 (unsigned)((seq)->first->count) ? \
1556 (seq)->first->data + (index) * sizeof(elem_type) : \
1557 cvGetSeqElem( (CvSeq*)(seq), (index) )))
1558 #define CV_GET_SEQ_ELEM( elem_type, seq, index ) CV_SEQ_ELEM( (seq), elem_type, (index) )
1560 /* Add element to sequence: */
1561 #define CV_WRITE_SEQ_ELEM_VAR( elem_ptr, writer ) \
1563 if( (writer).ptr >= (writer).block_max ) \
1565 cvCreateSeqBlock( &writer); \
1567 memcpy((writer).ptr, elem_ptr, (writer).seq->elem_size);\
1568 (writer).ptr += (writer).seq->elem_size; \
1571 #define CV_WRITE_SEQ_ELEM( elem, writer ) \
1573 assert( (writer).seq->elem_size == sizeof(elem)); \
1574 if( (writer).ptr >= (writer).block_max ) \
1576 cvCreateSeqBlock( &writer); \
1578 assert( (writer).ptr <= (writer).block_max - sizeof(elem));\
1579 memcpy((writer).ptr, &(elem), sizeof(elem)); \
1580 (writer).ptr += sizeof(elem); \
1584 /* Move reader position forward: */
1585 #define CV_NEXT_SEQ_ELEM( elem_size, reader ) \
1587 if( ((reader).ptr += (elem_size)) >= (reader).block_max ) \
1589 cvChangeSeqBlock( &(reader), 1 ); \
1594 /* Move reader position backward: */
1595 #define CV_PREV_SEQ_ELEM( elem_size, reader ) \
1597 if( ((reader).ptr -= (elem_size)) < (reader).block_min ) \
1599 cvChangeSeqBlock( &(reader), -1 ); \
1603 /* Read element and move read position forward: */
1604 #define CV_READ_SEQ_ELEM( elem, reader ) \
1606 assert( (reader).seq->elem_size == sizeof(elem)); \
1607 memcpy( &(elem), (reader).ptr, sizeof((elem))); \
1608 CV_NEXT_SEQ_ELEM( sizeof(elem), reader ) \
1611 /* Read element and move read position backward: */
1612 #define CV_REV_READ_SEQ_ELEM( elem, reader ) \
1614 assert( (reader).seq->elem_size == sizeof(elem)); \
1615 memcpy(&(elem), (reader).ptr, sizeof((elem))); \
1616 CV_PREV_SEQ_ELEM( sizeof(elem), reader ) \
1620 #define CV_READ_CHAIN_POINT( _pt, reader ) \
1622 (_pt) = (reader).pt; \
1623 if( (reader).ptr ) \
1625 CV_READ_SEQ_ELEM( (reader).code, (reader)); \
1626 assert( ((reader).code & ~7) == 0 ); \
1627 (reader).pt.x += (reader).deltas[(int)(reader).code][0]; \
1628 (reader).pt.y += (reader).deltas[(int)(reader).code][1]; \
1632 #define CV_CURRENT_POINT( reader ) (*((CvPoint*)((reader).ptr)))
1633 #define CV_PREV_POINT( reader ) (*((CvPoint*)((reader).prev_elem)))
1635 #define CV_READ_EDGE( pt1, pt2, reader ) \
1637 assert( sizeof(pt1) == sizeof(CvPoint) && \
1638 sizeof(pt2) == sizeof(CvPoint) && \
1639 reader.seq->elem_size == sizeof(CvPoint)); \
1640 (pt1) = CV_PREV_POINT( reader ); \
1641 (pt2) = CV_CURRENT_POINT( reader ); \
1642 (reader).prev_elem = (reader).ptr; \
1643 CV_NEXT_SEQ_ELEM( sizeof(CvPoint), (reader)); \
1646 /************ Graph macros ************/
1648 /* Return next graph edge for given vertex: */
1649 #define CV_NEXT_GRAPH_EDGE( edge, vertex ) \
1650 (assert((edge)->vtx[0] == (vertex) || (edge)->vtx[1] == (vertex)), \
1651 (edge)->next[(edge)->vtx[1] == (vertex)])
1655 /****************************************************************************************\
1656 * Data structures for persistence (a.k.a serialization) functionality *
1657 \****************************************************************************************/
1659 /* "black box" file storage */
1660 typedef struct CvFileStorage CvFileStorage;
1662 /* Storage flags: */
1663 #define CV_STORAGE_READ 0
1664 #define CV_STORAGE_WRITE 1
1665 #define CV_STORAGE_WRITE_TEXT CV_STORAGE_WRITE
1666 #define CV_STORAGE_WRITE_BINARY CV_STORAGE_WRITE
1667 #define CV_STORAGE_APPEND 2
1669 /* List of attributes: */
1670 typedef struct CvAttrList
1672 const char** attr; /* NULL-terminated array of (attribute_name,attribute_value) pairs. */
1673 struct CvAttrList* next; /* Pointer to next chunk of the attributes list. */
1677 CV_INLINE CvAttrList cvAttrList( const char** attr CV_DEFAULT(NULL),
1678 CvAttrList* next CV_DEFAULT(NULL) )
1689 #define CV_NODE_NONE 0
1690 #define CV_NODE_INT 1
1691 #define CV_NODE_INTEGER CV_NODE_INT
1692 #define CV_NODE_REAL 2
1693 #define CV_NODE_FLOAT CV_NODE_REAL
1694 #define CV_NODE_STR 3
1695 #define CV_NODE_STRING CV_NODE_STR
1696 #define CV_NODE_REF 4 /* not used */
1697 #define CV_NODE_SEQ 5
1698 #define CV_NODE_MAP 6
1699 #define CV_NODE_TYPE_MASK 7
1701 #define CV_NODE_TYPE(flags) ((flags) & CV_NODE_TYPE_MASK)
1703 /* file node flags */
1704 #define CV_NODE_FLOW 8 /* Used only for writing structures in YAML format. */
1705 #define CV_NODE_USER 16
1706 #define CV_NODE_EMPTY 32
1707 #define CV_NODE_NAMED 64
1709 #define CV_NODE_IS_INT(flags) (CV_NODE_TYPE(flags) == CV_NODE_INT)
1710 #define CV_NODE_IS_REAL(flags) (CV_NODE_TYPE(flags) == CV_NODE_REAL)
1711 #define CV_NODE_IS_STRING(flags) (CV_NODE_TYPE(flags) == CV_NODE_STRING)
1712 #define CV_NODE_IS_SEQ(flags) (CV_NODE_TYPE(flags) == CV_NODE_SEQ)
1713 #define CV_NODE_IS_MAP(flags) (CV_NODE_TYPE(flags) == CV_NODE_MAP)
1714 #define CV_NODE_IS_COLLECTION(flags) (CV_NODE_TYPE(flags) >= CV_NODE_SEQ)
1715 #define CV_NODE_IS_FLOW(flags) (((flags) & CV_NODE_FLOW) != 0)
1716 #define CV_NODE_IS_EMPTY(flags) (((flags) & CV_NODE_EMPTY) != 0)
1717 #define CV_NODE_IS_USER(flags) (((flags) & CV_NODE_USER) != 0)
1718 #define CV_NODE_HAS_NAME(flags) (((flags) & CV_NODE_NAMED) != 0)
1720 #define CV_NODE_SEQ_SIMPLE 256
1721 #define CV_NODE_SEQ_IS_SIMPLE(seq) (((seq)->flags & CV_NODE_SEQ_SIMPLE) != 0)
1723 typedef struct CvString
1730 /* All the keys (names) of elements in the readed file storage
1731 are stored in the hash to speed up the lookup operations: */
1732 typedef struct CvStringHashNode
1736 struct CvStringHashNode* next;
1740 typedef struct CvGenericHash CvFileNodeHash;
1742 /* Basic element of the file storage - scalar or collection: */
1743 typedef struct CvFileNode
1746 struct CvTypeInfo* info; /* type information
1747 (only for user-defined object, for others it is 0) */
1750 double f; /* scalar floating-point number */
1751 int i; /* scalar integer number */
1752 CvString str; /* text string */
1753 CvSeq* seq; /* sequence (ordered collection of file nodes) */
1754 CvFileNodeHash* map; /* map (collection of named file nodes) */
1762 typedef int (CV_CDECL *CvIsInstanceFunc)( const void* struct_ptr );
1763 typedef void (CV_CDECL *CvReleaseFunc)( void** struct_dblptr );
1764 typedef void* (CV_CDECL *CvReadFunc)( CvFileStorage* storage, CvFileNode* node );
1765 typedef void (CV_CDECL *CvWriteFunc)( CvFileStorage* storage, const char* name,
1766 const void* struct_ptr, CvAttrList attributes );
1767 typedef void* (CV_CDECL *CvCloneFunc)( const void* struct_ptr );
1772 typedef struct CvTypeInfo
1776 struct CvTypeInfo* prev;
1777 struct CvTypeInfo* next;
1778 const char* type_name;
1779 CvIsInstanceFunc is_instance;
1780 CvReleaseFunc release;
1788 /**** System data types ******/
1790 typedef struct CvPluginFuncInfo
1793 void* default_func_addr;
1794 const char* func_names;
1800 typedef struct CvModuleInfo
1802 struct CvModuleInfo* next;
1804 const char* version;
1805 CvPluginFuncInfo* func_tab;
1809 #endif /*_CXCORE_TYPES_H_*/