Allow to read PNG image of color_type PNG_COLOR_TYPE_PALETTE with alpha channel

Correct reading PNG color type palette with or without alpha imread flags -1 or 1
Better not using pnginfo.h, using png_get_tRNS instead
This commit is contained in:
Hanusz Leszek
2013-08-19 19:05:37 +02:00
parent abf372d759
commit cf39ba5801
2 changed files with 111 additions and 10 deletions

View File

@@ -171,7 +171,9 @@ bool PngDecoder::readHeader()
if( !m_buf.empty() || m_f )
{
png_uint_32 wdth, hght;
int bit_depth, color_type;
int bit_depth, color_type, num_trans=0;
png_bytep trans;
png_color_16p trans_values;
png_read_info( png_ptr, info_ptr );
@@ -187,15 +189,22 @@ bool PngDecoder::readHeader()
{
switch(color_type)
{
case PNG_COLOR_TYPE_RGB:
case PNG_COLOR_TYPE_PALETTE:
m_type = CV_8UC3;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
m_type = CV_8UC4;
break;
default:
m_type = CV_8UC1;
case PNG_COLOR_TYPE_RGB:
m_type = CV_8UC3;
break;
case PNG_COLOR_TYPE_PALETTE:
png_get_tRNS( png_ptr, info_ptr, &trans, &num_trans, &trans_values);
//Check if there is a transparency value in the palette
if ( num_trans > 0 )
m_type = CV_8UC4;
else
m_type = CV_8UC3;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
m_type = CV_8UC4;
break;
default:
m_type = CV_8UC1;
}
if( bit_depth == 16 )
m_type = CV_MAKETYPE(CV_16U, CV_MAT_CN(m_type));