mirror of
https://github.com/opencv/opencv.git
synced 2025-06-07 17:44:04 +08:00
Merge pull request #25453 from Kumataro:fix25452
imgcodecs: jpeg: refactoring for JpegEncoder
This commit is contained in:
commit
5b0843728e
@ -619,11 +619,6 @@ bool JpegEncoder::write( const Mat& img, const std::vector<int>& params )
|
|||||||
|
|
||||||
std::vector<uchar> out_buf(1 << 12);
|
std::vector<uchar> out_buf(1 << 12);
|
||||||
|
|
||||||
#ifndef JCS_EXTENSIONS
|
|
||||||
AutoBuffer<uchar> _buffer;
|
|
||||||
uchar* buffer;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct jpeg_compress_struct cinfo;
|
struct jpeg_compress_struct cinfo;
|
||||||
JpegErrorMgr jerr;
|
JpegErrorMgr jerr;
|
||||||
JpegDestination dest;
|
JpegDestination dest;
|
||||||
@ -658,14 +653,40 @@ bool JpegEncoder::write( const Mat& img, const std::vector<int>& params )
|
|||||||
int _channels = img.channels();
|
int _channels = img.channels();
|
||||||
int channels = _channels > 1 ? 3 : 1;
|
int channels = _channels > 1 ? 3 : 1;
|
||||||
|
|
||||||
|
bool doDirectWrite = false;
|
||||||
|
switch( _channels )
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
cinfo.input_components = 1;
|
||||||
|
cinfo.in_color_space = JCS_GRAYSCALE;
|
||||||
|
doDirectWrite = true; // GRAY -> GRAY
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
#ifdef JCS_EXTENSIONS
|
#ifdef JCS_EXTENSIONS
|
||||||
cinfo.input_components = _channels;
|
cinfo.input_components = 3;
|
||||||
cinfo.in_color_space = _channels == 3 ? JCS_EXT_BGR
|
cinfo.in_color_space = JCS_EXT_BGR;
|
||||||
: _channels == 4 ? JCS_EXT_BGRX : JCS_GRAYSCALE;
|
doDirectWrite = true; // BGR -> BGR
|
||||||
#else
|
#else
|
||||||
cinfo.input_components = channels;
|
cinfo.input_components = 3;
|
||||||
cinfo.in_color_space = channels > 1 ? JCS_RGB : JCS_GRAYSCALE;
|
cinfo.in_color_space = JCS_RGB;
|
||||||
|
doDirectWrite = false; // BGR -> RGB
|
||||||
#endif
|
#endif
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
#ifdef JCS_EXTENSIONS
|
||||||
|
cinfo.input_components = 4;
|
||||||
|
cinfo.in_color_space = JCS_EXT_BGRX;
|
||||||
|
doDirectWrite = true; // BGRX -> BGRX
|
||||||
|
#else
|
||||||
|
cinfo.input_components = 3;
|
||||||
|
cinfo.in_color_space = JCS_RGB;
|
||||||
|
doDirectWrite = false; // BGRA -> RGB
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
CV_Error(cv::Error::StsError, cv::format("Unsupported number of _channels: %06d", _channels) );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
int quality = 95;
|
int quality = 95;
|
||||||
int progressive = 0;
|
int progressive = 0;
|
||||||
@ -781,30 +802,35 @@ bool JpegEncoder::write( const Mat& img, const std::vector<int>& params )
|
|||||||
|
|
||||||
jpeg_start_compress( &cinfo, TRUE );
|
jpeg_start_compress( &cinfo, TRUE );
|
||||||
|
|
||||||
#ifndef JCS_EXTENSIONS
|
if( doDirectWrite )
|
||||||
if( channels > 1 )
|
|
||||||
_buffer.allocate(width*channels);
|
|
||||||
buffer = _buffer.data();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
for( int y = 0; y < height; y++ )
|
|
||||||
{
|
{
|
||||||
uchar *data = img.data + img.step*y, *ptr = data;
|
for( int y = 0; y < height; y++ )
|
||||||
|
|
||||||
#ifndef JCS_EXTENSIONS
|
|
||||||
if( _channels == 3 )
|
|
||||||
{
|
{
|
||||||
icvCvt_BGR2RGB_8u_C3R( data, 0, buffer, 0, Size(width,1) );
|
uchar *data = const_cast<uchar*>(img.ptr<uchar>(y));
|
||||||
ptr = buffer;
|
jpeg_write_scanlines( &cinfo, &data, 1 );
|
||||||
}
|
}
|
||||||
else if( _channels == 4 )
|
}
|
||||||
{
|
else
|
||||||
icvCvt_BGRA2BGR_8u_C4C3R( data, 0, buffer, 0, Size(width,1), 2 );
|
{
|
||||||
ptr = buffer;
|
CV_Check(_channels, (_channels == 3) || (_channels == 4), "Unsupported number of channels(indirect write)");
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
jpeg_write_scanlines( &cinfo, &ptr, 1 );
|
AutoBuffer<uchar> _buffer;
|
||||||
|
_buffer.allocate(width*channels);
|
||||||
|
uchar *buffer = _buffer.data();
|
||||||
|
|
||||||
|
for( int y = 0; y < height; y++ )
|
||||||
|
{
|
||||||
|
uchar *data = const_cast<uchar*>(img.ptr<uchar>(y));
|
||||||
|
if( _channels == 3 )
|
||||||
|
{
|
||||||
|
icvCvt_BGR2RGB_8u_C3R( data, 0, buffer, 0, Size(width,1) );
|
||||||
|
}
|
||||||
|
else // if( _channels == 4 )
|
||||||
|
{
|
||||||
|
icvCvt_BGRA2BGR_8u_C4C3R( data, 0, buffer, 0, Size(width,1), 2 );
|
||||||
|
}
|
||||||
|
jpeg_write_scanlines( &cinfo, &buffer, 1 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
jpeg_finish_compress( &cinfo );
|
jpeg_finish_compress( &cinfo );
|
||||||
|
Loading…
Reference in New Issue
Block a user