@ -723,6 +723,7 @@ static bool imwrite_( const String& filename, const std::vector<Mat>& img_vec,
Mat temp ;
if ( ! encoder - > isFormatSupported ( image . depth ( ) ) )
{
CV_LOG_ONCE_WARNING ( NULL , " Unsupported depth image for selected encoder is fallbacked to CV_8U. " ) ;
CV_Assert ( encoder - > isFormatSupported ( CV_8U ) ) ;
image . convertTo ( temp , CV_8U ) ;
image = temp ;
@ -787,10 +788,12 @@ static bool imwrite_( const String& filename, const std::vector<Mat>& img_vec,
catch ( const cv : : Exception & e )
{
CV_LOG_ERROR ( NULL , " imwrite_(' " < < filename < < " '): can't write data: " < < e . what ( ) ) ;
code = false ;
}
catch ( . . . )
{
CV_LOG_ERROR ( NULL , " imwrite_(' " < < filename < < " '): can't write data: unknown exception " ) ;
code = false ;
}
return code ;
@ -978,7 +981,7 @@ imdecodemulti_(const Mat& buf, int flags, std::vector<Mat>& mats, int start, int
ImageDecoder decoder = findDecoder ( buf_row ) ;
if ( ! decoder )
return 0 ;
return false ;
// Try to decode image by RGB instead of BGR.
if ( flags & IMREAD_COLOR_RGB & & flags ! = IMREAD_UNCHANGED )
@ -995,7 +998,7 @@ imdecodemulti_(const Mat& buf, int flags, std::vector<Mat>& mats, int start, int
filename = tempfile ( ) ;
FILE * f = fopen ( filename . c_str ( ) , " wb " ) ;
if ( ! f )
return 0 ;
return false ;
size_t bufSize = buf_row . total ( ) * buf . elemSize ( ) ;
if ( fwrite ( buf_row . ptr ( ) , 1 , bufSize , f ) ! = bufSize )
{
@ -1121,29 +1124,46 @@ bool imdecodemulti(InputArray _buf, int flags, CV_OUT std::vector<Mat>& mats, co
}
}
bool imencode ( const String & ext , InputArray _ima ge ,
bool imencode ( const String & ext , InputArray _img ,
std : : vector < uchar > & buf , const std : : vector < int > & params_ )
{
CV_TRACE_FUNCTION ( ) ;
Mat image = _image . getMat ( ) ;
CV_Assert ( ! image . empty ( ) ) ;
int channels = image . channels ( ) ;
CV_Assert ( channels = = 1 | | channels = = 3 | | channels = = 4 ) ;
ImageEncoder encoder = findEncoder ( ext ) ;
if ( ! encoder )
CV_Error ( Error : : StsError , " could not find encoder for the specified extension " ) ;
std : : vector < Mat > img_vec ;
CV_Assert ( ! _img . empty ( ) ) ;
if ( _img . isMatVector ( ) | | _img . isUMatVector ( ) )
_img . getMatVector ( img_vec ) ;
else
img_vec . push_back ( _img . getMat ( ) ) ;
CV_Assert ( ! img_vec . empty ( ) ) ;
const bool isMultiImg = img_vec . size ( ) > 1 ;
std : : vector < Mat > write_vec ;
for ( size_t page = 0 ; page < img_vec . size ( ) ; page + + )
{
Mat image = img_vec [ page ] ;
CV_Assert ( ! image . empty ( ) ) ;
const int channels = image . channels ( ) ;
CV_Assert ( channels = = 1 | | channels = = 3 | | channels = = 4 ) ;
Mat temp ;
if ( ! encoder - > isFormatSupported ( image . depth ( ) ) )
{
CV_LOG_ONCE_WARNING ( NULL , " Unsupported depth image for selected encoder is fallbacked to CV_8U. " ) ;
CV_Assert ( encoder - > isFormatSupported ( CV_8U ) ) ;
Mat temp ;
image . convertTo ( temp , CV_8U ) ;
image . convertTo ( temp , CV_8U ) ;
image = temp ;
}
write_vec . push_back ( image ) ;
}
# if CV_VERSION_MAJOR < 5 && defined(HAVE_IMGCODEC_HDR)
bool fixed = false ;
std : : vector < int > params_pair ( 2 ) ;
@ -1166,23 +1186,37 @@ bool imencode( const String& ext, InputArray _image,
CV_Check ( params . size ( ) , ( params . size ( ) & 1 ) = = 0 , " Encoding 'params' must be key-value pairs " ) ;
CV_CheckLE ( params . size ( ) , ( size_t ) ( CV_IO_MAX_IMAGE_PARAMS * 2 ) , " " ) ;
bool code ;
if ( encoder - > setDestination ( buf ) )
bool code = false ;
String filename ;
if ( ! encoder - > setDestination ( buf ) )
{
code = encoder - > write ( image , params ) ;
encoder - > throwOnEror ( ) ;
filename = tempfile ( ) ;
code = encoder - > setDestination ( filename ) ;
CV_Assert ( code ) ;
}
try {
if ( ! isMultiImg )
code = encoder - > write ( write_vec [ 0 ] , params ) ;
else
{
String filename = tempfile ( ) ;
code = encoder - > setDestination ( filename ) ;
CV_Assert ( code ) ;
code = encoder - > writemulti ( write_vec , params ) ;
code = encoder - > write ( image , params ) ;
encoder - > throwOnEror ( ) ;
CV_Assert ( code ) ;
}
catch ( const cv : : Exception & e )
{
CV_LOG_ERROR ( NULL , " imencode(): can't encode data: " < < e . what ( ) ) ;
code = false ;
}
catch ( . . . )
{
CV_LOG_ERROR ( NULL , " imencode(): can't encode data: unknown exception " ) ;
code = false ;
}
if ( ! filename . empty ( ) & & code )
{
FILE * f = fopen ( filename . c_str ( ) , " rb " ) ;
CV_Assert ( f ! = 0 ) ;
fseek ( f , 0 , SEEK_END ) ;
@ -1196,6 +1230,12 @@ bool imencode( const String& ext, InputArray _image,
return code ;
}
bool imencodemulti ( const String & ext , InputArrayOfArrays imgs ,
std : : vector < uchar > & buf , const std : : vector < int > & params )
{
return imencode ( ext , imgs , buf , params ) ;
}
bool haveImageReader ( const String & filename )
{
ImageDecoder decoder = cv : : findDecoder ( filename ) ;