]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/include/opencv/cxcore.h
Fix python bindings broken due to {cv,cxcore}.hpp
[opencv.git] / opencv / include / opencv / cxcore.h
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                           License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
15 // Third party copyrights are property of their respective owners.\r
16 //\r
17 // Redistribution and use in source and binary forms, with or without modification,\r
18 // are permitted provided that the following conditions are met:\r
19 //\r
20 //   * Redistribution's of source code must retain the above copyright notice,\r
21 //     this list of conditions and the following disclaimer.\r
22 //\r
23 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
24 //     this list of conditions and the following disclaimer in the documentation\r
25 //     and/or other materials provided with the distribution.\r
26 //\r
27 //   * The name of the copyright holders may not be used to endorse or promote products\r
28 //     derived from this software without specific prior written permission.\r
29 //\r
30 // This software is provided by the copyright holders and contributors "as is" and\r
31 // any express or implied warranties, including, but not limited to, the implied\r
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
33 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
34 // indirect, incidental, special, exemplary, or consequential damages\r
35 // (including, but not limited to, procurement of substitute goods or services;\r
36 // loss of use, data, or profits; or business interruption) however caused\r
37 // and on any theory of liability, whether in contract, strict liability,\r
38 // or tort (including negligence or otherwise) arising in any way out of\r
39 // the use of this software, even if advised of the possibility of such damage.\r
40 //\r
41 //M*/\r
42 \r
43 \r
44 #ifndef _CXCORE_H_\r
45 #define _CXCORE_H_\r
46 \r
47 #ifdef __IPL_H__\r
48 #define HAVE_IPL\r
49 #endif\r
50 \r
51 #ifndef SKIP_INCLUDES\r
52   #if defined HAVE_IPL && !defined __IPL_H__\r
53     #ifndef _INC_WINDOWS\r
54         #define CV_PRETEND_WINDOWS\r
55         #define _INC_WINDOWS\r
56         typedef struct tagBITMAPINFOHEADER BITMAPINFOHEADER;\r
57         typedef int BOOL;\r
58     #endif\r
59     #if defined WIN32 || defined WIN64\r
60       #include "ipl.h"\r
61     #else\r
62       #include "ipl/ipl.h"\r
63     #endif\r
64     #ifdef CV_PRETEND_WINDOWS\r
65         #undef _INC_WINDOWS\r
66     #endif\r
67   #endif\r
68 #endif // SKIP_INCLUDES\r
69 \r
70 #include "cxtypes.h"\r
71 #include "cxerror.h"\r
72 #include "cvver.h"\r
73 \r
74 #ifdef __cplusplus\r
75 extern "C" {\r
76 #endif\r
77 \r
78 /****************************************************************************************\\r
79 *          Array allocation, deallocation, initialization and access to elements         *\r
80 \****************************************************************************************/\r
81 \r
82 /* <malloc> wrapper.\r
83    If there is no enough memory, the function\r
84    (as well as other OpenCV functions that call cvAlloc)\r
85    raises an error. */\r
86 CVAPI(void*)  cvAlloc( size_t size );\r
87 \r
88 /* <free> wrapper.\r
89    Here and further all the memory releasing functions\r
90    (that all call cvFree) take double pointer in order to\r
91    to clear pointer to the data after releasing it.\r
92    Passing pointer to NULL pointer is Ok: nothing happens in this case\r
93 */\r
94 CVAPI(void)   cvFree_( void* ptr );\r
95 #define cvFree(ptr) (cvFree_(*(ptr)), *(ptr)=0)\r
96 \r
97 /* Allocates and initializes IplImage header */\r
98 CVAPI(IplImage*)  cvCreateImageHeader( CvSize size, int depth, int channels );\r
99 \r
100 /* Inializes IplImage header */\r
101 CVAPI(IplImage*) cvInitImageHeader( IplImage* image, CvSize size, int depth,\r
102                                    int channels, int origin CV_DEFAULT(0),\r
103                                    int align CV_DEFAULT(4));\r
104 \r
105 /* Creates IPL image (header and data) */\r
106 CVAPI(IplImage*)  cvCreateImage( CvSize size, int depth, int channels );\r
107 \r
108 /* Releases (i.e. deallocates) IPL image header */\r
109 CVAPI(void)  cvReleaseImageHeader( IplImage** image );\r
110 \r
111 /* Releases IPL image header and data */\r
112 CVAPI(void)  cvReleaseImage( IplImage** image );\r
113 \r
114 /* Creates a copy of IPL image (widthStep may differ) */\r
115 CVAPI(IplImage*) cvCloneImage( const IplImage* image );\r
116 \r
117 /* Sets a Channel Of Interest (only a few functions support COI) -\r
118    use cvCopy to extract the selected channel and/or put it back */\r
119 CVAPI(void)  cvSetImageCOI( IplImage* image, int coi );\r
120 \r
121 /* Retrieves image Channel Of Interest */\r
122 CVAPI(int)  cvGetImageCOI( const IplImage* image );\r
123 \r
124 /* Sets image ROI (region of interest) (COI is not changed) */\r
125 CVAPI(void)  cvSetImageROI( IplImage* image, CvRect rect );\r
126 \r
127 /* Resets image ROI and COI */\r
128 CVAPI(void)  cvResetImageROI( IplImage* image );\r
129 \r
130 /* Retrieves image ROI */\r
131 CVAPI(CvRect) cvGetImageROI( const IplImage* image );\r
132 \r
133 /* Allocates and initalizes CvMat header */\r
134 CVAPI(CvMat*)  cvCreateMatHeader( int rows, int cols, int type );\r
135 \r
136 #define CV_AUTOSTEP  0x7fffffff\r
137 \r
138 /* Initializes CvMat header */\r
139 CVAPI(CvMat*) cvInitMatHeader( CvMat* mat, int rows, int cols,\r
140                               int type, void* data CV_DEFAULT(NULL),\r
141                               int step CV_DEFAULT(CV_AUTOSTEP) );\r
142 \r
143 /* Allocates and initializes CvMat header and allocates data */\r
144 CVAPI(CvMat*)  cvCreateMat( int rows, int cols, int type );\r
145 \r
146 /* Releases CvMat header and deallocates matrix data\r
147    (reference counting is used for data) */\r
148 CVAPI(void)  cvReleaseMat( CvMat** mat );\r
149 \r
150 /* Decrements CvMat data reference counter and deallocates the data if\r
151    it reaches 0 */\r
152 CV_INLINE  void  cvDecRefData( CvArr* arr )\r
153 {\r
154     if( CV_IS_MAT( arr ))\r
155     {\r
156         CvMat* mat = (CvMat*)arr;\r
157         mat->data.ptr = NULL;\r
158         if( mat->refcount != NULL && --*mat->refcount == 0 )\r
159             cvFree( &mat->refcount );\r
160         mat->refcount = NULL;\r
161     }\r
162     else if( CV_IS_MATND( arr ))\r
163     {\r
164         CvMatND* mat = (CvMatND*)arr;\r
165         mat->data.ptr = NULL;\r
166         if( mat->refcount != NULL && --*mat->refcount == 0 )\r
167             cvFree( &mat->refcount );\r
168         mat->refcount = NULL;\r
169     }\r
170 }\r
171 \r
172 /* Increments CvMat data reference counter */\r
173 CV_INLINE  int  cvIncRefData( CvArr* arr )\r
174 {\r
175     int refcount = 0;\r
176     if( CV_IS_MAT( arr ))\r
177     {\r
178         CvMat* mat = (CvMat*)arr;\r
179         if( mat->refcount != NULL )\r
180             refcount = ++*mat->refcount;\r
181     }\r
182     else if( CV_IS_MATND( arr ))\r
183     {\r
184         CvMatND* mat = (CvMatND*)arr;\r
185         if( mat->refcount != NULL )\r
186             refcount = ++*mat->refcount;\r
187     }\r
188     return refcount;\r
189 }\r
190 \r
191 \r
192 /* Creates an exact copy of the input matrix (except, may be, step value) */\r
193 CVAPI(CvMat*) cvCloneMat( const CvMat* mat );\r
194 \r
195 \r
196 /* Makes a new matrix from <rect> subrectangle of input array.\r
197    No data is copied */\r
198 CVAPI(CvMat*) cvGetSubRect( const CvArr* arr, CvMat* submat, CvRect rect );\r
199 #define cvGetSubArr cvGetSubRect\r
200 \r
201 /* Selects row span of the input array: arr(start_row:delta_row:end_row,:)\r
202     (end_row is not included into the span). */\r
203 CVAPI(CvMat*) cvGetRows( const CvArr* arr, CvMat* submat,\r
204                         int start_row, int end_row,\r
205                         int delta_row CV_DEFAULT(1));\r
206 \r
207 CV_INLINE  CvMat*  cvGetRow( const CvArr* arr, CvMat* submat, int row )\r
208 {\r
209     return cvGetRows( arr, submat, row, row + 1, 1 );\r
210 }\r
211 \r
212 \r
213 /* Selects column span of the input array: arr(:,start_col:end_col)\r
214    (end_col is not included into the span) */\r
215 CVAPI(CvMat*) cvGetCols( const CvArr* arr, CvMat* submat,\r
216                         int start_col, int end_col );\r
217 \r
218 CV_INLINE  CvMat*  cvGetCol( const CvArr* arr, CvMat* submat, int col )\r
219 {\r
220     return cvGetCols( arr, submat, col, col + 1 );\r
221 }\r
222 \r
223 /* Select a diagonal of the input array.\r
224    (diag = 0 means the main diagonal, >0 means a diagonal above the main one,\r
225    <0 - below the main one).\r
226    The diagonal will be represented as a column (nx1 matrix). */\r
227 CVAPI(CvMat*) cvGetDiag( const CvArr* arr, CvMat* submat,\r
228                             int diag CV_DEFAULT(0));\r
229 \r
230 /* low-level scalar <-> raw data conversion functions */\r
231 CVAPI(void) cvScalarToRawData( const CvScalar* scalar, void* data, int type,\r
232                               int extend_to_12 CV_DEFAULT(0) );\r
233 \r
234 CVAPI(void) cvRawDataToScalar( const void* data, int type, CvScalar* scalar );\r
235 \r
236 /* Allocates and initializes CvMatND header */\r
237 CVAPI(CvMatND*)  cvCreateMatNDHeader( int dims, const int* sizes, int type );\r
238 \r
239 /* Allocates and initializes CvMatND header and allocates data */\r
240 CVAPI(CvMatND*)  cvCreateMatND( int dims, const int* sizes, int type );\r
241 \r
242 /* Initializes preallocated CvMatND header */\r
243 CVAPI(CvMatND*)  cvInitMatNDHeader( CvMatND* mat, int dims, const int* sizes,\r
244                                     int type, void* data CV_DEFAULT(NULL) );\r
245 \r
246 /* Releases CvMatND */\r
247 CV_INLINE  void  cvReleaseMatND( CvMatND** mat )\r
248 {\r
249     cvReleaseMat( (CvMat**)mat );\r
250 }\r
251 \r
252 /* Creates a copy of CvMatND (except, may be, steps) */\r
253 CVAPI(CvMatND*) cvCloneMatND( const CvMatND* mat );\r
254 \r
255 /* Allocates and initializes CvSparseMat header and allocates data */\r
256 CVAPI(CvSparseMat*)  cvCreateSparseMat( int dims, const int* sizes, int type );\r
257 \r
258 /* Releases CvSparseMat */\r
259 CVAPI(void)  cvReleaseSparseMat( CvSparseMat** mat );\r
260 \r
261 /* Creates a copy of CvSparseMat (except, may be, zero items) */\r
262 CVAPI(CvSparseMat*) cvCloneSparseMat( const CvSparseMat* mat );\r
263 \r
264 /* Initializes sparse array iterator\r
265    (returns the first node or NULL if the array is empty) */\r
266 CVAPI(CvSparseNode*) cvInitSparseMatIterator( const CvSparseMat* mat,\r
267                                               CvSparseMatIterator* mat_iterator );\r
268 \r
269 // returns next sparse array node (or NULL if there is no more nodes)\r
270 CV_INLINE CvSparseNode* cvGetNextSparseNode( CvSparseMatIterator* mat_iterator )\r
271 {\r
272     if( mat_iterator->node->next )\r
273         return mat_iterator->node = mat_iterator->node->next;\r
274     else\r
275     {\r
276         int idx;\r
277         for( idx = ++mat_iterator->curidx; idx < mat_iterator->mat->hashsize; idx++ )\r
278         {\r
279             CvSparseNode* node = (CvSparseNode*)mat_iterator->mat->hashtable[idx];\r
280             if( node )\r
281             {\r
282                 mat_iterator->curidx = idx;\r
283                 return mat_iterator->node = node;\r
284             }\r
285         }\r
286         return NULL;\r
287     }\r
288 }\r
289 \r
290 /**************** matrix iterator: used for n-ary operations on dense arrays *********/\r
291 \r
292 #define CV_MAX_ARR 10\r
293 \r
294 typedef struct CvNArrayIterator\r
295 {\r
296     int count; /* number of arrays */\r
297     int dims; /* number of dimensions to iterate */\r
298     CvSize size; /* maximal common linear size: { width = size, height = 1 } */\r
299     uchar* ptr[CV_MAX_ARR]; /* pointers to the array slices */\r
300     int stack[CV_MAX_DIM]; /* for internal use */\r
301     CvMatND* hdr[CV_MAX_ARR]; /* pointers to the headers of the\r
302                                  matrices that are processed */\r
303 }\r
304 CvNArrayIterator;\r
305 \r
306 #define CV_NO_DEPTH_CHECK     1\r
307 #define CV_NO_CN_CHECK        2\r
308 #define CV_NO_SIZE_CHECK      4\r
309 \r
310 /* initializes iterator that traverses through several arrays simulteneously\r
311    (the function together with cvNextArraySlice is used for\r
312     N-ari element-wise operations) */\r
313 CVAPI(int) cvInitNArrayIterator( int count, CvArr** arrs,\r
314                                  const CvArr* mask, CvMatND* stubs,\r
315                                  CvNArrayIterator* array_iterator,\r
316                                  int flags CV_DEFAULT(0) );\r
317 \r
318 /* returns zero value if iteration is finished, non-zero (slice length) otherwise */\r
319 CVAPI(int) cvNextNArraySlice( CvNArrayIterator* array_iterator );\r
320 \r
321 \r
322 /* Returns type of array elements:\r
323    CV_8UC1 ... CV_64FC4 ... */\r
324 CVAPI(int) cvGetElemType( const CvArr* arr );\r
325 \r
326 /* Retrieves number of an array dimensions and\r
327    optionally sizes of the dimensions */\r
328 CVAPI(int) cvGetDims( const CvArr* arr, int* sizes CV_DEFAULT(NULL) );\r
329 \r
330 \r
331 /* Retrieves size of a particular array dimension.\r
332    For 2d arrays cvGetDimSize(arr,0) returns number of rows (image height)\r
333    and cvGetDimSize(arr,1) returns number of columns (image width) */\r
334 CVAPI(int) cvGetDimSize( const CvArr* arr, int index );\r
335 \r
336 \r
337 /* ptr = &arr(idx0,idx1,...). All indexes are zero-based,\r
338    the major dimensions go first (e.g. (y,x) for 2D, (z,y,x) for 3D */\r
339 CVAPI(uchar*) cvPtr1D( const CvArr* arr, int idx0, int* type CV_DEFAULT(NULL));\r
340 CVAPI(uchar*) cvPtr2D( const CvArr* arr, int idx0, int idx1, int* type CV_DEFAULT(NULL) );\r
341 CVAPI(uchar*) cvPtr3D( const CvArr* arr, int idx0, int idx1, int idx2,\r
342                       int* type CV_DEFAULT(NULL));\r
343 \r
344 /* For CvMat or IplImage number of indices should be 2\r
345    (row index (y) goes first, column index (x) goes next).\r
346    For CvMatND or CvSparseMat number of infices should match number of <dims> and\r
347    indices order should match the array dimension order. */\r
348 CVAPI(uchar*) cvPtrND( const CvArr* arr, const int* idx, int* type CV_DEFAULT(NULL),\r
349                       int create_node CV_DEFAULT(1),\r
350                       unsigned* precalc_hashval CV_DEFAULT(NULL));\r
351 \r
352 /* value = arr(idx0,idx1,...) */\r
353 CVAPI(CvScalar) cvGet1D( const CvArr* arr, int idx0 );\r
354 CVAPI(CvScalar) cvGet2D( const CvArr* arr, int idx0, int idx1 );\r
355 CVAPI(CvScalar) cvGet3D( const CvArr* arr, int idx0, int idx1, int idx2 );\r
356 CVAPI(CvScalar) cvGetND( const CvArr* arr, const int* idx );\r
357 \r
358 /* for 1-channel arrays */\r
359 CVAPI(double) cvGetReal1D( const CvArr* arr, int idx0 );\r
360 CVAPI(double) cvGetReal2D( const CvArr* arr, int idx0, int idx1 );\r
361 CVAPI(double) cvGetReal3D( const CvArr* arr, int idx0, int idx1, int idx2 );\r
362 CVAPI(double) cvGetRealND( const CvArr* arr, const int* idx );\r
363 \r
364 /* arr(idx0,idx1,...) = value */\r
365 CVAPI(void) cvSet1D( CvArr* arr, int idx0, CvScalar value );\r
366 CVAPI(void) cvSet2D( CvArr* arr, int idx0, int idx1, CvScalar value );\r
367 CVAPI(void) cvSet3D( CvArr* arr, int idx0, int idx1, int idx2, CvScalar value );\r
368 CVAPI(void) cvSetND( CvArr* arr, const int* idx, CvScalar value );\r
369 \r
370 /* for 1-channel arrays */\r
371 CVAPI(void) cvSetReal1D( CvArr* arr, int idx0, double value );\r
372 CVAPI(void) cvSetReal2D( CvArr* arr, int idx0, int idx1, double value );\r
373 CVAPI(void) cvSetReal3D( CvArr* arr, int idx0,\r
374                         int idx1, int idx2, double value );\r
375 CVAPI(void) cvSetRealND( CvArr* arr, const int* idx, double value );\r
376 \r
377 /* clears element of ND dense array,\r
378    in case of sparse arrays it deletes the specified node */\r
379 CVAPI(void) cvClearND( CvArr* arr, const int* idx );\r
380 \r
381 /* Converts CvArr (IplImage or CvMat,...) to CvMat.\r
382    If the last parameter is non-zero, function can\r
383    convert multi(>2)-dimensional array to CvMat as long as\r
384    the last array's dimension is continous. The resultant\r
385    matrix will be have appropriate (a huge) number of rows */\r
386 CVAPI(CvMat*) cvGetMat( const CvArr* arr, CvMat* header,\r
387                        int* coi CV_DEFAULT(NULL),\r
388                        int allowND CV_DEFAULT(0));\r
389 \r
390 /* Converts CvArr (IplImage or CvMat) to IplImage */\r
391 CVAPI(IplImage*) cvGetImage( const CvArr* arr, IplImage* image_header );\r
392 \r
393 \r
394 /* Changes a shape of multi-dimensional array.\r
395    new_cn == 0 means that number of channels remains unchanged.\r
396    new_dims == 0 means that number and sizes of dimensions remain the same\r
397    (unless they need to be changed to set the new number of channels)\r
398    if new_dims == 1, there is no need to specify new dimension sizes\r
399    The resultant configuration should be achievable w/o data copying.\r
400    If the resultant array is sparse, CvSparseMat header should be passed\r
401    to the function else if the result is 1 or 2 dimensional,\r
402    CvMat header should be passed to the function\r
403    else CvMatND header should be passed */\r
404 CVAPI(CvArr*) cvReshapeMatND( const CvArr* arr,\r
405                              int sizeof_header, CvArr* header,\r
406                              int new_cn, int new_dims, int* new_sizes );\r
407 \r
408 #define cvReshapeND( arr, header, new_cn, new_dims, new_sizes )   \\r
409       cvReshapeMatND( (arr), sizeof(*(header)), (header),         \\r
410                       (new_cn), (new_dims), (new_sizes))\r
411 \r
412 CVAPI(CvMat*) cvReshape( const CvArr* arr, CvMat* header,\r
413                         int new_cn, int new_rows CV_DEFAULT(0) );\r
414 \r
415 /* Repeats source 2d array several times in both horizontal and\r
416    vertical direction to fill destination array */\r
417 CVAPI(void) cvRepeat( const CvArr* src, CvArr* dst );\r
418 \r
419 /* Allocates array data */\r
420 CVAPI(void)  cvCreateData( CvArr* arr );\r
421 \r
422 /* Releases array data */\r
423 CVAPI(void)  cvReleaseData( CvArr* arr );\r
424 \r
425 /* Attaches user data to the array header. The step is reffered to\r
426    the pre-last dimension. That is, all the planes of the array\r
427    must be joint (w/o gaps) */\r
428 CVAPI(void)  cvSetData( CvArr* arr, void* data, int step );\r
429 \r
430 /* Retrieves raw data of CvMat, IplImage or CvMatND.\r
431    In the latter case the function raises an error if\r
432    the array can not be represented as a matrix */\r
433 CVAPI(void) cvGetRawData( const CvArr* arr, uchar** data,\r
434                          int* step CV_DEFAULT(NULL),\r
435                          CvSize* roi_size CV_DEFAULT(NULL));\r
436 \r
437 /* Returns width and height of array in elements */\r
438 CVAPI(CvSize) cvGetSize( const CvArr* arr );\r
439 \r
440 /* Copies source array to destination array */\r
441 CVAPI(void)  cvCopy( const CvArr* src, CvArr* dst,\r
442                      const CvArr* mask CV_DEFAULT(NULL) );\r
443 \r
444 /* Sets all or "masked" elements of input array\r
445    to the same value*/\r
446 CVAPI(void)  cvSet( CvArr* arr, CvScalar value,\r
447                     const CvArr* mask CV_DEFAULT(NULL) );\r
448 \r
449 /* Clears all the array elements (sets them to 0) */\r
450 CVAPI(void)  cvSetZero( CvArr* arr );\r
451 #define cvZero  cvSetZero\r
452 \r
453 \r
454 /* Splits a multi-channel array into the set of single-channel arrays or\r
455    extracts particular [color] plane */\r
456 CVAPI(void)  cvSplit( const CvArr* src, CvArr* dst0, CvArr* dst1,\r
457                       CvArr* dst2, CvArr* dst3 );\r
458 \r
459 /* Merges a set of single-channel arrays into the single multi-channel array\r
460    or inserts one particular [color] plane to the array */\r
461 CVAPI(void)  cvMerge( const CvArr* src0, const CvArr* src1,\r
462                       const CvArr* src2, const CvArr* src3,\r
463                       CvArr* dst );\r
464 \r
465 /* Copies several channels from input arrays to\r
466    certain channels of output arrays */\r
467 CVAPI(void)  cvMixChannels( const CvArr** src, int src_count,\r
468                             CvArr** dst, int dst_count,\r
469                             const int* from_to, int pair_count );\r
470 \r
471 /* Performs linear transformation on every source array element:\r
472    dst(x,y,c) = scale*src(x,y,c)+shift.\r
473    Arbitrary combination of input and output array depths are allowed\r
474    (number of channels must be the same), thus the function can be used\r
475    for type conversion */\r
476 CVAPI(void)  cvConvertScale( const CvArr* src, CvArr* dst,\r
477                              double scale CV_DEFAULT(1),\r
478                              double shift CV_DEFAULT(0) );\r
479 #define cvCvtScale cvConvertScale\r
480 #define cvScale  cvConvertScale\r
481 #define cvConvert( src, dst )  cvConvertScale( (src), (dst), 1, 0 )\r
482 \r
483 \r
484 /* Performs linear transformation on every source array element,\r
485    stores absolute value of the result:\r
486    dst(x,y,c) = abs(scale*src(x,y,c)+shift).\r
487    destination array must have 8u type.\r
488    In other cases one may use cvConvertScale + cvAbsDiffS */\r
489 CVAPI(void)  cvConvertScaleAbs( const CvArr* src, CvArr* dst,\r
490                                 double scale CV_DEFAULT(1),\r
491                                 double shift CV_DEFAULT(0) );\r
492 #define cvCvtScaleAbs  cvConvertScaleAbs\r
493 \r
494 \r
495 /* checks termination criteria validity and\r
496    sets eps to default_eps (if it is not set),\r
497    max_iter to default_max_iters (if it is not set)\r
498 */\r
499 CVAPI(CvTermCriteria) cvCheckTermCriteria( CvTermCriteria criteria,\r
500                                            double default_eps,\r
501                                            int default_max_iters );\r
502 \r
503 /****************************************************************************************\\r
504 *                   Arithmetic, logic and comparison operations                          *\r
505 \****************************************************************************************/\r
506 \r
507 /* dst(mask) = src1(mask) + src2(mask) */\r
508 CVAPI(void)  cvAdd( const CvArr* src1, const CvArr* src2, CvArr* dst,\r
509                     const CvArr* mask CV_DEFAULT(NULL));\r
510 \r
511 /* dst(mask) = src(mask) + value */\r
512 CVAPI(void)  cvAddS( const CvArr* src, CvScalar value, CvArr* dst,\r
513                      const CvArr* mask CV_DEFAULT(NULL));\r
514 \r
515 /* dst(mask) = src1(mask) - src2(mask) */\r
516 CVAPI(void)  cvSub( const CvArr* src1, const CvArr* src2, CvArr* dst,\r
517                     const CvArr* mask CV_DEFAULT(NULL));\r
518 \r
519 /* dst(mask) = src(mask) - value = src(mask) + (-value) */\r
520 CV_INLINE  void  cvSubS( const CvArr* src, CvScalar value, CvArr* dst,\r
521                          const CvArr* mask CV_DEFAULT(NULL))\r
522 {\r
523     cvAddS( src, cvScalar( -value.val[0], -value.val[1], -value.val[2], -value.val[3]),\r
524             dst, mask );\r
525 }\r
526 \r
527 /* dst(mask) = value - src(mask) */\r
528 CVAPI(void)  cvSubRS( const CvArr* src, CvScalar value, CvArr* dst,\r
529                       const CvArr* mask CV_DEFAULT(NULL));\r
530 \r
531 /* dst(idx) = src1(idx) * src2(idx) * scale\r
532    (scaled element-wise multiplication of 2 arrays) */\r
533 CVAPI(void)  cvMul( const CvArr* src1, const CvArr* src2,\r
534                     CvArr* dst, double scale CV_DEFAULT(1) );\r
535 \r
536 /* element-wise division/inversion with scaling:\r
537     dst(idx) = src1(idx) * scale / src2(idx)\r
538     or dst(idx) = scale / src2(idx) if src1 == 0 */\r
539 CVAPI(void)  cvDiv( const CvArr* src1, const CvArr* src2,\r
540                     CvArr* dst, double scale CV_DEFAULT(1));\r
541 \r
542 /* dst = src1 * scale + src2 */\r
543 CVAPI(void)  cvScaleAdd( const CvArr* src1, CvScalar scale,\r
544                          const CvArr* src2, CvArr* dst );\r
545 #define cvAXPY( A, real_scalar, B, C ) cvScaleAdd(A, cvRealScalar(real_scalar), B, C)\r
546 \r
547 /* dst = src1 * alpha + src2 * beta + gamma */\r
548 CVAPI(void)  cvAddWeighted( const CvArr* src1, double alpha,\r
549                             const CvArr* src2, double beta,\r
550                             double gamma, CvArr* dst );\r
551 \r
552 /* result = sum_i(src1(i) * src2(i)) (results for all channels are accumulated together) */\r
553 CVAPI(double)  cvDotProduct( const CvArr* src1, const CvArr* src2 );\r
554 \r
555 /* dst(idx) = src1(idx) & src2(idx) */\r
556 CVAPI(void) cvAnd( const CvArr* src1, const CvArr* src2,\r
557                   CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));\r
558 \r
559 /* dst(idx) = src(idx) & value */\r
560 CVAPI(void) cvAndS( const CvArr* src, CvScalar value,\r
561                    CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));\r
562 \r
563 /* dst(idx) = src1(idx) | src2(idx) */\r
564 CVAPI(void) cvOr( const CvArr* src1, const CvArr* src2,\r
565                  CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));\r
566 \r
567 /* dst(idx) = src(idx) | value */\r
568 CVAPI(void) cvOrS( const CvArr* src, CvScalar value,\r
569                   CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));\r
570 \r
571 /* dst(idx) = src1(idx) ^ src2(idx) */\r
572 CVAPI(void) cvXor( const CvArr* src1, const CvArr* src2,\r
573                   CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));\r
574 \r
575 /* dst(idx) = src(idx) ^ value */\r
576 CVAPI(void) cvXorS( const CvArr* src, CvScalar value,\r
577                    CvArr* dst, const CvArr* mask CV_DEFAULT(NULL));\r
578 \r
579 /* dst(idx) = ~src(idx) */\r
580 CVAPI(void) cvNot( const CvArr* src, CvArr* dst );\r
581 \r
582 /* dst(idx) = lower(idx) <= src(idx) < upper(idx) */\r
583 CVAPI(void) cvInRange( const CvArr* src, const CvArr* lower,\r
584                       const CvArr* upper, CvArr* dst );\r
585 \r
586 /* dst(idx) = lower <= src(idx) < upper */\r
587 CVAPI(void) cvInRangeS( const CvArr* src, CvScalar lower,\r
588                        CvScalar upper, CvArr* dst );\r
589 \r
590 #define CV_CMP_EQ   0\r
591 #define CV_CMP_GT   1\r
592 #define CV_CMP_GE   2\r
593 #define CV_CMP_LT   3\r
594 #define CV_CMP_LE   4\r
595 #define CV_CMP_NE   5\r
596 \r
597 /* The comparison operation support single-channel arrays only.\r
598    Destination image should be 8uC1 or 8sC1 */\r
599 \r
600 /* dst(idx) = src1(idx) _cmp_op_ src2(idx) */\r
601 CVAPI(void) cvCmp( const CvArr* src1, const CvArr* src2, CvArr* dst, int cmp_op );\r
602 \r
603 /* dst(idx) = src1(idx) _cmp_op_ value */\r
604 CVAPI(void) cvCmpS( const CvArr* src, double value, CvArr* dst, int cmp_op );\r
605 \r
606 /* dst(idx) = min(src1(idx),src2(idx)) */\r
607 CVAPI(void) cvMin( const CvArr* src1, const CvArr* src2, CvArr* dst );\r
608 \r
609 /* dst(idx) = max(src1(idx),src2(idx)) */\r
610 CVAPI(void) cvMax( const CvArr* src1, const CvArr* src2, CvArr* dst );\r
611 \r
612 /* dst(idx) = min(src(idx),value) */\r
613 CVAPI(void) cvMinS( const CvArr* src, double value, CvArr* dst );\r
614 \r
615 /* dst(idx) = max(src(idx),value) */\r
616 CVAPI(void) cvMaxS( const CvArr* src, double value, CvArr* dst );\r
617 \r
618 /* dst(x,y,c) = abs(src1(x,y,c) - src2(x,y,c)) */\r
619 CVAPI(void) cvAbsDiff( const CvArr* src1, const CvArr* src2, CvArr* dst );\r
620 \r
621 /* dst(x,y,c) = abs(src(x,y,c) - value(c)) */\r
622 CVAPI(void) cvAbsDiffS( const CvArr* src, CvArr* dst, CvScalar value );\r
623 #define cvAbs( src, dst ) cvAbsDiffS( (src), (dst), cvScalarAll(0))\r
624 \r
625 /****************************************************************************************\\r
626 *                                Math operations                                         *\r
627 \****************************************************************************************/\r
628 \r
629 /* Does cartesian->polar coordinates conversion.\r
630    Either of output components (magnitude or angle) is optional */\r
631 CVAPI(void)  cvCartToPolar( const CvArr* x, const CvArr* y,\r
632                             CvArr* magnitude, CvArr* angle CV_DEFAULT(NULL),\r
633                             int angle_in_degrees CV_DEFAULT(0));\r
634 \r
635 /* Does polar->cartesian coordinates conversion.\r
636    Either of output components (magnitude or angle) is optional.\r
637    If magnitude is missing it is assumed to be all 1's */\r
638 CVAPI(void)  cvPolarToCart( const CvArr* magnitude, const CvArr* angle,\r
639                             CvArr* x, CvArr* y,\r
640                             int angle_in_degrees CV_DEFAULT(0));\r
641 \r
642 /* Does powering: dst(idx) = src(idx)^power */\r
643 CVAPI(void)  cvPow( const CvArr* src, CvArr* dst, double power );\r
644 \r
645 /* Does exponention: dst(idx) = exp(src(idx)).\r
646    Overflow is not handled yet. Underflow is handled.\r
647    Maximal relative error is ~7e-6 for single-precision input */\r
648 CVAPI(void)  cvExp( const CvArr* src, CvArr* dst );\r
649 \r
650 /* Calculates natural logarithms: dst(idx) = log(abs(src(idx))).\r
651    Logarithm of 0 gives large negative number(~-700)\r
652    Maximal relative error is ~3e-7 for single-precision output\r
653 */\r
654 CVAPI(void)  cvLog( const CvArr* src, CvArr* dst );\r
655 \r
656 /* Fast arctangent calculation */\r
657 CVAPI(float) cvFastArctan( float y, float x );\r
658 \r
659 /* Fast cubic root calculation */\r
660 CVAPI(float)  cvCbrt( float value );\r
661 \r
662 /* Checks array values for NaNs, Infs or simply for too large numbers\r
663    (if CV_CHECK_RANGE is set). If CV_CHECK_QUIET is set,\r
664    no runtime errors is raised (function returns zero value in case of "bad" values).\r
665    Otherwise cvError is called */\r
666 #define  CV_CHECK_RANGE    1\r
667 #define  CV_CHECK_QUIET    2\r
668 CVAPI(int)  cvCheckArr( const CvArr* arr, int flags CV_DEFAULT(0),\r
669                         double min_val CV_DEFAULT(0), double max_val CV_DEFAULT(0));\r
670 #define cvCheckArray cvCheckArr\r
671 \r
672 #define CV_RAND_UNI      0\r
673 #define CV_RAND_NORMAL   1\r
674 CVAPI(void) cvRandArr( CvRNG* rng, CvArr* arr, int dist_type,\r
675                       CvScalar param1, CvScalar param2 );\r
676 \r
677 CVAPI(void) cvRandShuffle( CvArr* mat, CvRNG* rng,\r
678                            double iter_factor CV_DEFAULT(1.));\r
679 \r
680 #define CV_SORT_EVERY_ROW 0\r
681 #define CV_SORT_EVERY_COLUMN 1\r
682 #define CV_SORT_ASCENDING 0\r
683 #define CV_SORT_DESCENDING 16\r
684 \r
685 CVAPI(void) cvSort( const CvArr* src, CvArr* dst CV_DEFAULT(NULL),\r
686                     CvArr* idxmat CV_DEFAULT(NULL),\r
687                     int flags CV_DEFAULT(0));\r
688 \r
689 /* Finds real roots of a cubic equation */\r
690 CVAPI(int) cvSolveCubic( const CvMat* coeffs, CvMat* roots );\r
691 \r
692 /* Finds all real and complex roots of a polynomial equation */\r
693 CVAPI(void) cvSolvePoly(const CvMat* coeffs, CvMat *roots2,\r
694                         int maxiter CV_DEFAULT(20), int fig CV_DEFAULT(100));\r
695 \r
696 /****************************************************************************************\\r
697 *                                Matrix operations                                       *\r
698 \****************************************************************************************/\r
699 \r
700 /* Calculates cross product of two 3d vectors */\r
701 CVAPI(void)  cvCrossProduct( const CvArr* src1, const CvArr* src2, CvArr* dst );\r
702 \r
703 /* Matrix transform: dst = A*B + C, C is optional */\r
704 #define cvMatMulAdd( src1, src2, src3, dst ) cvGEMM( (src1), (src2), 1., (src3), 1., (dst), 0 )\r
705 #define cvMatMul( src1, src2, dst )  cvMatMulAdd( (src1), (src2), NULL, (dst))\r
706 \r
707 #define CV_GEMM_A_T 1\r
708 #define CV_GEMM_B_T 2\r
709 #define CV_GEMM_C_T 4\r
710 /* Extended matrix transform:\r
711    dst = alpha*op(A)*op(B) + beta*op(C), where op(X) is X or X^T */\r
712 CVAPI(void)  cvGEMM( const CvArr* src1, const CvArr* src2, double alpha,\r
713                      const CvArr* src3, double beta, CvArr* dst,\r
714                      int tABC CV_DEFAULT(0));\r
715 #define cvMatMulAddEx cvGEMM\r
716 \r
717 /* Transforms each element of source array and stores\r
718    resultant vectors in destination array */\r
719 CVAPI(void)  cvTransform( const CvArr* src, CvArr* dst,\r
720                           const CvMat* transmat,\r
721                           const CvMat* shiftvec CV_DEFAULT(NULL));\r
722 #define cvMatMulAddS cvTransform\r
723 \r
724 /* Does perspective transform on every element of input array */\r
725 CVAPI(void)  cvPerspectiveTransform( const CvArr* src, CvArr* dst,\r
726                                      const CvMat* mat );\r
727 \r
728 /* Calculates (A-delta)*(A-delta)^T (order=0) or (A-delta)^T*(A-delta) (order=1) */\r
729 CVAPI(void) cvMulTransposed( const CvArr* src, CvArr* dst, int order,\r
730                              const CvArr* delta CV_DEFAULT(NULL),\r
731                              double scale CV_DEFAULT(1.) );\r
732 \r
733 /* Tranposes matrix. Square matrices can be transposed in-place */\r
734 CVAPI(void)  cvTranspose( const CvArr* src, CvArr* dst );\r
735 #define cvT cvTranspose\r
736 \r
737 /* Completes the symmetric matrix from the lower (LtoR=0) or from the upper (LtoR!=0) part */\r
738 CVAPI(void)  cvCompleteSymm( CvMat* matrix, int LtoR CV_DEFAULT(0) );\r
739 \r
740 /* Mirror array data around horizontal (flip=0),\r
741    vertical (flip=1) or both(flip=-1) axises:\r
742    cvFlip(src) flips images vertically and sequences horizontally (inplace) */\r
743 CVAPI(void)  cvFlip( const CvArr* src, CvArr* dst CV_DEFAULT(NULL),\r
744                      int flip_mode CV_DEFAULT(0));\r
745 #define cvMirror cvFlip\r
746 \r
747 \r
748 #define CV_SVD_MODIFY_A   1\r
749 #define CV_SVD_U_T        2\r
750 #define CV_SVD_V_T        4\r
751 \r
752 /* Performs Singular Value Decomposition of a matrix */\r
753 CVAPI(void)   cvSVD( CvArr* A, CvArr* W, CvArr* U CV_DEFAULT(NULL),\r
754                      CvArr* V CV_DEFAULT(NULL), int flags CV_DEFAULT(0));\r
755 \r
756 /* Performs Singular Value Back Substitution (solves A*X = B):\r
757    flags must be the same as in cvSVD */\r
758 CVAPI(void)   cvSVBkSb( const CvArr* W, const CvArr* U,\r
759                         const CvArr* V, const CvArr* B,\r
760                         CvArr* X, int flags );\r
761 \r
762 #define CV_LU  0\r
763 #define CV_SVD 1\r
764 #define CV_SVD_SYM 2\r
765 #define CV_CHOLESKY 3\r
766 #define CV_QR  4\r
767 #define CV_NORMAL 16\r
768 \r
769 /* Inverts matrix */\r
770 CVAPI(double)  cvInvert( const CvArr* src, CvArr* dst,\r
771                          int method CV_DEFAULT(CV_LU));\r
772 #define cvInv cvInvert\r
773 \r
774 /* Solves linear system (src1)*(dst) = (src2)\r
775    (returns 0 if src1 is a singular and CV_LU method is used) */\r
776 CVAPI(int)  cvSolve( const CvArr* src1, const CvArr* src2, CvArr* dst,\r
777                      int method CV_DEFAULT(CV_LU));\r
778 \r
779 /* Calculates determinant of input matrix */\r
780 CVAPI(double) cvDet( const CvArr* mat );\r
781 \r
782 /* Calculates trace of the matrix (sum of elements on the main diagonal) */\r
783 CVAPI(CvScalar) cvTrace( const CvArr* mat );\r
784 \r
785 /* Finds eigen values and vectors of a symmetric matrix */\r
786 CVAPI(void)  cvEigenVV( CvArr* mat, CvArr* evects,\r
787                         CvArr* evals, double eps CV_DEFAULT(0));\r
788 \r
789 /* Makes an identity matrix (mat_ij = i == j) */\r
790 CVAPI(void)  cvSetIdentity( CvArr* mat, CvScalar value CV_DEFAULT(cvRealScalar(1)) );\r
791 \r
792 /* Fills matrix with given range of numbers */\r
793 CVAPI(CvArr*)  cvRange( CvArr* mat, double start, double end );\r
794 \r
795 /* Calculates covariation matrix for a set of vectors */\r
796 /* transpose([v1-avg, v2-avg,...]) * [v1-avg,v2-avg,...] */\r
797 #define CV_COVAR_SCRAMBLED 0\r
798 \r
799 /* [v1-avg, v2-avg,...] * transpose([v1-avg,v2-avg,...]) */\r
800 #define CV_COVAR_NORMAL    1\r
801 \r
802 /* do not calc average (i.e. mean vector) - use the input vector instead\r
803    (useful for calculating covariance matrix by parts) */\r
804 #define CV_COVAR_USE_AVG   2\r
805 \r
806 /* scale the covariance matrix coefficients by number of the vectors */\r
807 #define CV_COVAR_SCALE     4\r
808 \r
809 /* all the input vectors are stored in a single matrix, as its rows */\r
810 #define CV_COVAR_ROWS      8\r
811 \r
812 /* all the input vectors are stored in a single matrix, as its columns */\r
813 #define CV_COVAR_COLS     16\r
814 \r
815 CVAPI(void)  cvCalcCovarMatrix( const CvArr** vects, int count,\r
816                                 CvArr* cov_mat, CvArr* avg, int flags );\r
817 \r
818 #define CV_PCA_DATA_AS_ROW 0\r
819 #define CV_PCA_DATA_AS_COL 1\r
820 #define CV_PCA_USE_AVG 2\r
821 CVAPI(void)  cvCalcPCA( const CvArr* data, CvArr* mean,\r
822                         CvArr* eigenvals, CvArr* eigenvects, int flags );\r
823 \r
824 CVAPI(void)  cvProjectPCA( const CvArr* data, const CvArr* mean,\r
825                            const CvArr* eigenvects, CvArr* result );\r
826 \r
827 CVAPI(void)  cvBackProjectPCA( const CvArr* proj, const CvArr* mean,\r
828                                const CvArr* eigenvects, CvArr* result );\r
829 \r
830 /* Calculates Mahalanobis(weighted) distance */\r
831 CVAPI(double)  cvMahalanobis( const CvArr* vec1, const CvArr* vec2, const CvArr* mat );\r
832 #define cvMahalonobis  cvMahalanobis\r
833 \r
834 /****************************************************************************************\\r
835 *                                    Array Statistics                                    *\r
836 \****************************************************************************************/\r
837 \r
838 /* Finds sum of array elements */\r
839 CVAPI(CvScalar)  cvSum( const CvArr* arr );\r
840 \r
841 /* Calculates number of non-zero pixels */\r
842 CVAPI(int)  cvCountNonZero( const CvArr* arr );\r
843 \r
844 /* Calculates mean value of array elements */\r
845 CVAPI(CvScalar)  cvAvg( const CvArr* arr, const CvArr* mask CV_DEFAULT(NULL) );\r
846 \r
847 /* Calculates mean and standard deviation of pixel values */\r
848 CVAPI(void)  cvAvgSdv( const CvArr* arr, CvScalar* mean, CvScalar* std_dev,\r
849                        const CvArr* mask CV_DEFAULT(NULL) );\r
850 \r
851 /* Finds global minimum, maximum and their positions */\r
852 CVAPI(void)  cvMinMaxLoc( const CvArr* arr, double* min_val, double* max_val,\r
853                           CvPoint* min_loc CV_DEFAULT(NULL),\r
854                           CvPoint* max_loc CV_DEFAULT(NULL),\r
855                           const CvArr* mask CV_DEFAULT(NULL) );\r
856 \r
857 /* types of array norm */\r
858 #define CV_C            1\r
859 #define CV_L1           2\r
860 #define CV_L2           4\r
861 #define CV_NORM_MASK    7\r
862 #define CV_RELATIVE     8\r
863 #define CV_DIFF         16\r
864 #define CV_MINMAX       32\r
865 \r
866 #define CV_DIFF_C       (CV_DIFF | CV_C)\r
867 #define CV_DIFF_L1      (CV_DIFF | CV_L1)\r
868 #define CV_DIFF_L2      (CV_DIFF | CV_L2)\r
869 #define CV_RELATIVE_C   (CV_RELATIVE | CV_C)\r
870 #define CV_RELATIVE_L1  (CV_RELATIVE | CV_L1)\r
871 #define CV_RELATIVE_L2  (CV_RELATIVE | CV_L2)\r
872 \r
873 /* Finds norm, difference norm or relative difference norm for an array (or two arrays) */\r
874 CVAPI(double)  cvNorm( const CvArr* arr1, const CvArr* arr2 CV_DEFAULT(NULL),\r
875                        int norm_type CV_DEFAULT(CV_L2),\r
876                        const CvArr* mask CV_DEFAULT(NULL) );\r
877 \r
878 CVAPI(void)  cvNormalize( const CvArr* src, CvArr* dst,\r
879                           double a CV_DEFAULT(1.), double b CV_DEFAULT(0.),\r
880                           int norm_type CV_DEFAULT(CV_L2),\r
881                           const CvArr* mask CV_DEFAULT(NULL) );\r
882 \r
883 \r
884 #define CV_REDUCE_SUM 0\r
885 #define CV_REDUCE_AVG 1\r
886 #define CV_REDUCE_MAX 2\r
887 #define CV_REDUCE_MIN 3\r
888 \r
889 CVAPI(void)  cvReduce( const CvArr* src, CvArr* dst, int dim CV_DEFAULT(-1),\r
890                        int op CV_DEFAULT(CV_REDUCE_SUM) );\r
891 \r
892 /****************************************************************************************\\r
893 *                      Discrete Linear Transforms and Related Functions                  *\r
894 \****************************************************************************************/\r
895 \r
896 #define CV_DXT_FORWARD  0\r
897 #define CV_DXT_INVERSE  1\r
898 #define CV_DXT_SCALE    2 /* divide result by size of array */\r
899 #define CV_DXT_INV_SCALE (CV_DXT_INVERSE + CV_DXT_SCALE)\r
900 #define CV_DXT_INVERSE_SCALE CV_DXT_INV_SCALE\r
901 #define CV_DXT_ROWS     4 /* transform each row individually */\r
902 #define CV_DXT_MUL_CONJ 8 /* conjugate the second argument of cvMulSpectrums */\r
903 \r
904 /* Discrete Fourier Transform:\r
905     complex->complex,\r
906     real->ccs (forward),\r
907     ccs->real (inverse) */\r
908 CVAPI(void)  cvDFT( const CvArr* src, CvArr* dst, int flags,\r
909                     int nonzero_rows CV_DEFAULT(0) );\r
910 #define cvFFT cvDFT\r
911 \r
912 /* Multiply results of DFTs: DFT(X)*DFT(Y) or DFT(X)*conj(DFT(Y)) */\r
913 CVAPI(void)  cvMulSpectrums( const CvArr* src1, const CvArr* src2,\r
914                              CvArr* dst, int flags );\r
915 \r
916 /* Finds optimal DFT vector size >= size0 */\r
917 CVAPI(int)  cvGetOptimalDFTSize( int size0 );\r
918 \r
919 /* Discrete Cosine Transform */\r
920 CVAPI(void)  cvDCT( const CvArr* src, CvArr* dst, int flags );\r
921 \r
922 /****************************************************************************************\\r
923 *                              Dynamic data structures                                   *\r
924 \****************************************************************************************/\r
925 \r
926 /* Calculates length of sequence slice (with support of negative indices). */\r
927 CVAPI(int) cvSliceLength( CvSlice slice, const CvSeq* seq );\r
928 \r
929 \r
930 /* Creates new memory storage.\r
931    block_size == 0 means that default,\r
932    somewhat optimal size, is used (currently, it is 64K) */\r
933 CVAPI(CvMemStorage*)  cvCreateMemStorage( int block_size CV_DEFAULT(0));\r
934 \r
935 \r
936 /* Creates a memory storage that will borrow memory blocks from parent storage */\r
937 CVAPI(CvMemStorage*)  cvCreateChildMemStorage( CvMemStorage* parent );\r
938 \r
939 \r
940 /* Releases memory storage. All the children of a parent must be released before\r
941    the parent. A child storage returns all the blocks to parent when it is released */\r
942 CVAPI(void)  cvReleaseMemStorage( CvMemStorage** storage );\r
943 \r
944 \r
945 /* Clears memory storage. This is the only way(!!!) (besides cvRestoreMemStoragePos)\r
946    to reuse memory allocated for the storage - cvClearSeq,cvClearSet ...\r
947    do not free any memory.\r
948    A child storage returns all the blocks to the parent when it is cleared */\r
949 CVAPI(void)  cvClearMemStorage( CvMemStorage* storage );\r
950 \r
951 /* Remember a storage "free memory" position */\r
952 CVAPI(void)  cvSaveMemStoragePos( const CvMemStorage* storage, CvMemStoragePos* pos );\r
953 \r
954 /* Restore a storage "free memory" position */\r
955 CVAPI(void)  cvRestoreMemStoragePos( CvMemStorage* storage, CvMemStoragePos* pos );\r
956 \r
957 /* Allocates continuous buffer of the specified size in the storage */\r
958 CVAPI(void*) cvMemStorageAlloc( CvMemStorage* storage, size_t size );\r
959 \r
960 /* Allocates string in memory storage */\r
961 CVAPI(CvString) cvMemStorageAllocString( CvMemStorage* storage, const char* ptr,\r
962                                         int len CV_DEFAULT(-1) );\r
963 \r
964 /* Creates new empty sequence that will reside in the specified storage */\r
965 CVAPI(CvSeq*)  cvCreateSeq( int seq_flags, int header_size,\r
966                             int elem_size, CvMemStorage* storage );\r
967 \r
968 /* Changes default size (granularity) of sequence blocks.\r
969    The default size is ~1Kbyte */\r
970 CVAPI(void)  cvSetSeqBlockSize( CvSeq* seq, int delta_elems );\r
971 \r
972 \r
973 /* Adds new element to the end of sequence. Returns pointer to the element */\r
974 CVAPI(schar*)  cvSeqPush( CvSeq* seq, void* element CV_DEFAULT(NULL));\r
975 \r
976 \r
977 /* Adds new element to the beginning of sequence. Returns pointer to it */\r
978 CVAPI(schar*)  cvSeqPushFront( CvSeq* seq, void* element CV_DEFAULT(NULL));\r
979 \r
980 \r
981 /* Removes the last element from sequence and optionally saves it */\r
982 CVAPI(void)  cvSeqPop( CvSeq* seq, void* element CV_DEFAULT(NULL));\r
983 \r
984 \r
985 /* Removes the first element from sequence and optioanally saves it */\r
986 CVAPI(void)  cvSeqPopFront( CvSeq* seq, void* element CV_DEFAULT(NULL));\r
987 \r
988 \r
989 #define CV_FRONT 1\r
990 #define CV_BACK 0\r
991 /* Adds several new elements to the end of sequence */\r
992 CVAPI(void)  cvSeqPushMulti( CvSeq* seq, void* elements,\r
993                              int count, int in_front CV_DEFAULT(0) );\r
994 \r
995 /* Removes several elements from the end of sequence and optionally saves them */\r
996 CVAPI(void)  cvSeqPopMulti( CvSeq* seq, void* elements,\r
997                             int count, int in_front CV_DEFAULT(0) );\r
998 \r
999 /* Inserts a new element in the middle of sequence.\r
1000    cvSeqInsert(seq,0,elem) == cvSeqPushFront(seq,elem) */\r
1001 CVAPI(schar*)  cvSeqInsert( CvSeq* seq, int before_index,\r
1002                             void* element CV_DEFAULT(NULL));\r
1003 \r
1004 /* Removes specified sequence element */\r
1005 CVAPI(void)  cvSeqRemove( CvSeq* seq, int index );\r
1006 \r
1007 \r
1008 /* Removes all the elements from the sequence. The freed memory\r
1009    can be reused later only by the same sequence unless cvClearMemStorage\r
1010    or cvRestoreMemStoragePos is called */\r
1011 CVAPI(void)  cvClearSeq( CvSeq* seq );\r
1012 \r
1013 \r
1014 /* Retrieves pointer to specified sequence element.\r
1015    Negative indices are supported and mean counting from the end\r
1016    (e.g -1 means the last sequence element) */\r
1017 CVAPI(schar*)  cvGetSeqElem( const CvSeq* seq, int index );\r
1018 \r
1019 /* Calculates index of the specified sequence element.\r
1020    Returns -1 if element does not belong to the sequence */\r
1021 CVAPI(int)  cvSeqElemIdx( const CvSeq* seq, const void* element,\r
1022                          CvSeqBlock** block CV_DEFAULT(NULL) );\r
1023 \r
1024 /* Initializes sequence writer. The new elements will be added to the end of sequence */\r
1025 CVAPI(void)  cvStartAppendToSeq( CvSeq* seq, CvSeqWriter* writer );\r
1026 \r
1027 \r
1028 /* Combination of cvCreateSeq and cvStartAppendToSeq */\r
1029 CVAPI(void)  cvStartWriteSeq( int seq_flags, int header_size,\r
1030                               int elem_size, CvMemStorage* storage,\r
1031                               CvSeqWriter* writer );\r
1032 \r
1033 /* Closes sequence writer, updates sequence header and returns pointer\r
1034    to the resultant sequence\r
1035    (which may be useful if the sequence was created using cvStartWriteSeq))\r
1036 */\r
1037 CVAPI(CvSeq*)  cvEndWriteSeq( CvSeqWriter* writer );\r
1038 \r
1039 \r
1040 /* Updates sequence header. May be useful to get access to some of previously\r
1041    written elements via cvGetSeqElem or sequence reader */\r
1042 CVAPI(void)   cvFlushSeqWriter( CvSeqWriter* writer );\r
1043 \r
1044 \r
1045 /* Initializes sequence reader.\r
1046    The sequence can be read in forward or backward direction */\r
1047 CVAPI(void) cvStartReadSeq( const CvSeq* seq, CvSeqReader* reader,\r
1048                            int reverse CV_DEFAULT(0) );\r
1049 \r
1050 \r
1051 /* Returns current sequence reader position (currently observed sequence element) */\r
1052 CVAPI(int)  cvGetSeqReaderPos( CvSeqReader* reader );\r
1053 \r
1054 \r
1055 /* Changes sequence reader position. It may seek to an absolute or\r
1056    to relative to the current position */\r
1057 CVAPI(void)   cvSetSeqReaderPos( CvSeqReader* reader, int index,\r
1058                                  int is_relative CV_DEFAULT(0));\r
1059 \r
1060 /* Copies sequence content to a continuous piece of memory */\r
1061 CVAPI(void*)  cvCvtSeqToArray( const CvSeq* seq, void* elements,\r
1062                                CvSlice slice CV_DEFAULT(CV_WHOLE_SEQ) );\r
1063 \r
1064 /* Creates sequence header for array.\r
1065    After that all the operations on sequences that do not alter the content\r
1066    can be applied to the resultant sequence */\r
1067 CVAPI(CvSeq*) cvMakeSeqHeaderForArray( int seq_type, int header_size,\r
1068                                        int elem_size, void* elements, int total,\r
1069                                        CvSeq* seq, CvSeqBlock* block );\r
1070 \r
1071 /* Extracts sequence slice (with or without copying sequence elements) */\r
1072 CVAPI(CvSeq*) cvSeqSlice( const CvSeq* seq, CvSlice slice,\r
1073                          CvMemStorage* storage CV_DEFAULT(NULL),\r
1074                          int copy_data CV_DEFAULT(0));\r
1075 \r
1076 CV_INLINE CvSeq* cvCloneSeq( const CvSeq* seq, CvMemStorage* storage CV_DEFAULT(NULL))\r
1077 {\r
1078     return cvSeqSlice( seq, CV_WHOLE_SEQ, storage, 1 );\r
1079 }\r
1080 \r
1081 /* Removes sequence slice */\r
1082 CVAPI(void)  cvSeqRemoveSlice( CvSeq* seq, CvSlice slice );\r
1083 \r
1084 /* Inserts a sequence or array into another sequence */\r
1085 CVAPI(void)  cvSeqInsertSlice( CvSeq* seq, int before_index, const CvArr* from_arr );\r
1086 \r
1087 /* a < b ? -1 : a > b ? 1 : 0 */\r
1088 typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata );\r
1089 \r
1090 /* Sorts sequence in-place given element comparison function */\r
1091 CVAPI(void) cvSeqSort( CvSeq* seq, CvCmpFunc func, void* userdata CV_DEFAULT(NULL) );\r
1092 \r
1093 /* Finds element in a [sorted] sequence */\r
1094 CVAPI(schar*) cvSeqSearch( CvSeq* seq, const void* elem, CvCmpFunc func,\r
1095                            int is_sorted, int* elem_idx,\r
1096                            void* userdata CV_DEFAULT(NULL) );\r
1097 \r
1098 /* Reverses order of sequence elements in-place */\r
1099 CVAPI(void) cvSeqInvert( CvSeq* seq );\r
1100 \r
1101 /* Splits sequence into one or more equivalence classes using the specified criteria */\r
1102 CVAPI(int)  cvSeqPartition( const CvSeq* seq, CvMemStorage* storage,\r
1103                             CvSeq** labels, CvCmpFunc is_equal, void* userdata );\r
1104 \r
1105 /************ Internal sequence functions ************/\r
1106 CVAPI(void)  cvChangeSeqBlock( void* reader, int direction );\r
1107 CVAPI(void)  cvCreateSeqBlock( CvSeqWriter* writer );\r
1108 \r
1109 \r
1110 /* Creates a new set */\r
1111 CVAPI(CvSet*)  cvCreateSet( int set_flags, int header_size,\r
1112                             int elem_size, CvMemStorage* storage );\r
1113 \r
1114 /* Adds new element to the set and returns pointer to it */\r
1115 CVAPI(int)  cvSetAdd( CvSet* set_header, CvSetElem* elem CV_DEFAULT(NULL),\r
1116                       CvSetElem** inserted_elem CV_DEFAULT(NULL) );\r
1117 \r
1118 /* Fast variant of cvSetAdd */\r
1119 CV_INLINE  CvSetElem* cvSetNew( CvSet* set_header )\r
1120 {\r
1121     CvSetElem* elem = set_header->free_elems;\r
1122     if( elem )\r
1123     {\r
1124         set_header->free_elems = elem->next_free;\r
1125         elem->flags = elem->flags & CV_SET_ELEM_IDX_MASK;\r
1126         set_header->active_count++;\r
1127     }\r
1128     else\r
1129         cvSetAdd( set_header, NULL, (CvSetElem**)&elem );\r
1130     return elem;\r
1131 }\r
1132 \r
1133 /* Removes set element given its pointer */\r
1134 CV_INLINE  void cvSetRemoveByPtr( CvSet* set_header, void* elem )\r
1135 {\r
1136     CvSetElem* _elem = (CvSetElem*)elem;\r
1137     assert( _elem->flags >= 0 /*&& (elem->flags & CV_SET_ELEM_IDX_MASK) < set_header->total*/ );\r
1138     _elem->next_free = set_header->free_elems;\r
1139     _elem->flags = (_elem->flags & CV_SET_ELEM_IDX_MASK) | CV_SET_ELEM_FREE_FLAG;\r
1140     set_header->free_elems = _elem;\r
1141     set_header->active_count--;\r
1142 }\r
1143 \r
1144 /* Removes element from the set by its index  */\r
1145 CVAPI(void)   cvSetRemove( CvSet* set_header, int index );\r
1146 \r
1147 /* Returns a set element by index. If the element doesn't belong to the set,\r
1148    NULL is returned */\r
1149 CV_INLINE CvSetElem* cvGetSetElem( const CvSet* set_header, int index )\r
1150 {\r
1151     CvSetElem* elem = (CvSetElem*)cvGetSeqElem( (CvSeq*)set_header, index );\r
1152     return elem && CV_IS_SET_ELEM( elem ) ? elem : 0;\r
1153 }\r
1154 \r
1155 /* Removes all the elements from the set */\r
1156 CVAPI(void)  cvClearSet( CvSet* set_header );\r
1157 \r
1158 /* Creates new graph */\r
1159 CVAPI(CvGraph*)  cvCreateGraph( int graph_flags, int header_size,\r
1160                                 int vtx_size, int edge_size,\r
1161                                 CvMemStorage* storage );\r
1162 \r
1163 /* Adds new vertex to the graph */\r
1164 CVAPI(int)  cvGraphAddVtx( CvGraph* graph, const CvGraphVtx* vtx CV_DEFAULT(NULL),\r
1165                            CvGraphVtx** inserted_vtx CV_DEFAULT(NULL) );\r
1166 \r
1167 \r
1168 /* Removes vertex from the graph together with all incident edges */\r
1169 CVAPI(int)  cvGraphRemoveVtx( CvGraph* graph, int index );\r
1170 CVAPI(int)  cvGraphRemoveVtxByPtr( CvGraph* graph, CvGraphVtx* vtx );\r
1171 \r
1172 \r
1173 /* Link two vertices specifed by indices or pointers if they\r
1174    are not connected or return pointer to already existing edge\r
1175    connecting the vertices.\r
1176    Functions return 1 if a new edge was created, 0 otherwise */\r
1177 CVAPI(int)  cvGraphAddEdge( CvGraph* graph,\r
1178                             int start_idx, int end_idx,\r
1179                             const CvGraphEdge* edge CV_DEFAULT(NULL),\r
1180                             CvGraphEdge** inserted_edge CV_DEFAULT(NULL) );\r
1181 \r
1182 CVAPI(int)  cvGraphAddEdgeByPtr( CvGraph* graph,\r
1183                                CvGraphVtx* start_vtx, CvGraphVtx* end_vtx,\r
1184                                const CvGraphEdge* edge CV_DEFAULT(NULL),\r
1185                                CvGraphEdge** inserted_edge CV_DEFAULT(NULL) );\r
1186 \r
1187 /* Remove edge connecting two vertices */\r
1188 CVAPI(void)  cvGraphRemoveEdge( CvGraph* graph, int start_idx, int end_idx );\r
1189 CVAPI(void)  cvGraphRemoveEdgeByPtr( CvGraph* graph, CvGraphVtx* start_vtx,\r
1190                                      CvGraphVtx* end_vtx );\r
1191 \r
1192 /* Find edge connecting two vertices */\r
1193 CVAPI(CvGraphEdge*)  cvFindGraphEdge( const CvGraph* graph, int start_idx, int end_idx );\r
1194 CVAPI(CvGraphEdge*)  cvFindGraphEdgeByPtr( const CvGraph* graph,\r
1195                                            const CvGraphVtx* start_vtx,\r
1196                                            const CvGraphVtx* end_vtx );\r
1197 #define cvGraphFindEdge cvFindGraphEdge\r
1198 #define cvGraphFindEdgeByPtr cvFindGraphEdgeByPtr\r
1199 \r
1200 /* Remove all vertices and edges from the graph */\r
1201 CVAPI(void)  cvClearGraph( CvGraph* graph );\r
1202 \r
1203 \r
1204 /* Count number of edges incident to the vertex */\r
1205 CVAPI(int)  cvGraphVtxDegree( const CvGraph* graph, int vtx_idx );\r
1206 CVAPI(int)  cvGraphVtxDegreeByPtr( const CvGraph* graph, const CvGraphVtx* vtx );\r
1207 \r
1208 \r
1209 /* Retrieves graph vertex by given index */\r
1210 #define cvGetGraphVtx( graph, idx ) (CvGraphVtx*)cvGetSetElem((CvSet*)(graph), (idx))\r
1211 \r
1212 /* Retrieves index of a graph vertex given its pointer */\r
1213 #define cvGraphVtxIdx( graph, vtx ) ((vtx)->flags & CV_SET_ELEM_IDX_MASK)\r
1214 \r
1215 /* Retrieves index of a graph edge given its pointer */\r
1216 #define cvGraphEdgeIdx( graph, edge ) ((edge)->flags & CV_SET_ELEM_IDX_MASK)\r
1217 \r
1218 #define cvGraphGetVtxCount( graph ) ((graph)->active_count)\r
1219 #define cvGraphGetEdgeCount( graph ) ((graph)->edges->active_count)\r
1220 \r
1221 #define  CV_GRAPH_VERTEX        1\r
1222 #define  CV_GRAPH_TREE_EDGE     2\r
1223 #define  CV_GRAPH_BACK_EDGE     4\r
1224 #define  CV_GRAPH_FORWARD_EDGE  8\r
1225 #define  CV_GRAPH_CROSS_EDGE    16\r
1226 #define  CV_GRAPH_ANY_EDGE      30\r
1227 #define  CV_GRAPH_NEW_TREE      32\r
1228 #define  CV_GRAPH_BACKTRACKING  64\r
1229 #define  CV_GRAPH_OVER          -1\r
1230 \r
1231 #define  CV_GRAPH_ALL_ITEMS    -1\r
1232 \r
1233 /* flags for graph vertices and edges */\r
1234 #define  CV_GRAPH_ITEM_VISITED_FLAG  (1 << 30)\r
1235 #define  CV_IS_GRAPH_VERTEX_VISITED(vtx) \\r
1236     (((CvGraphVtx*)(vtx))->flags & CV_GRAPH_ITEM_VISITED_FLAG)\r
1237 #define  CV_IS_GRAPH_EDGE_VISITED(edge) \\r
1238     (((CvGraphEdge*)(edge))->flags & CV_GRAPH_ITEM_VISITED_FLAG)\r
1239 #define  CV_GRAPH_SEARCH_TREE_NODE_FLAG   (1 << 29)\r
1240 #define  CV_GRAPH_FORWARD_EDGE_FLAG       (1 << 28)\r
1241 \r
1242 typedef struct CvGraphScanner\r
1243 {\r
1244     CvGraphVtx* vtx;       /* current graph vertex (or current edge origin) */\r
1245     CvGraphVtx* dst;       /* current graph edge destination vertex */\r
1246     CvGraphEdge* edge;     /* current edge */\r
1247 \r
1248     CvGraph* graph;        /* the graph */\r
1249     CvSeq*   stack;        /* the graph vertex stack */\r
1250     int      index;        /* the lower bound of certainly visited vertices */\r
1251     int      mask;         /* event mask */\r
1252 }\r
1253 CvGraphScanner;\r
1254 \r
1255 /* Creates new graph scanner. */\r
1256 CVAPI(CvGraphScanner*)  cvCreateGraphScanner( CvGraph* graph,\r
1257                                              CvGraphVtx* vtx CV_DEFAULT(NULL),\r
1258                                              int mask CV_DEFAULT(CV_GRAPH_ALL_ITEMS));\r
1259 \r
1260 /* Releases graph scanner. */\r
1261 CVAPI(void) cvReleaseGraphScanner( CvGraphScanner** scanner );\r
1262 \r
1263 /* Get next graph element */\r
1264 CVAPI(int)  cvNextGraphItem( CvGraphScanner* scanner );\r
1265 \r
1266 /* Creates a copy of graph */\r
1267 CVAPI(CvGraph*) cvCloneGraph( const CvGraph* graph, CvMemStorage* storage );\r
1268 \r
1269 /****************************************************************************************\\r
1270 *                                     Drawing                                            *\r
1271 \****************************************************************************************/\r
1272 \r
1273 /****************************************************************************************\\r
1274 *       Drawing functions work with images/matrices of arbitrary type.                   *\r
1275 *       For color images the channel order is BGR[A]                                     *\r
1276 *       Antialiasing is supported only for 8-bit image now.                              *\r
1277 *       All the functions include parameter color that means rgb value (that may be      *\r
1278 *       constructed with CV_RGB macro) for color images and brightness                   *\r
1279 *       for grayscale images.                                                            *\r
1280 *       If a drawn figure is partially or completely outside of the image, it is clipped.*\r
1281 \****************************************************************************************/\r
1282 \r
1283 #define CV_RGB( r, g, b )  cvScalar( (b), (g), (r), 0 )\r
1284 #define CV_FILLED -1\r
1285 \r
1286 #define CV_AA 16\r
1287 \r
1288 /* Draws 4-connected, 8-connected or antialiased line segment connecting two points */\r
1289 CVAPI(void)  cvLine( CvArr* img, CvPoint pt1, CvPoint pt2,\r
1290                      CvScalar color, int thickness CV_DEFAULT(1),\r
1291                      int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );\r
1292 \r
1293 /* Draws a rectangle given two opposite corners of the rectangle (pt1 & pt2),\r
1294    if thickness<0 (e.g. thickness == CV_FILLED), the filled box is drawn */\r
1295 CVAPI(void)  cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2,\r
1296                           CvScalar color, int thickness CV_DEFAULT(1),\r
1297                           int line_type CV_DEFAULT(8),\r
1298                           int shift CV_DEFAULT(0));\r
1299 \r
1300 /* Draws a circle with specified center and radius.\r
1301    Thickness works in the same way as with cvRectangle */\r
1302 CVAPI(void)  cvCircle( CvArr* img, CvPoint center, int radius,\r
1303                        CvScalar color, int thickness CV_DEFAULT(1),\r
1304                        int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0));\r
1305 \r
1306 /* Draws ellipse outline, filled ellipse, elliptic arc or filled elliptic sector,\r
1307    depending on <thickness>, <start_angle> and <end_angle> parameters. The resultant figure\r
1308    is rotated by <angle>. All the angles are in degrees */\r
1309 CVAPI(void)  cvEllipse( CvArr* img, CvPoint center, CvSize axes,\r
1310                         double angle, double start_angle, double end_angle,\r
1311                         CvScalar color, int thickness CV_DEFAULT(1),\r
1312                         int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0));\r
1313 \r
1314 CV_INLINE  void  cvEllipseBox( CvArr* img, CvBox2D box, CvScalar color,\r
1315                                int thickness CV_DEFAULT(1),\r
1316                                int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) )\r
1317 {\r
1318     CvSize axes;\r
1319     axes.width = cvRound(box.size.height*0.5);\r
1320     axes.height = cvRound(box.size.width*0.5);\r
1321 \r
1322     cvEllipse( img, cvPointFrom32f( box.center ), axes, box.angle,\r
1323                0, 360, color, thickness, line_type, shift );\r
1324 }\r
1325 \r
1326 /* Fills convex or monotonous polygon. */\r
1327 CVAPI(void)  cvFillConvexPoly( CvArr* img, CvPoint* pts, int npts, CvScalar color,\r
1328                                int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0));\r
1329 \r
1330 /* Fills an area bounded by one or more arbitrary polygons */\r
1331 CVAPI(void)  cvFillPoly( CvArr* img, CvPoint** pts, int* npts, int contours, CvScalar color,\r
1332                          int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );\r
1333 \r
1334 /* Draws one or more polygonal curves */\r
1335 CVAPI(void)  cvPolyLine( CvArr* img, CvPoint** pts, int* npts, int contours,\r
1336                          int is_closed, CvScalar color, int thickness CV_DEFAULT(1),\r
1337                          int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );\r
1338 \r
1339 #define cvDrawRect cvRectangle\r
1340 #define cvDrawLine cvLine\r
1341 #define cvDrawCircle cvCircle\r
1342 #define cvDrawEllipse cvEllipse\r
1343 #define cvDrawPolyLine cvPolyLine\r
1344 \r
1345 /* Clips the line segment connecting *pt1 and *pt2\r
1346    by the rectangular window\r
1347    (0<=x<img_size.width, 0<=y<img_size.height). */\r
1348 CVAPI(int) cvClipLine( CvSize img_size, CvPoint* pt1, CvPoint* pt2 );\r
1349 \r
1350 /* Initializes line iterator. Initially, line_iterator->ptr will point\r
1351    to pt1 (or pt2, see left_to_right description) location in the image.\r
1352    Returns the number of pixels on the line between the ending points. */\r
1353 CVAPI(int)  cvInitLineIterator( const CvArr* image, CvPoint pt1, CvPoint pt2,\r
1354                                 CvLineIterator* line_iterator,\r
1355                                 int connectivity CV_DEFAULT(8),\r
1356                                 int left_to_right CV_DEFAULT(0));\r
1357 \r
1358 /* Moves iterator to the next line point */\r
1359 #define CV_NEXT_LINE_POINT( line_iterator )                     \\r
1360 {                                                               \\r
1361     int _line_iterator_mask = (line_iterator).err < 0 ? -1 : 0; \\r
1362     (line_iterator).err += (line_iterator).minus_delta +        \\r
1363         ((line_iterator).plus_delta & _line_iterator_mask);     \\r
1364     (line_iterator).ptr += (line_iterator).minus_step +         \\r
1365         ((line_iterator).plus_step & _line_iterator_mask);      \\r
1366 }\r
1367 \r
1368 \r
1369 /* basic font types */\r
1370 #define CV_FONT_HERSHEY_SIMPLEX         0\r
1371 #define CV_FONT_HERSHEY_PLAIN           1\r
1372 #define CV_FONT_HERSHEY_DUPLEX          2\r
1373 #define CV_FONT_HERSHEY_COMPLEX         3\r
1374 #define CV_FONT_HERSHEY_TRIPLEX         4\r
1375 #define CV_FONT_HERSHEY_COMPLEX_SMALL   5\r
1376 #define CV_FONT_HERSHEY_SCRIPT_SIMPLEX  6\r
1377 #define CV_FONT_HERSHEY_SCRIPT_COMPLEX  7\r
1378 \r
1379 /* font flags */\r
1380 #define CV_FONT_ITALIC                 16\r
1381 \r
1382 #define CV_FONT_VECTOR0    CV_FONT_HERSHEY_SIMPLEX\r
1383 \r
1384 /* Font structure */\r
1385 typedef struct CvFont\r
1386 {\r
1387     int         font_face; /* =CV_FONT_* */\r
1388     const int*  ascii; /* font data and metrics */\r
1389     const int*  greek;\r
1390     const int*  cyrillic;\r
1391     float       hscale, vscale;\r
1392     float       shear; /* slope coefficient: 0 - normal, >0 - italic */\r
1393     int         thickness; /* letters thickness */\r
1394     float       dx; /* horizontal interval between letters */\r
1395     int         line_type;\r
1396 }\r
1397 CvFont;\r
1398 \r
1399 /* Initializes font structure used further in cvPutText */\r
1400 CVAPI(void)  cvInitFont( CvFont* font, int font_face,\r
1401                          double hscale, double vscale,\r
1402                          double shear CV_DEFAULT(0),\r
1403                          int thickness CV_DEFAULT(1),\r
1404                          int line_type CV_DEFAULT(8));\r
1405 \r
1406 CV_INLINE CvFont cvFont( double scale, int thickness CV_DEFAULT(1) )\r
1407 {\r
1408     CvFont font;\r
1409     cvInitFont( &font, CV_FONT_HERSHEY_PLAIN, scale, scale, 0, thickness, CV_AA );\r
1410     return font;\r
1411 }\r
1412 \r
1413 /* Renders text stroke with specified font and color at specified location.\r
1414    CvFont should be initialized with cvInitFont */\r
1415 CVAPI(void)  cvPutText( CvArr* img, const char* text, CvPoint org,\r
1416                         const CvFont* font, CvScalar color );\r
1417 \r
1418 /* Calculates bounding box of text stroke (useful for alignment) */\r
1419 CVAPI(void)  cvGetTextSize( const char* text_string, const CvFont* font,\r
1420                             CvSize* text_size, int* baseline );\r
1421 \r
1422 /* Unpacks color value, if arrtype is CV_8UC?, <color> is treated as\r
1423    packed color value, otherwise the first channels (depending on arrtype)\r
1424    of destination scalar are set to the same value = <color> */\r
1425 CVAPI(CvScalar)  cvColorToScalar( double packed_color, int arrtype );\r
1426 \r
1427 /* Returns the polygon points which make up the given ellipse.  The ellipse is define by\r
1428    the box of size 'axes' rotated 'angle' around the 'center'.  A partial sweep\r
1429    of the ellipse arc can be done by spcifying arc_start and arc_end to be something\r
1430    other than 0 and 360, respectively.  The input array 'pts' must be large enough to\r
1431    hold the result.  The total number of points stored into 'pts' is returned by this\r
1432    function. */\r
1433 CVAPI(int) cvEllipse2Poly( CvPoint center, CvSize axes,\r
1434                  int angle, int arc_start, int arc_end, CvPoint * pts, int delta );\r
1435 \r
1436 /* Draws contour outlines or filled interiors on the image */\r
1437 CVAPI(void)  cvDrawContours( CvArr *img, CvSeq* contour,\r
1438                              CvScalar external_color, CvScalar hole_color,\r
1439                              int max_level, int thickness CV_DEFAULT(1),\r
1440                              int line_type CV_DEFAULT(8),\r
1441                              CvPoint offset CV_DEFAULT(cvPoint(0,0)));\r
1442 \r
1443 /* Does look-up transformation. Elements of the source array\r
1444    (that should be 8uC1 or 8sC1) are used as indexes in lutarr 256-element table */\r
1445 CVAPI(void) cvLUT( const CvArr* src, CvArr* dst, const CvArr* lut );\r
1446 \r
1447 \r
1448 /******************* Iteration through the sequence tree *****************/\r
1449 typedef struct CvTreeNodeIterator\r
1450 {\r
1451     const void* node;\r
1452     int level;\r
1453     int max_level;\r
1454 }\r
1455 CvTreeNodeIterator;\r
1456 \r
1457 CVAPI(void) cvInitTreeNodeIterator( CvTreeNodeIterator* tree_iterator,\r
1458                                    const void* first, int max_level );\r
1459 CVAPI(void*) cvNextTreeNode( CvTreeNodeIterator* tree_iterator );\r
1460 CVAPI(void*) cvPrevTreeNode( CvTreeNodeIterator* tree_iterator );\r
1461 \r
1462 /* Inserts sequence into tree with specified "parent" sequence.\r
1463    If parent is equal to frame (e.g. the most external contour),\r
1464    then added contour will have null pointer to parent. */\r
1465 CVAPI(void) cvInsertNodeIntoTree( void* node, void* parent, void* frame );\r
1466 \r
1467 /* Removes contour from tree (together with the contour children). */\r
1468 CVAPI(void) cvRemoveNodeFromTree( void* node, void* frame );\r
1469 \r
1470 /* Gathers pointers to all the sequences,\r
1471    accessible from the <first>, to the single sequence */\r
1472 CVAPI(CvSeq*) cvTreeToNodeSeq( const void* first, int header_size,\r
1473                               CvMemStorage* storage );\r
1474 \r
1475 /* The function implements the K-means algorithm for clustering an array of sample\r
1476    vectors in a specified number of classes */\r
1477 #define CV_KMEANS_USE_INITIAL_LABELS    1\r
1478 CVAPI(int) cvKMeans2( const CvArr* samples, int cluster_count, CvArr* labels,\r
1479                       CvTermCriteria termcrit, int attempts CV_DEFAULT(1),\r
1480                       CvRNG* rng CV_DEFAULT(0), int flags CV_DEFAULT(0),\r
1481                       CvArr* _centers CV_DEFAULT(0), double* compactness CV_DEFAULT(0) );\r
1482 \r
1483 /****************************************************************************************\\r
1484 *                                    System functions                                    *\r
1485 \****************************************************************************************/\r
1486 \r
1487 /* Add the function pointers table with associated information to the IPP primitives list */\r
1488 CVAPI(int)  cvRegisterModule( const CvModuleInfo* module_info );\r
1489 \r
1490 /* Loads optimized functions from IPP, MKL etc. or switches back to pure C code */\r
1491 CVAPI(int)  cvUseOptimized( int on_off );\r
1492 \r
1493 /* Retrieves information about the registered modules and loaded optimized plugins */\r
1494 CVAPI(void)  cvGetModuleInfo( const char* module_name,\r
1495                               const char** version,\r
1496                               const char** loaded_addon_plugins );\r
1497 \r
1498 /* Get current OpenCV error status */\r
1499 CVAPI(int) cvGetErrStatus( void );\r
1500 \r
1501 /* Sets error status silently */\r
1502 CVAPI(void) cvSetErrStatus( int status );\r
1503 \r
1504 #define CV_ErrModeLeaf     0   /* Print error and exit program */\r
1505 #define CV_ErrModeParent   1   /* Print error and continue */\r
1506 #define CV_ErrModeSilent   2   /* Don't print and continue */\r
1507 \r
1508 /* Retrives current error processing mode */\r
1509 CVAPI(int)  cvGetErrMode( void );\r
1510 \r
1511 /* Sets error processing mode, returns previously used mode */\r
1512 CVAPI(int) cvSetErrMode( int mode );\r
1513 \r
1514 /* Sets error status and performs some additonal actions (displaying message box,\r
1515    writing message to stderr, terminating application etc.)\r
1516    depending on the current error mode */\r
1517 CVAPI(void) cvError( int status, const char* func_name,\r
1518                     const char* err_msg, const char* file_name, int line );\r
1519 \r
1520 /* Retrieves textual description of the error given its code */\r
1521 CVAPI(const char*) cvErrorStr( int status );\r
1522 \r
1523 /* Retrieves detailed information about the last error occured */\r
1524 CVAPI(int) cvGetErrInfo( const char** errcode_desc, const char** description,\r
1525                         const char** filename, int* line );\r
1526 \r
1527 /* Maps IPP error codes to the counterparts from OpenCV */\r
1528 CVAPI(int) cvErrorFromIppStatus( int ipp_status );\r
1529 \r
1530 typedef int (CV_CDECL *CvErrorCallback)( int status, const char* func_name,\r
1531                     const char* err_msg, const char* file_name, int line, void* userdata );\r
1532 \r
1533 /* Assigns a new error-handling function */\r
1534 CVAPI(CvErrorCallback) cvRedirectError( CvErrorCallback error_handler,\r
1535                                        void* userdata CV_DEFAULT(NULL),\r
1536                                        void** prev_userdata CV_DEFAULT(NULL) );\r
1537 \r
1538 /*\r
1539     Output to:\r
1540         cvNulDevReport - nothing\r
1541         cvStdErrReport - console(fprintf(stderr,...))\r
1542         cvGuiBoxReport - MessageBox(WIN32)\r
1543 */\r
1544 CVAPI(int) cvNulDevReport( int status, const char* func_name, const char* err_msg,\r
1545                           const char* file_name, int line, void* userdata );\r
1546 \r
1547 CVAPI(int) cvStdErrReport( int status, const char* func_name, const char* err_msg,\r
1548                           const char* file_name, int line, void* userdata );\r
1549 \r
1550 CVAPI(int) cvGuiBoxReport( int status, const char* func_name, const char* err_msg,\r
1551                           const char* file_name, int line, void* userdata );\r
1552 \r
1553 typedef void* (CV_CDECL *CvAllocFunc)(size_t size, void* userdata);\r
1554 typedef int (CV_CDECL *CvFreeFunc)(void* pptr, void* userdata);\r
1555 \r
1556 /* Set user-defined memory managment functions (substitutors for malloc and free) that\r
1557    will be called by cvAlloc, cvFree and higher-level functions (e.g. cvCreateImage) */\r
1558 CVAPI(void) cvSetMemoryManager( CvAllocFunc alloc_func CV_DEFAULT(NULL),\r
1559                                CvFreeFunc free_func CV_DEFAULT(NULL),\r
1560                                void* userdata CV_DEFAULT(NULL));\r
1561 \r
1562 \r
1563 typedef IplImage* (CV_STDCALL* Cv_iplCreateImageHeader)\r
1564                             (int,int,int,char*,char*,int,int,int,int,int,\r
1565                             IplROI*,IplImage*,void*,IplTileInfo*);\r
1566 typedef void (CV_STDCALL* Cv_iplAllocateImageData)(IplImage*,int,int);\r
1567 typedef void (CV_STDCALL* Cv_iplDeallocate)(IplImage*,int);\r
1568 typedef IplROI* (CV_STDCALL* Cv_iplCreateROI)(int,int,int,int,int);\r
1569 typedef IplImage* (CV_STDCALL* Cv_iplCloneImage)(const IplImage*);\r
1570 \r
1571 /* Makes OpenCV use IPL functions for IplImage allocation/deallocation */\r
1572 CVAPI(void) cvSetIPLAllocators( Cv_iplCreateImageHeader create_header,\r
1573                                Cv_iplAllocateImageData allocate_data,\r
1574                                Cv_iplDeallocate deallocate,\r
1575                                Cv_iplCreateROI create_roi,\r
1576                                Cv_iplCloneImage clone_image );\r
1577 \r
1578 #define CV_TURN_ON_IPL_COMPATIBILITY()                                  \\r
1579     cvSetIPLAllocators( iplCreateImageHeader, iplAllocateImage,         \\r
1580                         iplDeallocate, iplCreateROI, iplCloneImage )\r
1581 \r
1582 /****************************************************************************************\\r
1583 *                                    Data Persistence                                    *\r
1584 \****************************************************************************************/\r
1585 \r
1586 /********************************** High-level functions ********************************/\r
1587 \r
1588 /* opens existing or creates new file storage */\r
1589 CVAPI(CvFileStorage*)  cvOpenFileStorage( const char* filename,\r
1590                                           CvMemStorage* memstorage,\r
1591                                           int flags );\r
1592 \r
1593 /* closes file storage and deallocates buffers */\r
1594 CVAPI(void) cvReleaseFileStorage( CvFileStorage** fs );\r
1595 \r
1596 /* returns attribute value or 0 (NULL) if there is no such attribute */\r
1597 CVAPI(const char*) cvAttrValue( const CvAttrList* attr, const char* attr_name );\r
1598 \r
1599 /* starts writing compound structure (map or sequence) */\r
1600 CVAPI(void) cvStartWriteStruct( CvFileStorage* fs, const char* name,\r
1601                                 int struct_flags, const char* type_name CV_DEFAULT(NULL),\r
1602                                 CvAttrList attributes CV_DEFAULT(cvAttrList()));\r
1603 \r
1604 /* finishes writing compound structure */\r
1605 CVAPI(void) cvEndWriteStruct( CvFileStorage* fs );\r
1606 \r
1607 /* writes an integer */\r
1608 CVAPI(void) cvWriteInt( CvFileStorage* fs, const char* name, int value );\r
1609 \r
1610 /* writes a floating-point number */\r
1611 CVAPI(void) cvWriteReal( CvFileStorage* fs, const char* name, double value );\r
1612 \r
1613 /* writes a string */\r
1614 CVAPI(void) cvWriteString( CvFileStorage* fs, const char* name,\r
1615                            const char* str, int quote CV_DEFAULT(0) );\r
1616 \r
1617 /* writes a comment */\r
1618 CVAPI(void) cvWriteComment( CvFileStorage* fs, const char* comment,\r
1619                             int eol_comment );\r
1620 \r
1621 /* writes instance of a standard type (matrix, image, sequence, graph etc.)\r
1622    or user-defined type */\r
1623 CVAPI(void) cvWrite( CvFileStorage* fs, const char* name, const void* ptr,\r
1624                          CvAttrList attributes CV_DEFAULT(cvAttrList()));\r
1625 \r
1626 /* starts the next stream */\r
1627 CVAPI(void) cvStartNextStream( CvFileStorage* fs );\r
1628 \r
1629 /* helper function: writes multiple integer or floating-point numbers */\r
1630 CVAPI(void) cvWriteRawData( CvFileStorage* fs, const void* src,\r
1631                                 int len, const char* dt );\r
1632 \r
1633 /* returns the hash entry corresponding to the specified literal key string or 0\r
1634    if there is no such a key in the storage */\r
1635 CVAPI(CvStringHashNode*) cvGetHashedKey( CvFileStorage* fs, const char* name,\r
1636                                         int len CV_DEFAULT(-1),\r
1637                                         int create_missing CV_DEFAULT(0));\r
1638 \r
1639 /* returns file node with the specified key within the specified map\r
1640    (collection of named nodes) */\r
1641 CVAPI(CvFileNode*) cvGetRootFileNode( const CvFileStorage* fs,\r
1642                                      int stream_index CV_DEFAULT(0) );\r
1643 \r
1644 /* returns file node with the specified key within the specified map\r
1645    (collection of named nodes) */\r
1646 CVAPI(CvFileNode*) cvGetFileNode( CvFileStorage* fs, CvFileNode* map,\r
1647                                  const CvStringHashNode* key,\r
1648                                  int create_missing CV_DEFAULT(0) );\r
1649 \r
1650 /* this is a slower version of cvGetFileNode that takes the key as a literal string */\r
1651 CVAPI(CvFileNode*) cvGetFileNodeByName( const CvFileStorage* fs,\r
1652                                        const CvFileNode* map,\r
1653                                        const char* name );\r
1654 \r
1655 CV_INLINE int cvReadInt( const CvFileNode* node, int default_value CV_DEFAULT(0) )\r
1656 {\r
1657     return !node ? default_value :\r
1658         CV_NODE_IS_INT(node->tag) ? node->data.i :\r
1659         CV_NODE_IS_REAL(node->tag) ? cvRound(node->data.f) : 0x7fffffff;\r
1660 }\r
1661 \r
1662 \r
1663 CV_INLINE int cvReadIntByName( const CvFileStorage* fs, const CvFileNode* map,\r
1664                          const char* name, int default_value CV_DEFAULT(0) )\r
1665 {\r
1666     return cvReadInt( cvGetFileNodeByName( fs, map, name ), default_value );\r
1667 }\r
1668 \r
1669 \r
1670 CV_INLINE double cvReadReal( const CvFileNode* node, double default_value CV_DEFAULT(0.) )\r
1671 {\r
1672     return !node ? default_value :\r
1673         CV_NODE_IS_INT(node->tag) ? (double)node->data.i :\r
1674         CV_NODE_IS_REAL(node->tag) ? node->data.f : 1e300;\r
1675 }\r
1676 \r
1677 \r
1678 CV_INLINE double cvReadRealByName( const CvFileStorage* fs, const CvFileNode* map,\r
1679                         const char* name, double default_value CV_DEFAULT(0.) )\r
1680 {\r
1681     return cvReadReal( cvGetFileNodeByName( fs, map, name ), default_value );\r
1682 }\r
1683 \r
1684 \r
1685 CV_INLINE const char* cvReadString( const CvFileNode* node,\r
1686                         const char* default_value CV_DEFAULT(NULL) )\r
1687 {\r
1688     return !node ? default_value : CV_NODE_IS_STRING(node->tag) ? node->data.str.ptr : 0;\r
1689 }\r
1690 \r
1691 \r
1692 CV_INLINE const char* cvReadStringByName( const CvFileStorage* fs, const CvFileNode* map,\r
1693                         const char* name, const char* default_value CV_DEFAULT(NULL) )\r
1694 {\r
1695     return cvReadString( cvGetFileNodeByName( fs, map, name ), default_value );\r
1696 }\r
1697 \r
1698 \r
1699 /* decodes standard or user-defined object and returns it */\r
1700 CVAPI(void*) cvRead( CvFileStorage* fs, CvFileNode* node,\r
1701                         CvAttrList* attributes CV_DEFAULT(NULL));\r
1702 \r
1703 /* decodes standard or user-defined object and returns it */\r
1704 CV_INLINE void* cvReadByName( CvFileStorage* fs, const CvFileNode* map,\r
1705                               const char* name, CvAttrList* attributes CV_DEFAULT(NULL) )\r
1706 {\r
1707     return cvRead( fs, cvGetFileNodeByName( fs, map, name ), attributes );\r
1708 }\r
1709 \r
1710 \r
1711 /* starts reading data from sequence or scalar numeric node */\r
1712 CVAPI(void) cvStartReadRawData( const CvFileStorage* fs, const CvFileNode* src,\r
1713                                CvSeqReader* reader );\r
1714 \r
1715 /* reads multiple numbers and stores them to array */\r
1716 CVAPI(void) cvReadRawDataSlice( const CvFileStorage* fs, CvSeqReader* reader,\r
1717                                int count, void* dst, const char* dt );\r
1718 \r
1719 /* combination of two previous functions for easier reading of whole sequences */\r
1720 CVAPI(void) cvReadRawData( const CvFileStorage* fs, const CvFileNode* src,\r
1721                           void* dst, const char* dt );\r
1722 \r
1723 /* writes a copy of file node to file storage */\r
1724 CVAPI(void) cvWriteFileNode( CvFileStorage* fs, const char* new_node_name,\r
1725                             const CvFileNode* node, int embed );\r
1726 \r
1727 /* returns name of file node */\r
1728 CVAPI(const char*) cvGetFileNodeName( const CvFileNode* node );\r
1729 \r
1730 /*********************************** Adding own types ***********************************/\r
1731 \r
1732 CVAPI(void) cvRegisterType( const CvTypeInfo* info );\r
1733 CVAPI(void) cvUnregisterType( const char* type_name );\r
1734 CVAPI(CvTypeInfo*) cvFirstType(void);\r
1735 CVAPI(CvTypeInfo*) cvFindType( const char* type_name );\r
1736 CVAPI(CvTypeInfo*) cvTypeOf( const void* struct_ptr );\r
1737 \r
1738 /* universal functions */\r
1739 CVAPI(void) cvRelease( void** struct_ptr );\r
1740 CVAPI(void*) cvClone( const void* struct_ptr );\r
1741 \r
1742 /* simple API for reading/writing data */\r
1743 CVAPI(void) cvSave( const char* filename, const void* struct_ptr,\r
1744                     const char* name CV_DEFAULT(NULL),\r
1745                     const char* comment CV_DEFAULT(NULL),\r
1746                     CvAttrList attributes CV_DEFAULT(cvAttrList()));\r
1747 CVAPI(void*) cvLoad( const char* filename,\r
1748                      CvMemStorage* memstorage CV_DEFAULT(NULL),\r
1749                      const char* name CV_DEFAULT(NULL),\r
1750                      const char** real_name CV_DEFAULT(NULL) );\r
1751 \r
1752 /*********************************** Measuring Execution Time ***************************/\r
1753 \r
1754 /* helper functions for RNG initialization and accurate time measurement:\r
1755    uses internal clock counter on x86 */\r
1756 CVAPI(int64)  cvGetTickCount( void );\r
1757 CVAPI(double) cvGetTickFrequency( void );\r
1758 \r
1759 /*********************************** Multi-Threading ************************************/\r
1760 \r
1761 /* retrieve/set the number of threads used in OpenMP implementations */\r
1762 CVAPI(int)  cvGetNumThreads( void );\r
1763 CVAPI(void) cvSetNumThreads( int threads CV_DEFAULT(0) );\r
1764 /* get index of the thread being executed */\r
1765 CVAPI(int)  cvGetThreadNum( void );\r
1766 \r
1767 /*************** Convenience functions for better interaction with HighGUI **************/\r
1768 \r
1769 typedef IplImage* (CV_CDECL * CvLoadImageFunc)( const char* filename, int colorness );\r
1770 typedef CvMat* (CV_CDECL * CvLoadImageMFunc)( const char* filename, int colorness );\r
1771 typedef int (CV_CDECL * CvSaveImageFunc)( const char* filename, const CvArr* image );\r
1772 typedef void (CV_CDECL * CvShowImageFunc)( const char* windowname, const CvArr* image );\r
1773 \r
1774 CVAPI(int) cvSetImageIOFunctions( CvLoadImageFunc _load_image, CvLoadImageMFunc _load_image_m,\r
1775                             CvSaveImageFunc _save_image, CvShowImageFunc _show_image );\r
1776 \r
1777 #define CV_SET_IMAGE_IO_FUNCTIONS() \\r
1778     cvSetImageIOFunctions( cvLoadImage, cvLoadImageM, cvSaveImage, cvShowImage )\r
1779 \r
1780 #ifdef __cplusplus\r
1781 }\r
1782 \r
1783 class CV_EXPORTS CvImage\r
1784 {\r
1785 public:\r
1786     CvImage() : image(0), refcount(0) {}\r
1787     CvImage( CvSize size, int depth, int channels )\r
1788     {\r
1789         image = cvCreateImage( size, depth, channels );\r
1790         refcount = image ? new int(1) : 0;\r
1791     }\r
1792 \r
1793     CvImage( IplImage* img ) : image(img)\r
1794     {\r
1795         refcount = image ? new int(1) : 0;\r
1796     }\r
1797 \r
1798     CvImage( const CvImage& img ) : image(img.image), refcount(img.refcount)\r
1799     {\r
1800         if( refcount ) ++(*refcount);\r
1801     }\r
1802 \r
1803     CvImage( const char* filename, const char* imgname=0, int color=-1 ) : image(0), refcount(0)\r
1804     { load( filename, imgname, color ); }\r
1805 \r
1806     CvImage( CvFileStorage* fs, const char* mapname, const char* imgname ) : image(0), refcount(0)\r
1807     { read( fs, mapname, imgname ); }\r
1808 \r
1809     CvImage( CvFileStorage* fs, const char* seqname, int idx ) : image(0), refcount(0)\r
1810     { read( fs, seqname, idx ); }\r
1811 \r
1812     ~CvImage()\r
1813     {\r
1814         if( refcount && !(--*refcount) )\r
1815         {\r
1816             cvReleaseImage( &image );\r
1817             delete refcount;\r
1818         }\r
1819     }\r
1820 \r
1821     CvImage clone() { return CvImage(image ? cvCloneImage(image) : 0); }\r
1822 \r
1823     void create( CvSize size, int depth, int channels )\r
1824     {\r
1825         if( !image || !refcount ||\r
1826             image->width != size.width || image->height != size.height ||\r
1827             image->depth != depth || image->nChannels != channels )\r
1828             attach( cvCreateImage( size, depth, channels ));\r
1829     }\r
1830 \r
1831     void release() { detach(); }\r
1832     void clear() { detach(); }\r
1833 \r
1834     void attach( IplImage* img, bool use_refcount=true )\r
1835     {\r
1836         if( refcount && --*refcount == 0 )\r
1837         {\r
1838             cvReleaseImage( &image );\r
1839             delete refcount;\r
1840         }\r
1841         image = img;\r
1842         refcount = use_refcount && image ? new int(1) : 0;\r
1843     }\r
1844 \r
1845     void detach()\r
1846     {\r
1847         if( refcount && --*refcount == 0 )\r
1848         {\r
1849             cvReleaseImage( &image );\r
1850             delete refcount;\r
1851         }\r
1852         image = 0;\r
1853         refcount = 0;\r
1854     }\r
1855 \r
1856     bool load( const char* filename, const char* imgname=0, int color=-1 );\r
1857     bool read( CvFileStorage* fs, const char* mapname, const char* imgname );\r
1858     bool read( CvFileStorage* fs, const char* seqname, int idx );\r
1859     void save( const char* filename, const char* imgname );\r
1860     void write( CvFileStorage* fs, const char* imgname );\r
1861 \r
1862     void show( const char* window_name );\r
1863     bool is_valid() { return image != 0; }\r
1864 \r
1865     int width() const { return image ? image->width : 0; }\r
1866     int height() const { return image ? image->height : 0; }\r
1867 \r
1868     CvSize size() const { return image ? cvSize(image->width, image->height) : cvSize(0,0); }\r
1869 \r
1870     CvSize roi_size() const\r
1871     {\r
1872         return !image ? cvSize(0,0) :\r
1873             !image->roi ? cvSize(image->width,image->height) :\r
1874             cvSize(image->roi->width, image->roi->height);\r
1875     }\r
1876 \r
1877     CvRect roi() const\r
1878     {\r
1879         return !image ? cvRect(0,0,0,0) :\r
1880             !image->roi ? cvRect(0,0,image->width,image->height) :\r
1881             cvRect(image->roi->xOffset,image->roi->yOffset,\r
1882                    image->roi->width,image->roi->height);\r
1883     }\r
1884 \r
1885     int coi() const { return !image || !image->roi ? 0 : image->roi->coi; }\r
1886 \r
1887     void set_roi(CvRect roi) { cvSetImageROI(image,roi); }\r
1888     void reset_roi() { cvResetImageROI(image); }\r
1889     void set_coi(int coi) { cvSetImageCOI(image,coi); }\r
1890     int depth() const { return image ? image->depth : 0; }\r
1891     int channels() const { return image ? image->nChannels : 0; }\r
1892     int pix_size() const { return image ? ((image->depth & 255)>>3)*image->nChannels : 0; }\r
1893 \r
1894     uchar* data() { return image ? (uchar*)image->imageData : 0; }\r
1895     const uchar* data() const { return image ? (const uchar*)image->imageData : 0; }\r
1896     int step() const { return image ? image->widthStep : 0; }\r
1897     int origin() const { return image ? image->origin : 0; }\r
1898 \r
1899     uchar* roi_row(int y)\r
1900     {\r
1901         assert(0<=y);\r
1902         assert(!image ?\r
1903                 1 : image->roi ?\r
1904                 y<image->roi->height : y<image->height);\r
1905         \r
1906         return !image ? 0 :\r
1907             !image->roi ?\r
1908                 (uchar*)(image->imageData + y*image->widthStep) :\r
1909                 (uchar*)(image->imageData + (y+image->roi->yOffset)*image->widthStep +\r
1910                 image->roi->xOffset*((image->depth & 255)>>3)*image->nChannels);\r
1911     }\r
1912 \r
1913     const uchar* roi_row(int y) const\r
1914     {\r
1915         assert(0<=y);\r
1916         assert(!image ?\r
1917                 1 : image->roi ?\r
1918                 y<image->roi->height : y<image->height); \r
1919 \r
1920         return !image ? 0 :\r
1921             !image->roi ?\r
1922                 (const uchar*)(image->imageData + y*image->widthStep) :\r
1923                 (const uchar*)(image->imageData + (y+image->roi->yOffset)*image->widthStep +\r
1924                 image->roi->xOffset*((image->depth & 255)>>3)*image->nChannels);\r
1925     }\r
1926 \r
1927     operator const IplImage* () const { return image; }\r
1928     operator IplImage* () { return image; }\r
1929 \r
1930     CvImage& operator = (const CvImage& img)\r
1931     {\r
1932         if( img.refcount )\r
1933             ++*img.refcount;\r
1934         if( refcount && !(--*refcount) )\r
1935             cvReleaseImage( &image );\r
1936         image=img.image;\r
1937         refcount=img.refcount;\r
1938         return *this;\r
1939     }\r
1940 \r
1941 protected:\r
1942     IplImage* image;\r
1943     int* refcount;\r
1944 };\r
1945 \r
1946 \r
1947 class CV_EXPORTS CvMatrix\r
1948 {\r
1949 public:\r
1950     CvMatrix() : matrix(0) {}\r
1951     CvMatrix( int rows, int cols, int type )\r
1952     { matrix = cvCreateMat( rows, cols, type ); }\r
1953 \r
1954     CvMatrix( int rows, int cols, int type, CvMat* hdr,\r
1955               void* data=0, int step=CV_AUTOSTEP )\r
1956     { matrix = cvInitMatHeader( hdr, rows, cols, type, data, step ); }\r
1957 \r
1958     CvMatrix( int rows, int cols, int type, CvMemStorage* storage, bool alloc_data=true );\r
1959 \r
1960     CvMatrix( int rows, int cols, int type, void* data, int step=CV_AUTOSTEP )\r
1961     { matrix = cvCreateMatHeader( rows, cols, type );\r
1962       cvSetData( matrix, data, step ); }\r
1963 \r
1964     CvMatrix( CvMat* m )\r
1965     { matrix = m; }\r
1966 \r
1967     CvMatrix( const CvMatrix& m )\r
1968     {\r
1969         matrix = m.matrix;\r
1970         addref();\r
1971     }\r
1972 \r
1973     CvMatrix( const char* filename, const char* matname=0, int color=-1 ) : matrix(0)\r
1974     {  load( filename, matname, color ); }\r
1975 \r
1976     CvMatrix( CvFileStorage* fs, const char* mapname, const char* matname ) : matrix(0)\r
1977     {  read( fs, mapname, matname ); }\r
1978 \r
1979     CvMatrix( CvFileStorage* fs, const char* seqname, int idx ) : matrix(0)\r
1980     {  read( fs, seqname, idx ); }\r
1981 \r
1982     ~CvMatrix()\r
1983     {\r
1984         release();\r
1985     }\r
1986 \r
1987     CvMatrix clone() { return CvMatrix(matrix ? cvCloneMat(matrix) : 0); }\r
1988 \r
1989     void set( CvMat* m, bool add_ref )\r
1990     {\r
1991         release();\r
1992         matrix = m;\r
1993         if( add_ref )\r
1994             addref();\r
1995     }\r
1996 \r
1997     void create( int rows, int cols, int type )\r
1998     {\r
1999         if( !matrix || !matrix->refcount ||\r
2000             matrix->rows != rows || matrix->cols != cols ||\r
2001             CV_MAT_TYPE(matrix->type) != type )\r
2002             set( cvCreateMat( rows, cols, type ), false );\r
2003     }\r
2004 \r
2005     void addref() const\r
2006     {\r
2007         if( matrix )\r
2008         {\r
2009             if( matrix->hdr_refcount )\r
2010                 ++matrix->hdr_refcount;\r
2011             else if( matrix->refcount )\r
2012                 ++*matrix->refcount;\r
2013         }\r
2014     }\r
2015 \r
2016     void release()\r
2017     {\r
2018         if( matrix )\r
2019         {\r
2020             if( matrix->hdr_refcount )\r
2021             {\r
2022                 if( --matrix->hdr_refcount == 0 )\r
2023                     cvReleaseMat( &matrix );\r
2024             }\r
2025             else if( matrix->refcount )\r
2026             {\r
2027                 if( --*matrix->refcount == 0 )\r
2028                     cvFree( &matrix->refcount );\r
2029             }\r
2030             matrix = 0;\r
2031         }\r
2032     }\r
2033 \r
2034     void clear()\r
2035     {\r
2036         release();\r
2037     }\r
2038 \r
2039     bool load( const char* filename, const char* matname=0, int color=-1 );\r
2040     bool read( CvFileStorage* fs, const char* mapname, const char* matname );\r
2041     bool read( CvFileStorage* fs, const char* seqname, int idx );\r
2042     void save( const char* filename, const char* matname );\r
2043     void write( CvFileStorage* fs, const char* matname );\r
2044 \r
2045     void show( const char* window_name );\r
2046 \r
2047     bool is_valid() { return matrix != 0; }\r
2048 \r
2049     int rows() const { return matrix ? matrix->rows : 0; }\r
2050     int cols() const { return matrix ? matrix->cols : 0; }\r
2051 \r
2052     CvSize size() const\r
2053     {\r
2054         return !matrix ? cvSize(0,0) : cvSize(matrix->rows,matrix->cols);\r
2055     }\r
2056 \r
2057     int type() const { return matrix ? CV_MAT_TYPE(matrix->type) : 0; }\r
2058     int depth() const { return matrix ? CV_MAT_DEPTH(matrix->type) : 0; }\r
2059     int channels() const { return matrix ? CV_MAT_CN(matrix->type) : 0; }\r
2060     int pix_size() const { return matrix ? CV_ELEM_SIZE(matrix->type) : 0; }\r
2061 \r
2062     uchar* data() { return matrix ? matrix->data.ptr : 0; }\r
2063     const uchar* data() const { return matrix ? matrix->data.ptr : 0; }\r
2064     int step() const { return matrix ? matrix->step : 0; }\r
2065 \r
2066     void set_data( void* data, int step=CV_AUTOSTEP )\r
2067     { cvSetData( matrix, data, step ); }\r
2068 \r
2069     uchar* row(int i) { return !matrix ? 0 : matrix->data.ptr + i*matrix->step; }\r
2070     const uchar* row(int i) const\r
2071     { return !matrix ? 0 : matrix->data.ptr + i*matrix->step; }\r
2072 \r
2073     operator const CvMat* () const { return matrix; }\r
2074     operator CvMat* () { return matrix; }\r
2075 \r
2076     CvMatrix& operator = (const CvMatrix& _m)\r
2077     {\r
2078         _m.addref();\r
2079         release();\r
2080         matrix = _m.matrix;\r
2081         return *this;\r
2082     }\r
2083 \r
2084 protected:\r
2085     CvMat* matrix;\r
2086 };\r
2087 \r
2088 \r
2089 // classes for automatic module/RTTI data registration/unregistration\r
2090 struct CV_EXPORTS CvModule\r
2091 {\r
2092     CvModule( CvModuleInfo* _info );\r
2093     ~CvModule();\r
2094     CvModuleInfo* info;\r
2095 \r
2096     static CvModuleInfo* first;\r
2097     static CvModuleInfo* last;\r
2098 };\r
2099 \r
2100 struct CV_EXPORTS CvType\r
2101 {\r
2102     CvType( const char* type_name,\r
2103             CvIsInstanceFunc is_instance, CvReleaseFunc release=0,\r
2104             CvReadFunc read=0, CvWriteFunc write=0, CvCloneFunc clone=0 );\r
2105     ~CvType();\r
2106     CvTypeInfo* info;\r
2107 \r
2108     static CvTypeInfo* first;\r
2109     static CvTypeInfo* last;\r
2110 };\r
2111 \r
2112 #endif\r
2113 \r
2114 #ifndef SKIP_INCLUDES // for now only expose old interface to swig\r
2115 #include "cxcore.hpp"\r
2116 #endif // SKIP_INCLUDES\r
2117 \r
2118 #endif /*_CXCORE_H_*/\r