Avoid unnecessary file I/Os in imdecode_() (Patch #2404)
Description
Avoid unnecessary file I/Os in imdecode_().
The variable "filename" is initialized with a temporary file name. By design, the "tempfile()" function creates a temporary file and deletes the file. In most cases the "filename" is not used at all. By only calling tempfile() when necessary we minimize file I/Os and we can get rid of the variable "removeTempFile".
I propose the following changing to "modules\highgui\src\loadsave.cpp:"
static void* imdecode_( const Mat& buf, int flags, int hdrtype, Mat* mat=0 ) { CV_Assert(buf.data && buf.isContinuous()); IplImage* image = 0; CvMat *matrix = 0; Mat temp, *data = &temp; string filename; ImageDecoder decoder = findDecoder(buf); if( decoder.empty() ) return 0; if( !decoder->setSource(buf) ) { filename = tempfile(); FILE* f = fopen( filename.c_str(), "wb" ); if( !f ) return 0; size_t bufSize = buf.cols*buf.rows*buf.elemSize(); fwrite( &buf.data[0], 1, bufSize, f ); fclose(f); decoder->setSource(filename); } if( !decoder->readHeader() ) { if( !filename.empty() ) remove(filename.c_str()); return 0; } CvSize size; size.width = decoder->width(); size.height = decoder->height(); int type = decoder->type(); if( flags != -1 ) { if( (flags & CV_LOAD_IMAGE_ANYDEPTH) == 0 ) type = CV_MAKETYPE(CV_8U, CV_MAT_CN(type)); if( (flags & CV_LOAD_IMAGE_COLOR) != 0 || ((flags & CV_LOAD_IMAGE_ANYCOLOR) != 0 && CV_MAT_CN(type) > 1) ) type = CV_MAKETYPE(CV_MAT_DEPTH(type), 3); else type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1); } if( hdrtype == LOAD_CVMAT || hdrtype == LOAD_MAT ) { if( hdrtype == LOAD_CVMAT ) { matrix = cvCreateMat( size.height, size.width, type ); temp = cvarrToMat(matrix); } else { mat->create( size.height, size.width, type ); data = mat; } } else { image = cvCreateImage( size, cvIplDepth(type), CV_MAT_CN(type) ); temp = cvarrToMat(image); } bool code = decoder->readData( *data ); if( !filename.empty() ) remove(filename.c_str()); if( !code ) { cvReleaseImage( &image ); cvReleaseMat( &matrix ); if( mat ) mat->release(); return 0; } return hdrtype == LOAD_CVMAT ? (void*)matrix : hdrtype == LOAD_IMAGE ? (void*)image : (void*)mat; }
Associated revisions
Merge pull request #2404 from yashdv:old_basic_structures_doc_fix
History
Updated by Alexander Smorkalov over 12 years ago
- Category set to highgui-images
- Assignee set to Vadim Pisarevsky
- Target version set to 2.4.3
Updated by Roy Reapor over 12 years ago
Patch submitted as a pull request https://github.com/Itseez/opencv/pull/34
Updated by Vadim Pisarevsky over 12 years ago
- Target version deleted (
2.4.3) - Assignee deleted (
Vadim Pisarevsky)
Updated by Andrey Kamaev over 12 years ago
- Assignee set to Andrey Kamaev
Updated by Andrey Kamaev over 12 years ago
Pull request is merged to master.
- Status changed from Open to Done
Updated by Andrey Kamaev about 12 years ago
- Target version set to 2.4.3