Update to 2.0.0 tree from current Fremantle build
[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 _WIN32 || defined WIN64 || 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, CvArr* evals,\r
787                         double eps CV_DEFAULT(0),\r
788                         int lowindex CV_DEFAULT(-1),\r
789                         int highindex CV_DEFAULT(-1));\r
790 \r
791 ///* Finds selected eigen values and vectors of a symmetric matrix */\r
792 //CVAPI(void)  cvSelectedEigenVV( CvArr* mat, CvArr* evects, CvArr* evals,\r
793 //                                int lowindex, int highindex );\r
794 \r
795 /* Makes an identity matrix (mat_ij = i == j) */\r
796 CVAPI(void)  cvSetIdentity( CvArr* mat, CvScalar value CV_DEFAULT(cvRealScalar(1)) );\r
797 \r
798 /* Fills matrix with given range of numbers */\r
799 CVAPI(CvArr*)  cvRange( CvArr* mat, double start, double end );\r
800 \r
801 /* Calculates covariation matrix for a set of vectors */\r
802 /* transpose([v1-avg, v2-avg,...]) * [v1-avg,v2-avg,...] */\r
803 #define CV_COVAR_SCRAMBLED 0\r
804 \r
805 /* [v1-avg, v2-avg,...] * transpose([v1-avg,v2-avg,...]) */\r
806 #define CV_COVAR_NORMAL    1\r
807 \r
808 /* do not calc average (i.e. mean vector) - use the input vector instead\r
809    (useful for calculating covariance matrix by parts) */\r
810 #define CV_COVAR_USE_AVG   2\r
811 \r
812 /* scale the covariance matrix coefficients by number of the vectors */\r
813 #define CV_COVAR_SCALE     4\r
814 \r
815 /* all the input vectors are stored in a single matrix, as its rows */\r
816 #define CV_COVAR_ROWS      8\r
817 \r
818 /* all the input vectors are stored in a single matrix, as its columns */\r
819 #define CV_COVAR_COLS     16\r
820 \r
821 CVAPI(void)  cvCalcCovarMatrix( const CvArr** vects, int count,\r
822                                 CvArr* cov_mat, CvArr* avg, int flags );\r
823 \r
824 #define CV_PCA_DATA_AS_ROW 0\r
825 #define CV_PCA_DATA_AS_COL 1\r
826 #define CV_PCA_USE_AVG 2\r
827 CVAPI(void)  cvCalcPCA( const CvArr* data, CvArr* mean,\r
828                         CvArr* eigenvals, CvArr* eigenvects, int flags );\r
829 \r
830 CVAPI(void)  cvProjectPCA( const CvArr* data, const CvArr* mean,\r
831                            const CvArr* eigenvects, CvArr* result );\r
832 \r
833 CVAPI(void)  cvBackProjectPCA( const CvArr* proj, const CvArr* mean,\r
834                                const CvArr* eigenvects, CvArr* result );\r
835 \r
836 /* Calculates Mahalanobis(weighted) distance */\r
837 CVAPI(double)  cvMahalanobis( const CvArr* vec1, const CvArr* vec2, const CvArr* mat );\r
838 #define cvMahalonobis  cvMahalanobis\r
839 \r
840 /****************************************************************************************\\r
841 *                                    Array Statistics                                    *\r
842 \****************************************************************************************/\r
843 \r
844 /* Finds sum of array elements */\r
845 CVAPI(CvScalar)  cvSum( const CvArr* arr );\r
846 \r
847 /* Calculates number of non-zero pixels */\r
848 CVAPI(int)  cvCountNonZero( const CvArr* arr );\r
849 \r
850 /* Calculates mean value of array elements */\r
851 CVAPI(CvScalar)  cvAvg( const CvArr* arr, const CvArr* mask CV_DEFAULT(NULL) );\r
852 \r
853 /* Calculates mean and standard deviation of pixel values */\r
854 CVAPI(void)  cvAvgSdv( const CvArr* arr, CvScalar* mean, CvScalar* std_dev,\r
855                        const CvArr* mask CV_DEFAULT(NULL) );\r
856 \r
857 /* Finds global minimum, maximum and their positions */\r
858 CVAPI(void)  cvMinMaxLoc( const CvArr* arr, double* min_val, double* max_val,\r
859                           CvPoint* min_loc CV_DEFAULT(NULL),\r
860                           CvPoint* max_loc CV_DEFAULT(NULL),\r
861                           const CvArr* mask CV_DEFAULT(NULL) );\r
862 \r
863 /* types of array norm */\r
864 #define CV_C            1\r
865 #define CV_L1           2\r
866 #define CV_L2           4\r
867 #define CV_NORM_MASK    7\r
868 #define CV_RELATIVE     8\r
869 #define CV_DIFF         16\r
870 #define CV_MINMAX       32\r
871 \r
872 #define CV_DIFF_C       (CV_DIFF | CV_C)\r
873 #define CV_DIFF_L1      (CV_DIFF | CV_L1)\r
874 #define CV_DIFF_L2      (CV_DIFF | CV_L2)\r
875 #define CV_RELATIVE_C   (CV_RELATIVE | CV_C)\r
876 #define CV_RELATIVE_L1  (CV_RELATIVE | CV_L1)\r
877 #define CV_RELATIVE_L2  (CV_RELATIVE | CV_L2)\r
878 \r
879 /* Finds norm, difference norm or relative difference norm for an array (or two arrays) */\r
880 CVAPI(double)  cvNorm( const CvArr* arr1, const CvArr* arr2 CV_DEFAULT(NULL),\r
881                        int norm_type CV_DEFAULT(CV_L2),\r
882                        const CvArr* mask CV_DEFAULT(NULL) );\r
883 \r
884 CVAPI(void)  cvNormalize( const CvArr* src, CvArr* dst,\r
885                           double a CV_DEFAULT(1.), double b CV_DEFAULT(0.),\r
886                           int norm_type CV_DEFAULT(CV_L2),\r
887                           const CvArr* mask CV_DEFAULT(NULL) );\r
888 \r
889 \r
890 #define CV_REDUCE_SUM 0\r
891 #define CV_REDUCE_AVG 1\r
892 #define CV_REDUCE_MAX 2\r
893 #define CV_REDUCE_MIN 3\r
894 \r
895 CVAPI(void)  cvReduce( const CvArr* src, CvArr* dst, int dim CV_DEFAULT(-1),\r
896                        int op CV_DEFAULT(CV_REDUCE_SUM) );\r
897 \r
898 /****************************************************************************************\\r
899 *                      Discrete Linear Transforms and Related Functions                  *\r
900 \****************************************************************************************/\r
901 \r
902 #define CV_DXT_FORWARD  0\r
903 #define CV_DXT_INVERSE  1\r
904 #define CV_DXT_SCALE    2 /* divide result by size of array */\r
905 #define CV_DXT_INV_SCALE (CV_DXT_INVERSE + CV_DXT_SCALE)\r
906 #define CV_DXT_INVERSE_SCALE CV_DXT_INV_SCALE\r
907 #define CV_DXT_ROWS     4 /* transform each row individually */\r
908 #define CV_DXT_MUL_CONJ 8 /* conjugate the second argument of cvMulSpectrums */\r
909 \r
910 /* Discrete Fourier Transform:\r
911     complex->complex,\r
912     real->ccs (forward),\r
913     ccs->real (inverse) */\r
914 CVAPI(void)  cvDFT( const CvArr* src, CvArr* dst, int flags,\r
915                     int nonzero_rows CV_DEFAULT(0) );\r
916 #define cvFFT cvDFT\r
917 \r
918 /* Multiply results of DFTs: DFT(X)*DFT(Y) or DFT(X)*conj(DFT(Y)) */\r
919 CVAPI(void)  cvMulSpectrums( const CvArr* src1, const CvArr* src2,\r
920                              CvArr* dst, int flags );\r
921 \r
922 /* Finds optimal DFT vector size >= size0 */\r
923 CVAPI(int)  cvGetOptimalDFTSize( int size0 );\r
924 \r
925 /* Discrete Cosine Transform */\r
926 CVAPI(void)  cvDCT( const CvArr* src, CvArr* dst, int flags );\r
927 \r
928 /****************************************************************************************\\r
929 *                              Dynamic data structures                                   *\r
930 \****************************************************************************************/\r
931 \r
932 /* Calculates length of sequence slice (with support of negative indices). */\r
933 CVAPI(int) cvSliceLength( CvSlice slice, const CvSeq* seq );\r
934 \r
935 \r
936 /* Creates new memory storage.\r
937    block_size == 0 means that default,\r
938    somewhat optimal size, is used (currently, it is 64K) */\r
939 CVAPI(CvMemStorage*)  cvCreateMemStorage( int block_size CV_DEFAULT(0));\r
940 \r
941 \r
942 /* Creates a memory storage that will borrow memory blocks from parent storage */\r
943 CVAPI(CvMemStorage*)  cvCreateChildMemStorage( CvMemStorage* parent );\r
944 \r
945 \r
946 /* Releases memory storage. All the children of a parent must be released before\r
947    the parent. A child storage returns all the blocks to parent when it is released */\r
948 CVAPI(void)  cvReleaseMemStorage( CvMemStorage** storage );\r
949 \r
950 \r
951 /* Clears memory storage. This is the only way(!!!) (besides cvRestoreMemStoragePos)\r
952    to reuse memory allocated for the storage - cvClearSeq,cvClearSet ...\r
953    do not free any memory.\r
954    A child storage returns all the blocks to the parent when it is cleared */\r
955 CVAPI(void)  cvClearMemStorage( CvMemStorage* storage );\r
956 \r
957 /* Remember a storage "free memory" position */\r
958 CVAPI(void)  cvSaveMemStoragePos( const CvMemStorage* storage, CvMemStoragePos* pos );\r
959 \r
960 /* Restore a storage "free memory" position */\r
961 CVAPI(void)  cvRestoreMemStoragePos( CvMemStorage* storage, CvMemStoragePos* pos );\r
962 \r
963 /* Allocates continuous buffer of the specified size in the storage */\r
964 CVAPI(void*) cvMemStorageAlloc( CvMemStorage* storage, size_t size );\r
965 \r
966 /* Allocates string in memory storage */\r
967 CVAPI(CvString) cvMemStorageAllocString( CvMemStorage* storage, const char* ptr,\r
968                                         int len CV_DEFAULT(-1) );\r
969 \r
970 /* Creates new empty sequence that will reside in the specified storage */\r
971 CVAPI(CvSeq*)  cvCreateSeq( int seq_flags, int header_size,\r
972                             int elem_size, CvMemStorage* storage );\r
973 \r
974 /* Changes default size (granularity) of sequence blocks.\r
975    The default size is ~1Kbyte */\r
976 CVAPI(void)  cvSetSeqBlockSize( CvSeq* seq, int delta_elems );\r
977 \r
978 \r
979 /* Adds new element to the end of sequence. Returns pointer to the element */\r
980 CVAPI(schar*)  cvSeqPush( CvSeq* seq, const void* element CV_DEFAULT(NULL));\r
981 \r
982 \r
983 /* Adds new element to the beginning of sequence. Returns pointer to it */\r
984 CVAPI(schar*)  cvSeqPushFront( CvSeq* seq, const void* element CV_DEFAULT(NULL));\r
985 \r
986 \r
987 /* Removes the last element from sequence and optionally saves it */\r
988 CVAPI(void)  cvSeqPop( CvSeq* seq, void* element CV_DEFAULT(NULL));\r
989 \r
990 \r
991 /* Removes the first element from sequence and optioanally saves it */\r
992 CVAPI(void)  cvSeqPopFront( CvSeq* seq, void* element CV_DEFAULT(NULL));\r
993 \r
994 \r
995 #define CV_FRONT 1\r
996 #define CV_BACK 0\r
997 /* Adds several new elements to the end of sequence */\r
998 CVAPI(void)  cvSeqPushMulti( CvSeq* seq, const void* elements,\r
999                              int count, int in_front CV_DEFAULT(0) );\r
1000 \r
1001 /* Removes several elements from the end of sequence and optionally saves them */\r
1002 CVAPI(void)  cvSeqPopMulti( CvSeq* seq, void* elements,\r
1003                             int count, int in_front CV_DEFAULT(0) );\r
1004 \r
1005 /* Inserts a new element in the middle of sequence.\r
1006    cvSeqInsert(seq,0,elem) == cvSeqPushFront(seq,elem) */\r
1007 CVAPI(schar*)  cvSeqInsert( CvSeq* seq, int before_index,\r
1008                             const void* element CV_DEFAULT(NULL));\r
1009 \r
1010 /* Removes specified sequence element */\r
1011 CVAPI(void)  cvSeqRemove( CvSeq* seq, int index );\r
1012 \r
1013 \r
1014 /* Removes all the elements from the sequence. The freed memory\r
1015    can be reused later only by the same sequence unless cvClearMemStorage\r
1016    or cvRestoreMemStoragePos is called */\r
1017 CVAPI(void)  cvClearSeq( CvSeq* seq );\r
1018 \r
1019 \r
1020 /* Retrieves pointer to specified sequence element.\r
1021    Negative indices are supported and mean counting from the end\r
1022    (e.g -1 means the last sequence element) */\r
1023 CVAPI(schar*)  cvGetSeqElem( const CvSeq* seq, int index );\r
1024 \r
1025 /* Calculates index of the specified sequence element.\r
1026    Returns -1 if element does not belong to the sequence */\r
1027 CVAPI(int)  cvSeqElemIdx( const CvSeq* seq, const void* element,\r
1028                          CvSeqBlock** block CV_DEFAULT(NULL) );\r
1029 \r
1030 /* Initializes sequence writer. The new elements will be added to the end of sequence */\r
1031 CVAPI(void)  cvStartAppendToSeq( CvSeq* seq, CvSeqWriter* writer );\r
1032 \r
1033 \r
1034 /* Combination of cvCreateSeq and cvStartAppendToSeq */\r
1035 CVAPI(void)  cvStartWriteSeq( int seq_flags, int header_size,\r
1036                               int elem_size, CvMemStorage* storage,\r
1037                               CvSeqWriter* writer );\r
1038 \r
1039 /* Closes sequence writer, updates sequence header and returns pointer\r
1040    to the resultant sequence\r
1041    (which may be useful if the sequence was created using cvStartWriteSeq))\r
1042 */\r
1043 CVAPI(CvSeq*)  cvEndWriteSeq( CvSeqWriter* writer );\r
1044 \r
1045 \r
1046 /* Updates sequence header. May be useful to get access to some of previously\r
1047    written elements via cvGetSeqElem or sequence reader */\r
1048 CVAPI(void)   cvFlushSeqWriter( CvSeqWriter* writer );\r
1049 \r
1050 \r
1051 /* Initializes sequence reader.\r
1052    The sequence can be read in forward or backward direction */\r
1053 CVAPI(void) cvStartReadSeq( const CvSeq* seq, CvSeqReader* reader,\r
1054                            int reverse CV_DEFAULT(0) );\r
1055 \r
1056 \r
1057 /* Returns current sequence reader position (currently observed sequence element) */\r
1058 CVAPI(int)  cvGetSeqReaderPos( CvSeqReader* reader );\r
1059 \r
1060 \r
1061 /* Changes sequence reader position. It may seek to an absolute or\r
1062    to relative to the current position */\r
1063 CVAPI(void)   cvSetSeqReaderPos( CvSeqReader* reader, int index,\r
1064                                  int is_relative CV_DEFAULT(0));\r
1065 \r
1066 /* Copies sequence content to a continuous piece of memory */\r
1067 CVAPI(void*)  cvCvtSeqToArray( const CvSeq* seq, void* elements,\r
1068                                CvSlice slice CV_DEFAULT(CV_WHOLE_SEQ) );\r
1069 \r
1070 /* Creates sequence header for array.\r
1071    After that all the operations on sequences that do not alter the content\r
1072    can be applied to the resultant sequence */\r
1073 CVAPI(CvSeq*) cvMakeSeqHeaderForArray( int seq_type, int header_size,\r
1074                                        int elem_size, void* elements, int total,\r
1075                                        CvSeq* seq, CvSeqBlock* block );\r
1076 \r
1077 /* Extracts sequence slice (with or without copying sequence elements) */\r
1078 CVAPI(CvSeq*) cvSeqSlice( const CvSeq* seq, CvSlice slice,\r
1079                          CvMemStorage* storage CV_DEFAULT(NULL),\r
1080                          int copy_data CV_DEFAULT(0));\r
1081 \r
1082 CV_INLINE CvSeq* cvCloneSeq( const CvSeq* seq, CvMemStorage* storage CV_DEFAULT(NULL))\r
1083 {\r
1084     return cvSeqSlice( seq, CV_WHOLE_SEQ, storage, 1 );\r
1085 }\r
1086 \r
1087 /* Removes sequence slice */\r
1088 CVAPI(void)  cvSeqRemoveSlice( CvSeq* seq, CvSlice slice );\r
1089 \r
1090 /* Inserts a sequence or array into another sequence */\r
1091 CVAPI(void)  cvSeqInsertSlice( CvSeq* seq, int before_index, const CvArr* from_arr );\r
1092 \r
1093 /* a < b ? -1 : a > b ? 1 : 0 */\r
1094 typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata );\r
1095 \r
1096 /* Sorts sequence in-place given element comparison function */\r
1097 CVAPI(void) cvSeqSort( CvSeq* seq, CvCmpFunc func, void* userdata CV_DEFAULT(NULL) );\r
1098 \r
1099 /* Finds element in a [sorted] sequence */\r
1100 CVAPI(schar*) cvSeqSearch( CvSeq* seq, const void* elem, CvCmpFunc func,\r
1101                            int is_sorted, int* elem_idx,\r
1102                            void* userdata CV_DEFAULT(NULL) );\r
1103 \r
1104 /* Reverses order of sequence elements in-place */\r
1105 CVAPI(void) cvSeqInvert( CvSeq* seq );\r
1106 \r
1107 /* Splits sequence into one or more equivalence classes using the specified criteria */\r
1108 CVAPI(int)  cvSeqPartition( const CvSeq* seq, CvMemStorage* storage,\r
1109                             CvSeq** labels, CvCmpFunc is_equal, void* userdata );\r
1110 \r
1111 /************ Internal sequence functions ************/\r
1112 CVAPI(void)  cvChangeSeqBlock( void* reader, int direction );\r
1113 CVAPI(void)  cvCreateSeqBlock( CvSeqWriter* writer );\r
1114 \r
1115 \r
1116 /* Creates a new set */\r
1117 CVAPI(CvSet*)  cvCreateSet( int set_flags, int header_size,\r
1118                             int elem_size, CvMemStorage* storage );\r
1119 \r
1120 /* Adds new element to the set and returns pointer to it */\r
1121 CVAPI(int)  cvSetAdd( CvSet* set_header, CvSetElem* elem CV_DEFAULT(NULL),\r
1122                       CvSetElem** inserted_elem CV_DEFAULT(NULL) );\r
1123 \r
1124 /* Fast variant of cvSetAdd */\r
1125 CV_INLINE  CvSetElem* cvSetNew( CvSet* set_header )\r
1126 {\r
1127     CvSetElem* elem = set_header->free_elems;\r
1128     if( elem )\r
1129     {\r
1130         set_header->free_elems = elem->next_free;\r
1131         elem->flags = elem->flags & CV_SET_ELEM_IDX_MASK;\r
1132         set_header->active_count++;\r
1133     }\r
1134     else\r
1135         cvSetAdd( set_header, NULL, (CvSetElem**)&elem );\r
1136     return elem;\r
1137 }\r
1138 \r
1139 /* Removes set element given its pointer */\r
1140 CV_INLINE  void cvSetRemoveByPtr( CvSet* set_header, void* elem )\r
1141 {\r
1142     CvSetElem* _elem = (CvSetElem*)elem;\r
1143     assert( _elem->flags >= 0 /*&& (elem->flags & CV_SET_ELEM_IDX_MASK) < set_header->total*/ );\r
1144     _elem->next_free = set_header->free_elems;\r
1145     _elem->flags = (_elem->flags & CV_SET_ELEM_IDX_MASK) | CV_SET_ELEM_FREE_FLAG;\r
1146     set_header->free_elems = _elem;\r
1147     set_header->active_count--;\r
1148 }\r
1149 \r
1150 /* Removes element from the set by its index  */\r
1151 CVAPI(void)   cvSetRemove( CvSet* set_header, int index );\r
1152 \r
1153 /* Returns a set element by index. If the element doesn't belong to the set,\r
1154    NULL is returned */\r
1155 CV_INLINE CvSetElem* cvGetSetElem( const CvSet* set_header, int index )\r
1156 {\r
1157     CvSetElem* elem = (CvSetElem*)cvGetSeqElem( (CvSeq*)set_header, index );\r
1158     return elem && CV_IS_SET_ELEM( elem ) ? elem : 0;\r
1159 }\r
1160 \r
1161 /* Removes all the elements from the set */\r
1162 CVAPI(void)  cvClearSet( CvSet* set_header );\r
1163 \r
1164 /* Creates new graph */\r
1165 CVAPI(CvGraph*)  cvCreateGraph( int graph_flags, int header_size,\r
1166                                 int vtx_size, int edge_size,\r
1167                                 CvMemStorage* storage );\r
1168 \r
1169 /* Adds new vertex to the graph */\r
1170 CVAPI(int)  cvGraphAddVtx( CvGraph* graph, const CvGraphVtx* vtx CV_DEFAULT(NULL),\r
1171                            CvGraphVtx** inserted_vtx CV_DEFAULT(NULL) );\r
1172 \r
1173 \r
1174 /* Removes vertex from the graph together with all incident edges */\r
1175 CVAPI(int)  cvGraphRemoveVtx( CvGraph* graph, int index );\r
1176 CVAPI(int)  cvGraphRemoveVtxByPtr( CvGraph* graph, CvGraphVtx* vtx );\r
1177 \r
1178 \r
1179 /* Link two vertices specifed by indices or pointers if they\r
1180    are not connected or return pointer to already existing edge\r
1181    connecting the vertices.\r
1182    Functions return 1 if a new edge was created, 0 otherwise */\r
1183 CVAPI(int)  cvGraphAddEdge( CvGraph* graph,\r
1184                             int start_idx, int end_idx,\r
1185                             const CvGraphEdge* edge CV_DEFAULT(NULL),\r
1186                             CvGraphEdge** inserted_edge CV_DEFAULT(NULL) );\r
1187 \r
1188 CVAPI(int)  cvGraphAddEdgeByPtr( CvGraph* graph,\r
1189                                CvGraphVtx* start_vtx, CvGraphVtx* end_vtx,\r
1190                                const CvGraphEdge* edge CV_DEFAULT(NULL),\r
1191                                CvGraphEdge** inserted_edge CV_DEFAULT(NULL) );\r
1192 \r
1193 /* Remove edge connecting two vertices */\r
1194 CVAPI(void)  cvGraphRemoveEdge( CvGraph* graph, int start_idx, int end_idx );\r
1195 CVAPI(void)  cvGraphRemoveEdgeByPtr( CvGraph* graph, CvGraphVtx* start_vtx,\r
1196                                      CvGraphVtx* end_vtx );\r
1197 \r
1198 /* Find edge connecting two vertices */\r
1199 CVAPI(CvGraphEdge*)  cvFindGraphEdge( const CvGraph* graph, int start_idx, int end_idx );\r
1200 CVAPI(CvGraphEdge*)  cvFindGraphEdgeByPtr( const CvGraph* graph,\r
1201                                            const CvGraphVtx* start_vtx,\r
1202                                            const CvGraphVtx* end_vtx );\r
1203 #define cvGraphFindEdge cvFindGraphEdge\r
1204 #define cvGraphFindEdgeByPtr cvFindGraphEdgeByPtr\r
1205 \r
1206 /* Remove all vertices and edges from the graph */\r
1207 CVAPI(void)  cvClearGraph( CvGraph* graph );\r
1208 \r
1209 \r
1210 /* Count number of edges incident to the vertex */\r
1211 CVAPI(int)  cvGraphVtxDegree( const CvGraph* graph, int vtx_idx );\r
1212 CVAPI(int)  cvGraphVtxDegreeByPtr( const CvGraph* graph, const CvGraphVtx* vtx );\r
1213 \r
1214 \r
1215 /* Retrieves graph vertex by given index */\r
1216 #define cvGetGraphVtx( graph, idx ) (CvGraphVtx*)cvGetSetElem((CvSet*)(graph), (idx))\r
1217 \r
1218 /* Retrieves index of a graph vertex given its pointer */\r
1219 #define cvGraphVtxIdx( graph, vtx ) ((vtx)->flags & CV_SET_ELEM_IDX_MASK)\r
1220 \r
1221 /* Retrieves index of a graph edge given its pointer */\r
1222 #define cvGraphEdgeIdx( graph, edge ) ((edge)->flags & CV_SET_ELEM_IDX_MASK)\r
1223 \r
1224 #define cvGraphGetVtxCount( graph ) ((graph)->active_count)\r
1225 #define cvGraphGetEdgeCount( graph ) ((graph)->edges->active_count)\r
1226 \r
1227 #define  CV_GRAPH_VERTEX        1\r
1228 #define  CV_GRAPH_TREE_EDGE     2\r
1229 #define  CV_GRAPH_BACK_EDGE     4\r
1230 #define  CV_GRAPH_FORWARD_EDGE  8\r
1231 #define  CV_GRAPH_CROSS_EDGE    16\r
1232 #define  CV_GRAPH_ANY_EDGE      30\r
1233 #define  CV_GRAPH_NEW_TREE      32\r
1234 #define  CV_GRAPH_BACKTRACKING  64\r
1235 #define  CV_GRAPH_OVER          -1\r
1236 \r
1237 #define  CV_GRAPH_ALL_ITEMS    -1\r
1238 \r
1239 /* flags for graph vertices and edges */\r
1240 #define  CV_GRAPH_ITEM_VISITED_FLAG  (1 << 30)\r
1241 #define  CV_IS_GRAPH_VERTEX_VISITED(vtx) \\r
1242     (((CvGraphVtx*)(vtx))->flags & CV_GRAPH_ITEM_VISITED_FLAG)\r
1243 #define  CV_IS_GRAPH_EDGE_VISITED(edge) \\r
1244     (((CvGraphEdge*)(edge))->flags & CV_GRAPH_ITEM_VISITED_FLAG)\r
1245 #define  CV_GRAPH_SEARCH_TREE_NODE_FLAG   (1 << 29)\r
1246 #define  CV_GRAPH_FORWARD_EDGE_FLAG       (1 << 28)\r
1247 \r
1248 typedef struct CvGraphScanner\r
1249 {\r
1250     CvGraphVtx* vtx;       /* current graph vertex (or current edge origin) */\r
1251     CvGraphVtx* dst;       /* current graph edge destination vertex */\r
1252     CvGraphEdge* edge;     /* current edge */\r
1253 \r
1254     CvGraph* graph;        /* the graph */\r
1255     CvSeq*   stack;        /* the graph vertex stack */\r
1256     int      index;        /* the lower bound of certainly visited vertices */\r
1257     int      mask;         /* event mask */\r
1258 }\r
1259 CvGraphScanner;\r
1260 \r
1261 /* Creates new graph scanner. */\r
1262 CVAPI(CvGraphScanner*)  cvCreateGraphScanner( CvGraph* graph,\r
1263                                              CvGraphVtx* vtx CV_DEFAULT(NULL),\r
1264                                              int mask CV_DEFAULT(CV_GRAPH_ALL_ITEMS));\r
1265 \r
1266 /* Releases graph scanner. */\r
1267 CVAPI(void) cvReleaseGraphScanner( CvGraphScanner** scanner );\r
1268 \r
1269 /* Get next graph element */\r
1270 CVAPI(int)  cvNextGraphItem( CvGraphScanner* scanner );\r
1271 \r
1272 /* Creates a copy of graph */\r
1273 CVAPI(CvGraph*) cvCloneGraph( const CvGraph* graph, CvMemStorage* storage );\r
1274 \r
1275 /****************************************************************************************\\r
1276 *                                     Drawing                                            *\r
1277 \****************************************************************************************/\r
1278 \r
1279 /****************************************************************************************\\r
1280 *       Drawing functions work with images/matrices of arbitrary type.                   *\r
1281 *       For color images the channel order is BGR[A]                                     *\r
1282 *       Antialiasing is supported only for 8-bit image now.                              *\r
1283 *       All the functions include parameter color that means rgb value (that may be      *\r
1284 *       constructed with CV_RGB macro) for color images and brightness                   *\r
1285 *       for grayscale images.                                                            *\r
1286 *       If a drawn figure is partially or completely outside of the image, it is clipped.*\r
1287 \****************************************************************************************/\r
1288 \r
1289 #define CV_RGB( r, g, b )  cvScalar( (b), (g), (r), 0 )\r
1290 #define CV_FILLED -1\r
1291 \r
1292 #define CV_AA 16\r
1293 \r
1294 /* Draws 4-connected, 8-connected or antialiased line segment connecting two points */\r
1295 CVAPI(void)  cvLine( CvArr* img, CvPoint pt1, CvPoint pt2,\r
1296                      CvScalar color, int thickness CV_DEFAULT(1),\r
1297                      int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );\r
1298 \r
1299 /* Draws a rectangle given two opposite corners of the rectangle (pt1 & pt2),\r
1300    if thickness<0 (e.g. thickness == CV_FILLED), the filled box is drawn */\r
1301 CVAPI(void)  cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2,\r
1302                           CvScalar color, int thickness CV_DEFAULT(1),\r
1303                           int line_type CV_DEFAULT(8),\r
1304                           int shift CV_DEFAULT(0));\r
1305 \r
1306 /* Draws a circle with specified center and radius.\r
1307    Thickness works in the same way as with cvRectangle */\r
1308 CVAPI(void)  cvCircle( CvArr* img, CvPoint center, int radius,\r
1309                        CvScalar color, int thickness CV_DEFAULT(1),\r
1310                        int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0));\r
1311 \r
1312 /* Draws ellipse outline, filled ellipse, elliptic arc or filled elliptic sector,\r
1313    depending on <thickness>, <start_angle> and <end_angle> parameters. The resultant figure\r
1314    is rotated by <angle>. All the angles are in degrees */\r
1315 CVAPI(void)  cvEllipse( CvArr* img, CvPoint center, CvSize axes,\r
1316                         double angle, double start_angle, double end_angle,\r
1317                         CvScalar color, int thickness CV_DEFAULT(1),\r
1318                         int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0));\r
1319 \r
1320 CV_INLINE  void  cvEllipseBox( CvArr* img, CvBox2D box, CvScalar color,\r
1321                                int thickness CV_DEFAULT(1),\r
1322                                int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) )\r
1323 {\r
1324     CvSize axes;\r
1325     axes.width = cvRound(box.size.height*0.5);\r
1326     axes.height = cvRound(box.size.width*0.5);\r
1327 \r
1328     cvEllipse( img, cvPointFrom32f( box.center ), axes, box.angle,\r
1329                0, 360, color, thickness, line_type, shift );\r
1330 }\r
1331 \r
1332 /* Fills convex or monotonous polygon. */\r
1333 CVAPI(void)  cvFillConvexPoly( CvArr* img, const CvPoint* pts, int npts, CvScalar color,\r
1334                                int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0));\r
1335 \r
1336 /* Fills an area bounded by one or more arbitrary polygons */\r
1337 CVAPI(void)  cvFillPoly( CvArr* img, CvPoint** pts, const int* npts,\r
1338                          int contours, CvScalar color,\r
1339                          int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );\r
1340 \r
1341 /* Draws one or more polygonal curves */\r
1342 CVAPI(void)  cvPolyLine( CvArr* img, CvPoint** pts, const int* npts, int contours,\r
1343                          int is_closed, CvScalar color, int thickness CV_DEFAULT(1),\r
1344                          int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );\r
1345 \r
1346 #define cvDrawRect cvRectangle\r
1347 #define cvDrawLine cvLine\r
1348 #define cvDrawCircle cvCircle\r
1349 #define cvDrawEllipse cvEllipse\r
1350 #define cvDrawPolyLine cvPolyLine\r
1351 \r
1352 /* Clips the line segment connecting *pt1 and *pt2\r
1353    by the rectangular window\r
1354    (0<=x<img_size.width, 0<=y<img_size.height). */\r
1355 CVAPI(int) cvClipLine( CvSize img_size, CvPoint* pt1, CvPoint* pt2 );\r
1356 \r
1357 /* Initializes line iterator. Initially, line_iterator->ptr will point\r
1358    to pt1 (or pt2, see left_to_right description) location in the image.\r
1359    Returns the number of pixels on the line between the ending points. */\r
1360 CVAPI(int)  cvInitLineIterator( const CvArr* image, CvPoint pt1, CvPoint pt2,\r
1361                                 CvLineIterator* line_iterator,\r
1362                                 int connectivity CV_DEFAULT(8),\r
1363                                 int left_to_right CV_DEFAULT(0));\r
1364 \r
1365 /* Moves iterator to the next line point */\r
1366 #define CV_NEXT_LINE_POINT( line_iterator )                     \\r
1367 {                                                               \\r
1368     int _line_iterator_mask = (line_iterator).err < 0 ? -1 : 0; \\r
1369     (line_iterator).err += (line_iterator).minus_delta +        \\r
1370         ((line_iterator).plus_delta & _line_iterator_mask);     \\r
1371     (line_iterator).ptr += (line_iterator).minus_step +         \\r
1372         ((line_iterator).plus_step & _line_iterator_mask);      \\r
1373 }\r
1374 \r
1375 \r
1376 /* basic font types */\r
1377 #define CV_FONT_HERSHEY_SIMPLEX         0\r
1378 #define CV_FONT_HERSHEY_PLAIN           1\r
1379 #define CV_FONT_HERSHEY_DUPLEX          2\r
1380 #define CV_FONT_HERSHEY_COMPLEX         3\r
1381 #define CV_FONT_HERSHEY_TRIPLEX         4\r
1382 #define CV_FONT_HERSHEY_COMPLEX_SMALL   5\r
1383 #define CV_FONT_HERSHEY_SCRIPT_SIMPLEX  6\r
1384 #define CV_FONT_HERSHEY_SCRIPT_COMPLEX  7\r
1385 \r
1386 /* font flags */\r
1387 #define CV_FONT_ITALIC                 16\r
1388 \r
1389 #define CV_FONT_VECTOR0    CV_FONT_HERSHEY_SIMPLEX\r
1390 \r
1391 /* Font structure */\r
1392 typedef struct CvFont\r
1393 {\r
1394     int         font_face; /* =CV_FONT_* */\r
1395     const int*  ascii; /* font data and metrics */\r
1396     const int*  greek;\r
1397     const int*  cyrillic;\r
1398     float       hscale, vscale;\r
1399     float       shear; /* slope coefficient: 0 - normal, >0 - italic */\r
1400     int         thickness; /* letters thickness */\r
1401     float       dx; /* horizontal interval between letters */\r
1402     int         line_type;\r
1403 }\r
1404 CvFont;\r
1405 \r
1406 /* Initializes font structure used further in cvPutText */\r
1407 CVAPI(void)  cvInitFont( CvFont* font, int font_face,\r
1408                          double hscale, double vscale,\r
1409                          double shear CV_DEFAULT(0),\r
1410                          int thickness CV_DEFAULT(1),\r
1411                          int line_type CV_DEFAULT(8));\r
1412 \r
1413 CV_INLINE CvFont cvFont( double scale, int thickness CV_DEFAULT(1) )\r
1414 {\r
1415     CvFont font;\r
1416     cvInitFont( &font, CV_FONT_HERSHEY_PLAIN, scale, scale, 0, thickness, CV_AA );\r
1417     return font;\r
1418 }\r
1419 \r
1420 /* Renders text stroke with specified font and color at specified location.\r
1421    CvFont should be initialized with cvInitFont */\r
1422 CVAPI(void)  cvPutText( CvArr* img, const char* text, CvPoint org,\r
1423                         const CvFont* font, CvScalar color );\r
1424 \r
1425 /* Calculates bounding box of text stroke (useful for alignment) */\r
1426 CVAPI(void)  cvGetTextSize( const char* text_string, const CvFont* font,\r
1427                             CvSize* text_size, int* baseline );\r
1428 \r
1429 /* Unpacks color value, if arrtype is CV_8UC?, <color> is treated as\r
1430    packed color value, otherwise the first channels (depending on arrtype)\r
1431    of destination scalar are set to the same value = <color> */\r
1432 CVAPI(CvScalar)  cvColorToScalar( double packed_color, int arrtype );\r
1433 \r
1434 /* Returns the polygon points which make up the given ellipse.  The ellipse is define by\r
1435    the box of size 'axes' rotated 'angle' around the 'center'.  A partial sweep\r
1436    of the ellipse arc can be done by spcifying arc_start and arc_end to be something\r
1437    other than 0 and 360, respectively.  The input array 'pts' must be large enough to\r
1438    hold the result.  The total number of points stored into 'pts' is returned by this\r
1439    function. */\r
1440 CVAPI(int) cvEllipse2Poly( CvPoint center, CvSize axes,\r
1441                  int angle, int arc_start, int arc_end, CvPoint * pts, int delta );\r
1442 \r
1443 /* Draws contour outlines or filled interiors on the image */\r
1444 CVAPI(void)  cvDrawContours( CvArr *img, CvSeq* contour,\r
1445                              CvScalar external_color, CvScalar hole_color,\r
1446                              int max_level, int thickness CV_DEFAULT(1),\r
1447                              int line_type CV_DEFAULT(8),\r
1448                              CvPoint offset CV_DEFAULT(cvPoint(0,0)));\r
1449 \r
1450 /* Does look-up transformation. Elements of the source array\r
1451    (that should be 8uC1 or 8sC1) are used as indexes in lutarr 256-element table */\r
1452 CVAPI(void) cvLUT( const CvArr* src, CvArr* dst, const CvArr* lut );\r
1453 \r
1454 \r
1455 /******************* Iteration through the sequence tree *****************/\r
1456 typedef struct CvTreeNodeIterator\r
1457 {\r
1458     const void* node;\r
1459     int level;\r
1460     int max_level;\r
1461 }\r
1462 CvTreeNodeIterator;\r
1463 \r
1464 CVAPI(void) cvInitTreeNodeIterator( CvTreeNodeIterator* tree_iterator,\r
1465                                    const void* first, int max_level );\r
1466 CVAPI(void*) cvNextTreeNode( CvTreeNodeIterator* tree_iterator );\r
1467 CVAPI(void*) cvPrevTreeNode( CvTreeNodeIterator* tree_iterator );\r
1468 \r
1469 /* Inserts sequence into tree with specified "parent" sequence.\r
1470    If parent is equal to frame (e.g. the most external contour),\r
1471    then added contour will have null pointer to parent. */\r
1472 CVAPI(void) cvInsertNodeIntoTree( void* node, void* parent, void* frame );\r
1473 \r
1474 /* Removes contour from tree (together with the contour children). */\r
1475 CVAPI(void) cvRemoveNodeFromTree( void* node, void* frame );\r
1476 \r
1477 /* Gathers pointers to all the sequences,\r
1478    accessible from the <first>, to the single sequence */\r
1479 CVAPI(CvSeq*) cvTreeToNodeSeq( const void* first, int header_size,\r
1480                               CvMemStorage* storage );\r
1481 \r
1482 /* The function implements the K-means algorithm for clustering an array of sample\r
1483    vectors in a specified number of classes */\r
1484 #define CV_KMEANS_USE_INITIAL_LABELS    1\r
1485 CVAPI(int) cvKMeans2( const CvArr* samples, int cluster_count, CvArr* labels,\r
1486                       CvTermCriteria termcrit, int attempts CV_DEFAULT(1),\r
1487                       CvRNG* rng CV_DEFAULT(0), int flags CV_DEFAULT(0),\r
1488                       CvArr* _centers CV_DEFAULT(0), double* compactness CV_DEFAULT(0) );\r
1489 \r
1490 /****************************************************************************************\\r
1491 *                                    System functions                                    *\r
1492 \****************************************************************************************/\r
1493 \r
1494 /* Add the function pointers table with associated information to the IPP primitives list */\r
1495 CVAPI(int)  cvRegisterModule( const CvModuleInfo* module_info );\r
1496 \r
1497 /* Loads optimized functions from IPP, MKL etc. or switches back to pure C code */\r
1498 CVAPI(int)  cvUseOptimized( int on_off );\r
1499 \r
1500 /* Retrieves information about the registered modules and loaded optimized plugins */\r
1501 CVAPI(void)  cvGetModuleInfo( const char* module_name,\r
1502                               const char** version,\r
1503                               const char** loaded_addon_plugins );\r
1504 \r
1505 /* Get current OpenCV error status */\r
1506 CVAPI(int) cvGetErrStatus( void );\r
1507 \r
1508 /* Sets error status silently */\r
1509 CVAPI(void) cvSetErrStatus( int status );\r
1510 \r
1511 #define CV_ErrModeLeaf     0   /* Print error and exit program */\r
1512 #define CV_ErrModeParent   1   /* Print error and continue */\r
1513 #define CV_ErrModeSilent   2   /* Don't print and continue */\r
1514 \r
1515 /* Retrives current error processing mode */\r
1516 CVAPI(int)  cvGetErrMode( void );\r
1517 \r
1518 /* Sets error processing mode, returns previously used mode */\r
1519 CVAPI(int) cvSetErrMode( int mode );\r
1520 \r
1521 /* Sets error status and performs some additonal actions (displaying message box,\r
1522    writing message to stderr, terminating application etc.)\r
1523    depending on the current error mode */\r
1524 CVAPI(void) cvError( int status, const char* func_name,\r
1525                     const char* err_msg, const char* file_name, int line );\r
1526 \r
1527 /* Retrieves textual description of the error given its code */\r
1528 CVAPI(const char*) cvErrorStr( int status );\r
1529 \r
1530 /* Retrieves detailed information about the last error occured */\r
1531 CVAPI(int) cvGetErrInfo( const char** errcode_desc, const char** description,\r
1532                         const char** filename, int* line );\r
1533 \r
1534 /* Maps IPP error codes to the counterparts from OpenCV */\r
1535 CVAPI(int) cvErrorFromIppStatus( int ipp_status );\r
1536 \r
1537 typedef int (CV_CDECL *CvErrorCallback)( int status, const char* func_name,\r
1538                     const char* err_msg, const char* file_name, int line, void* userdata );\r
1539 \r
1540 /* Assigns a new error-handling function */\r
1541 CVAPI(CvErrorCallback) cvRedirectError( CvErrorCallback error_handler,\r
1542                                        void* userdata CV_DEFAULT(NULL),\r
1543                                        void** prev_userdata CV_DEFAULT(NULL) );\r
1544 \r
1545 /*\r
1546     Output to:\r
1547         cvNulDevReport - nothing\r
1548         cvStdErrReport - console(fprintf(stderr,...))\r
1549         cvGuiBoxReport - MessageBox(WIN32)\r
1550 */\r
1551 CVAPI(int) cvNulDevReport( int status, const char* func_name, const char* err_msg,\r
1552                           const char* file_name, int line, void* userdata );\r
1553 \r
1554 CVAPI(int) cvStdErrReport( int status, const char* func_name, const char* err_msg,\r
1555                           const char* file_name, int line, void* userdata );\r
1556 \r
1557 CVAPI(int) cvGuiBoxReport( int status, const char* func_name, const char* err_msg,\r
1558                           const char* file_name, int line, void* userdata );\r
1559 \r
1560 typedef void* (CV_CDECL *CvAllocFunc)(size_t size, void* userdata);\r
1561 typedef int (CV_CDECL *CvFreeFunc)(void* pptr, void* userdata);\r
1562 \r
1563 /* Set user-defined memory managment functions (substitutors for malloc and free) that\r
1564    will be called by cvAlloc, cvFree and higher-level functions (e.g. cvCreateImage) */\r
1565 CVAPI(void) cvSetMemoryManager( CvAllocFunc alloc_func CV_DEFAULT(NULL),\r
1566                                CvFreeFunc free_func CV_DEFAULT(NULL),\r
1567                                void* userdata CV_DEFAULT(NULL));\r
1568 \r
1569 \r
1570 typedef IplImage* (CV_STDCALL* Cv_iplCreateImageHeader)\r
1571                             (int,int,int,char*,char*,int,int,int,int,int,\r
1572                             IplROI*,IplImage*,void*,IplTileInfo*);\r
1573 typedef void (CV_STDCALL* Cv_iplAllocateImageData)(IplImage*,int,int);\r
1574 typedef void (CV_STDCALL* Cv_iplDeallocate)(IplImage*,int);\r
1575 typedef IplROI* (CV_STDCALL* Cv_iplCreateROI)(int,int,int,int,int);\r
1576 typedef IplImage* (CV_STDCALL* Cv_iplCloneImage)(const IplImage*);\r
1577 \r
1578 /* Makes OpenCV use IPL functions for IplImage allocation/deallocation */\r
1579 CVAPI(void) cvSetIPLAllocators( Cv_iplCreateImageHeader create_header,\r
1580                                Cv_iplAllocateImageData allocate_data,\r
1581                                Cv_iplDeallocate deallocate,\r
1582                                Cv_iplCreateROI create_roi,\r
1583                                Cv_iplCloneImage clone_image );\r
1584 \r
1585 #define CV_TURN_ON_IPL_COMPATIBILITY()                                  \\r
1586     cvSetIPLAllocators( iplCreateImageHeader, iplAllocateImage,         \\r
1587                         iplDeallocate, iplCreateROI, iplCloneImage )\r
1588 \r
1589 /****************************************************************************************\\r
1590 *                                    Data Persistence                                    *\r
1591 \****************************************************************************************/\r
1592 \r
1593 /********************************** High-level functions ********************************/\r
1594 \r
1595 /* opens existing or creates new file storage */\r
1596 CVAPI(CvFileStorage*)  cvOpenFileStorage( const char* filename,\r
1597                                           CvMemStorage* memstorage,\r
1598                                           int flags );\r
1599 \r
1600 /* closes file storage and deallocates buffers */\r
1601 CVAPI(void) cvReleaseFileStorage( CvFileStorage** fs );\r
1602 \r
1603 /* returns attribute value or 0 (NULL) if there is no such attribute */\r
1604 CVAPI(const char*) cvAttrValue( const CvAttrList* attr, const char* attr_name );\r
1605 \r
1606 /* starts writing compound structure (map or sequence) */\r
1607 CVAPI(void) cvStartWriteStruct( CvFileStorage* fs, const char* name,\r
1608                                 int struct_flags, const char* type_name CV_DEFAULT(NULL),\r
1609                                 CvAttrList attributes CV_DEFAULT(cvAttrList()));\r
1610 \r
1611 /* finishes writing compound structure */\r
1612 CVAPI(void) cvEndWriteStruct( CvFileStorage* fs );\r
1613 \r
1614 /* writes an integer */\r
1615 CVAPI(void) cvWriteInt( CvFileStorage* fs, const char* name, int value );\r
1616 \r
1617 /* writes a floating-point number */\r
1618 CVAPI(void) cvWriteReal( CvFileStorage* fs, const char* name, double value );\r
1619 \r
1620 /* writes a string */\r
1621 CVAPI(void) cvWriteString( CvFileStorage* fs, const char* name,\r
1622                            const char* str, int quote CV_DEFAULT(0) );\r
1623 \r
1624 /* writes a comment */\r
1625 CVAPI(void) cvWriteComment( CvFileStorage* fs, const char* comment,\r
1626                             int eol_comment );\r
1627 \r
1628 /* writes instance of a standard type (matrix, image, sequence, graph etc.)\r
1629    or user-defined type */\r
1630 CVAPI(void) cvWrite( CvFileStorage* fs, const char* name, const void* ptr,\r
1631                          CvAttrList attributes CV_DEFAULT(cvAttrList()));\r
1632 \r
1633 /* starts the next stream */\r
1634 CVAPI(void) cvStartNextStream( CvFileStorage* fs );\r
1635 \r
1636 /* helper function: writes multiple integer or floating-point numbers */\r
1637 CVAPI(void) cvWriteRawData( CvFileStorage* fs, const void* src,\r
1638                                 int len, const char* dt );\r
1639 \r
1640 /* returns the hash entry corresponding to the specified literal key string or 0\r
1641    if there is no such a key in the storage */\r
1642 CVAPI(CvStringHashNode*) cvGetHashedKey( CvFileStorage* fs, const char* name,\r
1643                                         int len CV_DEFAULT(-1),\r
1644                                         int create_missing CV_DEFAULT(0));\r
1645 \r
1646 /* returns file node with the specified key within the specified map\r
1647    (collection of named nodes) */\r
1648 CVAPI(CvFileNode*) cvGetRootFileNode( const CvFileStorage* fs,\r
1649                                      int stream_index CV_DEFAULT(0) );\r
1650 \r
1651 /* returns file node with the specified key within the specified map\r
1652    (collection of named nodes) */\r
1653 CVAPI(CvFileNode*) cvGetFileNode( CvFileStorage* fs, CvFileNode* map,\r
1654                                  const CvStringHashNode* key,\r
1655                                  int create_missing CV_DEFAULT(0) );\r
1656 \r
1657 /* this is a slower version of cvGetFileNode that takes the key as a literal string */\r
1658 CVAPI(CvFileNode*) cvGetFileNodeByName( const CvFileStorage* fs,\r
1659                                        const CvFileNode* map,\r
1660                                        const char* name );\r
1661 \r
1662 CV_INLINE int cvReadInt( const CvFileNode* node, int default_value CV_DEFAULT(0) )\r
1663 {\r
1664     return !node ? default_value :\r
1665         CV_NODE_IS_INT(node->tag) ? node->data.i :\r
1666         CV_NODE_IS_REAL(node->tag) ? cvRound(node->data.f) : 0x7fffffff;\r
1667 }\r
1668 \r
1669 \r
1670 CV_INLINE int cvReadIntByName( const CvFileStorage* fs, const CvFileNode* map,\r
1671                          const char* name, int default_value CV_DEFAULT(0) )\r
1672 {\r
1673     return cvReadInt( cvGetFileNodeByName( fs, map, name ), default_value );\r
1674 }\r
1675 \r
1676 \r
1677 CV_INLINE double cvReadReal( const CvFileNode* node, double default_value CV_DEFAULT(0.) )\r
1678 {\r
1679     return !node ? default_value :\r
1680         CV_NODE_IS_INT(node->tag) ? (double)node->data.i :\r
1681         CV_NODE_IS_REAL(node->tag) ? node->data.f : 1e300;\r
1682 }\r
1683 \r
1684 \r
1685 CV_INLINE double cvReadRealByName( const CvFileStorage* fs, const CvFileNode* map,\r
1686                         const char* name, double default_value CV_DEFAULT(0.) )\r
1687 {\r
1688     return cvReadReal( cvGetFileNodeByName( fs, map, name ), default_value );\r
1689 }\r
1690 \r
1691 \r
1692 CV_INLINE const char* cvReadString( const CvFileNode* node,\r
1693                         const char* default_value CV_DEFAULT(NULL) )\r
1694 {\r
1695     return !node ? default_value : CV_NODE_IS_STRING(node->tag) ? node->data.str.ptr : 0;\r
1696 }\r
1697 \r
1698 \r
1699 CV_INLINE const char* cvReadStringByName( const CvFileStorage* fs, const CvFileNode* map,\r
1700                         const char* name, const char* default_value CV_DEFAULT(NULL) )\r
1701 {\r
1702     return cvReadString( cvGetFileNodeByName( fs, map, name ), default_value );\r
1703 }\r
1704 \r
1705 \r
1706 /* decodes standard or user-defined object and returns it */\r
1707 CVAPI(void*) cvRead( CvFileStorage* fs, CvFileNode* node,\r
1708                         CvAttrList* attributes CV_DEFAULT(NULL));\r
1709 \r
1710 /* decodes standard or user-defined object and returns it */\r
1711 CV_INLINE void* cvReadByName( CvFileStorage* fs, const CvFileNode* map,\r
1712                               const char* name, CvAttrList* attributes CV_DEFAULT(NULL) )\r
1713 {\r
1714     return cvRead( fs, cvGetFileNodeByName( fs, map, name ), attributes );\r
1715 }\r
1716 \r
1717 \r
1718 /* starts reading data from sequence or scalar numeric node */\r
1719 CVAPI(void) cvStartReadRawData( const CvFileStorage* fs, const CvFileNode* src,\r
1720                                CvSeqReader* reader );\r
1721 \r
1722 /* reads multiple numbers and stores them to array */\r
1723 CVAPI(void) cvReadRawDataSlice( const CvFileStorage* fs, CvSeqReader* reader,\r
1724                                int count, void* dst, const char* dt );\r
1725 \r
1726 /* combination of two previous functions for easier reading of whole sequences */\r
1727 CVAPI(void) cvReadRawData( const CvFileStorage* fs, const CvFileNode* src,\r
1728                           void* dst, const char* dt );\r
1729 \r
1730 /* writes a copy of file node to file storage */\r
1731 CVAPI(void) cvWriteFileNode( CvFileStorage* fs, const char* new_node_name,\r
1732                             const CvFileNode* node, int embed );\r
1733 \r
1734 /* returns name of file node */\r
1735 CVAPI(const char*) cvGetFileNodeName( const CvFileNode* node );\r
1736 \r
1737 /*********************************** Adding own types ***********************************/\r
1738 \r
1739 CVAPI(void) cvRegisterType( const CvTypeInfo* info );\r
1740 CVAPI(void) cvUnregisterType( const char* type_name );\r
1741 CVAPI(CvTypeInfo*) cvFirstType(void);\r
1742 CVAPI(CvTypeInfo*) cvFindType( const char* type_name );\r
1743 CVAPI(CvTypeInfo*) cvTypeOf( const void* struct_ptr );\r
1744 \r
1745 /* universal functions */\r
1746 CVAPI(void) cvRelease( void** struct_ptr );\r
1747 CVAPI(void*) cvClone( const void* struct_ptr );\r
1748 \r
1749 /* simple API for reading/writing data */\r
1750 CVAPI(void) cvSave( const char* filename, const void* struct_ptr,\r
1751                     const char* name CV_DEFAULT(NULL),\r
1752                     const char* comment CV_DEFAULT(NULL),\r
1753                     CvAttrList attributes CV_DEFAULT(cvAttrList()));\r
1754 CVAPI(void*) cvLoad( const char* filename,\r
1755                      CvMemStorage* memstorage CV_DEFAULT(NULL),\r
1756                      const char* name CV_DEFAULT(NULL),\r
1757                      const char** real_name CV_DEFAULT(NULL) );\r
1758 \r
1759 /*********************************** Measuring Execution Time ***************************/\r
1760 \r
1761 /* helper functions for RNG initialization and accurate time measurement:\r
1762    uses internal clock counter on x86 */\r
1763 CVAPI(int64)  cvGetTickCount( void );\r
1764 CVAPI(double) cvGetTickFrequency( void );\r
1765 \r
1766 /*********************************** Multi-Threading ************************************/\r
1767 \r
1768 /* retrieve/set the number of threads used in OpenMP implementations */\r
1769 CVAPI(int)  cvGetNumThreads( void );\r
1770 CVAPI(void) cvSetNumThreads( int threads CV_DEFAULT(0) );\r
1771 /* get index of the thread being executed */\r
1772 CVAPI(int)  cvGetThreadNum( void );\r
1773 \r
1774 /*************** Convenience functions for better interaction with HighGUI **************/\r
1775 \r
1776 typedef IplImage* (CV_CDECL * CvLoadImageFunc)( const char* filename, int colorness );\r
1777 typedef CvMat* (CV_CDECL * CvLoadImageMFunc)( const char* filename, int colorness );\r
1778 typedef int (CV_CDECL * CvSaveImageFunc)( const char* filename, const CvArr* image,\r
1779                                           const int* params );\r
1780 typedef void (CV_CDECL * CvShowImageFunc)( const char* windowname, const CvArr* image );\r
1781 \r
1782 CVAPI(int) cvSetImageIOFunctions( CvLoadImageFunc _load_image, CvLoadImageMFunc _load_image_m,\r
1783                             CvSaveImageFunc _save_image, CvShowImageFunc _show_image );\r
1784 \r
1785 #define CV_SET_IMAGE_IO_FUNCTIONS() \\r
1786     cvSetImageIOFunctions( cvLoadImage, cvLoadImageM, cvSaveImage, cvShowImage )\r
1787 \r
1788 #ifdef __cplusplus\r
1789 }\r
1790 \r
1791 class CV_EXPORTS CvImage\r
1792 {\r
1793 public:\r
1794     CvImage() : image(0), refcount(0) {}\r
1795     CvImage( CvSize size, int depth, int channels )\r
1796     {\r
1797         image = cvCreateImage( size, depth, channels );\r
1798         refcount = image ? new int(1) : 0;\r
1799     }\r
1800 \r
1801     CvImage( IplImage* img ) : image(img)\r
1802     {\r
1803         refcount = image ? new int(1) : 0;\r
1804     }\r
1805 \r
1806     CvImage( const CvImage& img ) : image(img.image), refcount(img.refcount)\r
1807     {\r
1808         if( refcount ) ++(*refcount);\r
1809     }\r
1810 \r
1811     CvImage( const char* filename, const char* imgname=0, int color=-1 ) : image(0), refcount(0)\r
1812     { load( filename, imgname, color ); }\r
1813 \r
1814     CvImage( CvFileStorage* fs, const char* mapname, const char* imgname ) : image(0), refcount(0)\r
1815     { read( fs, mapname, imgname ); }\r
1816 \r
1817     CvImage( CvFileStorage* fs, const char* seqname, int idx ) : image(0), refcount(0)\r
1818     { read( fs, seqname, idx ); }\r
1819 \r
1820     ~CvImage()\r
1821     {\r
1822         if( refcount && !(--*refcount) )\r
1823         {\r
1824             cvReleaseImage( &image );\r
1825             delete refcount;\r
1826         }\r
1827     }\r
1828 \r
1829     CvImage clone() { return CvImage(image ? cvCloneImage(image) : 0); }\r
1830 \r
1831     void create( CvSize size, int depth, int channels )\r
1832     {\r
1833         if( !image || !refcount ||\r
1834             image->width != size.width || image->height != size.height ||\r
1835             image->depth != depth || image->nChannels != channels )\r
1836             attach( cvCreateImage( size, depth, channels ));\r
1837     }\r
1838 \r
1839     void release() { detach(); }\r
1840     void clear() { detach(); }\r
1841 \r
1842     void attach( IplImage* img, bool use_refcount=true )\r
1843     {\r
1844         if( refcount && --*refcount == 0 )\r
1845         {\r
1846             cvReleaseImage( &image );\r
1847             delete refcount;\r
1848         }\r
1849         image = img;\r
1850         refcount = use_refcount && image ? new int(1) : 0;\r
1851     }\r
1852 \r
1853     void detach()\r
1854     {\r
1855         if( refcount && --*refcount == 0 )\r
1856         {\r
1857             cvReleaseImage( &image );\r
1858             delete refcount;\r
1859         }\r
1860         image = 0;\r
1861         refcount = 0;\r
1862     }\r
1863 \r
1864     bool load( const char* filename, const char* imgname=0, int color=-1 );\r
1865     bool read( CvFileStorage* fs, const char* mapname, const char* imgname );\r
1866     bool read( CvFileStorage* fs, const char* seqname, int idx );\r
1867     void save( const char* filename, const char* imgname, const int* params=0 );\r
1868     void write( CvFileStorage* fs, const char* imgname );\r
1869 \r
1870     void show( const char* window_name );\r
1871     bool is_valid() { return image != 0; }\r
1872 \r
1873     int width() const { return image ? image->width : 0; }\r
1874     int height() const { return image ? image->height : 0; }\r
1875 \r
1876     CvSize size() const { return image ? cvSize(image->width, image->height) : cvSize(0,0); }\r
1877 \r
1878     CvSize roi_size() const\r
1879     {\r
1880         return !image ? cvSize(0,0) :\r
1881             !image->roi ? cvSize(image->width,image->height) :\r
1882             cvSize(image->roi->width, image->roi->height);\r
1883     }\r
1884 \r
1885     CvRect roi() const\r
1886     {\r
1887         return !image ? cvRect(0,0,0,0) :\r
1888             !image->roi ? cvRect(0,0,image->width,image->height) :\r
1889             cvRect(image->roi->xOffset,image->roi->yOffset,\r
1890                    image->roi->width,image->roi->height);\r
1891     }\r
1892 \r
1893     int coi() const { return !image || !image->roi ? 0 : image->roi->coi; }\r
1894 \r
1895     void set_roi(CvRect roi) { cvSetImageROI(image,roi); }\r
1896     void reset_roi() { cvResetImageROI(image); }\r
1897     void set_coi(int coi) { cvSetImageCOI(image,coi); }\r
1898     int depth() const { return image ? image->depth : 0; }\r
1899     int channels() const { return image ? image->nChannels : 0; }\r
1900     int pix_size() const { return image ? ((image->depth & 255)>>3)*image->nChannels : 0; }\r
1901 \r
1902     uchar* data() { return image ? (uchar*)image->imageData : 0; }\r
1903     const uchar* data() const { return image ? (const uchar*)image->imageData : 0; }\r
1904     int step() const { return image ? image->widthStep : 0; }\r
1905     int origin() const { return image ? image->origin : 0; }\r
1906 \r
1907     uchar* roi_row(int y)\r
1908     {\r
1909         assert(0<=y);\r
1910         assert(!image ?\r
1911                 1 : image->roi ?\r
1912                 y<image->roi->height : y<image->height);\r
1913         \r
1914         return !image ? 0 :\r
1915             !image->roi ?\r
1916                 (uchar*)(image->imageData + y*image->widthStep) :\r
1917                 (uchar*)(image->imageData + (y+image->roi->yOffset)*image->widthStep +\r
1918                 image->roi->xOffset*((image->depth & 255)>>3)*image->nChannels);\r
1919     }\r
1920 \r
1921     const uchar* roi_row(int y) const\r
1922     {\r
1923         assert(0<=y);\r
1924         assert(!image ?\r
1925                 1 : image->roi ?\r
1926                 y<image->roi->height : y<image->height); \r
1927 \r
1928         return !image ? 0 :\r
1929             !image->roi ?\r
1930                 (const uchar*)(image->imageData + y*image->widthStep) :\r
1931                 (const uchar*)(image->imageData + (y+image->roi->yOffset)*image->widthStep +\r
1932                 image->roi->xOffset*((image->depth & 255)>>3)*image->nChannels);\r
1933     }\r
1934 \r
1935     operator const IplImage* () const { return image; }\r
1936     operator IplImage* () { return image; }\r
1937 \r
1938     CvImage& operator = (const CvImage& img)\r
1939     {\r
1940         if( img.refcount )\r
1941             ++*img.refcount;\r
1942         if( refcount && !(--*refcount) )\r
1943             cvReleaseImage( &image );\r
1944         image=img.image;\r
1945         refcount=img.refcount;\r
1946         return *this;\r
1947     }\r
1948 \r
1949 protected:\r
1950     IplImage* image;\r
1951     int* refcount;\r
1952 };\r
1953 \r
1954 \r
1955 class CV_EXPORTS CvMatrix\r
1956 {\r
1957 public:\r
1958     CvMatrix() : matrix(0) {}\r
1959     CvMatrix( int rows, int cols, int type )\r
1960     { matrix = cvCreateMat( rows, cols, type ); }\r
1961 \r
1962     CvMatrix( int rows, int cols, int type, CvMat* hdr,\r
1963               void* data=0, int step=CV_AUTOSTEP )\r
1964     { matrix = cvInitMatHeader( hdr, rows, cols, type, data, step ); }\r
1965 \r
1966     CvMatrix( int rows, int cols, int type, CvMemStorage* storage, bool alloc_data=true );\r
1967 \r
1968     CvMatrix( int rows, int cols, int type, void* data, int step=CV_AUTOSTEP )\r
1969     { matrix = cvCreateMatHeader( rows, cols, type );\r
1970       cvSetData( matrix, data, step ); }\r
1971 \r
1972     CvMatrix( CvMat* m )\r
1973     { matrix = m; }\r
1974 \r
1975     CvMatrix( const CvMatrix& m )\r
1976     {\r
1977         matrix = m.matrix;\r
1978         addref();\r
1979     }\r
1980 \r
1981     CvMatrix( const char* filename, const char* matname=0, int color=-1 ) : matrix(0)\r
1982     {  load( filename, matname, color ); }\r
1983 \r
1984     CvMatrix( CvFileStorage* fs, const char* mapname, const char* matname ) : matrix(0)\r
1985     {  read( fs, mapname, matname ); }\r
1986 \r
1987     CvMatrix( CvFileStorage* fs, const char* seqname, int idx ) : matrix(0)\r
1988     {  read( fs, seqname, idx ); }\r
1989 \r
1990     ~CvMatrix()\r
1991     {\r
1992         release();\r
1993     }\r
1994 \r
1995     CvMatrix clone() { return CvMatrix(matrix ? cvCloneMat(matrix) : 0); }\r
1996 \r
1997     void set( CvMat* m, bool add_ref )\r
1998     {\r
1999         release();\r
2000         matrix = m;\r
2001         if( add_ref )\r
2002             addref();\r
2003     }\r
2004 \r
2005     void create( int rows, int cols, int type )\r
2006     {\r
2007         if( !matrix || !matrix->refcount ||\r
2008             matrix->rows != rows || matrix->cols != cols ||\r
2009             CV_MAT_TYPE(matrix->type) != type )\r
2010             set( cvCreateMat( rows, cols, type ), false );\r
2011     }\r
2012 \r
2013     void addref() const\r
2014     {\r
2015         if( matrix )\r
2016         {\r
2017             if( matrix->hdr_refcount )\r
2018                 ++matrix->hdr_refcount;\r
2019             else if( matrix->refcount )\r
2020                 ++*matrix->refcount;\r
2021         }\r
2022     }\r
2023 \r
2024     void release()\r
2025     {\r
2026         if( matrix )\r
2027         {\r
2028             if( matrix->hdr_refcount )\r
2029             {\r
2030                 if( --matrix->hdr_refcount == 0 )\r
2031                     cvReleaseMat( &matrix );\r
2032             }\r
2033             else if( matrix->refcount )\r
2034             {\r
2035                 if( --*matrix->refcount == 0 )\r
2036                     cvFree( &matrix->refcount );\r
2037             }\r
2038             matrix = 0;\r
2039         }\r
2040     }\r
2041 \r
2042     void clear()\r
2043     {\r
2044         release();\r
2045     }\r
2046 \r
2047     bool load( const char* filename, const char* matname=0, int color=-1 );\r
2048     bool read( CvFileStorage* fs, const char* mapname, const char* matname );\r
2049     bool read( CvFileStorage* fs, const char* seqname, int idx );\r
2050     void save( const char* filename, const char* matname, const int* params=0 );\r
2051     void write( CvFileStorage* fs, const char* matname );\r
2052 \r
2053     void show( const char* window_name );\r
2054 \r
2055     bool is_valid() { return matrix != 0; }\r
2056 \r
2057     int rows() const { return matrix ? matrix->rows : 0; }\r
2058     int cols() const { return matrix ? matrix->cols : 0; }\r
2059 \r
2060     CvSize size() const\r
2061     {\r
2062         return !matrix ? cvSize(0,0) : cvSize(matrix->rows,matrix->cols);\r
2063     }\r
2064 \r
2065     int type() const { return matrix ? CV_MAT_TYPE(matrix->type) : 0; }\r
2066     int depth() const { return matrix ? CV_MAT_DEPTH(matrix->type) : 0; }\r
2067     int channels() const { return matrix ? CV_MAT_CN(matrix->type) : 0; }\r
2068     int pix_size() const { return matrix ? CV_ELEM_SIZE(matrix->type) : 0; }\r
2069 \r
2070     uchar* data() { return matrix ? matrix->data.ptr : 0; }\r
2071     const uchar* data() const { return matrix ? matrix->data.ptr : 0; }\r
2072     int step() const { return matrix ? matrix->step : 0; }\r
2073 \r
2074     void set_data( void* data, int step=CV_AUTOSTEP )\r
2075     { cvSetData( matrix, data, step ); }\r
2076 \r
2077     uchar* row(int i) { return !matrix ? 0 : matrix->data.ptr + i*matrix->step; }\r
2078     const uchar* row(int i) const\r
2079     { return !matrix ? 0 : matrix->data.ptr + i*matrix->step; }\r
2080 \r
2081     operator const CvMat* () const { return matrix; }\r
2082     operator CvMat* () { return matrix; }\r
2083 \r
2084     CvMatrix& operator = (const CvMatrix& _m)\r
2085     {\r
2086         _m.addref();\r
2087         release();\r
2088         matrix = _m.matrix;\r
2089         return *this;\r
2090     }\r
2091 \r
2092 protected:\r
2093     CvMat* matrix;\r
2094 };\r
2095 \r
2096 \r
2097 // classes for automatic module/RTTI data registration/unregistration\r
2098 struct CV_EXPORTS CvModule\r
2099 {\r
2100     CvModule( CvModuleInfo* _info );\r
2101     ~CvModule();\r
2102     CvModuleInfo* info;\r
2103 \r
2104     static CvModuleInfo* first;\r
2105     static CvModuleInfo* last;\r
2106 };\r
2107 \r
2108 struct CV_EXPORTS CvType\r
2109 {\r
2110     CvType( const char* type_name,\r
2111             CvIsInstanceFunc is_instance, CvReleaseFunc release=0,\r
2112             CvReadFunc read=0, CvWriteFunc write=0, CvCloneFunc clone=0 );\r
2113     ~CvType();\r
2114     CvTypeInfo* info;\r
2115 \r
2116     static CvTypeInfo* first;\r
2117     static CvTypeInfo* last;\r
2118 };\r
2119 \r
2120 #endif\r
2121 \r
2122 #ifndef SKIP_INCLUDES // for now only expose old interface to swig\r
2123 #include "cxcore.hpp"\r
2124 #endif // SKIP_INCLUDES\r
2125 \r
2126 #endif /*_CXCORE_H_*/\r