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 46class CV_EXPORTS CvImage 47{ 48public: 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 && --*refcount == 0 ) 100 { 101 cvReleaseImage( &image ); 102 delete refcount; 103 } 104 image = img; 105 refcount = use_refcount && image ? new int(1) : 0; 106 } 107 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 ); 126 bool is_valid() { return image != 0; } 127 128 int width() const { return image ? image->width : 0; } 129 int height() const { return image ? image->height : 0; } 130 131 CvSize size() const { return image ? cvSize(image->width, image->height) : cvSize(0,0); } 132 133 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 140 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 148 int coi() const { return !image || !image->roi ? 0 : image->roi->coi; } 149 150 void set_roi(CvRect roi) { cvSetImageROI(image,roi); } 151 void reset_roi() { cvResetImageROI(image); } 152 void set_coi(int coi) { cvSetImageCOI(image,coi); } 153 int depth() const { return image ? image->depth : 0; } 154 int channels() const { return image ? image->nChannels : 0; } 155 int pix_size() const { return image ? ((image->depth & 255)>>3)*image->nChannels : 0; } 156 157 uchar* data() { return image ? (uchar*)image->imageData : 0; } 158 const uchar* data() const { return image ? (const uchar*)image->imageData : 0; } 159 int step() const { return image ? image->widthStep : 0; } 160 int origin() const { return image ? image->origin : 0; } 161 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 176 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 190 operator const IplImage* () const { return image; } 191 operator IplImage* () { return image; } 192 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 204protected: 205 IplImage* image; 206 int* refcount; 207}; 208 209 210class CV_EXPORTS CvMatrix 211{ 212public: 213 CvMatrix() : matrix(0) {} 214 CvMatrix( int rows, int cols, int type ) 215 { matrix = cvCreateMat( rows, cols, type ); } 216 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 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 227 CvMatrix( CvMat* m ) 228 { matrix = m; } 229 230 CvMatrix( const CvMatrix& m ) 231 { 232 matrix = m.matrix; 233 addref(); 234 } 235 236 CvMatrix( const char* filename, const char* matname=0, int color=-1 ) : matrix(0) 237 { load( filename, matname, color ); } 238 239 CvMatrix( CvFileStorage* fs, const char* mapname, const char* matname ) : matrix(0) 240 { read( fs, mapname, matname ); } 241 242 CvMatrix( CvFileStorage* fs, const char* seqname, int idx ) : matrix(0) 243 { read( fs, seqname, idx ); } 244 245 ~CvMatrix() 246 { 247 release(); 248 } 249 250 CvMatrix clone() { return CvMatrix(matrix ? cvCloneMat(matrix) : 0); } 251 252 void set( CvMat* m, bool add_ref ) 253 { 254 release(); 255 matrix = m; 256 if( add_ref ) 257 addref(); 258 } 259 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 268 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 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 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 310 bool is_valid() { return matrix != 0; } 311 312 int rows() const { return matrix ? matrix->rows : 0; } 313 int cols() const { return matrix ? matrix->cols : 0; } 314 315 CvSize size() const 316 { 317 return !matrix ? cvSize(0,0) : cvSize(matrix->rows,matrix->cols); 318 } 319 320 int type() const { return matrix ? CV_MAT_TYPE(matrix->type) : 0; } 321 int depth() const { return matrix ? CV_MAT_DEPTH(matrix->type) : 0; } 322 int channels() const { return matrix ? CV_MAT_CN(matrix->type) : 0; } 323 int pix_size() const { return matrix ? CV_ELEM_SIZE(matrix->type) : 0; } 324 325 uchar* data() { return matrix ? matrix->data.ptr : 0; } 326 const uchar* data() const { return matrix ? matrix->data.ptr : 0; } 327 int step() const { return matrix ? matrix->step : 0; } 328 329 void set_data( void* data, int step=CV_AUTOSTEP ) 330 { cvSetData( matrix, data, step ); } 331 332 uchar* row(int i) { return !matrix ? 0 : matrix->data.ptr + i*matrix->step; } 333 const uchar* row(int i) const 334 { return !matrix ? 0 : matrix->data.ptr + i*matrix->step; } 335 336 operator const CvMat* () const { return matrix; } 337 operator CvMat* () { return matrix; } 338 339 CvMatrix& operator = (const CvMatrix& _m) 340 { 341 _m.addref(); 342 release(); 343 matrix = _m.matrix; 344 return *this; 345 } 346 347protected: 348 CvMat* matrix; 349}; 350 351 352// classes for automatic module/RTTI data registration/unregistration 353struct CV_EXPORTS CvModule 354{ 355 CvModule( CvModuleInfo* _info ); 356 ~CvModule(); 357 CvModuleInfo* info; 358 359 static CvModuleInfo* first; 360 static CvModuleInfo* last; 361}; 362 363struct 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