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