mirror of
https://github.com/opencv/opencv.git
synced 2025-01-18 06:03:15 +08:00
Improve error reporting of JPEG image encoder
OpenCV issue #2604 After this patch applied an attempt to encode empty images produces exception saying "Raw image encoder error: Empty JPEG image (DNL not supported)"
This commit is contained in:
parent
8521ac5d21
commit
255cd61a8c
@ -123,6 +123,15 @@ ImageEncoder BaseImageEncoder::newEncoder() const
|
|||||||
return ImageEncoder();
|
return ImageEncoder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BaseImageEncoder::throwOnEror() const
|
||||||
|
{
|
||||||
|
if(!m_last_error.empty())
|
||||||
|
{
|
||||||
|
std::string msg = "Raw image encoder error: " + m_last_error;
|
||||||
|
CV_Error( CV_BadImageSize, msg.c_str() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* End of file. */
|
/* End of file. */
|
||||||
|
@ -100,12 +100,16 @@ public:
|
|||||||
virtual string getDescription() const;
|
virtual string getDescription() const;
|
||||||
virtual ImageEncoder newEncoder() const;
|
virtual ImageEncoder newEncoder() const;
|
||||||
|
|
||||||
|
virtual void throwOnEror() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
string m_description;
|
string m_description;
|
||||||
|
|
||||||
string m_filename;
|
string m_filename;
|
||||||
vector<uchar>* m_buf;
|
vector<uchar>* m_buf;
|
||||||
bool m_buf_supported;
|
bool m_buf_supported;
|
||||||
|
|
||||||
|
string m_last_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -537,8 +537,10 @@ ImageEncoder JpegEncoder::newEncoder() const
|
|||||||
return new JpegEncoder;
|
return new JpegEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JpegEncoder::write( const Mat& img, const vector<int>& params )
|
bool JpegEncoder::write( const Mat& img, const vector<int>& params )
|
||||||
{
|
{
|
||||||
|
m_last_error.clear();
|
||||||
|
|
||||||
struct fileWrapper
|
struct fileWrapper
|
||||||
{
|
{
|
||||||
FILE* f;
|
FILE* f;
|
||||||
@ -633,6 +635,14 @@ bool JpegEncoder::write( const Mat& img, const vector<int>& params )
|
|||||||
}
|
}
|
||||||
|
|
||||||
_exit_:
|
_exit_:
|
||||||
|
|
||||||
|
if(!result)
|
||||||
|
{
|
||||||
|
char jmsg_buf[JMSG_LENGTH_MAX];
|
||||||
|
jerr.pub.format_message((j_common_ptr)&cinfo, jmsg_buf);
|
||||||
|
m_last_error = jmsg_buf;
|
||||||
|
}
|
||||||
|
|
||||||
jpeg_destroy_compress( &cinfo );
|
jpeg_destroy_compress( &cinfo );
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -426,6 +426,7 @@ bool imencode( const string& ext, InputArray _image,
|
|||||||
if( encoder->setDestination(buf) )
|
if( encoder->setDestination(buf) )
|
||||||
{
|
{
|
||||||
code = encoder->write(image, params);
|
code = encoder->write(image, params);
|
||||||
|
encoder->throwOnEror();
|
||||||
CV_Assert( code );
|
CV_Assert( code );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -433,8 +434,11 @@ bool imencode( const string& ext, InputArray _image,
|
|||||||
string filename = tempfile();
|
string filename = tempfile();
|
||||||
code = encoder->setDestination(filename);
|
code = encoder->setDestination(filename);
|
||||||
CV_Assert( code );
|
CV_Assert( code );
|
||||||
|
|
||||||
code = encoder->write(image, params);
|
code = encoder->write(image, params);
|
||||||
|
encoder->throwOnEror();
|
||||||
CV_Assert( code );
|
CV_Assert( code );
|
||||||
|
|
||||||
FILE* f = fopen( filename.c_str(), "rb" );
|
FILE* f = fopen( filename.c_str(), "rb" );
|
||||||
CV_Assert(f != 0);
|
CV_Assert(f != 0);
|
||||||
fseek( f, 0, SEEK_END );
|
fseek( f, 0, SEEK_END );
|
||||||
|
@ -282,3 +282,12 @@ TEST(Highgui_ImreadVSCvtColor, regression)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_JPEG
|
||||||
|
TEST(Highgui_Jpeg, encode_empty)
|
||||||
|
{
|
||||||
|
cv::Mat img;
|
||||||
|
std::vector<uchar> jpegImg;
|
||||||
|
|
||||||
|
ASSERT_THROW(cv::imencode(".jpg", img, jpegImg), cv::Exception);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user