mirror of
https://github.com/opencv/opencv.git
synced 2025-06-07 17:44:04 +08:00
Merge pull request #24875 from tailsu:sd/jpeg-turbo-color-extensions
jpeg: use libjpeg-turbo built-in color conversions
This commit is contained in:
commit
48ba45f856
@ -409,7 +409,9 @@ bool JpegDecoder::readData( Mat& img )
|
||||
{
|
||||
jpeg_decompress_struct* cinfo = &((JpegState*)m_state)->cinfo;
|
||||
JpegErrorMgr* jerr = &((JpegState*)m_state)->jerr;
|
||||
#ifndef JCS_EXTENSIONS
|
||||
JSAMPARRAY buffer = 0;
|
||||
#endif
|
||||
|
||||
if( setjmp( jerr->setjmp_buffer ) == 0 )
|
||||
{
|
||||
@ -429,6 +431,18 @@ bool JpegDecoder::readData( Mat& img )
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef JCS_EXTENSIONS
|
||||
if( color )
|
||||
{
|
||||
cinfo->out_color_space = JCS_EXT_BGR;
|
||||
cinfo->out_color_components = 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
cinfo->out_color_space = JCS_GRAYSCALE;
|
||||
cinfo->out_color_components = 1;
|
||||
}
|
||||
#else
|
||||
if( color )
|
||||
{
|
||||
if( cinfo->num_components != 4 )
|
||||
@ -455,6 +469,7 @@ bool JpegDecoder::readData( Mat& img )
|
||||
cinfo->out_color_components = 4;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Check for Exif marker APP1
|
||||
jpeg_saved_marker_ptr exif_marker = NULL;
|
||||
@ -481,12 +496,17 @@ bool JpegDecoder::readData( Mat& img )
|
||||
|
||||
jpeg_start_decompress( cinfo );
|
||||
|
||||
#ifndef JCS_EXTENSIONS
|
||||
buffer = (*cinfo->mem->alloc_sarray)((j_common_ptr)cinfo,
|
||||
JPOOL_IMAGE, m_width*4, 1 );
|
||||
#endif
|
||||
|
||||
uchar* data = img.ptr();
|
||||
for( ; m_height--; data += step )
|
||||
{
|
||||
#ifdef JCS_EXTENSIONS
|
||||
jpeg_read_scanlines( cinfo, &data, 1 );
|
||||
#else
|
||||
jpeg_read_scanlines( cinfo, buffer, 1 );
|
||||
if( color )
|
||||
{
|
||||
@ -502,6 +522,7 @@ bool JpegDecoder::readData( Mat& img )
|
||||
else
|
||||
icvCvt_CMYK2Gray_8u_C4C1R( buffer[0], 0, data, 0, Size(m_width,1) );
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
result = true;
|
||||
@ -593,8 +614,11 @@ bool JpegEncoder::write( const Mat& img, const std::vector<int>& params )
|
||||
int width = img.cols, height = img.rows;
|
||||
|
||||
std::vector<uchar> out_buf(1 << 12);
|
||||
|
||||
#ifndef JCS_EXTENSIONS
|
||||
AutoBuffer<uchar> _buffer;
|
||||
uchar* buffer;
|
||||
#endif
|
||||
|
||||
struct jpeg_compress_struct cinfo;
|
||||
JpegErrorMgr jerr;
|
||||
@ -629,8 +653,15 @@ bool JpegEncoder::write( const Mat& img, const std::vector<int>& params )
|
||||
|
||||
int _channels = img.channels();
|
||||
int channels = _channels > 1 ? 3 : 1;
|
||||
|
||||
#ifdef JCS_EXTENSIONS
|
||||
cinfo.input_components = _channels;
|
||||
cinfo.in_color_space = _channels == 3 ? JCS_EXT_BGR
|
||||
: _channels == 4 ? JCS_EXT_BGRX : JCS_GRAYSCALE;
|
||||
#else
|
||||
cinfo.input_components = channels;
|
||||
cinfo.in_color_space = channels > 1 ? JCS_RGB : JCS_GRAYSCALE;
|
||||
#endif
|
||||
|
||||
int quality = 95;
|
||||
int progressive = 0;
|
||||
@ -746,14 +777,17 @@ bool JpegEncoder::write( const Mat& img, const std::vector<int>& params )
|
||||
|
||||
jpeg_start_compress( &cinfo, TRUE );
|
||||
|
||||
#ifndef JCS_EXTENSIONS
|
||||
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;
|
||||
|
||||
#ifndef JCS_EXTENSIONS
|
||||
if( _channels == 3 )
|
||||
{
|
||||
icvCvt_BGR2RGB_8u_C3R( data, 0, buffer, 0, Size(width,1) );
|
||||
@ -764,6 +798,7 @@ bool JpegEncoder::write( const Mat& img, const std::vector<int>& params )
|
||||
icvCvt_BGRA2BGR_8u_C4C3R( data, 0, buffer, 0, Size(width,1), 2 );
|
||||
ptr = buffer;
|
||||
}
|
||||
#endif
|
||||
|
||||
jpeg_write_scanlines( &cinfo, &ptr, 1 );
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user