Update the changelog
[opencv] / cxcore / include / cxcore.hpp
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_HPP_
44 #define _CXCORE_HPP_
45
46 class CV_EXPORTS CvImage
47 {
48 public:
49     CvImage() : image(0), refcount(0) {}
50     CvImage( CvSize size, int depth, int channels )
51     {
52         image = cvCreateImage( size, depth, channels );
53         refcount = image ? new int(1) : 0;
54     }
55
56     CvImage( IplImage* img ) : image(img)
57     {
58         refcount = image ? new int(1) : 0;
59     }
60
61     CvImage( const CvImage& img ) : image(img.image), refcount(img.refcount)
62     {
63         if( refcount ) ++(*refcount);
64     }
65
66     CvImage( const char* filename, const char* imgname=0, int color=-1 ) : image(0), refcount(0)
67     { load( filename, imgname, color ); }
68
69     CvImage( CvFileStorage* fs, const char* mapname, const char* imgname ) : image(0), refcount(0)
70     { read( fs, mapname, imgname ); }
71
72     CvImage( CvFileStorage* fs, const char* seqname, int idx ) : image(0), refcount(0)
73     { read( fs, seqname, idx ); }
74
75     ~CvImage()
76     {
77         if( refcount && !(--*refcount) )
78         {
79             cvReleaseImage( &image );
80             delete refcount;
81         }
82     }
83
84     CvImage clone() { return CvImage(image ? cvCloneImage(image) : 0); }
85
86     void create( CvSize size, int depth, int channels )
87     {
88         if( !image || !refcount ||
89             image->width != size.width || image->height != size.height ||
90             image->depth != depth || image->nChannels != channels )
91             attach( cvCreateImage( size, depth, channels ));
92     }
93
94     void release() { detach(); }
95     void clear() { detach(); }
96
97     void attach( IplImage* img, bool use_refcount=true )
98     {
99         if( refcount )
100         {
101             if( --*refcount == 0 )
102                 cvReleaseImage( &image );
103             delete refcount;
104         }
105         image = img;
106         refcount = use_refcount && image ? new int(1) : 0;
107     }
108
109     void detach()
110     {
111         if( refcount )
112         {
113             if( --*refcount == 0 )
114                 cvReleaseImage( &image );
115             delete refcount;
116             refcount = 0;
117         }
118         image = 0;
119     }
120
121     bool load( const char* filename, const char* imgname=0, int color=-1 );
122     bool read( CvFileStorage* fs, const char* mapname, const char* imgname );
123     bool read( CvFileStorage* fs, const char* seqname, int idx );
124     void save( const char* filename, const char* imgname );
125     void write( CvFileStorage* fs, const char* imgname );
126
127     void show( const char* window_name );
128     bool is_valid() { return image != 0; }
129
130     int width() const { return image ? image->width : 0; }
131     int height() const { return image ? image->height : 0; }
132
133     CvSize size() const { return image ? cvSize(image->width, image->height) : cvSize(0,0); }
134
135     CvSize roi_size() const
136     {
137         return !image ? cvSize(0,0) :
138             !image->roi ? cvSize(image->width,image->height) :
139             cvSize(image->roi->width, image->roi->height);
140     }
141
142     CvRect roi() const
143     {
144         return !image ? cvRect(0,0,0,0) :
145             !image->roi ? cvRect(0,0,image->width,image->height) :
146             cvRect(image->roi->xOffset,image->roi->yOffset,
147                    image->roi->width,image->roi->height);
148     }
149
150     int coi() const { return !image || !image->roi ? 0 : image->roi->coi; }
151
152     void set_roi(CvRect roi) { cvSetImageROI(image,roi); }
153     void reset_roi() { cvResetImageROI(image); }
154     void set_coi(int coi) { cvSetImageCOI(image,coi); }
155     int depth() const { return image ? image->depth : 0; }
156     int channels() const { return image ? image->nChannels : 0; }
157     int pix_size() const { return image ? ((image->depth & 255)>>3)*image->nChannels : 0; }
158
159     uchar* data() { return image ? (uchar*)image->imageData : 0; }
160     const uchar* data() const { return image ? (const uchar*)image->imageData : 0; }
161     int step() const { return image ? image->widthStep : 0; }
162     int origin() const { return image ? image->origin : 0; }
163
164     uchar* roi_row(int y)
165     {
166         assert(0<=y);
167         assert(!image ?
168                 1 : image->roi ?
169                 y<image->roi->height : y<image->height);
170         
171         return !image ? 0 :
172             !image->roi ?
173                 (uchar*)(image->imageData + y*image->widthStep) :
174                 (uchar*)(image->imageData + (y+image->roi->yOffset)*image->widthStep +
175                 image->roi->xOffset*((image->depth & 255)>>3)*image->nChannels);
176     }
177
178     const uchar* roi_row(int y) const
179     {
180         assert(0<=y);
181         assert(!image ?
182                 1 : image->roi ?
183                 y<image->roi->height : y<image->height); 
184
185         return !image ? 0 :
186             !image->roi ?
187                 (const uchar*)(image->imageData + y*image->widthStep) :
188                 (const uchar*)(image->imageData + (y+image->roi->yOffset)*image->widthStep +
189                 image->roi->xOffset*((image->depth & 255)>>3)*image->nChannels);
190     }
191
192     operator const IplImage* () const { return image; }
193     operator IplImage* () { return image; }
194
195     CvImage& operator = (const CvImage& img)
196     {
197         if( img.refcount )
198             ++*img.refcount;
199         if( refcount && !(--*refcount) )
200             cvReleaseImage( &image );
201         image=img.image;
202         refcount=img.refcount;
203         return *this;
204     }
205
206 protected:
207     IplImage* image;
208     int* refcount;
209 };
210
211
212 class CV_EXPORTS CvMatrix
213 {
214 public:
215     CvMatrix() : matrix(0) {}
216     CvMatrix( int rows, int cols, int type )
217     { matrix = cvCreateMat( rows, cols, type ); }
218
219     CvMatrix( int rows, int cols, int type, CvMat* hdr,
220               void* data=0, int step=CV_AUTOSTEP )
221     { matrix = cvInitMatHeader( hdr, rows, cols, type, data, step ); }
222
223     CvMatrix( int rows, int cols, int type, CvMemStorage* storage, bool alloc_data=true );
224
225     CvMatrix( int rows, int cols, int type, void* data, int step=CV_AUTOSTEP )
226     { matrix = cvCreateMatHeader( rows, cols, type );
227       cvSetData( matrix, data, step ); }
228
229     CvMatrix( CvMat* m )
230     { matrix = m; }
231
232     CvMatrix( const CvMatrix& m )
233     {
234         matrix = m.matrix;
235         addref();
236     }
237
238     CvMatrix( const char* filename, const char* matname=0, int color=-1 ) : matrix(0)
239     {  load( filename, matname, color ); }
240
241     CvMatrix( CvFileStorage* fs, const char* mapname, const char* matname ) : matrix(0)
242     {  read( fs, mapname, matname ); }
243
244     CvMatrix( CvFileStorage* fs, const char* seqname, int idx ) : matrix(0)
245     {  read( fs, seqname, idx ); }
246
247     ~CvMatrix()
248     {
249         release();
250     }
251
252     CvMatrix clone() { return CvMatrix(matrix ? cvCloneMat(matrix) : 0); }
253
254     void set( CvMat* m, bool add_ref )
255     {
256         release();
257         matrix = m;
258         if( add_ref )
259             addref();
260     }
261
262     void create( int rows, int cols, int type )
263     {
264         if( !matrix || !matrix->refcount ||
265             matrix->rows != rows || matrix->cols != cols ||
266             CV_MAT_TYPE(matrix->type) != type )
267             set( cvCreateMat( rows, cols, type ), false );
268     }
269
270     void addref() const
271     {
272         if( matrix )
273         {
274             if( matrix->hdr_refcount )
275                 ++matrix->hdr_refcount;
276             else if( matrix->refcount )
277                 ++*matrix->refcount;
278         }
279     }
280
281     void release()
282     {
283         if( matrix )
284         {
285             if( matrix->hdr_refcount )
286             {
287                 if( --matrix->hdr_refcount == 0 )
288                     cvReleaseMat( &matrix );
289             }
290             else if( matrix->refcount )
291             {
292                 if( --*matrix->refcount == 0 )
293                     cvFree( &matrix->refcount );
294             }
295             matrix = 0;
296         }
297     }
298
299     void clear()
300     {
301         release();
302     }
303
304     bool load( const char* filename, const char* matname=0, int color=-1 );
305     bool read( CvFileStorage* fs, const char* mapname, const char* matname );
306     bool read( CvFileStorage* fs, const char* seqname, int idx );
307     void save( const char* filename, const char* matname );
308     void write( CvFileStorage* fs, const char* matname );
309
310     void show( const char* window_name );
311
312     bool is_valid() { return matrix != 0; }
313
314     int rows() const { return matrix ? matrix->rows : 0; }
315     int cols() const { return matrix ? matrix->cols : 0; }
316
317     CvSize size() const
318     {
319         return !matrix ? cvSize(0,0) : cvSize(matrix->rows,matrix->cols);
320     }
321
322     int type() const { return matrix ? CV_MAT_TYPE(matrix->type) : 0; }
323     int depth() const { return matrix ? CV_MAT_DEPTH(matrix->type) : 0; }
324     int channels() const { return matrix ? CV_MAT_CN(matrix->type) : 0; }
325     int pix_size() const { return matrix ? CV_ELEM_SIZE(matrix->type) : 0; }
326
327     uchar* data() { return matrix ? matrix->data.ptr : 0; }
328     const uchar* data() const { return matrix ? matrix->data.ptr : 0; }
329     int step() const { return matrix ? matrix->step : 0; }
330
331     void set_data( void* data, int step=CV_AUTOSTEP )
332     { cvSetData( matrix, data, step ); }
333
334     uchar* row(int i) { return !matrix ? 0 : matrix->data.ptr + i*matrix->step; }
335     const uchar* row(int i) const
336     { return !matrix ? 0 : matrix->data.ptr + i*matrix->step; }
337
338     operator const CvMat* () const { return matrix; }
339     operator CvMat* () { return matrix; }
340
341     CvMatrix& operator = (const CvMatrix& _m)
342     {
343         _m.addref();
344         release();
345         matrix = _m.matrix;
346         return *this;
347     }
348
349 protected:
350     CvMat* matrix;
351 };
352
353
354 typedef IplImage* (CV_CDECL * CvLoadImageFunc)( const char* filename, int colorness );
355 typedef CvMat* (CV_CDECL * CvLoadImageMFunc)( const char* filename, int colorness );
356 typedef int (CV_CDECL * CvSaveImageFunc)( const char* filename, const CvArr* image );
357 typedef void (CV_CDECL * CvShowImageFunc)( const char* windowname, const CvArr* image );
358
359 CVAPI(int) cvSetImageIOFunctions( CvLoadImageFunc _load_image, CvLoadImageMFunc _load_image_m,
360                             CvSaveImageFunc _save_image, CvShowImageFunc _show_image );
361
362 #define CV_SET_IMAGE_IO_FUNCTIONS() \
363     cvSetImageIOFunctions( cvLoadImage, cvLoadImageM, cvSaveImage, cvShowImage )
364
365 // classes for automatic module/RTTI data registration/unregistration
366 struct CV_EXPORTS CvModule
367 {
368     CvModule( CvModuleInfo* _info );
369     ~CvModule();
370     CvModuleInfo* info;
371
372     static CvModuleInfo* first;
373     static CvModuleInfo* last;
374 };
375
376 struct CV_EXPORTS CvType
377 {
378     CvType( const char* type_name,
379             CvIsInstanceFunc is_instance, CvReleaseFunc release=0,
380             CvReadFunc read=0, CvWriteFunc write=0, CvCloneFunc clone=0 );
381     ~CvType();
382     CvTypeInfo* info;
383
384     static CvTypeInfo* first;
385     static CvTypeInfo* last;
386 };
387
388 #endif /*_CXCORE_HPP_*/