X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e854ed477efa1efdb17d2c9ae74d11b2c5e111e9..f45fac95d6067ed7d08b890b98e9bc6033bbb05e:/src/common/imagpng.cpp diff --git a/src/common/imagpng.cpp b/src/common/imagpng.cpp index 7370823ed9..480cc507f9 100644 --- a/src/common/imagpng.cpp +++ b/src/common/imagpng.cpp @@ -287,7 +287,7 @@ FindMaskColour(unsigned char **lines, png_uint_32 width, png_uint_32 height, } } - if ( !h.FindFirstUnusedColour(&rMask, &gMask, &bMask, rMask, gMask, bMask) ) + if ( !h.FindFirstUnusedColour(&rMask, &gMask, &bMask) ) { wxLogWarning(_("Too many colours in PNG, the image may be slightly blurred.")); @@ -435,14 +435,8 @@ void CopyDataFromPNG(wxImage *image, if ( transparency == Transparency_Mask ) { - if ( IsTransparent( a ) && ( ! ( rMask || gMask || bMask ) ) ) - { - rMask = r; - gMask = g; - bMask = b; - - FindMaskColour(lines, width, height, rMask, gMask, bMask ); - } + FindMaskColour(lines, width, height, + rMask, gMask, bMask); } else // transparency == Transparency_Alpha { @@ -670,9 +664,11 @@ bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbos // explanation why this line is mandatory png_set_write_fn( png_ptr, &wxinfo, wx_PNG_stream_writer, NULL); + const bool usesAlpha = (image->HasAlpha() || image->HasMask() ); + const int bytesPerPixel = usesAlpha ? 4 : 3; png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8, - PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + usesAlpha ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); png_color_8 sig_bit; sig_bit.red = 8; @@ -684,7 +680,7 @@ bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbos png_set_shift( png_ptr, &sig_bit ); png_set_packing( png_ptr ); - unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 ); + unsigned char *data = (unsigned char *)malloc( image->GetWidth()*bytesPerPixel ); if (!data) { png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); @@ -696,23 +692,27 @@ bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbos unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3); for (int x = 0; x < image->GetWidth(); x++) { - data[(x << 2) + 0] = *ptr++; - data[(x << 2) + 1] = *ptr++; - data[(x << 2) + 2] = *ptr++; - if ( image->HasAlpha() ) - { - data[(x << 2) + 3] = image->GetAlpha(x, y); - } - else if (( !image->HasMask() ) || \ - (data[(x << 2) + 0] != image->GetMaskRed()) || \ - (data[(x << 2) + 1] != image->GetMaskGreen()) || \ - (data[(x << 2) + 2] != image->GetMaskBlue())) - { - data[(x << 2) + 3] = 255; - } - else + register const int index = x * bytesPerPixel; + data[index + 0] = *ptr++; + data[index + 1] = *ptr++; + data[index + 2] = *ptr++; + + if (usesAlpha) { - data[(x << 2) + 3] = 0; + if ( image->HasAlpha() ) + { + data[index + 3] = image->GetAlpha(x, y); + } + else if ( (data[index + 0] != image->GetMaskRed()) + || (data[index + 1] != image->GetMaskGreen()) + || (data[index + 2] != image->GetMaskBlue()) ) + { + data[index + 3] = 255; + } + else + { + data[index + 3] = 0; + } } } png_bytep row_ptr = data;