diff --git a/modules/imgcodecs/src/grfmt_png.cpp b/modules/imgcodecs/src/grfmt_png.cpp index e672e0cc9..c8ff2447f 100644 --- a/modules/imgcodecs/src/grfmt_png.cpp +++ b/modules/imgcodecs/src/grfmt_png.cpp @@ -190,16 +190,14 @@ bool PngDecoder::readHeader() switch(color_type) { 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 ) + png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values); + if( num_trans > 0 ) m_type = CV_8UC4; else m_type = CV_8UC3; break; + case PNG_COLOR_TYPE_GRAY_ALPHA: case PNG_COLOR_TYPE_RGB_ALPHA: m_type = CV_8UC4; break; @@ -255,12 +253,13 @@ bool PngDecoder::readData( Mat& img ) * stripping alpha.. 18.11.2004 Axel Walthelm */ png_set_strip_alpha( png_ptr ); - } + } else + png_set_tRNS_to_alpha( png_ptr ); if( m_color_type == PNG_COLOR_TYPE_PALETTE ) png_set_palette_to_rgb( png_ptr ); - if( m_color_type == PNG_COLOR_TYPE_GRAY && m_bit_depth < 8 ) + if( (m_color_type & PNG_COLOR_MASK_COLOR) == 0 && m_bit_depth < 8 ) #if (PNG_LIBPNG_VER_MAJOR*10000 + PNG_LIBPNG_VER_MINOR*100 + PNG_LIBPNG_VER_RELEASE >= 10209) || \ (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR == 0 && PNG_LIBPNG_VER_RELEASE >= 18) png_set_expand_gray_1_2_4_to_8( png_ptr ); @@ -268,7 +267,7 @@ bool PngDecoder::readData( Mat& img ) png_set_gray_1_2_4_to_8( png_ptr ); #endif - if( CV_MAT_CN(m_type) > 1 && color ) + if( (m_color_type & PNG_COLOR_MASK_COLOR) && color ) png_set_bgr( png_ptr ); // convert RGB to BGR else if( color ) png_set_gray_to_rgb( png_ptr ); // Gray->RGB