X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/20af6fd5d30a69ce61a527d1c0a57be7165e4321..43c42c18d36c703a88b1b7b697bac27fe5608eca:/src/msw/dib.cpp diff --git a/src/msw/dib.cpp b/src/msw/dib.cpp index d6d127a340..b12baf0f83 100644 --- a/src/msw/dib.cpp +++ b/src/msw/dib.cpp @@ -45,9 +45,7 @@ #include #include -#if !defined(__MWERKS__) - #include -#endif +#include #include "wx/msw/dib.h" @@ -137,18 +135,10 @@ bool wxDIB::Create(int width, int height, int depth) return true; } -bool wxDIB::Create(const wxBitmap& bmp) -{ - wxCHECK_MSG( bmp.Ok(), false, wxT("wxDIB::Create(): invalid bitmap") ); - - if ( !Create(GetHbitmapOf(bmp)) ) - return false; - - return true; -} - bool wxDIB::Create(HBITMAP hbmp) { + wxCHECK_MSG( hbmp, false, wxT("wxDIB::Create(): invalid bitmap") ); + // this bitmap could already be a DIB section in which case we don't need // to convert it to DIB DIBSECTION ds; @@ -585,7 +575,7 @@ HGLOBAL wxDIB::ConvertFromBitmap(HBITMAP hbmp) wxPalette *wxDIB::CreatePalette() const { // GetDIBColorTable not available in eVC3 -#if defined(_WIN32_WCE) && _WIN32_WCE < 400 +#if !defined(__WXMSW__) || defined(_WIN32_WCE) && _WIN32_WCE < 400 return NULL; #else wxCHECK_MSG( m_handle, NULL, wxT("wxDIB::CreatePalette(): invalid object") ); @@ -666,9 +656,9 @@ wxPalette *wxDIB::CreatePalette() const #if wxUSE_IMAGE -bool wxDIB::Create(const wxImage& image) +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(); @@ -691,27 +681,48 @@ bool wxDIB::Create(const wxImage& image) unsigned char *dstLineStart = (unsigned char *)m_data; for ( int y = 0; y < h; y++ ) { - // copy one DIB line + // Copy one DIB line. Note that RGB components order is reversed in + // Windows bitmaps compared to wxImage and is actually BGR. unsigned char *dst = dstLineStart; if ( alpha ) { - for ( int x = 0; x < w; x++ ) + int x; + + switch ( pf ) { - // RGB order is reversed, and we need to premultiply - // all channels by alpha value for use with ::AlphaBlend. - const unsigned char a = *alpha++; - *dst++ = (unsigned char)((src[2] * a + 127) / 255); - *dst++ = (unsigned char)((src[1] * a + 127) / 255); - *dst++ = (unsigned char)((src[0] * a + 127) / 255); - *dst++ = a; - src += 3; + case PixelFormat_PreMultiplied: + // Pre-multiply pixel values so that the DIB could be used + // with ::AlphaBlend(). + for ( x = 0; x < w; x++ ) + { + const unsigned char a = *alpha++; + *dst++ = (unsigned char)((src[2] * a + 127) / 255); + *dst++ = (unsigned char)((src[1] * a + 127) / 255); + *dst++ = (unsigned char)((src[0] * a + 127) / 255); + *dst++ = a; + src += 3; + } + break; + + case PixelFormat_NotPreMultiplied: + // Just copy pixel data without changing it. + for ( x = 0; x < w; x++ ) + { + *dst++ = src[2]; + *dst++ = src[1]; + *dst++ = src[0]; + + *dst++ = *alpha++; + src += 3; + } + break; } + } else // no alpha channel { for ( int x = 0; x < w; x++ ) { - // RGB order is reversed. *dst++ = src[2]; *dst++ = src[1]; *dst++ = src[0]; @@ -740,14 +751,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 @@ -780,13 +798,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++; @@ -804,6 +839,9 @@ wxImage wxDIB::ConvertToImage() const srcLineStart += srcBytesPerLine; } + if ( hasOpaque && hasTransparent ) + hasAlpha = true; + if ( !hasAlpha && image.HasAlpha() ) image.ClearAlpha();