1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 /* ////////////////////////////////////////////////////////////////////
44 // C++ classes for image and matrices
50 /////////////////////////////// CvImage implementation //////////////////////////////////
52 static CvLoadImageFunc load_image = 0;
53 static CvLoadImageMFunc load_image_m = 0;
54 static CvSaveImageFunc save_image = 0;
55 static CvShowImageFunc show_image = 0;
58 icvIsXmlOrYaml( const char* filename )
60 const char* suffix = strrchr( filename, '.' );
62 strcmp( suffix, ".xml" ) == 0 ||
63 strcmp( suffix, ".Xml" ) == 0 ||
64 strcmp( suffix, ".XML" ) == 0 ||
65 strcmp( suffix, ".yml" ) == 0 ||
66 strcmp( suffix, ".Yml" ) == 0 ||
67 strcmp( suffix, ".YML" ) == 0 ||
68 strcmp( suffix, ".yaml" ) == 0 ||
69 strcmp( suffix, ".Yaml" ) == 0 ||
70 strcmp( suffix, ".YAML" ) == 0;
75 icvRetrieveImage( void* obj )
79 CV_FUNCNAME( "icvRetrieveImage" );
83 if( CV_IS_IMAGE(obj) )
85 else if( CV_IS_MAT(obj) )
87 CvMat* m = (CvMat*)obj;
88 CV_CALL( img = cvCreateImageHeader( cvSize(m->cols,m->rows),
89 CV_MAT_DEPTH(m->type), CV_MAT_CN(m->type) ));
90 cvSetData( img, m->data.ptr, m->step );
91 img->imageDataOrigin = (char*)m->refcount;
92 m->data.ptr = 0; m->step = 0;
98 CV_ERROR( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
107 bool CvImage::load( const char* filename, const char* imgname, int color )
111 CV_FUNCNAME( "CvImage::read" );
115 if( icvIsXmlOrYaml(filename) )
117 img = icvRetrieveImage(cvLoad(filename,0,imgname));
118 if( (img->nChannels > 1) != (color == 0) )
119 CV_ERROR( CV_StsNotImplemented,
120 "RGB<->Grayscale conversion is not implemented for images stored in XML/YAML" );
122 IplImage* temp_img = 0;
123 CV_CALL( temp_img = cvCreateImage( cvGetSize(img), img->depth, color > 0 ? 3 : 1 ));
124 cvCvtColor( img, temp_img, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
125 cvReleaseImage( &img );
132 img = load_image( filename, color );
134 CV_ERROR( CV_StsNotImplemented,
135 "Loading an image stored in such a format requires HigGUI.\n"
136 "Link it to your program and call any function from it\n" );
147 bool CvImage::read( CvFileStorage* fs, const char* mapname, const char* imgname )
154 CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
156 obj = cvReadByName( fs, mapnode, imgname );
159 obj = cvReadByName( fs, 0, imgname );
161 img = icvRetrieveImage(obj);
167 bool CvImage::read( CvFileStorage* fs, const char* seqname, int idx )
171 CvFileNode* seqnode = seqname ?
172 cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);
174 if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
175 obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
176 img = icvRetrieveImage(obj);
182 void CvImage::save( const char* filename, const char* imgname )
184 CV_FUNCNAME( "CvImage::write" );
189 if( icvIsXmlOrYaml( filename ) )
190 cvSave( filename, image, imgname );
194 save_image( filename, image );
196 CV_ERROR( CV_StsNotImplemented,
197 "Saving an image in such a format requires HigGUI.\n"
198 "Link it to your program and call any function from it\n" );
205 void CvImage::write( CvFileStorage* fs, const char* imgname )
208 cvWrite( fs, imgname, image );
212 void CvImage::show( const char* window_name )
214 CV_FUNCNAME( "CvMatrix::show" );
221 CV_ERROR( CV_StsNotImplemented,
222 "CvImage::show method requires HighGUI.\n"
223 "Link it to your program and call any function from it\n" );
224 show_image( window_name, image );
231 /////////////////////////////// CvMatrix implementation //////////////////////////////////
233 CvMatrix::CvMatrix( int rows, int cols, int type, CvMemStorage* storage, bool alloc_data )
237 matrix = (CvMat*)cvMemStorageAlloc( storage, sizeof(*matrix) );
238 cvInitMatHeader( matrix, rows, cols, type, alloc_data ?
239 cvMemStorageAlloc( storage, rows*cols*CV_ELEM_SIZE(type) ) : 0 );
246 icvRetrieveMatrix( void* obj )
250 CV_FUNCNAME( "icvRetrieveMatrix" );
256 else if( CV_IS_IMAGE(obj) )
258 IplImage* img = (IplImage*)obj;
259 CvMat hdr, *src = cvGetMat( img, &hdr );
260 CV_CALL( m = cvCreateMat( src->rows, src->cols, src->type ));
261 CV_CALL( cvCopy( src, m ));
262 cvReleaseImage( &img );
267 CV_ERROR( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
276 bool CvMatrix::load( const char* filename, const char* matname, int color )
280 CV_FUNCNAME( "CvMatrix::read" );
284 if( icvIsXmlOrYaml(filename) )
286 m = icvRetrieveMatrix(cvLoad(filename,0,matname));
288 if( (CV_MAT_CN(m->type) > 1) != (color == 0) )
289 CV_ERROR( CV_StsNotImplemented,
290 "RGB<->Grayscale conversion is not implemented for matrices stored in XML/YAML" );
293 CV_CALL( temp_mat = cvCreateMat( m->rows, m->cols,
294 CV_MAKETYPE(CV_MAT_DEPTH(m->type), color > 0 ? 3 : 1 )));
295 cvCvtColor( m, temp_mat, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
303 m = load_image_m( filename, color );
305 CV_ERROR( CV_StsNotImplemented,
306 "Loading an image stored in such a format requires HigGUI.\n"
307 "Link it to your program and call any function from it\n" );
318 bool CvMatrix::read( CvFileStorage* fs, const char* mapname, const char* matname )
325 CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
327 obj = cvReadByName( fs, mapnode, matname );
330 obj = cvReadByName( fs, 0, matname );
332 m = icvRetrieveMatrix(obj);
338 bool CvMatrix::read( CvFileStorage* fs, const char* seqname, int idx )
342 CvFileNode* seqnode = seqname ?
343 cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);
345 if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
346 obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
347 m = icvRetrieveMatrix(obj);
353 void CvMatrix::save( const char* filename, const char* matname )
355 CV_FUNCNAME( "CvMatrix::write" );
360 if( icvIsXmlOrYaml( filename ) )
361 cvSave( filename, matrix, matname );
365 save_image( filename, matrix );
367 CV_ERROR( CV_StsNotImplemented,
368 "Saving a matrixe in such a format requires HigGUI.\n"
369 "Link it to your program and call any function from it\n" );
376 void CvMatrix::write( CvFileStorage* fs, const char* matname )
379 cvWrite( fs, matname, matrix );
383 void CvMatrix::show( const char* window_name )
385 CV_FUNCNAME( "CvMatrix::show" );
392 CV_ERROR( CV_StsNotImplemented,
393 "CvMatrix::show method requires HighGUI.\n"
394 "Link it to your program and call any function from it\n" );
395 show_image( window_name, matrix );
403 cvSetImageIOFunctions( CvLoadImageFunc _load_image, CvLoadImageMFunc _load_image_m,
404 CvSaveImageFunc _save_image, CvShowImageFunc _show_image )
406 load_image = _load_image;
407 load_image_m = _load_image_m;
408 save_image = _save_image;
409 show_image = _show_image;
416 CvImage a(cvSize(300,200),8,3), b(cvSize(300,200),8,3);
417 CvRNG rng = cvRNG(-1);
419 CV_SET_IMAGE_IO_FUNCTIONS();
421 cvNamedWindow( "test", 1 );
424 cvRandArr( &rng, a, CV_RAND_UNI, cvScalarAll(0), cvScalarAll(100) );
425 cvCircle( b, cvPoint(100,100), 70, cvScalar(0,255,0), -1, CV_AA, 0 );