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