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