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.
43 // Loading and saving IPL images.
51 /****************************************************************************************\
53 \****************************************************************************************/
57 static vector<ImageDecoder> decoders;
58 static vector<ImageEncoder> encoders;
60 ImageDecoder findDecoder( const string& filename )
63 for( i = 0; i < decoders.size(); i++ )
65 size_t len = decoders[i]->signatureLength();
66 maxlen = std::max(maxlen, len);
69 FILE* f= fopen( filename.c_str(), "rb" );
71 return ImageDecoder();
72 string signature(maxlen, ' ');
73 maxlen = fread( &signature[0], 1, maxlen, f );
75 signature = signature.substr(0, maxlen);
77 for( i = 0; i < decoders.size(); i++ )
79 if( decoders[i]->checkSignature(signature) )
80 return decoders[i]->newDecoder();
83 return ImageDecoder();
86 ImageDecoder findDecoder( const Mat& buf )
90 if( buf.rows*buf.cols < 1 || !buf.isContinuous() )
91 return ImageDecoder();
93 for( i = 0; i < decoders.size(); i++ )
95 size_t len = decoders[i]->signatureLength();
96 maxlen = std::max(maxlen, len);
99 size_t bufSize = buf.rows*buf.cols*buf.elemSize();
100 maxlen = std::min(maxlen, bufSize);
101 string signature(maxlen, ' ');
102 memcpy( &signature[0], buf.data, maxlen );
104 for( i = 0; i < decoders.size(); i++ )
106 if( decoders[i]->checkSignature(signature) )
107 return decoders[i]->newDecoder();
110 return ImageDecoder();
113 ImageEncoder findEncoder( const string& _ext )
115 if( _ext.size() <= 1 )
116 return ImageEncoder();
118 const char* ext = strrchr( _ext.c_str(), '.' );
120 return ImageEncoder();
122 for( ext++; isalnum(ext[len]) && len < 128; len++ )
125 for( size_t i = 0; i < encoders.size(); i++ )
127 string description = encoders[i]->getDescription();
128 const char* descr = strchr( description.c_str(), '(' );
132 descr = strchr( descr + 1, '.' );
136 for( descr++; isalnum(descr[j]) && j < len; j++ )
138 int c1 = tolower(ext[j]);
139 int c2 = tolower(descr[j]);
143 if( j == len && !isalnum(descr[j]))
144 return encoders[i]->newEncoder();
149 return ImageEncoder();
152 struct ImageCodecInitializer
154 ImageCodecInitializer()
156 decoders.push_back( new BmpDecoder );
157 encoders.push_back( new BmpEncoder );
159 decoders.push_back( new JpegDecoder );
160 encoders.push_back( new JpegEncoder );
162 decoders.push_back( new SunRasterDecoder );
163 encoders.push_back( new SunRasterEncoder );
164 decoders.push_back( new PxMDecoder );
165 encoders.push_back( new PxMEncoder );
167 decoders.push_back( new TiffDecoder );
169 encoders.push_back( new TiffEncoder );
171 decoders.push_back( new PngDecoder );
172 encoders.push_back( new PngEncoder );
175 decoders.push_back( new Jpeg2KDecoder );
176 encoders.push_back( new Jpeg2KEncoder );
179 decoders.push_back( new ExrDecoder );
180 encoders.push_back( new ExrEncoder );
182 // because it is a generic image I/O API, supporting many formats,
183 // it should be last in the list.
185 decoders.push_back( new ImageIODecoder );
186 encoders.push_back( new ImageIOEncoder );
191 static ImageCodecInitializer initialize_codecs;
194 enum { LOAD_CVMAT=0, LOAD_IMAGE=1, LOAD_MAT=2 };
197 imread_( const string& filename, int flags, int hdrtype, Mat* mat=0 )
201 Mat temp, *data = &temp;
203 ImageDecoder decoder = findDecoder(filename);
204 if( decoder.empty() )
206 decoder->setSource(filename);
207 if( !decoder->readHeader() )
211 size.width = decoder->width();
212 size.height = decoder->height();
214 int type = decoder->type();
217 if( (flags & CV_LOAD_IMAGE_ANYDEPTH) == 0 )
218 type = CV_MAKETYPE(CV_8U, CV_MAT_CN(type));
220 if( (flags & CV_LOAD_IMAGE_COLOR) != 0 ||
221 ((flags & CV_LOAD_IMAGE_ANYCOLOR) != 0 && CV_MAT_CN(type) > 1) )
222 type = CV_MAKETYPE(CV_MAT_DEPTH(type), 3);
224 type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1);
227 if( hdrtype == LOAD_CVMAT || hdrtype == LOAD_MAT )
229 if( hdrtype == LOAD_CVMAT )
231 matrix = cvCreateMat( size.height, size.width, type );
232 temp = cvarrToMat(matrix);
236 mat->create( size.height, size.width, type );
242 image = cvCreateImage( size, cvIplDepth(type), CV_MAT_CN(type) );
243 temp = cvarrToMat(image);
246 if( !decoder->readData( *data ))
248 cvReleaseImage( &image );
249 cvReleaseMat( &matrix );
255 return hdrtype == LOAD_CVMAT ? (void*)matrix :
256 hdrtype == LOAD_IMAGE ? (void*)image : (void*)mat;
259 Mat imread( const string& filename, int flags )
262 imread_( filename, flags, LOAD_MAT, &img );
266 static bool imwrite_( const string& filename, const Mat& image,
267 const vector<int>& params, bool flipv )
270 const Mat* pimage = ℑ
272 CV_Assert( image.channels() == 1 || image.channels() == 3 || image.channels() == 4 );
274 ImageEncoder encoder = findEncoder( filename );
275 if( encoder.empty() )
276 CV_Error( CV_StsError, "could not find a writer for the specified extension" );
278 if( !encoder->isFormatSupported(image.depth()) )
280 CV_Assert( encoder->isFormatSupported(CV_8U) );
281 image.convertTo( temp, CV_8U );
287 flip(*pimage, temp, 0);
291 encoder->setDestination( filename );
292 bool code = encoder->write( *pimage, params );
298 bool imwrite( const string& filename, const Mat& img,
299 const vector<int>& params )
301 return imwrite_(filename, img, params, false);
305 imdecode_( const Mat& buf, int flags, int hdrtype, Mat* mat=0 )
307 CV_Assert(buf.data && buf.isContinuous());
310 Mat temp, *data = &temp;
311 char fnamebuf[L_tmpnam];
312 const char* filename = 0;
314 ImageDecoder decoder = findDecoder(buf);
315 if( decoder.empty() )
318 if( !decoder->setSource(buf) )
320 filename = tmpnam(fnamebuf);
321 FILE* f = fopen( filename, "wb" );
324 size_t bufSize = buf.cols*buf.rows*buf.elemSize();
325 fwrite( &buf.data, 1, bufSize, f );
327 decoder->setSource(filename);
330 if( !decoder->readHeader() )
338 size.width = decoder->width();
339 size.height = decoder->height();
341 int type = decoder->type();
344 if( (flags & CV_LOAD_IMAGE_ANYDEPTH) == 0 )
345 type = CV_MAKETYPE(CV_8U, CV_MAT_CN(type));
347 if( (flags & CV_LOAD_IMAGE_COLOR) != 0 ||
348 ((flags & CV_LOAD_IMAGE_ANYCOLOR) != 0 && CV_MAT_CN(type) > 1) )
349 type = CV_MAKETYPE(CV_MAT_DEPTH(type), 3);
351 type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1);
354 if( hdrtype == LOAD_CVMAT || hdrtype == LOAD_MAT )
356 if( hdrtype == LOAD_CVMAT )
358 matrix = cvCreateMat( size.height, size.width, type );
359 temp = cvarrToMat(matrix);
363 mat->create( size.height, size.width, type );
369 image = cvCreateImage( size, cvIplDepth(type), CV_MAT_CN(type) );
370 temp = cvarrToMat(image);
373 bool code = decoder->readData( *data );
379 cvReleaseImage( &image );
380 cvReleaseMat( &matrix );
386 return hdrtype == LOAD_CVMAT ? (void*)matrix :
387 hdrtype == LOAD_IMAGE ? (void*)image : (void*)mat;
391 Mat imdecode( const Mat& buf, int flags )
394 imdecode_( buf, flags, LOAD_MAT, &img );
398 bool imencode( const string& ext, const Mat& image,
399 vector<uchar>& buf, const vector<int>& params )
402 const Mat* pimage = ℑ
404 int channels = image.channels();
405 CV_Assert( channels == 1 || channels == 3 || channels == 4 );
407 ImageEncoder encoder = findEncoder( ext );
408 if( encoder.empty() )
409 CV_Error( CV_StsError, "could not find encoder for the specified extension" );
411 if( !encoder->isFormatSupported(image.depth()) )
413 CV_Assert( encoder->isFormatSupported(CV_8U) );
414 image.convertTo(temp, CV_8U);
419 if( encoder->setDestination(buf) )
421 code = encoder->write(image, params);
426 char fnamebuf[L_tmpnam];
427 const char* filename = tmpnam(fnamebuf);
428 code = encoder->setDestination(filename);
430 code = encoder->write(image, params);
432 FILE* f = fopen( filename, "rb" );
434 fseek( f, 0, SEEK_END );
436 buf.resize((size_t)pos);
437 fseek( f, 0, SEEK_SET );
438 buf.resize(fread( &buf[0], 1, buf.size(), f ));
447 /****************************************************************************************\
448 * HighGUI loading & saving function implementation *
449 \****************************************************************************************/
451 static int icvSetCXCOREBindings(void)
453 return CV_SET_IMAGE_IO_FUNCTIONS();
456 int cxcore_bindings_initialized = icvSetCXCOREBindings();
459 cvHaveImageReader( const char* filename )
461 cv::ImageDecoder decoder = cv::findDecoder(filename);
462 return !decoder.empty();
465 CV_IMPL int cvHaveImageWriter( const char* filename )
467 cv::ImageEncoder encoder = cv::findEncoder(filename);
468 return !encoder.empty();
472 cvLoadImage( const char* filename, int iscolor )
474 return (IplImage*)cv::imread_(filename, iscolor, cv::LOAD_IMAGE );
478 cvLoadImageM( const char* filename, int iscolor )
480 return (CvMat*)cv::imread_( filename, iscolor, cv::LOAD_CVMAT );
484 cvSaveImage( const char* filename, const CvArr* arr, const int* _params )
489 for( ; _params[i] > 0; i += 2 )
492 return cv::imwrite_(filename, cv::cvarrToMat(arr),
493 i > 0 ? cv::vector<int>(_params, _params+i) : cv::vector<int>(),
494 CV_IS_IMAGE(arr) && ((const IplImage*)arr)->origin == IPL_ORIGIN_BL );
497 /* decode image stored in the buffer */
499 cvDecodeImage( const CvMat* _buf, int iscolor )
501 CV_Assert( _buf && CV_IS_MAT_CONT(_buf->type) );
502 cv::Mat buf(1, _buf->rows*_buf->cols*CV_ELEM_SIZE(_buf->type), CV_8U, _buf->data.ptr);
503 return (IplImage*)cv::imdecode_(buf, iscolor, cv::LOAD_IMAGE );
507 cvDecodeImageM( const CvMat* _buf, int iscolor )
509 CV_Assert( _buf && CV_IS_MAT_CONT(_buf->type) );
510 cv::Mat buf(1, _buf->rows*_buf->cols*CV_ELEM_SIZE(_buf->type), CV_8U, _buf->data.ptr);
511 return (CvMat*)cv::imdecode_(buf, iscolor, cv::LOAD_CVMAT );
515 cvEncodeImage( const char* ext, const CvArr* arr, const int* _params )
520 for( ; _params[i] > 0; i += 2 )
523 cv::Mat img = cv::cvarrToMat(arr);
524 if( CV_IS_IMAGE(arr) && ((const IplImage*)arr)->origin == IPL_ORIGIN_BL )
527 cv::flip(img, temp, 0);
530 cv::vector<uchar> buf;
532 bool code = cv::imencode(ext, img, buf,
533 i > 0 ? std::vector<int>(_params, _params+i) : std::vector<int>() );
536 CvMat* _buf = cvCreateMat(1, (int)buf.size(), CV_8U);
537 memcpy( _buf->data.ptr, &buf[0], buf.size() );