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