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
48 #include "precomp.hpp"
49 #include "opencv2/opencv_modules.hpp"
50 #ifdef HAVE_OPENCV_HIGHGUI
51 # include "opencv2/highgui/highgui_c.h"
54 /////////////////////////////// CvImage implementation //////////////////////////////////
57 icvIsXmlOrYaml( const char* filename )
59 const char* suffix = strrchr( filename, '.' );
61 (strcmp( suffix, ".xml" ) == 0 ||
62 strcmp( suffix, ".Xml" ) == 0 ||
63 strcmp( suffix, ".XML" ) == 0 ||
64 strcmp( suffix, ".yml" ) == 0 ||
65 strcmp( suffix, ".Yml" ) == 0 ||
66 strcmp( suffix, ".YML" ) == 0 ||
67 strcmp( suffix, ".yaml" ) == 0 ||
68 strcmp( suffix, ".Yaml" ) == 0 ||
69 strcmp( suffix, ".YAML" ) == 0);
74 icvRetrieveImage( void* obj )
78 if( CV_IS_IMAGE(obj) )
80 else if( CV_IS_MAT(obj) )
82 CvMat* m = (CvMat*)obj;
83 img = cvCreateImageHeader( cvSize(m->cols,m->rows),
84 CV_MAT_DEPTH(m->type), CV_MAT_CN(m->type) );
85 cvSetData( img, m->data.ptr, m->step );
86 img->imageDataOrigin = (char*)m->refcount;
87 m->data.ptr = 0; m->step = 0;
93 CV_Error( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
100 bool CvImage::load( const char* filename, const char* imgname, int color )
104 if( icvIsXmlOrYaml(filename) )
106 img = icvRetrieveImage(cvLoad(filename,0,imgname));
107 if( (img->nChannels > 1) != (color == 0) )
108 CV_Error( CV_StsNotImplemented,
109 "RGB<->Grayscale conversion is not implemented for images stored in XML/YAML" );
111 IplImage* temp_img = 0;
112 temp_img = cvCreateImage( cvGetSize(img), img->depth, color > 0 ? 3 : 1 ));
113 cvCvtColor( img, temp_img, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
114 cvReleaseImage( &img );
118 #ifdef HAVE_OPENCV_HIGHGUI
120 img = cvLoadImage( filename, color );
128 bool CvImage::read( CvFileStorage* fs, const char* mapname, const char* imgname )
135 CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
137 obj = cvReadByName( fs, mapnode, imgname );
140 obj = cvReadByName( fs, 0, imgname );
142 img = icvRetrieveImage(obj);
148 bool CvImage::read( CvFileStorage* fs, const char* seqname, int idx )
152 CvFileNode* seqnode = seqname ?
153 cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);
155 if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
156 obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
157 img = icvRetrieveImage(obj);
163 void CvImage::save( const char* filename, const char* imgname, const int* params )
167 if( icvIsXmlOrYaml( filename ) )
168 cvSave( filename, image, imgname );
169 #ifdef HAVE_OPENCV_HIGHGUI
171 cvSaveImage( filename, image, params );
178 void CvImage::write( CvFileStorage* fs, const char* imgname )
181 cvWrite( fs, imgname, image );
185 void CvImage::show( const char* window_name )
187 #ifdef HAVE_OPENCV_HIGHGUI
189 cvShowImage( window_name, image );
196 /////////////////////////////// CvMatrix implementation //////////////////////////////////
198 CvMatrix::CvMatrix( int _rows, int _cols, int _type, CvMemStorage* storage, bool alloc_data )
202 matrix = (CvMat*)cvMemStorageAlloc( storage, sizeof(*matrix) );
203 cvInitMatHeader( matrix, _rows, _cols, _type, alloc_data ?
204 cvMemStorageAlloc( storage, _rows*_cols*CV_ELEM_SIZE(_type) ) : 0 );
211 icvRetrieveMatrix( void* obj )
217 else if( CV_IS_IMAGE(obj) )
219 IplImage* img = (IplImage*)obj;
220 CvMat hdr, *src = cvGetMat( img, &hdr );
221 m = cvCreateMat( src->rows, src->cols, src->type );
223 cvReleaseImage( &img );
228 CV_Error( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
235 bool CvMatrix::load( const char* filename, const char* matname, int color )
238 if( icvIsXmlOrYaml(filename) )
240 m = icvRetrieveMatrix(cvLoad(filename,0,matname));
242 if( (CV_MAT_CN(m->type) > 1) != (color == 0) )
243 CV_Error( CV_StsNotImplemented,
244 "RGB<->Grayscale conversion is not implemented for matrices stored in XML/YAML" );
247 temp_mat = cvCreateMat( m->rows, m->cols,
248 CV_MAKETYPE(CV_MAT_DEPTH(m->type), color > 0 ? 3 : 1 )));
249 cvCvtColor( m, temp_mat, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
254 #ifdef HAVE_OPENCV_HIGHGUI
256 m = cvLoadImageM( filename, color );
264 bool CvMatrix::read( CvFileStorage* fs, const char* mapname, const char* matname )
271 CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
273 obj = cvReadByName( fs, mapnode, matname );
276 obj = cvReadByName( fs, 0, matname );
278 m = icvRetrieveMatrix(obj);
284 bool CvMatrix::read( CvFileStorage* fs, const char* seqname, int idx )
288 CvFileNode* seqnode = seqname ?
289 cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);
291 if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
292 obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
293 m = icvRetrieveMatrix(obj);
299 void CvMatrix::save( const char* filename, const char* matname, const int* params )
303 if( icvIsXmlOrYaml( filename ) )
304 cvSave( filename, matrix, matname );
305 #ifdef HAVE_OPENCV_HIGHGUI
307 cvSaveImage( filename, matrix, params );
314 void CvMatrix::write( CvFileStorage* fs, const char* matname )
317 cvWrite( fs, matname, matrix );
321 void CvMatrix::show( const char* window_name )
323 #ifdef HAVE_OPENCV_HIGHGUI
325 cvShowImage( window_name, matrix );