X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fd90675b89c7f0b8fb9c6749f8314a26e23b1b95..eea4d01c65f9b29baa1193db762b4c6b8144af24:/src/msw/dib.cpp diff --git a/src/msw/dib.cpp b/src/msw/dib.cpp index 23ed673d49..4eff321612 100644 --- a/src/msw/dib.cpp +++ b/src/msw/dib.cpp @@ -660,7 +660,7 @@ wxPalette *wxDIB::CreatePalette() const bool wxDIB::Create(const wxImage& image, PixelFormat pf) { - wxCHECK_MSG( image.Ok(), false, wxT("invalid wxImage in wxDIB ctor") ); + wxCHECK_MSG( image.IsOk(), false, wxT("invalid wxImage in wxDIB ctor") ); const int h = image.GetHeight(); const int w = image.GetWidth(); @@ -688,12 +688,14 @@ bool wxDIB::Create(const wxImage& image, PixelFormat pf) unsigned char *dst = dstLineStart; if ( alpha ) { + int x; + switch ( pf ) { case PixelFormat_PreMultiplied: // Pre-multiply pixel values so that the DIB could be used // with ::AlphaBlend(). - for ( int x = 0; x < w; x++ ) + for ( x = 0; x < w; x++ ) { const unsigned char a = *alpha++; *dst++ = (unsigned char)((src[2] * a + 127) / 255); @@ -706,7 +708,7 @@ bool wxDIB::Create(const wxImage& image, PixelFormat pf) case PixelFormat_NotPreMultiplied: // Just copy pixel data without changing it. - for ( int x = 0; x < w; x++ ) + for ( x = 0; x < w; x++ ) { *dst++ = src[2]; *dst++ = src[1]; @@ -751,14 +753,21 @@ wxImage wxDIB::ConvertToImage() const const int w = GetWidth(); const int h = GetHeight(); wxImage image(w, h, false /* don't bother clearing memory */); - if ( !image.Ok() ) + if ( !image.IsOk() ) { wxFAIL_MSG( wxT("could not allocate data for image") ); return wxNullImage; } const int bpp = GetDepth(); + + // Remember if we have any "real" transparency, i.e. either any partially + // transparent pixels or not all pixels are fully opaque or fully + // transparent. bool hasAlpha = false; + bool hasOpaque = false; + bool hasTransparent = false; + if ( bpp == 32 ) { // 32 bit bitmaps may be either 0RGB or ARGB and we don't know in @@ -791,13 +800,30 @@ wxImage wxDIB::ConvertToImage() const // premultiplication done in Create() above const unsigned char a = *src; *alpha++ = a; + + // Check what kind of alpha do we have. + switch ( a ) + { + case 0: + hasTransparent = true; + break; + + default: + // Anything in between means we have real transparency + // and must use alpha channel. + hasAlpha = true; + break; + + case 255: + hasOpaque = true; + break; + } + if ( a > 0 ) { dst[0] = (dst[0] * 255) / a; dst[1] = (dst[1] * 255) / a; dst[2] = (dst[2] * 255) / a; - - hasAlpha = true; } src++; @@ -815,6 +841,9 @@ wxImage wxDIB::ConvertToImage() const srcLineStart += srcBytesPerLine; } + if ( hasOpaque && hasTransparent ) + hasAlpha = true; + if ( !hasAlpha && image.HasAlpha() ) image.ClearAlpha();