X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/04ef50df3a0fa3c343800c554e609f98fc7575cc..0fe5e8fea017ecb73f8aaa8ceb745feb269b3f79:/src/msw/bitmap.cpp diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index d6e1ddd207..99e6867101 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -40,6 +40,8 @@ #include "wx/icon.h" #endif +//#include "device.h" + #include "wx/msw/private.h" #include "wx/log.h" @@ -88,10 +90,13 @@ void wxBitmapRefData::Free() if ( m_hBitmap) { + // printf("About to delete bitmap %d\n", (int) (HBITMAP) m_hBitmap); +#if 1 if ( !::DeleteObject((HBITMAP)m_hBitmap) ) { wxLogLastError(wxT("DeleteObject(hbitmap)")); } +#endif } delete m_bitmapMask; @@ -107,8 +112,6 @@ void wxBitmap::Init() { // m_refData = NULL; done in the base class ctor - if ( wxTheBitmapList ) - wxTheBitmapList->AddBitmap(this); } #ifdef __WIN32__ @@ -144,6 +147,10 @@ bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon) refData->m_bitmapMask = new wxMask((WXHBITMAP) wxInvertMask(iconInfo.hbmMask, w, h)); + + // delete the old one now as we don't need it any more + ::DeleteObject(iconInfo.hbmMask); + #if WXWIN_COMPATIBILITY_2 refData->m_ok = TRUE; #endif // WXWIN_COMPATIBILITY_2 @@ -218,8 +225,6 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon) wxBitmap::~wxBitmap() { - if (wxTheBitmapList) - wxTheBitmapList->DeleteObject(this); } wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) @@ -242,9 +247,9 @@ wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) // we assume that it is in XBM format which is not quite the same as // the format CreateBitmap() wants because the order of bytes in the // line is inversed! - static const size_t bytesPerLine = (width + 7) / 8; - static const size_t padding = bytesPerLine % 2; - static const size_t len = height * ( padding + bytesPerLine ); + const size_t bytesPerLine = (width + 7) / 8; + const size_t padding = bytesPerLine % 2; + const size_t len = height * ( padding + bytesPerLine ); data = (char *)malloc(len); const char *src = bits; char *dst = data; @@ -297,15 +302,15 @@ bool wxBitmap::CreateFromXpm(const char **data) Init(); wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") ) - + wxXPMDecoder decoder; wxImage img = decoder.ReadData(data); wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") ) - + *this = wxBitmap(img); return TRUE; #else - return FALSE; + return FALSE; #endif } @@ -332,7 +337,6 @@ wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) bool wxBitmap::Create(int w, int h, int d) { -#ifndef __WXMICROWIN__ UnRef(); m_refData = new wxBitmapRefData; @@ -342,7 +346,7 @@ bool wxBitmap::Create(int w, int h, int d) GetBitmapData()->m_depth = d; HBITMAP hbmp; - +#ifndef __WXMICROWIN__ if ( d > 0 ) { hbmp = ::CreateBitmap(w, h, 1, d, NULL); @@ -352,6 +356,7 @@ bool wxBitmap::Create(int w, int h, int d) } } else +#endif { ScreenHDC dc; hbmp = ::CreateCompatibleBitmap(dc, w, h); @@ -369,9 +374,6 @@ bool wxBitmap::Create(int w, int h, int d) GetBitmapData()->m_ok = hbmp != 0; #endif // WXWIN_COMPATIBILITY_2 return Ok(); -#else - return FALSE; -#endif } // ---------------------------------------------------------------------------- @@ -382,6 +384,126 @@ bool wxBitmap::Create(int w, int h, int d) bool wxBitmap::CreateFromImage( const wxImage& image, int depth ) { +#ifdef __WXMICROWIN__ + + // Set this to 1 to experiment with mask code, + // which currently doesn't work +#define USE_MASKS 0 + + m_refData = new wxBitmapRefData(); + + // Initial attempt at a simple-minded implementation. + // The bitmap will always be created at the screen depth, + // so the 'depth' argument is ignored. + + HDC hScreenDC = ::GetDC(NULL); + // printf("Screen planes = %d, bpp = %d\n", hScreenDC->psd->planes, hScreenDC->psd->bpp); + int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL); + + HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight()); + HBITMAP hMaskBitmap = NULL; + HBITMAP hOldMaskBitmap = NULL; + HDC hMaskDC = NULL; + unsigned char maskR = 0; + unsigned char maskG = 0; + unsigned char maskB = 0; + + // printf("Created bitmap %d\n", (int) hBitmap); + if (hBitmap == NULL) + { + ::ReleaseDC(NULL, hScreenDC); + return FALSE; + } + HDC hMemDC = ::CreateCompatibleDC(hScreenDC); + + HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap); + ::ReleaseDC(NULL, hScreenDC); + + // created an mono-bitmap for the possible mask + bool hasMask = image.HasMask(); + + if ( hasMask ) + { +#if USE_MASKS + // FIXME: we should be able to pass bpp = 1, but + // GdBlit can't handle a different depth +#if 0 + hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL ); +#else + hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight()); +#endif + maskR = image.GetMaskRed(); + maskG = image.GetMaskGreen(); + maskB = image.GetMaskBlue(); + + if (!hMaskBitmap) + { + hasMask = FALSE; + } + else + { + hScreenDC = ::GetDC(NULL); + hMaskDC = ::CreateCompatibleDC(hScreenDC); + ::ReleaseDC(NULL, hScreenDC); + + hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap); + } +#else + hasMask = FALSE; +#endif + } + + int i, j; + for (i = 0; i < image.GetWidth(); i++) + { + for (j = 0; j < image.GetHeight(); j++) + { + unsigned char red = image.GetRed(i, j); + unsigned char green = image.GetGreen(i, j); + unsigned char blue = image.GetBlue(i, j); + + ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue)); + + if (hasMask) + { + // scan the bitmap for the transparent colour and set the corresponding + // pixels in the mask to BLACK and the rest to WHITE + if (maskR == red && maskG == green && maskB == blue) + ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0)); + else + ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255)); + } + } + } + + ::SelectObject(hMemDC, hOldBitmap); + ::DeleteDC(hMemDC); + if (hasMask) + { + ::SelectObject(hMaskDC, hOldMaskBitmap); + ::DeleteDC(hMaskDC); + + ((wxBitmapRefData*)m_refData)->m_bitmapMask = new wxMask((WXHBITMAP) hMaskBitmap); + } + + SetWidth(image.GetWidth()); + SetHeight(image.GetHeight()); + SetDepth(screenDepth); + SetHBITMAP( (WXHBITMAP) hBitmap ); + +#if wxUSE_PALETTE + // Copy the palette from the source image + SetPalette(image.GetPalette()); +#endif // wxUSE_PALETTE + +#if WXWIN_COMPATIBILITY_2 + // check the wxBitmap object + GetBitmapData()->SetOk(); +#endif // WXWIN_COMPATIBILITY_2 + + return TRUE; + +#else wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") ) m_refData = new wxBitmapRefData(); @@ -427,6 +549,11 @@ bool wxBitmap::CreateFromImage( const wxImage& image, int depth ) if (depth == -1) depth = wxDisplayDepth(); SetDepth( depth ); +#if wxUSE_PALETTE + // Copy the palette from the source image + SetPalette(image.GetPalette()); +#endif // wxUSE_PALETTE + // create a DIB header int headersize = sizeof(BITMAPINFOHEADER); BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize ); @@ -463,12 +590,14 @@ bool wxBitmap::CreateFromImage( const wxImage& image, int depth ) hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight ); ::SelectObject( memdc, hbitmap); +#if wxUSE_PALETTE HPALETTE hOldPalette = 0; if (image.GetPalette().Ok()) { hOldPalette = ::SelectPalette(memdc, (HPALETTE) image.GetPalette().GetHPALETTE(), FALSE); ::RealizePalette(memdc); } +#endif // wxUSE_PALETTE // copy image data into DIB data and then into DDB (in a loop) unsigned char *data = image.GetData(); @@ -519,8 +648,10 @@ bool wxBitmap::CreateFromImage( const wxImage& image, int depth ) } SetHBITMAP( (WXHBITMAP) hbitmap ); +#if wxUSE_PALETTE if (hOldPalette) SelectPalette(memdc, hOldPalette, FALSE); +#endif // wxUSE_PALETTE // similarly, created an mono-bitmap for the possible mask if( image.HasMask() ) @@ -601,13 +732,75 @@ bool wxBitmap::CreateFromImage( const wxImage& image, int depth ) GetBitmapData()->SetOk(); #endif // WXWIN_COMPATIBILITY_2 - if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); - return TRUE; +#endif } wxImage wxBitmap::ConvertToImage() const { +#ifdef __WXMICROWIN__ + // Initial attempt at a simple-minded implementation. + // The bitmap will always be created at the screen depth, + // so the 'depth' argument is ignored. + // TODO: transparency (create a mask image) + + if (!Ok()) + { + wxFAIL_MSG( wxT("bitmap is invalid") ); + return wxNullImage; + } + + wxImage image; + + wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); + + // create an wxImage object + int width = GetWidth(); + int height = GetHeight(); + image.Create( width, height ); + unsigned char *data = image.GetData(); + if( !data ) + { + wxFAIL_MSG( wxT("could not allocate data for image") ); + return wxNullImage; + } + + HDC hScreenDC = ::GetDC(NULL); + + HDC hMemDC = ::CreateCompatibleDC(hScreenDC); + ::ReleaseDC(NULL, hScreenDC); + + HBITMAP hBitmap = (HBITMAP) GetHBITMAP(); + + HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap); + + int i, j; + for (i = 0; i < GetWidth(); i++) + { + for (j = 0; j < GetHeight(); j++) + { + COLORREF color = ::GetPixel(hMemDC, i, j); + unsigned char red = GetRValue(color); + unsigned char green = GetGValue(color); + unsigned char blue = GetBValue(color); + + image.SetRGB(i, j, red, green, blue); + } + } + + ::SelectObject(hMemDC, hOldBitmap); + ::DeleteDC(hMemDC); + +#if wxUSE_PALETTE + // Copy the palette from the source image + if (GetPalette()) + image.SetPalette(* GetPalette()); +#endif // wxUSE_PALETTE + + return image; + +#else // __MICROWIN__ + wxImage image; wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); @@ -732,6 +925,7 @@ wxImage wxBitmap::ConvertToImage() const free(lpBits); return image; +#endif } #endif // wxUSE_IMAGE @@ -782,7 +976,9 @@ bool wxBitmap::Create(void *data, long type, int width, int height, int depth) return handler->Create(this, data, type, width, height, depth); } -bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette) +bool wxBitmap::SaveFile(const wxString& filename, + int type, + const wxPalette *palette) { wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler); @@ -811,7 +1007,6 @@ bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *pal wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const { -#ifndef __WXMICROWIN__ wxCHECK_MSG( Ok() && (rect.x >= 0) && (rect.y >= 0) && (rect.x+rect.width <= GetWidth()) && @@ -821,35 +1016,41 @@ wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const wxBitmap ret( rect.width, rect.height, GetDepth() ); wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); +#ifndef __WXMICROWIN__ // copy bitmap data - HDC dcSrc = ::CreateCompatibleDC(NULL); - HDC dcDst = ::CreateCompatibleDC(NULL); - SelectObject(dcSrc, (HBITMAP) GetHBITMAP()); - SelectObject(dcDst, (HBITMAP) ret.GetHBITMAP()); - BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY); + MemoryHDC dcSrc, dcDst; + + { + SelectInHDC selectSrc(dcSrc, GetHbitmap()), + selectDst(dcDst, GetHbitmapOf(ret)); + + if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height, + dcSrc, rect.x, rect.y, SRCCOPY) ) + { + wxLogLastError(_T("BitBlt")); + } + } // copy mask if there is one - if (GetMask()) + if ( GetMask() ) { HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0); - SelectObject(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()); - SelectObject(dcDst, (HBITMAP) hbmpMask); - BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY); + SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()), + selectDst(dcDst, hbmpMask); + + if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height, + dcSrc, rect.x, rect.y, SRCCOPY) ) + { + wxLogLastError(_T("BitBlt")); + } wxMask *mask = new wxMask((WXHBITMAP) hbmpMask); ret.SetMask(mask); } - - SelectObject(dcDst, NULL); - SelectObject(dcSrc, NULL); - DeleteDC(dcDst); - DeleteDC(dcSrc); +#endif // !__WXMICROWIN__ return ret; -#else - return wxBitmap(); -#endif } // ---------------------------------------------------------------------------- @@ -872,6 +1073,8 @@ void wxBitmap::SetOk(bool isOk) } #endif // WXWIN_COMPATIBILITY_2 +#if wxUSE_PALETTE + void wxBitmap::SetPalette(const wxPalette& palette) { EnsureHasData(); @@ -879,6 +1082,8 @@ void wxBitmap::SetPalette(const wxPalette& palette) GetBitmapData()->m_bitmapPalette = palette; } +#endif // wxUSE_PALETTE + void wxBitmap::SetMask(wxMask *mask) { EnsureHasData(); @@ -893,7 +1098,7 @@ void wxBitmap::SetMask(wxMask *mask) wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const { #ifdef __WXMICROWIN__ - return wxBitmap(); + return *this; #else wxMemoryDC memDC; wxBitmap tmpBitmap(GetWidth(), GetHeight(), dc.GetDepth()); @@ -901,6 +1106,7 @@ wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const LPBITMAPINFO lpDib; void *lpBits = (void*) NULL; +#if wxUSE_PALETTE if( GetPalette() && GetPalette()->Ok() ) { tmpBitmap.SetPalette(*GetPalette()); @@ -917,6 +1123,9 @@ wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const memDC.SelectObject(tmpBitmap); memDC.SetPalette( palette ); } +#else // !wxUSE_PALETTE + hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE); +#endif // wxUSE_PALETTE/!wxUSE_PALETTE // set the height negative because in a DIB the order of the lines is // reversed @@ -1022,6 +1231,8 @@ bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) ::DeleteObject((HBITMAP) m_maskBitmap); m_maskBitmap = 0; } + +#if wxUSE_PALETTE if (bitmap.Ok() && bitmap.GetPalette()->Ok()) { unsigned char red, green, blue; @@ -1031,6 +1242,8 @@ bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) return Create(bitmap, transparentColour); } } +#endif // wxUSE_PALETTE + return FALSE; } @@ -1052,7 +1265,7 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) // scan the bitmap for the transparent colour and set the corresponding // pixels in the mask to BLACK and the rest to WHITE - COLORREF maskColour = wxColourToRGB(colour); + COLORREF maskColour = RGB(colour.Red(), colour.Green(), colour.Blue()); m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0); HDC srcDC = ::CreateCompatibleDC(NULL); @@ -1084,32 +1297,13 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) ok = FALSE; } - // this is not very efficient, but I can't think of a better way of doing - // it - for ( int w = 0; ok && (w < width); w++ ) + if ( ok ) { - for ( int h = 0; ok && (h < height); h++ ) - { - COLORREF col = GetPixel(srcDC, w, h); - if ( col == CLR_INVALID ) - { - wxLogLastError(wxT("GetPixel")); - - // doesn't make sense to continue - ok = FALSE; - - break; - } - - if ( col == maskColour ) - { - ::SetPixel(destDC, w, h, RGB(0, 0, 0)); - } - else - { - ::SetPixel(destDC, w, h, RGB(255, 255, 255)); - } - } + // this will create a monochrome bitmap with 0 points for the pixels + // which have the same value as the background colour and 1 for the + // others + ::SetBkColor(srcDC, maskColour); + ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY); } ::SelectObject(srcDC, hbmpSrcOld); @@ -1118,9 +1312,9 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) ::DeleteDC(destDC); return ok; -#else +#else // __WXMICROWIN__ return FALSE; -#endif +#endif // __WXMICROWIN__/!__WXMICROWIN__ } // ----------------------------------------------------------------------------