Commit 17130477 authored by Andrey Kamaev's avatar Andrey Kamaev Committed by OpenCV Buildbot

Merge pull request #344 from taka-no-me:improve_jpeg_encoder_errors

parents 09d93af9 255cd61a
...@@ -123,6 +123,15 @@ ImageEncoder BaseImageEncoder::newEncoder() const ...@@ -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: ...@@ -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 ...@@ -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 ) ...@@ -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, ...@@ -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, ...@@ -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) ...@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment