Commit c65c67ef authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

preserve alpha channel in PNG, when imread(..., -1) is called (ticket #1107)

parent 69427996
...@@ -167,9 +167,18 @@ bool PngDecoder::readHeader() ...@@ -167,9 +167,18 @@ bool PngDecoder::readHeader()
if( bit_depth <= 8 || bit_depth == 16 ) if( bit_depth <= 8 || bit_depth == 16 )
{ {
m_type = color_type == PNG_COLOR_TYPE_RGB || switch(color_type)
color_type == PNG_COLOR_TYPE_RGB_ALPHA || {
color_type == PNG_COLOR_TYPE_PALETTE ? CV_8UC3 : CV_8UC1; 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;
}
if( bit_depth == 16 ) if( bit_depth == 16 )
m_type = CV_MAKETYPE(CV_16U, CV_MAT_CN(m_type)); m_type = CV_MAKETYPE(CV_16U, CV_MAT_CN(m_type));
result = true; result = true;
...@@ -210,15 +219,18 @@ bool PngDecoder::readData( Mat& img ) ...@@ -210,15 +219,18 @@ bool PngDecoder::readData( Mat& img )
else if( !isBigEndian() ) else if( !isBigEndian() )
png_set_swap( png_ptr ); png_set_swap( png_ptr );
/* observation: png_read_image() writes 400 bytes beyond if(img.channels() < 4)
* end of data when reading a 400x118 color png {
* "mpplus_sand.png". OpenCV crashes even with demo /* observation: png_read_image() writes 400 bytes beyond
* programs. Looking at the loaded image I'd say we get 4 * end of data when reading a 400x118 color png
* bytes per pixel instead of 3 bytes per pixel. Test * "mpplus_sand.png". OpenCV crashes even with demo
* indicate that it is a good idea to always ask for * programs. Looking at the loaded image I'd say we get 4
* stripping alpha.. 18.11.2004 Axel Walthelm * bytes per pixel instead of 3 bytes per pixel. Test
*/ * indicate that it is a good idea to always ask for
png_set_strip_alpha( png_ptr ); * stripping alpha.. 18.11.2004 Axel Walthelm
*/
png_set_strip_alpha( png_ptr );
}
if( m_color_type == PNG_COLOR_TYPE_PALETTE ) if( m_color_type == PNG_COLOR_TYPE_PALETTE )
png_set_palette_to_rgb( png_ptr ); png_set_palette_to_rgb( png_ptr );
......
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