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 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43 
44 #ifndef OPENCV_CORE_TYPES_H
45 #define OPENCV_CORE_TYPES_H
46 
47 #ifdef CV__ENABLE_C_API_CTORS  // invalid C API ctors (must be removed)
48 #if defined(_WIN32) && !defined(CV__SKIP_MESSAGE_MALFORMED_C_API_CTORS)
49 #error "C API ctors don't work on Win32: https://github.com/opencv/opencv/issues/15990"
50 #endif
51 #endif
52 
53 //#define CV__VALIDATE_UNUNITIALIZED_VARS 1  // C++11 & GCC only
54 
55 #ifdef __cplusplus
56 
57 #ifdef CV__VALIDATE_UNUNITIALIZED_VARS
58 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
59 #define CV_STRUCT_INITIALIZER {0,}
60 #else
61 #if defined(__GNUC__) && __GNUC__ == 4  // GCC 4.x warns on "= {}" initialization, fixed in GCC 5.0
62 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
63 #endif
64 #define CV_STRUCT_INITIALIZER {}
65 #endif
66 
67 #else
68 #define CV_STRUCT_INITIALIZER {0}
69 #endif
70 
71 
72 #ifdef HAVE_IPL
73 #  ifndef __IPL_H__
74 #    if defined _WIN32
75 #      include <ipl.h>
76 #    else
77 #      include <ipl/ipl.h>
78 #    endif
79 #  endif
80 #elif defined __IPL_H__
81 #  define HAVE_IPL
82 #endif
83 
84 #include "opencv2/core/cvdef.h"
85 
86 #ifndef SKIP_INCLUDES
87 #include <assert.h>
88 #include <stdlib.h>
89 #include <string.h>
90 #include <float.h>
91 #endif // SKIP_INCLUDES
92 
93 #if defined _WIN32
94 #  define CV_CDECL __cdecl
95 #  define CV_STDCALL __stdcall
96 #else
97 #  define CV_CDECL
98 #  define CV_STDCALL
99 #endif
100 
101 #ifndef CV_DEFAULT
102 #  ifdef __cplusplus
103 #    define CV_DEFAULT(val) = val
104 #  else
105 #    define CV_DEFAULT(val)
106 #  endif
107 #endif
108 
109 #ifndef CV_EXTERN_C_FUNCPTR
110 #  ifdef __cplusplus
111 #    define CV_EXTERN_C_FUNCPTR(x) extern "C" { typedef x; }
112 #  else
113 #    define CV_EXTERN_C_FUNCPTR(x) typedef x
114 #  endif
115 #endif
116 
117 #ifndef CVAPI
118 #  define CVAPI(rettype) CV_EXTERN_C CV_EXPORTS rettype CV_CDECL
119 #endif
120 
121 #ifndef CV_IMPL
122 #  define CV_IMPL CV_EXTERN_C
123 #endif
124 
125 #ifdef __cplusplus
126 #  include "opencv2/core.hpp"
127 #endif
128 
129 /** @addtogroup core_c
130     @{
131 */
132 
133 /** @brief This is the "metatype" used *only* as a function parameter.
134 
135 It denotes that the function accepts arrays of multiple types, such as IplImage*, CvMat* or even
136 CvSeq* sometimes. The particular array type is determined at runtime by analyzing the first 4
137 bytes of the header. In C++ interface the role of CvArr is played by InputArray and OutputArray.
138  */
139 typedef void CvArr;
140 
141 typedef int CVStatus;
142 
143 /** @see cv::Error::Code */
144 enum {
145  CV_StsOk=                       0,  /**< everything is ok                */
146  CV_StsBackTrace=               -1,  /**< pseudo error for back trace     */
147  CV_StsError=                   -2,  /**< unknown /unspecified error      */
148  CV_StsInternal=                -3,  /**< internal error (bad state)      */
149  CV_StsNoMem=                   -4,  /**< insufficient memory             */
150  CV_StsBadArg=                  -5,  /**< function arg/param is bad       */
151  CV_StsBadFunc=                 -6,  /**< unsupported function            */
152  CV_StsNoConv=                  -7,  /**< iter. didn't converge           */
153  CV_StsAutoTrace=               -8,  /**< tracing                         */
154  CV_HeaderIsNull=               -9,  /**< image header is NULL            */
155  CV_BadImageSize=              -10,  /**< image size is invalid           */
156  CV_BadOffset=                 -11,  /**< offset is invalid               */
157  CV_BadDataPtr=                -12,  /**/
158  CV_BadStep=                   -13,  /**< image step is wrong, this may happen for a non-continuous matrix */
159  CV_BadModelOrChSeq=           -14,  /**/
160  CV_BadNumChannels=            -15,  /**< bad number of channels, for example, some functions accept only single channel matrices */
161  CV_BadNumChannel1U=           -16,  /**/
162  CV_BadDepth=                  -17,  /**< input image depth is not supported by the function */
163  CV_BadAlphaChannel=           -18,  /**/
164  CV_BadOrder=                  -19,  /**< number of dimensions is out of range */
165  CV_BadOrigin=                 -20,  /**< incorrect input origin               */
166  CV_BadAlign=                  -21,  /**< incorrect input align                */
167  CV_BadCallBack=               -22,  /**/
168  CV_BadTileSize=               -23,  /**/
169  CV_BadCOI=                    -24,  /**< input COI is not supported           */
170  CV_BadROISize=                -25,  /**< incorrect input roi                  */
171  CV_MaskIsTiled=               -26,  /**/
172  CV_StsNullPtr=                -27,  /**< null pointer */
173  CV_StsVecLengthErr=           -28,  /**< incorrect vector length */
174  CV_StsFilterStructContentErr= -29,  /**< incorrect filter structure content */
175  CV_StsKernelStructContentErr= -30,  /**< incorrect transform kernel content */
176  CV_StsFilterOffsetErr=        -31,  /**< incorrect filter offset value */
177  CV_StsBadSize=                -201, /**< the input/output structure size is incorrect  */
178  CV_StsDivByZero=              -202, /**< division by zero */
179  CV_StsInplaceNotSupported=    -203, /**< in-place operation is not supported */
180  CV_StsObjectNotFound=         -204, /**< request can't be completed */
181  CV_StsUnmatchedFormats=       -205, /**< formats of input/output arrays differ */
182  CV_StsBadFlag=                -206, /**< flag is wrong or not supported */
183  CV_StsBadPoint=               -207, /**< bad CvPoint */
184  CV_StsBadMask=                -208, /**< bad format of mask (neither 8uC1 nor 8sC1)*/
185  CV_StsUnmatchedSizes=         -209, /**< sizes of input/output structures do not match */
186  CV_StsUnsupportedFormat=      -210, /**< the data format/type is not supported by the function*/
187  CV_StsOutOfRange=             -211, /**< some of parameters are out of range */
188  CV_StsParseError=             -212, /**< invalid syntax/structure of the parsed file */
189  CV_StsNotImplemented=         -213, /**< the requested function/feature is not implemented */
190  CV_StsBadMemBlock=            -214, /**< an allocated block has been corrupted */
191  CV_StsAssert=                 -215, /**< assertion failed   */
192  CV_GpuNotSupported=           -216, /**< no CUDA support    */
193  CV_GpuApiCallError=           -217, /**< GPU API call error */
194  CV_OpenGlNotSupported=        -218, /**< no OpenGL support  */
195  CV_OpenGlApiCallError=        -219, /**< OpenGL API call error */
196  CV_OpenCLApiCallError=        -220, /**< OpenCL API call error */
197  CV_OpenCLDoubleNotSupported=  -221,
198  CV_OpenCLInitError=           -222, /**< OpenCL initialization error */
199  CV_OpenCLNoAMDBlasFft=        -223
200 };
201 
202 /****************************************************************************************\
203 *                             Common macros and inline functions                         *
204 \****************************************************************************************/
205 
206 #define CV_SWAP(a,b,t) ((t) = (a), (a) = (b), (b) = (t))
207 
208 /** min & max without jumps */
209 #define  CV_IMIN(a, b)  ((a) ^ (((a)^(b)) & (((a) < (b)) - 1)))
210 
211 #define  CV_IMAX(a, b)  ((a) ^ (((a)^(b)) & (((a) > (b)) - 1)))
212 
213 /** absolute value without jumps */
214 #ifndef __cplusplus
215 #  define  CV_IABS(a)     (((a) ^ ((a) < 0 ? -1 : 0)) - ((a) < 0 ? -1 : 0))
216 #else
217 #  define  CV_IABS(a)     abs(a)
218 #endif
219 #define  CV_CMP(a,b)    (((a) > (b)) - ((a) < (b)))
220 #define  CV_SIGN(a)     CV_CMP((a),0)
221 
222 #define cvInvSqrt(value) ((float)(1./sqrt(value)))
223 #define cvSqrt(value)  ((float)sqrt(value))
224 
225 
226 /*************** Random number generation *******************/
227 
228 typedef uint64 CvRNG;
229 
230 #define CV_RNG_COEFF 4164903690U
231 
232 /** @brief Initializes a random number generator state.
233 
234 The function initializes a random number generator and returns the state. The pointer to the state
235 can be then passed to the cvRandInt, cvRandReal and cvRandArr functions. In the current
236 implementation a multiply-with-carry generator is used.
237 @param seed 64-bit value used to initiate a random sequence
238 @sa the C++ class RNG replaced CvRNG.
239  */
240 CV_INLINE CvRNG cvRNG( int64 seed CV_DEFAULT(-1))
241 {
242     CvRNG rng = seed ? (uint64)seed : (uint64)(int64)-1;
243     return rng;
244 }
245 
246 /** @brief Returns a 32-bit unsigned integer and updates RNG.
247 
248 The function returns a uniformly-distributed random 32-bit unsigned integer and updates the RNG
249 state. It is similar to the rand() function from the C runtime library, except that OpenCV functions
250 always generates a 32-bit random number, regardless of the platform.
251 @param rng CvRNG state initialized by cvRNG.
252  */
cvRandInt(CvRNG * rng)253 CV_INLINE unsigned cvRandInt( CvRNG* rng )
254 {
255     uint64 temp = *rng;
256     temp = (uint64)(unsigned)temp*CV_RNG_COEFF + (temp >> 32);
257     *rng = temp;
258     return (unsigned)temp;
259 }
260 
261 /** @brief Returns a floating-point random number and updates RNG.
262 
263 The function returns a uniformly-distributed random floating-point number between 0 and 1 (1 is not
264 included).
265 @param rng RNG state initialized by cvRNG
266  */
cvRandReal(CvRNG * rng)267 CV_INLINE double cvRandReal( CvRNG* rng )
268 {
269     return cvRandInt(rng)*2.3283064365386962890625e-10 /* 2^-32 */;
270 }
271 
272 /****************************************************************************************\
273 *                                  Image type (IplImage)                                 *
274 \****************************************************************************************/
275 
276 #ifndef HAVE_IPL
277 
278 /*
279  * The following definitions (until #endif)
280  * is an extract from IPL headers.
281  * Copyright (c) 1995 Intel Corporation.
282  */
283 #define IPL_DEPTH_SIGN 0x80000000
284 
285 #define IPL_DEPTH_1U     1
286 #define IPL_DEPTH_8U     8
287 #define IPL_DEPTH_16U   16
288 #define IPL_DEPTH_32F   32
289 
290 #define IPL_DEPTH_8S  (IPL_DEPTH_SIGN| 8)
291 #define IPL_DEPTH_16S (IPL_DEPTH_SIGN|16)
292 #define IPL_DEPTH_32S (IPL_DEPTH_SIGN|32)
293 
294 #define IPL_DATA_ORDER_PIXEL  0
295 #define IPL_DATA_ORDER_PLANE  1
296 
297 #define IPL_ORIGIN_TL 0
298 #define IPL_ORIGIN_BL 1
299 
300 #define IPL_ALIGN_4BYTES   4
301 #define IPL_ALIGN_8BYTES   8
302 #define IPL_ALIGN_16BYTES 16
303 #define IPL_ALIGN_32BYTES 32
304 
305 #define IPL_ALIGN_DWORD   IPL_ALIGN_4BYTES
306 #define IPL_ALIGN_QWORD   IPL_ALIGN_8BYTES
307 
308 #define IPL_BORDER_CONSTANT   0
309 #define IPL_BORDER_REPLICATE  1
310 #define IPL_BORDER_REFLECT    2
311 #define IPL_BORDER_WRAP       3
312 
313 #ifdef __cplusplus
314 typedef struct _IplImage IplImage;
315 CV_EXPORTS _IplImage cvIplImage(const cv::Mat& m);
316 #endif
317 
318 /** The IplImage is taken from the Intel Image Processing Library, in which the format is native. OpenCV
319 only supports a subset of possible IplImage formats, as outlined in the parameter list above.
320 
321 In addition to the above restrictions, OpenCV handles ROIs differently. OpenCV functions require
322 that the image size or ROI size of all source and destination images match exactly. On the other
323 hand, the Intel Image Processing Library processes the area of intersection between the source and
324 destination images (or ROIs), allowing them to vary independently.
325 */
326 typedef struct
327 _IplImage
328 {
329     int  nSize;             /**< sizeof(IplImage) */
330     int  ID;                /**< version (=0)*/
331     int  nChannels;         /**< Most of OpenCV functions support 1,2,3 or 4 channels */
332     int  alphaChannel;      /**< Ignored by OpenCV */
333     int  depth;             /**< Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
334                                IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported.  */
335     char colorModel[4];     /**< Ignored by OpenCV */
336     char channelSeq[4];     /**< ditto */
337     int  dataOrder;         /**< 0 - interleaved color channels, 1 - separate color channels.
338                                cvCreateImage can only create interleaved images */
339     int  origin;            /**< 0 - top-left origin,
340                                1 - bottom-left origin (Windows bitmaps style).  */
341     int  align;             /**< Alignment of image rows (4 or 8).
342                                OpenCV ignores it and uses widthStep instead.    */
343     int  width;             /**< Image width in pixels.                           */
344     int  height;            /**< Image height in pixels.                          */
345     struct _IplROI *roi;    /**< Image ROI. If NULL, the whole image is selected. */
346     struct _IplImage *maskROI;      /**< Must be NULL. */
347     void  *imageId;                 /**< "           " */
348     struct _IplTileInfo *tileInfo;  /**< "           " */
349     int  imageSize;         /**< Image data size in bytes
350                                (==image->height*image->widthStep
351                                in case of interleaved data)*/
352     char *imageData;        /**< Pointer to aligned image data.         */
353     int  widthStep;         /**< Size of aligned image row in bytes.    */
354     int  BorderMode[4];     /**< Ignored by OpenCV.                     */
355     int  BorderConst[4];    /**< Ditto.                                 */
356     char *imageDataOrigin;  /**< Pointer to very origin of image data
357                                (not necessarily aligned) -
358                                needed for correct deallocation */
359 
360 #if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
_IplImage_IplImage361     _IplImage()
362     {
363         memset(this, 0, sizeof(*this));  // valid for POD structure
364         nSize = sizeof(IplImage);
365     }
_IplImage_IplImage366     _IplImage(const cv::Mat& m) { *this = cvIplImage(m); }
367 #endif
368 }
369 IplImage;
370 
cvIplImage()371 CV_INLINE IplImage cvIplImage()
372 {
373 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
374     IplImage self = CV_STRUCT_INITIALIZER; self.nSize = sizeof(IplImage); return self;
375 #else
376     return _IplImage();
377 #endif
378 }
379 
380 typedef struct _IplTileInfo IplTileInfo;
381 
382 typedef struct _IplROI
383 {
384     int  coi; /**< 0 - no COI (all channels are selected), 1 - 0th channel is selected ...*/
385     int  xOffset;
386     int  yOffset;
387     int  width;
388     int  height;
389 }
390 IplROI;
391 
392 typedef struct _IplConvKernel
393 {
394     int  nCols;
395     int  nRows;
396     int  anchorX;
397     int  anchorY;
398     int *values;
399     int  nShiftR;
400 }
401 IplConvKernel;
402 
403 typedef struct _IplConvKernelFP
404 {
405     int  nCols;
406     int  nRows;
407     int  anchorX;
408     int  anchorY;
409     float *values;
410 }
411 IplConvKernelFP;
412 
413 #define IPL_IMAGE_HEADER 1
414 #define IPL_IMAGE_DATA   2
415 #define IPL_IMAGE_ROI    4
416 
417 #endif/*HAVE_IPL*/
418 
419 /** extra border mode */
420 #define IPL_BORDER_REFLECT_101    4
421 #define IPL_BORDER_TRANSPARENT    5
422 
423 #define IPL_IMAGE_MAGIC_VAL  ((int)sizeof(IplImage))
424 #define CV_TYPE_NAME_IMAGE "opencv-image"
425 
426 #define CV_IS_IMAGE_HDR(img) \
427     ((img) != NULL && ((const IplImage*)(img))->nSize == sizeof(IplImage))
428 
429 #define CV_IS_IMAGE(img) \
430     (CV_IS_IMAGE_HDR(img) && ((IplImage*)img)->imageData != NULL)
431 
432 /** for storing double-precision
433    floating point data in IplImage's */
434 #define IPL_DEPTH_64F  64
435 
436 /** get reference to pixel at (col,row),
437    for multi-channel images (col) should be multiplied by number of channels */
438 #define CV_IMAGE_ELEM( image, elemtype, row, col )       \
439     (((elemtype*)((image)->imageData + (image)->widthStep*(row)))[(col)])
440 
441 /****************************************************************************************\
442 *                                  Matrix type (CvMat)                                   *
443 \****************************************************************************************/
444 
445 #define CV_AUTO_STEP  0x7fffffff
446 #define CV_WHOLE_ARR  cvSlice( 0, 0x3fffffff )
447 
448 #define CV_MAGIC_MASK       0xFFFF0000
449 #define CV_MAT_MAGIC_VAL    0x42420000
450 #define CV_TYPE_NAME_MAT    "opencv-matrix"
451 
452 #ifdef __cplusplus
453 typedef struct CvMat CvMat;
454 CV_INLINE CvMat cvMat(const cv::Mat& m);
455 #endif
456 
457 /** Matrix elements are stored row by row. Element (i, j) (i - 0-based row index, j - 0-based column
458 index) of a matrix can be retrieved or modified using CV_MAT_ELEM macro:
459 
460     uchar pixval = CV_MAT_ELEM(grayimg, uchar, i, j)
461     CV_MAT_ELEM(cameraMatrix, float, 0, 2) = image.width*0.5f;
462 
463 To access multiple-channel matrices, you can use
464 CV_MAT_ELEM(matrix, type, i, j\*nchannels + channel_idx).
465 
466 @deprecated CvMat is now obsolete; consider using Mat instead.
467  */
468 typedef struct CvMat
469 {
470     int type;
471     int step;
472 
473     /* for internal use only */
474     int* refcount;
475     int hdr_refcount;
476 
477     union
478     {
479         uchar* ptr;
480         short* s;
481         int* i;
482         float* fl;
483         double* db;
484     } data;
485 
486 #ifdef __cplusplus
487     union
488     {
489         int rows;
490         int height;
491     };
492 
493     union
494     {
495         int cols;
496         int width;
497     };
498 #else
499     int rows;
500     int cols;
501 #endif
502 
503 #if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
CvMatCvMat504     CvMat() {}
CvMatCvMat505     CvMat(const cv::Mat& m) { *this = cvMat(m); }
506 #endif
507 }
508 CvMat;
509 
510 
511 #define CV_IS_MAT_HDR(mat) \
512     ((mat) != NULL && \
513     (((const CvMat*)(mat))->type & CV_MAGIC_MASK) == CV_MAT_MAGIC_VAL && \
514     ((const CvMat*)(mat))->cols > 0 && ((const CvMat*)(mat))->rows > 0)
515 
516 #define CV_IS_MAT_HDR_Z(mat) \
517     ((mat) != NULL && \
518     (((const CvMat*)(mat))->type & CV_MAGIC_MASK) == CV_MAT_MAGIC_VAL && \
519     ((const CvMat*)(mat))->cols >= 0 && ((const CvMat*)(mat))->rows >= 0)
520 
521 #define CV_IS_MAT(mat) \
522     (CV_IS_MAT_HDR(mat) && ((const CvMat*)(mat))->data.ptr != NULL)
523 
524 #define CV_IS_MASK_ARR(mat) \
525     (((mat)->type & (CV_MAT_TYPE_MASK & ~CV_8SC1)) == 0)
526 
527 #define CV_ARE_TYPES_EQ(mat1, mat2) \
528     ((((mat1)->type ^ (mat2)->type) & CV_MAT_TYPE_MASK) == 0)
529 
530 #define CV_ARE_CNS_EQ(mat1, mat2) \
531     ((((mat1)->type ^ (mat2)->type) & CV_MAT_CN_MASK) == 0)
532 
533 #define CV_ARE_DEPTHS_EQ(mat1, mat2) \
534     ((((mat1)->type ^ (mat2)->type) & CV_MAT_DEPTH_MASK) == 0)
535 
536 #define CV_ARE_SIZES_EQ(mat1, mat2) \
537     ((mat1)->rows == (mat2)->rows && (mat1)->cols == (mat2)->cols)
538 
539 #define CV_IS_MAT_CONST(mat)  \
540     (((mat)->rows|(mat)->cols) == 1)
541 
542 #define IPL2CV_DEPTH(depth) \
543     ((((CV_8U)+(CV_16U<<4)+(CV_32F<<8)+(CV_64F<<16)+(CV_8S<<20)+ \
544     (CV_16S<<24)+(CV_32S<<28)) >> ((((depth) & 0xF0) >> 2) + \
545     (((depth) & IPL_DEPTH_SIGN) ? 20 : 0))) & 15)
546 
547 /** Inline constructor. No data is allocated internally!!!
548  * (Use together with cvCreateData, or use cvCreateMat instead to
549  * get a matrix with allocated data):
550  */
cvMat(int rows,int cols,int type,void * data CV_DEFAULT (NULL))551 CV_INLINE CvMat cvMat( int rows, int cols, int type, void* data CV_DEFAULT(NULL))
552 {
553     CvMat m;
554 
555     assert( (unsigned)CV_MAT_DEPTH(type) <= CV_64F );
556     type = CV_MAT_TYPE(type);
557     m.type = CV_MAT_MAGIC_VAL | CV_MAT_CONT_FLAG | type;
558     m.cols = cols;
559     m.rows = rows;
560     m.step = m.cols*CV_ELEM_SIZE(type);
561     m.data.ptr = (uchar*)data;
562     m.refcount = NULL;
563     m.hdr_refcount = 0;
564 
565     return m;
566 }
567 
568 #ifdef __cplusplus
569 
cvMat(const cv::Mat & m)570 CV_INLINE CvMat cvMat(const cv::Mat& m)
571 {
572     CvMat self;
573     CV_DbgAssert(m.dims <= 2);
574     self = cvMat(m.rows, m.dims == 1 ? 1 : m.cols, m.type(), m.data);
575     self.step = (int)m.step[0];
576     self.type = (self.type & ~cv::Mat::CONTINUOUS_FLAG) | (m.flags & cv::Mat::CONTINUOUS_FLAG);
577     return self;
578 }
cvMat()579 CV_INLINE CvMat cvMat()
580 {
581 #if !defined(CV__ENABLE_C_API_CTORS)
582     CvMat self = CV_STRUCT_INITIALIZER; return self;
583 #else
584     return CvMat();
585 #endif
586 }
cvMat(const CvMat & m)587 CV_INLINE CvMat cvMat(const CvMat& m)
588 {
589 #if !defined(CV__ENABLE_C_API_CTORS)
590     CvMat self = CV_STRUCT_INITIALIZER; memcpy(&self, &m, sizeof(self)); return self;
591 #else
592     return CvMat(m);
593 #endif
594 }
595 
596 #endif // __cplusplus
597 
598 
599 #define CV_MAT_ELEM_PTR_FAST( mat, row, col, pix_size )  \
600     (assert( (unsigned)(row) < (unsigned)(mat).rows &&   \
601              (unsigned)(col) < (unsigned)(mat).cols ),   \
602      (mat).data.ptr + (size_t)(mat).step*(row) + (pix_size)*(col))
603 
604 #define CV_MAT_ELEM_PTR( mat, row, col )                 \
605     CV_MAT_ELEM_PTR_FAST( mat, row, col, CV_ELEM_SIZE((mat).type) )
606 
607 #define CV_MAT_ELEM( mat, elemtype, row, col )           \
608     (*(elemtype*)CV_MAT_ELEM_PTR_FAST( mat, row, col, sizeof(elemtype)))
609 
610 /** @brief Returns the particular element of single-channel floating-point matrix.
611 
612 The function is a fast replacement for cvGetReal2D in the case of single-channel floating-point
613 matrices. It is faster because it is inline, it does fewer checks for array type and array element
614 type, and it checks for the row and column ranges only in debug mode.
615 @param mat Input matrix
616 @param row The zero-based index of row
617 @param col The zero-based index of column
618  */
cvmGet(const CvMat * mat,int row,int col)619 CV_INLINE  double  cvmGet( const CvMat* mat, int row, int col )
620 {
621     int type;
622 
623     type = CV_MAT_TYPE(mat->type);
624     assert( (unsigned)row < (unsigned)mat->rows &&
625             (unsigned)col < (unsigned)mat->cols );
626 
627     if( type == CV_32FC1 )
628         return ((float*)(void*)(mat->data.ptr + (size_t)mat->step*row))[col];
629     else
630     {
631         assert( type == CV_64FC1 );
632         return ((double*)(void*)(mat->data.ptr + (size_t)mat->step*row))[col];
633     }
634 }
635 
636 /** @brief Sets a specific element of a single-channel floating-point matrix.
637 
638 The function is a fast replacement for cvSetReal2D in the case of single-channel floating-point
639 matrices. It is faster because it is inline, it does fewer checks for array type and array element
640 type, and it checks for the row and column ranges only in debug mode.
641 @param mat The matrix
642 @param row The zero-based index of row
643 @param col The zero-based index of column
644 @param value The new value of the matrix element
645  */
cvmSet(CvMat * mat,int row,int col,double value)646 CV_INLINE  void  cvmSet( CvMat* mat, int row, int col, double value )
647 {
648     int type;
649     type = CV_MAT_TYPE(mat->type);
650     assert( (unsigned)row < (unsigned)mat->rows &&
651             (unsigned)col < (unsigned)mat->cols );
652 
653     if( type == CV_32FC1 )
654         ((float*)(void*)(mat->data.ptr + (size_t)mat->step*row))[col] = (float)value;
655     else
656     {
657         assert( type == CV_64FC1 );
658         ((double*)(void*)(mat->data.ptr + (size_t)mat->step*row))[col] = value;
659     }
660 }
661 
662 
cvIplDepth(int type)663 CV_INLINE int cvIplDepth( int type )
664 {
665     int depth = CV_MAT_DEPTH(type);
666     return CV_ELEM_SIZE1(depth)*8 | (depth == CV_8S || depth == CV_16S ||
667            depth == CV_32S ? IPL_DEPTH_SIGN : 0);
668 }
669 
670 
671 /****************************************************************************************\
672 *                       Multi-dimensional dense array (CvMatND)                          *
673 \****************************************************************************************/
674 
675 #define CV_MATND_MAGIC_VAL    0x42430000
676 #define CV_TYPE_NAME_MATND    "opencv-nd-matrix"
677 
678 #define CV_MAX_DIM            32
679 
680 #ifdef __cplusplus
681 typedef struct CvMatND CvMatND;
682 CV_EXPORTS CvMatND cvMatND(const cv::Mat& m);
683 #endif
684 
685 /**
686   @deprecated consider using cv::Mat instead
687   */
688 typedef struct
689 CvMatND
690 {
691     int type;
692     int dims;
693 
694     int* refcount;
695     int hdr_refcount;
696 
697     union
698     {
699         uchar* ptr;
700         float* fl;
701         double* db;
702         int* i;
703         short* s;
704     } data;
705 
706     struct
707     {
708         int size;
709         int step;
710     }
711     dim[CV_MAX_DIM];
712 
713 #if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
CvMatNDCvMatND714     CvMatND() {}
CvMatNDCvMatND715     CvMatND(const cv::Mat& m) { *this = cvMatND(m); }
716 #endif
717 }
718 CvMatND;
719 
720 
cvMatND()721 CV_INLINE CvMatND cvMatND()
722 {
723 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
724     CvMatND self = CV_STRUCT_INITIALIZER; return self;
725 #else
726     return CvMatND();
727 #endif
728 }
729 
730 #define CV_IS_MATND_HDR(mat) \
731     ((mat) != NULL && (((const CvMatND*)(mat))->type & CV_MAGIC_MASK) == CV_MATND_MAGIC_VAL)
732 
733 #define CV_IS_MATND(mat) \
734     (CV_IS_MATND_HDR(mat) && ((const CvMatND*)(mat))->data.ptr != NULL)
735 
736 
737 /****************************************************************************************\
738 *                      Multi-dimensional sparse array (CvSparseMat)                      *
739 \****************************************************************************************/
740 
741 #define CV_SPARSE_MAT_MAGIC_VAL    0x42440000
742 #define CV_TYPE_NAME_SPARSE_MAT    "opencv-sparse-matrix"
743 
744 struct CvSet;
745 
746 typedef struct CvSparseMat
747 {
748     int type;
749     int dims;
750     int* refcount;
751     int hdr_refcount;
752 
753     struct CvSet* heap;
754     void** hashtable;
755     int hashsize;
756     int valoffset;
757     int idxoffset;
758     int size[CV_MAX_DIM];
759 
760 #ifdef __cplusplus
761     CV_EXPORTS void copyToSparseMat(cv::SparseMat& m) const;
762 #endif
763 }
764 CvSparseMat;
765 
766 #ifdef __cplusplus
767 CV_EXPORTS CvSparseMat* cvCreateSparseMat(const cv::SparseMat& m);
768 #endif
769 
770 #define CV_IS_SPARSE_MAT_HDR(mat) \
771     ((mat) != NULL && \
772     (((const CvSparseMat*)(mat))->type & CV_MAGIC_MASK) == CV_SPARSE_MAT_MAGIC_VAL)
773 
774 #define CV_IS_SPARSE_MAT(mat) \
775     CV_IS_SPARSE_MAT_HDR(mat)
776 
777 /**************** iteration through a sparse array *****************/
778 
779 typedef struct CvSparseNode
780 {
781     unsigned hashval;
782     struct CvSparseNode* next;
783 }
784 CvSparseNode;
785 
786 typedef struct CvSparseMatIterator
787 {
788     CvSparseMat* mat;
789     CvSparseNode* node;
790     int curidx;
791 }
792 CvSparseMatIterator;
793 
794 #define CV_NODE_VAL(mat,node)   ((void*)((uchar*)(node) + (mat)->valoffset))
795 #define CV_NODE_IDX(mat,node)   ((int*)((uchar*)(node) + (mat)->idxoffset))
796 
797 /****************************************************************************************\
798 *                                         Histogram                                      *
799 \****************************************************************************************/
800 
801 typedef int CvHistType;
802 
803 #define CV_HIST_MAGIC_VAL     0x42450000
804 #define CV_HIST_UNIFORM_FLAG  (1 << 10)
805 
806 /** indicates whether bin ranges are set already or not */
807 #define CV_HIST_RANGES_FLAG   (1 << 11)
808 
809 #define CV_HIST_ARRAY         0
810 #define CV_HIST_SPARSE        1
811 #define CV_HIST_TREE          CV_HIST_SPARSE
812 
813 /** should be used as a parameter only,
814    it turns to CV_HIST_UNIFORM_FLAG of hist->type */
815 #define CV_HIST_UNIFORM       1
816 
817 typedef struct CvHistogram
818 {
819     int     type;
820     CvArr*  bins;
821     float   thresh[CV_MAX_DIM][2];  /**< For uniform histograms.                      */
822     float** thresh2;                /**< For non-uniform histograms.                  */
823     CvMatND mat;                    /**< Embedded matrix header for array histograms. */
824 }
825 CvHistogram;
826 
827 #define CV_IS_HIST( hist ) \
828     ((hist) != NULL  && \
829      (((CvHistogram*)(hist))->type & CV_MAGIC_MASK) == CV_HIST_MAGIC_VAL && \
830      (hist)->bins != NULL)
831 
832 #define CV_IS_UNIFORM_HIST( hist ) \
833     (((hist)->type & CV_HIST_UNIFORM_FLAG) != 0)
834 
835 #define CV_IS_SPARSE_HIST( hist ) \
836     CV_IS_SPARSE_MAT((hist)->bins)
837 
838 #define CV_HIST_HAS_RANGES( hist ) \
839     (((hist)->type & CV_HIST_RANGES_FLAG) != 0)
840 
841 /****************************************************************************************\
842 *                      Other supplementary data type definitions                         *
843 \****************************************************************************************/
844 
845 /*************************************** CvRect *****************************************/
846 /** @sa Rect_ */
847 typedef struct CvRect
848 {
849     int x;
850     int y;
851     int width;
852     int height;
853 
854 #ifdef CV__VALIDATE_UNUNITIALIZED_VARS
CvRectCvRect855     CvRect() __attribute__(( warning("Non-initialized variable") )) {};
CvRectCvRect856     template<typename _Tp> CvRect(const std::initializer_list<_Tp> list)
857     {
858         CV_Assert(list.size() == 0 || list.size() == 4);
859         x = y = width = height = 0;
860         if (list.size() == 4)
861         {
862             x = list.begin()[0]; y = list.begin()[1]; width = list.begin()[2]; height = list.begin()[3];
863         }
864     };
865 #elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
xCvRect866     CvRect(int _x = 0, int _y = 0, int w = 0, int h = 0): x(_x), y(_y), width(w), height(h) {}
867     template<typename _Tp>
CvRectCvRect868     CvRect(const cv::Rect_<_Tp>& r): x(cv::saturate_cast<int>(r.x)), y(cv::saturate_cast<int>(r.y)), width(cv::saturate_cast<int>(r.width)), height(cv::saturate_cast<int>(r.height)) {}
869 #endif
870 #ifdef __cplusplus
871     template<typename _Tp>
872     operator cv::Rect_<_Tp>() const { return cv::Rect_<_Tp>((_Tp)x, (_Tp)y, (_Tp)width, (_Tp)height); }
873 #endif
874 }
875 CvRect;
876 
877 /** constructs CvRect structure. */
cvRect(int x,int y,int width,int height)878 CV_INLINE  CvRect  cvRect( int x, int y, int width, int height )
879 {
880 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
881     CvRect r = {x, y, width, height};
882 #else
883     CvRect r(x, y , width, height);
884 #endif
885     return r;
886 }
887 #ifdef __cplusplus
cvRect(const cv::Rect & rc)888 CV_INLINE CvRect cvRect(const cv::Rect& rc) { return cvRect(rc.x, rc.y, rc.width, rc.height); }
889 #endif
890 
cvRectToROI(CvRect rect,int coi)891 CV_INLINE  IplROI  cvRectToROI( CvRect rect, int coi )
892 {
893     IplROI roi;
894     roi.xOffset = rect.x;
895     roi.yOffset = rect.y;
896     roi.width = rect.width;
897     roi.height = rect.height;
898     roi.coi = coi;
899 
900     return roi;
901 }
902 
903 
cvROIToRect(IplROI roi)904 CV_INLINE  CvRect  cvROIToRect( IplROI roi )
905 {
906     return cvRect( roi.xOffset, roi.yOffset, roi.width, roi.height );
907 }
908 
909 /*********************************** CvTermCriteria *************************************/
910 
911 #define CV_TERMCRIT_ITER    1
912 #define CV_TERMCRIT_NUMBER  CV_TERMCRIT_ITER
913 #define CV_TERMCRIT_EPS     2
914 
915 /** @sa TermCriteria
916  */
917 typedef struct CvTermCriteria
918 {
919     int    type;  /**< may be combination of
920                      CV_TERMCRIT_ITER
921                      CV_TERMCRIT_EPS */
922     int    max_iter;
923     double epsilon;
924 #if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
typeCvTermCriteria925     CvTermCriteria(int _type = 0, int _iter = 0, double _eps = 0) : type(_type), max_iter(_iter), epsilon(_eps)  {}
CvTermCriteriaCvTermCriteria926     CvTermCriteria(const cv::TermCriteria& t) : type(t.type), max_iter(t.maxCount), epsilon(t.epsilon)  {}
927 #endif
928 #ifdef __cplusplus
TermCriteriaCvTermCriteria929     operator cv::TermCriteria() const { return cv::TermCriteria(type, max_iter, epsilon); }
930 #endif
931 }
932 CvTermCriteria;
933 
cvTermCriteria(int type,int max_iter,double epsilon)934 CV_INLINE  CvTermCriteria  cvTermCriteria( int type, int max_iter, double epsilon )
935 {
936 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
937     CvTermCriteria t = { type, max_iter, (float)epsilon};
938 #else
939     CvTermCriteria t(type, max_iter, epsilon);
940 #endif
941     return t;
942 }
943 #ifdef __cplusplus
cvTermCriteria(const cv::TermCriteria & t)944 CV_INLINE CvTermCriteria cvTermCriteria(const cv::TermCriteria& t) { return cvTermCriteria(t.type, t.maxCount, t.epsilon); }
945 #endif
946 
947 
948 /******************************* CvPoint and variants ***********************************/
949 
950 typedef struct CvPoint
951 {
952     int x;
953     int y;
954 
955 #ifdef CV__VALIDATE_UNUNITIALIZED_VARS
CvPointCvPoint956     CvPoint() __attribute__(( warning("Non-initialized variable") )) {}
CvPointCvPoint957     template<typename _Tp> CvPoint(const std::initializer_list<_Tp> list)
958     {
959         CV_Assert(list.size() == 0 || list.size() == 2);
960         x = y = 0;
961         if (list.size() == 2)
962         {
963             x = list.begin()[0]; y = list.begin()[1];
964         }
965     };
966 #elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
xCvPoint967     CvPoint(int _x = 0, int _y = 0): x(_x), y(_y) {}
968     template<typename _Tp>
CvPointCvPoint969     CvPoint(const cv::Point_<_Tp>& pt): x((int)pt.x), y((int)pt.y) {}
970 #endif
971 #ifdef __cplusplus
972     template<typename _Tp>
973     operator cv::Point_<_Tp>() const { return cv::Point_<_Tp>(cv::saturate_cast<_Tp>(x), cv::saturate_cast<_Tp>(y)); }
974 #endif
975 }
976 CvPoint;
977 
978 /** constructs CvPoint structure. */
cvPoint(int x,int y)979 CV_INLINE  CvPoint  cvPoint( int x, int y )
980 {
981 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
982     CvPoint p = {x, y};
983 #else
984     CvPoint p(x, y);
985 #endif
986     return p;
987 }
988 #ifdef __cplusplus
cvPoint(const cv::Point & pt)989 CV_INLINE CvPoint cvPoint(const cv::Point& pt) { return cvPoint(pt.x, pt.y); }
990 #endif
991 
992 typedef struct CvPoint2D32f
993 {
994     float x;
995     float y;
996 
997 #ifdef CV__VALIDATE_UNUNITIALIZED_VARS
CvPoint2D32fCvPoint2D32f998     CvPoint2D32f() __attribute__(( warning("Non-initialized variable") )) {}
CvPoint2D32fCvPoint2D32f999     template<typename _Tp> CvPoint2D32f(const std::initializer_list<_Tp> list)
1000     {
1001         CV_Assert(list.size() == 0 || list.size() == 2);
1002         x = y = 0;
1003         if (list.size() == 2)
1004         {
1005             x = list.begin()[0]; y = list.begin()[1];
1006         }
1007     };
1008 #elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
xCvPoint2D32f1009     CvPoint2D32f(float _x = 0, float _y = 0): x(_x), y(_y) {}
1010     template<typename _Tp>
CvPoint2D32fCvPoint2D32f1011     CvPoint2D32f(const cv::Point_<_Tp>& pt): x((float)pt.x), y((float)pt.y) {}
1012 #endif
1013 #ifdef __cplusplus
1014     template<typename _Tp>
1015     operator cv::Point_<_Tp>() const { return cv::Point_<_Tp>(cv::saturate_cast<_Tp>(x), cv::saturate_cast<_Tp>(y)); }
1016 #endif
1017 }
1018 CvPoint2D32f;
1019 
1020 /** constructs CvPoint2D32f structure. */
cvPoint2D32f(double x,double y)1021 CV_INLINE  CvPoint2D32f  cvPoint2D32f( double x, double y )
1022 {
1023 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1024     CvPoint2D32f p = { (float)x, (float)y };
1025 #else
1026     CvPoint2D32f p((float)x, (float)y);
1027 #endif
1028     return p;
1029 }
1030 
1031 #ifdef __cplusplus
1032 template<typename _Tp>
cvPoint2D32f(const cv::Point_<_Tp> & pt)1033 CvPoint2D32f cvPoint2D32f(const cv::Point_<_Tp>& pt)
1034 {
1035 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1036     CvPoint2D32f p = { (float)pt.x, (float)pt.y };
1037 #else
1038     CvPoint2D32f p((float)pt.x, (float)pt.y);
1039 #endif
1040     return p;
1041 }
1042 #endif
1043 
1044 /** converts CvPoint to CvPoint2D32f. */
cvPointTo32f(CvPoint point)1045 CV_INLINE  CvPoint2D32f  cvPointTo32f( CvPoint point )
1046 {
1047     return cvPoint2D32f( (float)point.x, (float)point.y );
1048 }
1049 
1050 /** converts CvPoint2D32f to CvPoint. */
cvPointFrom32f(CvPoint2D32f point)1051 CV_INLINE  CvPoint  cvPointFrom32f( CvPoint2D32f point )
1052 {
1053 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1054     CvPoint ipt = { cvRound(point.x), cvRound(point.y) };
1055 #else
1056     CvPoint ipt(cvRound(point.x), cvRound(point.y));
1057 #endif
1058     return ipt;
1059 }
1060 
1061 
1062 typedef struct CvPoint3D32f
1063 {
1064     float x;
1065     float y;
1066     float z;
1067 
1068 #ifdef CV__VALIDATE_UNUNITIALIZED_VARS
CvPoint3D32fCvPoint3D32f1069     CvPoint3D32f() __attribute__(( warning("Non-initialized variable") )) {}
CvPoint3D32fCvPoint3D32f1070     template<typename _Tp> CvPoint3D32f(const std::initializer_list<_Tp> list)
1071     {
1072         CV_Assert(list.size() == 0 || list.size() == 3);
1073         x = y = z = 0;
1074         if (list.size() == 3)
1075         {
1076             x = list.begin()[0]; y = list.begin()[1]; z = list.begin()[2];
1077         }
1078     };
1079 #elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
xCvPoint3D32f1080     CvPoint3D32f(float _x = 0, float _y = 0, float _z = 0): x(_x), y(_y), z(_z) {}
1081     template<typename _Tp>
CvPoint3D32fCvPoint3D32f1082     CvPoint3D32f(const cv::Point3_<_Tp>& pt): x((float)pt.x), y((float)pt.y), z((float)pt.z) {}
1083 #endif
1084 #ifdef __cplusplus
1085     template<typename _Tp>
1086     operator cv::Point3_<_Tp>() const { return cv::Point3_<_Tp>(cv::saturate_cast<_Tp>(x), cv::saturate_cast<_Tp>(y), cv::saturate_cast<_Tp>(z)); }
1087 #endif
1088 }
1089 CvPoint3D32f;
1090 
1091 /** constructs CvPoint3D32f structure. */
cvPoint3D32f(double x,double y,double z)1092 CV_INLINE  CvPoint3D32f  cvPoint3D32f( double x, double y, double z )
1093 {
1094 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1095     CvPoint3D32f p = { (float)x, (float)y, (float)z };
1096 #else
1097     CvPoint3D32f p((float)x, (float)y, (float)z);
1098 #endif
1099     return p;
1100 }
1101 
1102 #ifdef __cplusplus
1103 template<typename _Tp>
cvPoint3D32f(const cv::Point3_<_Tp> & pt)1104 CvPoint3D32f cvPoint3D32f(const cv::Point3_<_Tp>& pt)
1105 {
1106 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1107     CvPoint3D32f p  = { (float)pt.x, (float)pt.y, (float)pt.z };
1108 #else
1109     CvPoint3D32f p((float)pt.x, (float)pt.y, (float)pt.z);
1110 #endif
1111     return p;
1112 }
1113 #endif
1114 
1115 
1116 typedef struct CvPoint2D64f
1117 {
1118     double x;
1119     double y;
1120 #ifdef CV__VALIDATE_UNUNITIALIZED_VARS
CvPoint2D64fCvPoint2D64f1121     CvPoint2D64f() __attribute__(( warning("Non-initialized variable") )) {}
CvPoint2D64fCvPoint2D64f1122     template<typename _Tp> CvPoint2D64f(const std::initializer_list<_Tp> list)
1123     {
1124         CV_Assert(list.size() == 0 || list.size() == 2);
1125         x = y = 0;
1126         if (list.size() == 2)
1127         {
1128             x = list.begin()[0]; y = list.begin()[1];
1129         }
1130     };
1131 #endif
1132 }
1133 CvPoint2D64f;
1134 
1135 /** constructs CvPoint2D64f structure.*/
cvPoint2D64f(double x,double y)1136 CV_INLINE  CvPoint2D64f  cvPoint2D64f( double x, double y )
1137 {
1138     CvPoint2D64f p = { x, y };
1139     return p;
1140 }
1141 
1142 
1143 typedef struct CvPoint3D64f
1144 {
1145     double x;
1146     double y;
1147     double z;
1148 #ifdef CV__VALIDATE_UNUNITIALIZED_VARS
CvPoint3D64fCvPoint3D64f1149     CvPoint3D64f() __attribute__(( warning("Non-initialized variable") )) {}
CvPoint3D64fCvPoint3D64f1150     template<typename _Tp> CvPoint3D64f(const std::initializer_list<_Tp> list)
1151     {
1152         CV_Assert(list.size() == 0 || list.size() == 3);
1153         x = y = z = 0;
1154         if (list.size() == 3)
1155         {
1156             x = list.begin()[0]; y = list.begin()[1]; z = list.begin()[2];
1157         }
1158     };
1159 #endif
1160 }
1161 CvPoint3D64f;
1162 
1163 /** constructs CvPoint3D64f structure. */
cvPoint3D64f(double x,double y,double z)1164 CV_INLINE  CvPoint3D64f  cvPoint3D64f( double x, double y, double z )
1165 {
1166     CvPoint3D64f p = { x, y, z };
1167     return p;
1168 }
1169 
1170 
1171 /******************************** CvSize's & CvBox **************************************/
1172 
1173 typedef struct CvSize
1174 {
1175     int width;
1176     int height;
1177 
1178 #ifdef CV__VALIDATE_UNUNITIALIZED_VARS
CvSizeCvSize1179     CvSize() __attribute__(( warning("Non-initialized variable") )) {}
CvSizeCvSize1180     template<typename _Tp> CvSize(const std::initializer_list<_Tp> list)
1181     {
1182         CV_Assert(list.size() == 0 || list.size() == 2);
1183         width = 0; height = 0;
1184         if (list.size() == 2)
1185         {
1186             width = list.begin()[0]; height = list.begin()[1];
1187         }
1188     };
1189 #elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
widthCvSize1190     CvSize(int w = 0, int h = 0): width(w), height(h) {}
1191     template<typename _Tp>
CvSizeCvSize1192     CvSize(const cv::Size_<_Tp>& sz): width(cv::saturate_cast<int>(sz.width)), height(cv::saturate_cast<int>(sz.height)) {}
1193 #endif
1194 #ifdef __cplusplus
1195     template<typename _Tp>
1196     operator cv::Size_<_Tp>() const { return cv::Size_<_Tp>(cv::saturate_cast<_Tp>(width), cv::saturate_cast<_Tp>(height)); }
1197 #endif
1198 }
1199 CvSize;
1200 
1201 /** constructs CvSize structure. */
cvSize(int width,int height)1202 CV_INLINE  CvSize  cvSize( int width, int height )
1203 {
1204 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1205     CvSize s = { width, height };
1206 #else
1207     CvSize s(width, height);
1208 #endif
1209     return s;
1210 }
1211 
1212 #ifdef __cplusplus
cvSize(const cv::Size & sz)1213 CV_INLINE CvSize cvSize(const cv::Size& sz)
1214 {
1215 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1216     CvSize s = { sz.width, sz.height };
1217 #else
1218     CvSize s(sz.width, sz.height);
1219 #endif
1220     return s;
1221 }
1222 #endif
1223 
1224 typedef struct CvSize2D32f
1225 {
1226     float width;
1227     float height;
1228 
1229 #ifdef CV__VALIDATE_UNUNITIALIZED_VARS
CvSize2D32fCvSize2D32f1230     CvSize2D32f() __attribute__(( warning("Non-initialized variable") )) {}
CvSize2D32fCvSize2D32f1231     template<typename _Tp> CvSize2D32f(const std::initializer_list<_Tp> list)
1232     {
1233         CV_Assert(list.size() == 0 || list.size() == 2);
1234         width = 0; height = 0;
1235         if (list.size() == 2)
1236         {
1237             width = list.begin()[0]; height = list.begin()[1];
1238         }
1239     };
1240 #elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
widthCvSize2D32f1241     CvSize2D32f(float w = 0, float h = 0): width(w), height(h) {}
1242     template<typename _Tp>
CvSize2D32fCvSize2D32f1243     CvSize2D32f(const cv::Size_<_Tp>& sz): width(cv::saturate_cast<float>(sz.width)), height(cv::saturate_cast<float>(sz.height)) {}
1244 #endif
1245 #ifdef __cplusplus
1246     template<typename _Tp>
1247     operator cv::Size_<_Tp>() const { return cv::Size_<_Tp>(cv::saturate_cast<_Tp>(width), cv::saturate_cast<_Tp>(height)); }
1248 #endif
1249 }
1250 CvSize2D32f;
1251 
1252 /** constructs CvSize2D32f structure. */
cvSize2D32f(double width,double height)1253 CV_INLINE  CvSize2D32f  cvSize2D32f( double width, double height )
1254 {
1255 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1256     CvSize2D32f s = { (float)width, (float)height };
1257 #else
1258     CvSize2D32f s((float)width, (float)height);
1259 #endif
1260     return s;
1261 }
1262 #ifdef __cplusplus
1263 template<typename _Tp>
cvSize2D32f(const cv::Size_<_Tp> & sz)1264 CvSize2D32f cvSize2D32f(const cv::Size_<_Tp>& sz)
1265 {
1266 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1267     CvSize2D32f s = { (float)sz.width, (float)sz.height };
1268 #else
1269     CvSize2D32f s((float)sz.width, (float)sz.height);
1270 #endif
1271     return s;
1272 }
1273 #endif
1274 
1275 /** @sa RotatedRect
1276  */
1277 typedef struct CvBox2D
1278 {
1279     CvPoint2D32f center;  /**< Center of the box.                          */
1280     CvSize2D32f  size;    /**< Box width and length.                       */
1281     float angle;          /**< Angle between the horizontal axis           */
1282                           /**< and the first side (i.e. length) in degrees */
1283 
1284 #if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
centerCvBox2D1285     CvBox2D(CvPoint2D32f c = CvPoint2D32f(), CvSize2D32f s = CvSize2D32f(), float a = 0) : center(c), size(s), angle(a) {}
CvBox2DCvBox2D1286     CvBox2D(const cv::RotatedRect& rr) : center(rr.center), size(rr.size), angle(rr.angle) {}
1287 #endif
1288 #ifdef __cplusplus
RotatedRectCvBox2D1289     operator cv::RotatedRect() const { return cv::RotatedRect(center, size, angle); }
1290 #endif
1291 }
1292 CvBox2D;
1293 
1294 
1295 #ifdef __cplusplus
1296 CV_INLINE CvBox2D cvBox2D(CvPoint2D32f c = CvPoint2D32f(), CvSize2D32f s = CvSize2D32f(), float a = 0)
1297 {
1298     CvBox2D self;
1299     self.center = c;
1300     self.size = s;
1301     self.angle = a;
1302     return self;
1303 }
cvBox2D(const cv::RotatedRect & rr)1304 CV_INLINE CvBox2D cvBox2D(const cv::RotatedRect& rr)
1305 {
1306     CvBox2D self;
1307     self.center = cvPoint2D32f(rr.center);
1308     self.size = cvSize2D32f(rr.size);
1309     self.angle = rr.angle;
1310     return self;
1311 }
1312 #endif
1313 
1314 
1315 /** Line iterator state: */
1316 typedef struct CvLineIterator
1317 {
1318     /** Pointer to the current point: */
1319     uchar* ptr;
1320 
1321     /* Bresenham algorithm state: */
1322     int  err;
1323     int  plus_delta;
1324     int  minus_delta;
1325     int  plus_step;
1326     int  minus_step;
1327 }
1328 CvLineIterator;
1329 
1330 
1331 
1332 /************************************* CvSlice ******************************************/
1333 #define CV_WHOLE_SEQ_END_INDEX 0x3fffffff
1334 #define CV_WHOLE_SEQ  cvSlice(0, CV_WHOLE_SEQ_END_INDEX)
1335 
1336 typedef struct CvSlice
1337 {
1338     int  start_index, end_index;
1339 
1340 #ifdef CV__VALIDATE_UNUNITIALIZED_VARS
CvSliceCvSlice1341     CvSlice() __attribute__(( warning("Non-initialized variable") )) {}
CvSliceCvSlice1342     template<typename _Tp> CvSlice(const std::initializer_list<_Tp> list)
1343     {
1344         CV_Assert(list.size() == 0 || list.size() == 2);
1345         start_index = end_index = 0;
1346         if (list.size() == 2)
1347         {
1348             start_index = list.begin()[0]; end_index = list.begin()[1];
1349         }
1350     };
1351 #endif
1352 #if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) && !defined(__CUDACC__)
start_indexCvSlice1353     CvSlice(int start = 0, int end = 0) : start_index(start), end_index(end) {}
CvSliceCvSlice1354     CvSlice(const cv::Range& r) { *this = (r.start != INT_MIN && r.end != INT_MAX) ? CvSlice(r.start, r.end) : CvSlice(0, CV_WHOLE_SEQ_END_INDEX); }
RangeCvSlice1355     operator cv::Range() const { return (start_index == 0 && end_index == CV_WHOLE_SEQ_END_INDEX ) ? cv::Range::all() : cv::Range(start_index, end_index); }
1356 #endif
1357 }
1358 CvSlice;
1359 
cvSlice(int start,int end)1360 CV_INLINE  CvSlice  cvSlice( int start, int end )
1361 {
1362 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) && !defined(__CUDACC__))
1363     CvSlice slice = { start, end };
1364 #else
1365     CvSlice slice(start, end);
1366 #endif
1367     return slice;
1368 }
1369 
1370 #if defined(__cplusplus)
cvSlice(const cv::Range & r)1371 CV_INLINE  CvSlice  cvSlice(const cv::Range& r)
1372 {
1373     CvSlice slice = (r.start != INT_MIN && r.end != INT_MAX) ? cvSlice(r.start, r.end) : cvSlice(0, CV_WHOLE_SEQ_END_INDEX);
1374     return slice;
1375 }
1376 #endif
1377 
1378 
1379 /************************************* CvScalar *****************************************/
1380 /** @sa Scalar_
1381  */
1382 typedef struct CvScalar
1383 {
1384     double val[4];
1385 
1386 #ifdef CV__VALIDATE_UNUNITIALIZED_VARS
CvScalarCvScalar1387     CvScalar() __attribute__(( warning("Non-initialized variable") )) {}
CvScalarCvScalar1388     CvScalar(const std::initializer_list<double> list)
1389     {
1390         CV_Assert(list.size() == 0 || list.size() == 4);
1391         val[0] = val[1] = val[2] = val[3] = 0;
1392         if (list.size() == 4)
1393         {
1394             val[0] = list.begin()[0]; val[1] = list.begin()[1]; val[2] = list.begin()[2]; val[3] = list.begin()[3];
1395         }
1396     };
1397 #elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)
CvScalarCvScalar1398     CvScalar() {}
1399     CvScalar(double d0, double d1 = 0, double d2 = 0, double d3 = 0) { val[0] = d0; val[1] = d1; val[2] = d2; val[3] = d3; }
1400     template<typename _Tp>
CvScalarCvScalar1401     CvScalar(const cv::Scalar_<_Tp>& s) { val[0] = s.val[0]; val[1] = s.val[1]; val[2] = s.val[2]; val[3] = s.val[3]; }
1402     template<typename _Tp, int cn>
CvScalarCvScalar1403     CvScalar(const cv::Vec<_Tp, cn>& v)
1404     {
1405         int i;
1406         for( i = 0; i < (cn < 4 ? cn : 4); i++ ) val[i] = v.val[i];
1407         for( ; i < 4; i++ ) val[i] = 0;
1408     }
1409 #endif
1410 #ifdef __cplusplus
1411     template<typename _Tp>
1412     operator cv::Scalar_<_Tp>() const { return cv::Scalar_<_Tp>(cv::saturate_cast<_Tp>(val[0]), cv::saturate_cast<_Tp>(val[1]), cv::saturate_cast<_Tp>(val[2]), cv::saturate_cast<_Tp>(val[3])); }
1413 #endif
1414 }
1415 CvScalar;
1416 
1417 CV_INLINE  CvScalar  cvScalar( double val0, double val1 CV_DEFAULT(0),
1418                                double val2 CV_DEFAULT(0), double val3 CV_DEFAULT(0))
1419 {
1420 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1421     CvScalar scalar = CV_STRUCT_INITIALIZER;
1422 #else
1423     CvScalar scalar;
1424 #endif
1425     scalar.val[0] = val0; scalar.val[1] = val1;
1426     scalar.val[2] = val2; scalar.val[3] = val3;
1427     return scalar;
1428 }
1429 
1430 #ifdef __cplusplus
cvScalar()1431 CV_INLINE CvScalar cvScalar()
1432 {
1433 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1434     CvScalar scalar = CV_STRUCT_INITIALIZER;
1435 #else
1436     CvScalar scalar;
1437 #endif
1438     scalar.val[0] = scalar.val[1] = scalar.val[2] = scalar.val[3] = 0;
1439     return scalar;
1440 }
cvScalar(const cv::Scalar & s)1441 CV_INLINE CvScalar cvScalar(const cv::Scalar& s)
1442 {
1443 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1444     CvScalar scalar = CV_STRUCT_INITIALIZER;
1445 #else
1446     CvScalar scalar;
1447 #endif
1448     scalar.val[0] = s.val[0];
1449     scalar.val[1] = s.val[1];
1450     scalar.val[2] = s.val[2];
1451     scalar.val[3] = s.val[3];
1452     return scalar;
1453 }
1454 #endif
1455 
cvRealScalar(double val0)1456 CV_INLINE  CvScalar  cvRealScalar( double val0 )
1457 {
1458 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1459     CvScalar scalar = CV_STRUCT_INITIALIZER;
1460 #else
1461     CvScalar scalar;
1462 #endif
1463     scalar.val[0] = val0;
1464     scalar.val[1] = scalar.val[2] = scalar.val[3] = 0;
1465     return scalar;
1466 }
1467 
cvScalarAll(double val0123)1468 CV_INLINE  CvScalar  cvScalarAll( double val0123 )
1469 {
1470 #if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus))
1471     CvScalar scalar = CV_STRUCT_INITIALIZER;
1472 #else
1473     CvScalar scalar;
1474 #endif
1475     scalar.val[0] = val0123;
1476     scalar.val[1] = val0123;
1477     scalar.val[2] = val0123;
1478     scalar.val[3] = val0123;
1479     return scalar;
1480 }
1481 
1482 /****************************************************************************************\
1483 *                                   Dynamic Data structures                              *
1484 \****************************************************************************************/
1485 
1486 /******************************** Memory storage ****************************************/
1487 
1488 typedef struct CvMemBlock
1489 {
1490     struct CvMemBlock*  prev;
1491     struct CvMemBlock*  next;
1492 }
1493 CvMemBlock;
1494 
1495 #define CV_STORAGE_MAGIC_VAL    0x42890000
1496 
1497 typedef struct CvMemStorage
1498 {
1499     int signature;
1500     CvMemBlock* bottom;           /**< First allocated block.                   */
1501     CvMemBlock* top;              /**< Current memory block - top of the stack. */
1502     struct  CvMemStorage* parent; /**< We get new blocks from parent as needed. */
1503     int block_size;               /**< Block size.                              */
1504     int free_space;               /**< Remaining free space in current block.   */
1505 }
1506 CvMemStorage;
1507 
1508 #define CV_IS_STORAGE(storage)  \
1509     ((storage) != NULL &&       \
1510     (((CvMemStorage*)(storage))->signature & CV_MAGIC_MASK) == CV_STORAGE_MAGIC_VAL)
1511 
1512 
1513 typedef struct CvMemStoragePos
1514 {
1515     CvMemBlock* top;
1516     int free_space;
1517 }
1518 CvMemStoragePos;
1519 
1520 
1521 /*********************************** Sequence *******************************************/
1522 
1523 typedef struct CvSeqBlock
1524 {
1525     struct CvSeqBlock*  prev; /**< Previous sequence block.                   */
1526     struct CvSeqBlock*  next; /**< Next sequence block.                       */
1527     int    start_index;       /**< Index of the first element in the block +  */
1528                               /**< sequence->first->start_index.              */
1529     int    count;             /**< Number of elements in the block.           */
1530     schar* data;              /**< Pointer to the first element of the block. */
1531 }
1532 CvSeqBlock;
1533 
1534 
1535 #define CV_TREE_NODE_FIELDS(node_type)                               \
1536     int       flags;             /**< Miscellaneous flags.     */      \
1537     int       header_size;       /**< Size of sequence header. */      \
1538     struct    node_type* h_prev; /**< Previous sequence.       */      \
1539     struct    node_type* h_next; /**< Next sequence.           */      \
1540     struct    node_type* v_prev; /**< 2nd previous sequence.   */      \
1541     struct    node_type* v_next  /**< 2nd next sequence.       */
1542 
1543 /**
1544    Read/Write sequence.
1545    Elements can be dynamically inserted to or deleted from the sequence.
1546 */
1547 #define CV_SEQUENCE_FIELDS()                                              \
1548     CV_TREE_NODE_FIELDS(CvSeq);                                           \
1549     int       total;          /**< Total number of elements.            */  \
1550     int       elem_size;      /**< Size of sequence element in bytes.   */  \
1551     schar*    block_max;      /**< Maximal bound of the last block.     */  \
1552     schar*    ptr;            /**< Current write pointer.               */  \
1553     int       delta_elems;    /**< Grow seq this many at a time.        */  \
1554     CvMemStorage* storage;    /**< Where the seq is stored.             */  \
1555     CvSeqBlock* free_blocks;  /**< Free blocks list.                    */  \
1556     CvSeqBlock* first;        /**< Pointer to the first sequence block. */
1557 
1558 typedef struct CvSeq
1559 {
1560     CV_SEQUENCE_FIELDS()
1561 }
1562 CvSeq;
1563 
1564 #define CV_TYPE_NAME_SEQ             "opencv-sequence"
1565 #define CV_TYPE_NAME_SEQ_TREE        "opencv-sequence-tree"
1566 
1567 /*************************************** Set ********************************************/
1568 /** @brief Set
1569   Order is not preserved. There can be gaps between sequence elements.
1570   After the element has been inserted it stays in the same place all the time.
1571   The MSB(most-significant or sign bit) of the first field (flags) is 0 iff the element exists.
1572 */
1573 #define CV_SET_ELEM_FIELDS(elem_type)   \
1574     int  flags;                         \
1575     struct elem_type* next_free;
1576 
1577 typedef struct CvSetElem
1578 {
1579     CV_SET_ELEM_FIELDS(CvSetElem)
1580 }
1581 CvSetElem;
1582 
1583 #define CV_SET_FIELDS()      \
1584     CV_SEQUENCE_FIELDS()     \
1585     CvSetElem* free_elems;   \
1586     int active_count;
1587 
1588 typedef struct CvSet
1589 {
1590     CV_SET_FIELDS()
1591 }
1592 CvSet;
1593 
1594 
1595 #define CV_SET_ELEM_IDX_MASK   ((1 << 26) - 1)
1596 #define CV_SET_ELEM_FREE_FLAG  (1 << (sizeof(int)*8-1))
1597 
1598 /** Checks whether the element pointed by ptr belongs to a set or not */
1599 #define CV_IS_SET_ELEM( ptr )  (((CvSetElem*)(ptr))->flags >= 0)
1600 
1601 /************************************* Graph ********************************************/
1602 
1603 /** @name Graph
1604 
1605 We represent a graph as a set of vertices. Vertices contain their adjacency lists (more exactly,
1606 pointers to first incoming or outcoming edge (or 0 if isolated vertex)). Edges are stored in
1607 another set. There is a singly-linked list of incoming/outcoming edges for each vertex.
1608 
1609 Each edge consists of:
1610 
1611 - Two pointers to the starting and ending vertices (vtx[0] and vtx[1] respectively).
1612 
1613     A graph may be oriented or not. In the latter case, edges between vertex i to vertex j are not
1614 distinguished during search operations.
1615 
1616 - Two pointers to next edges for the starting and ending vertices, where next[0] points to the
1617 next edge in the vtx[0] adjacency list and next[1] points to the next edge in the vtx[1]
1618 adjacency list.
1619 
1620 @see CvGraphEdge, CvGraphVtx, CvGraphVtx2D, CvGraph
1621 @{
1622 */
1623 #define CV_GRAPH_EDGE_FIELDS()      \
1624     int flags;                      \
1625     float weight;                   \
1626     struct CvGraphEdge* next[2];    \
1627     struct CvGraphVtx* vtx[2];
1628 
1629 
1630 #define CV_GRAPH_VERTEX_FIELDS()    \
1631     int flags;                      \
1632     struct CvGraphEdge* first;
1633 
1634 
1635 typedef struct CvGraphEdge
1636 {
1637     CV_GRAPH_EDGE_FIELDS()
1638 }
1639 CvGraphEdge;
1640 
1641 typedef struct CvGraphVtx
1642 {
1643     CV_GRAPH_VERTEX_FIELDS()
1644 }
1645 CvGraphVtx;
1646 
1647 typedef struct CvGraphVtx2D
1648 {
1649     CV_GRAPH_VERTEX_FIELDS()
1650     CvPoint2D32f* ptr;
1651 }
1652 CvGraphVtx2D;
1653 
1654 /**
1655    Graph is "derived" from the set (this is set a of vertices)
1656    and includes another set (edges)
1657 */
1658 #define  CV_GRAPH_FIELDS()   \
1659     CV_SET_FIELDS()          \
1660     CvSet* edges;
1661 
1662 typedef struct CvGraph
1663 {
1664     CV_GRAPH_FIELDS()
1665 }
1666 CvGraph;
1667 
1668 #define CV_TYPE_NAME_GRAPH "opencv-graph"
1669 
1670 /** @} */
1671 
1672 /*********************************** Chain/Contour *************************************/
1673 
1674 typedef struct CvChain
1675 {
1676     CV_SEQUENCE_FIELDS()
1677     CvPoint  origin;
1678 }
1679 CvChain;
1680 
1681 #define CV_CONTOUR_FIELDS()  \
1682     CV_SEQUENCE_FIELDS()     \
1683     CvRect rect;             \
1684     int color;               \
1685     int reserved[3];
1686 
1687 typedef struct CvContour
1688 {
1689     CV_CONTOUR_FIELDS()
1690 }
1691 CvContour;
1692 
1693 typedef CvContour CvPoint2DSeq;
1694 
1695 /****************************************************************************************\
1696 *                                    Sequence types                                      *
1697 \****************************************************************************************/
1698 
1699 #define CV_SEQ_MAGIC_VAL             0x42990000
1700 
1701 #define CV_IS_SEQ(seq) \
1702     ((seq) != NULL && (((CvSeq*)(seq))->flags & CV_MAGIC_MASK) == CV_SEQ_MAGIC_VAL)
1703 
1704 #define CV_SET_MAGIC_VAL             0x42980000
1705 #define CV_IS_SET(set) \
1706     ((set) != NULL && (((CvSeq*)(set))->flags & CV_MAGIC_MASK) == CV_SET_MAGIC_VAL)
1707 
1708 #define CV_SEQ_ELTYPE_BITS           12
1709 #define CV_SEQ_ELTYPE_MASK           ((1 << CV_SEQ_ELTYPE_BITS) - 1)
1710 
1711 #define CV_SEQ_ELTYPE_POINT          CV_32SC2  /**< (x,y) */
1712 #define CV_SEQ_ELTYPE_CODE           CV_8UC1   /**< freeman code: 0..7 */
1713 #define CV_SEQ_ELTYPE_GENERIC        0
1714 #define CV_SEQ_ELTYPE_PTR            CV_MAKE_TYPE(CV_8U, 8 /*sizeof(void*)*/)
1715 #define CV_SEQ_ELTYPE_PPOINT         CV_SEQ_ELTYPE_PTR  /**< &(x,y) */
1716 #define CV_SEQ_ELTYPE_INDEX          CV_32SC1  /**< #(x,y) */
1717 #define CV_SEQ_ELTYPE_GRAPH_EDGE     0  /**< &next_o, &next_d, &vtx_o, &vtx_d */
1718 #define CV_SEQ_ELTYPE_GRAPH_VERTEX   0  /**< first_edge, &(x,y) */
1719 #define CV_SEQ_ELTYPE_TRIAN_ATR      0  /**< vertex of the binary tree   */
1720 #define CV_SEQ_ELTYPE_CONNECTED_COMP 0  /**< connected component  */
1721 #define CV_SEQ_ELTYPE_POINT3D        CV_32FC3  /**< (x,y,z)  */
1722 
1723 #define CV_SEQ_KIND_BITS        2
1724 #define CV_SEQ_KIND_MASK        (((1 << CV_SEQ_KIND_BITS) - 1)<<CV_SEQ_ELTYPE_BITS)
1725 
1726 /** types of sequences */
1727 #define CV_SEQ_KIND_GENERIC     (0 << CV_SEQ_ELTYPE_BITS)
1728 #define CV_SEQ_KIND_CURVE       (1 << CV_SEQ_ELTYPE_BITS)
1729 #define CV_SEQ_KIND_BIN_TREE    (2 << CV_SEQ_ELTYPE_BITS)
1730 
1731 /** types of sparse sequences (sets) */
1732 #define CV_SEQ_KIND_GRAPH       (1 << CV_SEQ_ELTYPE_BITS)
1733 #define CV_SEQ_KIND_SUBDIV2D    (2 << CV_SEQ_ELTYPE_BITS)
1734 
1735 #define CV_SEQ_FLAG_SHIFT       (CV_SEQ_KIND_BITS + CV_SEQ_ELTYPE_BITS)
1736 
1737 /** flags for curves */
1738 #define CV_SEQ_FLAG_CLOSED     (1 << CV_SEQ_FLAG_SHIFT)
1739 #define CV_SEQ_FLAG_SIMPLE     (0 << CV_SEQ_FLAG_SHIFT)
1740 #define CV_SEQ_FLAG_CONVEX     (0 << CV_SEQ_FLAG_SHIFT)
1741 #define CV_SEQ_FLAG_HOLE       (2 << CV_SEQ_FLAG_SHIFT)
1742 
1743 /** flags for graphs */
1744 #define CV_GRAPH_FLAG_ORIENTED (1 << CV_SEQ_FLAG_SHIFT)
1745 
1746 #define CV_GRAPH               CV_SEQ_KIND_GRAPH
1747 #define CV_ORIENTED_GRAPH      (CV_SEQ_KIND_GRAPH|CV_GRAPH_FLAG_ORIENTED)
1748 
1749 /** point sets */
1750 #define CV_SEQ_POINT_SET       (CV_SEQ_KIND_GENERIC| CV_SEQ_ELTYPE_POINT)
1751 #define CV_SEQ_POINT3D_SET     (CV_SEQ_KIND_GENERIC| CV_SEQ_ELTYPE_POINT3D)
1752 #define CV_SEQ_POLYLINE        (CV_SEQ_KIND_CURVE  | CV_SEQ_ELTYPE_POINT)
1753 #define CV_SEQ_POLYGON         (CV_SEQ_FLAG_CLOSED | CV_SEQ_POLYLINE )
1754 #define CV_SEQ_CONTOUR         CV_SEQ_POLYGON
1755 #define CV_SEQ_SIMPLE_POLYGON  (CV_SEQ_FLAG_SIMPLE | CV_SEQ_POLYGON  )
1756 
1757 /** chain-coded curves */
1758 #define CV_SEQ_CHAIN           (CV_SEQ_KIND_CURVE  | CV_SEQ_ELTYPE_CODE)
1759 #define CV_SEQ_CHAIN_CONTOUR   (CV_SEQ_FLAG_CLOSED | CV_SEQ_CHAIN)
1760 
1761 /** binary tree for the contour */
1762 #define CV_SEQ_POLYGON_TREE    (CV_SEQ_KIND_BIN_TREE  | CV_SEQ_ELTYPE_TRIAN_ATR)
1763 
1764 /** sequence of the connected components */
1765 #define CV_SEQ_CONNECTED_COMP  (CV_SEQ_KIND_GENERIC  | CV_SEQ_ELTYPE_CONNECTED_COMP)
1766 
1767 /** sequence of the integer numbers */
1768 #define CV_SEQ_INDEX           (CV_SEQ_KIND_GENERIC  | CV_SEQ_ELTYPE_INDEX)
1769 
1770 #define CV_SEQ_ELTYPE( seq )   ((seq)->flags & CV_SEQ_ELTYPE_MASK)
1771 #define CV_SEQ_KIND( seq )     ((seq)->flags & CV_SEQ_KIND_MASK )
1772 
1773 /** flag checking */
1774 #define CV_IS_SEQ_INDEX( seq )      ((CV_SEQ_ELTYPE(seq) == CV_SEQ_ELTYPE_INDEX) && \
1775                                      (CV_SEQ_KIND(seq) == CV_SEQ_KIND_GENERIC))
1776 
1777 #define CV_IS_SEQ_CURVE( seq )      (CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE)
1778 #define CV_IS_SEQ_CLOSED( seq )     (((seq)->flags & CV_SEQ_FLAG_CLOSED) != 0)
1779 #define CV_IS_SEQ_CONVEX( seq )     0
1780 #define CV_IS_SEQ_HOLE( seq )       (((seq)->flags & CV_SEQ_FLAG_HOLE) != 0)
1781 #define CV_IS_SEQ_SIMPLE( seq )     1
1782 
1783 /** type checking macros */
1784 #define CV_IS_SEQ_POINT_SET( seq ) \
1785     ((CV_SEQ_ELTYPE(seq) == CV_32SC2 || CV_SEQ_ELTYPE(seq) == CV_32FC2))
1786 
1787 #define CV_IS_SEQ_POINT_SUBSET( seq ) \
1788     (CV_IS_SEQ_INDEX( seq ) || CV_SEQ_ELTYPE(seq) == CV_SEQ_ELTYPE_PPOINT)
1789 
1790 #define CV_IS_SEQ_POLYLINE( seq )   \
1791     (CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE && CV_IS_SEQ_POINT_SET(seq))
1792 
1793 #define CV_IS_SEQ_POLYGON( seq )   \
1794     (CV_IS_SEQ_POLYLINE(seq) && CV_IS_SEQ_CLOSED(seq))
1795 
1796 #define CV_IS_SEQ_CHAIN( seq )   \
1797     (CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE && (seq)->elem_size == 1)
1798 
1799 #define CV_IS_SEQ_CONTOUR( seq )   \
1800     (CV_IS_SEQ_CLOSED(seq) && (CV_IS_SEQ_POLYLINE(seq) || CV_IS_SEQ_CHAIN(seq)))
1801 
1802 #define CV_IS_SEQ_CHAIN_CONTOUR( seq ) \
1803     (CV_IS_SEQ_CHAIN( seq ) && CV_IS_SEQ_CLOSED( seq ))
1804 
1805 #define CV_IS_SEQ_POLYGON_TREE( seq ) \
1806     (CV_SEQ_ELTYPE (seq) ==  CV_SEQ_ELTYPE_TRIAN_ATR &&    \
1807     CV_SEQ_KIND( seq ) ==  CV_SEQ_KIND_BIN_TREE )
1808 
1809 #define CV_IS_GRAPH( seq )    \
1810     (CV_IS_SET(seq) && CV_SEQ_KIND((CvSet*)(seq)) == CV_SEQ_KIND_GRAPH)
1811 
1812 #define CV_IS_GRAPH_ORIENTED( seq )   \
1813     (((seq)->flags & CV_GRAPH_FLAG_ORIENTED) != 0)
1814 
1815 #define CV_IS_SUBDIV2D( seq )  \
1816     (CV_IS_SET(seq) && CV_SEQ_KIND((CvSet*)(seq)) == CV_SEQ_KIND_SUBDIV2D)
1817 
1818 /****************************************************************************************/
1819 /*                            Sequence writer & reader                                  */
1820 /****************************************************************************************/
1821 
1822 #define CV_SEQ_WRITER_FIELDS()                                     \
1823     int          header_size;                                      \
1824     CvSeq*       seq;        /**< the sequence written */            \
1825     CvSeqBlock*  block;      /**< current block */                   \
1826     schar*       ptr;        /**< pointer to free space */           \
1827     schar*       block_min;  /**< pointer to the beginning of block*/\
1828     schar*       block_max;  /**< pointer to the end of block */
1829 
1830 typedef struct CvSeqWriter
1831 {
1832     CV_SEQ_WRITER_FIELDS()
1833 }
1834 CvSeqWriter;
1835 
1836 
1837 #define CV_SEQ_READER_FIELDS()                                      \
1838     int          header_size;                                       \
1839     CvSeq*       seq;        /**< sequence, beign read */             \
1840     CvSeqBlock*  block;      /**< current block */                    \
1841     schar*       ptr;        /**< pointer to element be read next */  \
1842     schar*       block_min;  /**< pointer to the beginning of block */\
1843     schar*       block_max;  /**< pointer to the end of block */      \
1844     int          delta_index;/**< = seq->first->start_index   */      \
1845     schar*       prev_elem;  /**< pointer to previous element */
1846 
1847 typedef struct CvSeqReader
1848 {
1849     CV_SEQ_READER_FIELDS()
1850 }
1851 CvSeqReader;
1852 
1853 /****************************************************************************************/
1854 /*                                Operations on sequences                               */
1855 /****************************************************************************************/
1856 
1857 #define  CV_SEQ_ELEM( seq, elem_type, index )                    \
1858 /** assert gives some guarantee that <seq> parameter is valid */  \
1859 (   assert(sizeof((seq)->first[0]) == sizeof(CvSeqBlock) &&      \
1860     (seq)->elem_size == sizeof(elem_type)),                      \
1861     (elem_type*)((seq)->first && (unsigned)index <               \
1862     (unsigned)((seq)->first->count) ?                            \
1863     (seq)->first->data + (index) * sizeof(elem_type) :           \
1864     cvGetSeqElem( (CvSeq*)(seq), (index) )))
1865 #define CV_GET_SEQ_ELEM( elem_type, seq, index ) CV_SEQ_ELEM( (seq), elem_type, (index) )
1866 
1867 /** Add element to sequence: */
1868 #define CV_WRITE_SEQ_ELEM_VAR( elem_ptr, writer )     \
1869 {                                                     \
1870     if( (writer).ptr >= (writer).block_max )          \
1871     {                                                 \
1872         cvCreateSeqBlock( &writer);                   \
1873     }                                                 \
1874     memcpy((writer).ptr, elem_ptr, (writer).seq->elem_size);\
1875     (writer).ptr += (writer).seq->elem_size;          \
1876 }
1877 
1878 #define CV_WRITE_SEQ_ELEM( elem, writer )             \
1879 {                                                     \
1880     assert( (writer).seq->elem_size == sizeof(elem)); \
1881     if( (writer).ptr >= (writer).block_max )          \
1882     {                                                 \
1883         cvCreateSeqBlock( &writer);                   \
1884     }                                                 \
1885     assert( (writer).ptr <= (writer).block_max - sizeof(elem));\
1886     memcpy((writer).ptr, &(elem), sizeof(elem));      \
1887     (writer).ptr += sizeof(elem);                     \
1888 }
1889 
1890 
1891 /** Move reader position forward: */
1892 #define CV_NEXT_SEQ_ELEM( elem_size, reader )                 \
1893 {                                                             \
1894     if( ((reader).ptr += (elem_size)) >= (reader).block_max ) \
1895     {                                                         \
1896         cvChangeSeqBlock( &(reader), 1 );                     \
1897     }                                                         \
1898 }
1899 
1900 
1901 /** Move reader position backward: */
1902 #define CV_PREV_SEQ_ELEM( elem_size, reader )                \
1903 {                                                            \
1904     if( ((reader).ptr -= (elem_size)) < (reader).block_min ) \
1905     {                                                        \
1906         cvChangeSeqBlock( &(reader), -1 );                   \
1907     }                                                        \
1908 }
1909 
1910 /** Read element and move read position forward: */
1911 #define CV_READ_SEQ_ELEM( elem, reader )                       \
1912 {                                                              \
1913     assert( (reader).seq->elem_size == sizeof(elem));          \
1914     memcpy( &(elem), (reader).ptr, sizeof((elem)));            \
1915     CV_NEXT_SEQ_ELEM( sizeof(elem), reader )                   \
1916 }
1917 
1918 /** Read element and move read position backward: */
1919 #define CV_REV_READ_SEQ_ELEM( elem, reader )                     \
1920 {                                                                \
1921     assert( (reader).seq->elem_size == sizeof(elem));            \
1922     memcpy(&(elem), (reader).ptr, sizeof((elem)));               \
1923     CV_PREV_SEQ_ELEM( sizeof(elem), reader )                     \
1924 }
1925 
1926 
1927 #define CV_READ_CHAIN_POINT( _pt, reader )                              \
1928 {                                                                       \
1929     (_pt) = (reader).pt;                                                \
1930     if( (reader).ptr )                                                  \
1931     {                                                                   \
1932         CV_READ_SEQ_ELEM( (reader).code, (reader));                     \
1933         assert( ((reader).code & ~7) == 0 );                            \
1934         (reader).pt.x += (reader).deltas[(int)(reader).code][0];        \
1935         (reader).pt.y += (reader).deltas[(int)(reader).code][1];        \
1936     }                                                                   \
1937 }
1938 
1939 #define CV_CURRENT_POINT( reader )  (*((CvPoint*)((reader).ptr)))
1940 #define CV_PREV_POINT( reader )     (*((CvPoint*)((reader).prev_elem)))
1941 
1942 #define CV_READ_EDGE( pt1, pt2, reader )               \
1943 {                                                      \
1944     assert( sizeof(pt1) == sizeof(CvPoint) &&          \
1945             sizeof(pt2) == sizeof(CvPoint) &&          \
1946             reader.seq->elem_size == sizeof(CvPoint)); \
1947     (pt1) = CV_PREV_POINT( reader );                   \
1948     (pt2) = CV_CURRENT_POINT( reader );                \
1949     (reader).prev_elem = (reader).ptr;                 \
1950     CV_NEXT_SEQ_ELEM( sizeof(CvPoint), (reader));      \
1951 }
1952 
1953 /************ Graph macros ************/
1954 
1955 /** Return next graph edge for given vertex: */
1956 #define  CV_NEXT_GRAPH_EDGE( edge, vertex )                              \
1957      (assert((edge)->vtx[0] == (vertex) || (edge)->vtx[1] == (vertex)),  \
1958       (edge)->next[(edge)->vtx[1] == (vertex)])
1959 
1960 
1961 
1962 /****************************************************************************************\
1963 *             Data structures for persistence (a.k.a serialization) functionality        *
1964 \****************************************************************************************/
1965 
1966 #if 0
1967 
1968 /** "black box" file storage */
1969 typedef struct CvFileStorage CvFileStorage;
1970 
1971 /** Storage flags: */
1972 #define CV_STORAGE_READ          0
1973 #define CV_STORAGE_WRITE         1
1974 #define CV_STORAGE_WRITE_TEXT    CV_STORAGE_WRITE
1975 #define CV_STORAGE_WRITE_BINARY  CV_STORAGE_WRITE
1976 #define CV_STORAGE_APPEND        2
1977 #define CV_STORAGE_MEMORY        4
1978 #define CV_STORAGE_FORMAT_MASK   (7<<3)
1979 #define CV_STORAGE_FORMAT_AUTO   0
1980 #define CV_STORAGE_FORMAT_XML    8
1981 #define CV_STORAGE_FORMAT_YAML  16
1982 #define CV_STORAGE_FORMAT_JSON  24
1983 #define CV_STORAGE_BASE64       64
1984 #define CV_STORAGE_WRITE_BASE64  (CV_STORAGE_BASE64 | CV_STORAGE_WRITE)
1985 
1986 /** @brief List of attributes. :
1987 
1988 In the current implementation, attributes are used to pass extra parameters when writing user
1989 objects (see cvWrite). XML attributes inside tags are not supported, aside from the object type
1990 specification (type_id attribute).
1991 @see cvAttrList, cvAttrValue
1992  */
1993 typedef struct CvAttrList
1994 {
1995     const char** attr;         /**< NULL-terminated array of (attribute_name,attribute_value) pairs. */
1996     struct CvAttrList* next;   /**< Pointer to next chunk of the attributes list.                    */
1997 }
1998 CvAttrList;
1999 
2000 /** initializes CvAttrList structure */
2001 CV_INLINE CvAttrList cvAttrList( const char** attr CV_DEFAULT(NULL),
2002                                  CvAttrList* next CV_DEFAULT(NULL) )
2003 {
2004     CvAttrList l;
2005     l.attr = attr;
2006     l.next = next;
2007 
2008     return l;
2009 }
2010 
2011 struct CvTypeInfo;
2012 
2013 #define CV_NODE_NONE        0
2014 #define CV_NODE_INT         1
2015 #define CV_NODE_INTEGER     CV_NODE_INT
2016 #define CV_NODE_REAL        2
2017 #define CV_NODE_FLOAT       CV_NODE_REAL
2018 #define CV_NODE_STR         3
2019 #define CV_NODE_STRING      CV_NODE_STR
2020 #define CV_NODE_REF         4 /**< not used */
2021 #define CV_NODE_SEQ         5
2022 #define CV_NODE_MAP         6
2023 #define CV_NODE_TYPE_MASK   7
2024 
2025 #define CV_NODE_TYPE(flags)  ((flags) & CV_NODE_TYPE_MASK)
2026 
2027 /** file node flags */
2028 #define CV_NODE_FLOW        8 /**<Used only for writing structures in YAML format. */
2029 #define CV_NODE_USER        16
2030 #define CV_NODE_EMPTY       32
2031 #define CV_NODE_NAMED       64
2032 
2033 #define CV_NODE_IS_INT(flags)        (CV_NODE_TYPE(flags) == CV_NODE_INT)
2034 #define CV_NODE_IS_REAL(flags)       (CV_NODE_TYPE(flags) == CV_NODE_REAL)
2035 #define CV_NODE_IS_STRING(flags)     (CV_NODE_TYPE(flags) == CV_NODE_STRING)
2036 #define CV_NODE_IS_SEQ(flags)        (CV_NODE_TYPE(flags) == CV_NODE_SEQ)
2037 #define CV_NODE_IS_MAP(flags)        (CV_NODE_TYPE(flags) == CV_NODE_MAP)
2038 #define CV_NODE_IS_COLLECTION(flags) (CV_NODE_TYPE(flags) >= CV_NODE_SEQ)
2039 #define CV_NODE_IS_FLOW(flags)       (((flags) & CV_NODE_FLOW) != 0)
2040 #define CV_NODE_IS_EMPTY(flags)      (((flags) & CV_NODE_EMPTY) != 0)
2041 #define CV_NODE_IS_USER(flags)       (((flags) & CV_NODE_USER) != 0)
2042 #define CV_NODE_HAS_NAME(flags)      (((flags) & CV_NODE_NAMED) != 0)
2043 
2044 #define CV_NODE_SEQ_SIMPLE 256
2045 #define CV_NODE_SEQ_IS_SIMPLE(seq) (((seq)->flags & CV_NODE_SEQ_SIMPLE) != 0)
2046 
2047 typedef struct CvString
2048 {
2049     int len;
2050     char* ptr;
2051 }
2052 CvString;
2053 
2054 /** All the keys (names) of elements in the read file storage
2055    are stored in the hash to speed up the lookup operations: */
2056 typedef struct CvStringHashNode
2057 {
2058     unsigned hashval;
2059     CvString str;
2060     struct CvStringHashNode* next;
2061 }
2062 CvStringHashNode;
2063 
2064 typedef struct CvGenericHash CvFileNodeHash;
2065 
2066 /** Basic element of the file storage - scalar or collection: */
2067 typedef struct CvFileNode
2068 {
2069     int tag;
2070     struct CvTypeInfo* info; /**< type information
2071             (only for user-defined object, for others it is 0) */
2072     union
2073     {
2074         double f; /**< scalar floating-point number */
2075         int i;    /**< scalar integer number */
2076         CvString str; /**< text string */
2077         CvSeq* seq; /**< sequence (ordered collection of file nodes) */
2078         CvFileNodeHash* map; /**< map (collection of named file nodes) */
2079     } data;
2080 }
2081 CvFileNode;
2082 
2083 #ifdef __cplusplus
2084 extern "C" {
2085 #endif
2086 typedef int (CV_CDECL *CvIsInstanceFunc)( const void* struct_ptr );
2087 typedef void (CV_CDECL *CvReleaseFunc)( void** struct_dblptr );
2088 typedef void* (CV_CDECL *CvReadFunc)( CvFileStorage* storage, CvFileNode* node );
2089 typedef void (CV_CDECL *CvWriteFunc)( CvFileStorage* storage, const char* name,
2090                                       const void* struct_ptr, CvAttrList attributes );
2091 typedef void* (CV_CDECL *CvCloneFunc)( const void* struct_ptr );
2092 #ifdef __cplusplus
2093 }
2094 #endif
2095 
2096 /** @brief Type information
2097 
2098 The structure contains information about one of the standard or user-defined types. Instances of the
2099 type may or may not contain a pointer to the corresponding CvTypeInfo structure. In any case, there
2100 is a way to find the type info structure for a given object using the cvTypeOf function.
2101 Alternatively, type info can be found by type name using cvFindType, which is used when an object
2102 is read from file storage. The user can register a new type with cvRegisterType that adds the type
2103 information structure into the beginning of the type list. Thus, it is possible to create
2104 specialized types from generic standard types and override the basic methods.
2105  */
2106 typedef struct CvTypeInfo
2107 {
2108     int flags; /**< not used */
2109     int header_size; /**< sizeof(CvTypeInfo) */
2110     struct CvTypeInfo* prev; /**< previous registered type in the list */
2111     struct CvTypeInfo* next; /**< next registered type in the list */
2112     const char* type_name; /**< type name, written to file storage */
2113     CvIsInstanceFunc is_instance; /**< checks if the passed object belongs to the type */
2114     CvReleaseFunc release; /**< releases object (memory etc.) */
2115     CvReadFunc read; /**< reads object from file storage */
2116     CvWriteFunc write; /**< writes object to file storage */
2117     CvCloneFunc clone; /**< creates a copy of the object */
2118 }
2119 CvTypeInfo;
2120 #endif
2121 
2122 /** @} */
2123 
2124 #endif /*OPENCV_CORE_TYPES_H*/
2125 
2126 /* End of file. */
2127