X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a3bf7524f394af039efe196a186f7969cbabcc19..adb799d6ef8e1ba754ab08b26e64fa40219f95f8:/src/msw/bitmap.cpp diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index 0e5ab48c9f..f0c834bb24 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -141,32 +141,44 @@ IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject) // ---------------------------------------------------------------------------- // decide whether we should create a DIB or a DDB for the given parameters -static bool wxShouldCreateDIB(int w, int h, int d, WXHDC hdc) -{ -#if wxUSE_WXDIB - // here is the logic: - // - // (a) if hdc is specified, the caller explicitly wants DDB - // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp or - // less DIBs anyhow) - // (c) finally, create DIBs under Win9x even if the depth hasn't been - // explicitly specified but the current display depth is 24 or more - // and the image is "big", i.e. > 16Mb which is the theoretical limit - // for DDBs under Win9x - // - // consequences (all of which seem to make sense): - // - // (i) by default, DDBs are created (depth == -1 usually) - // (ii) DIBs can be created by explicitly specifying the depth - // (iii) using a DC always forces creating a DDB - return !hdc && - (d >= 24 || - (d == -1 && - wxDIB::GetLineSize(w, wxDisplayDepth())*h > 16*1024*1024)); -#else - return FALSE; -#endif -} +// +// NB: we always use DIBs under Windows CE as this is much simpler (even if +// also less efficient...) and we obviously can't use them if there is no +// DIB support compiled in at all +#ifdef __WXWINCE__ + static inline bool wxShouldCreateDIB(int, int, int, WXHDC) { return true; } + + #define ALWAYS_USE_DIB +#elif !wxUSE_WXDIB + // no sense in defining wxShouldCreateDIB() as we can't compile code + // executed if it is true, so we have to use #if's anyhow + #define NEVER_USE_DIB +#else // wxUSE_WXDIB && !__WXWINCE__ + static inline bool wxShouldCreateDIB(int w, int h, int d, WXHDC hdc) + { + // here is the logic: + // + // (a) if hdc is specified, the caller explicitly wants DDB + // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp + // or less DIBs anyhow) + // (c) finally, create DIBs under Win9x even if the depth hasn't been + // explicitly specified but the current display depth is 24 or + // more and the image is "big", i.e. > 16Mb which is the + // theoretical limit for DDBs under Win9x + // + // consequences (all of which seem to make sense): + // + // (i) by default, DDBs are created (depth == -1 usually) + // (ii) DIBs can be created by explicitly specifying the depth + // (iii) using a DC always forces creating a DDB + return !hdc && + (d >= 24 || + (d == -1 && + wxDIB::GetLineSize(w, wxDisplayDepth())*h > 16*1024*1024)); + } + + #define SOMETIMES_USE_DIB +#endif // different DIB usage scenarious // ---------------------------------------------------------------------------- // wxBitmapRefData @@ -275,13 +287,7 @@ bool wxBitmap::CopyFromCursor(const wxCursor& cursor) if ( !cursor.Ok() ) return FALSE; -#ifdef __WIN16__ - wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") ); - - return FALSE; -#else return CopyFromIconOrCursor(cursor); -#endif // Win16 } bool wxBitmap::CopyFromIcon(const wxIcon& icon) @@ -291,47 +297,22 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon) if ( !icon.Ok() ) return FALSE; - // GetIconInfo() doesn't exist under Win16 and I don't know any other way - // to create a bitmap from icon there - but using this way we won't have - // the mask (FIXME) -#ifdef __WIN16__ - int width = icon.GetWidth(), - height = icon.GetHeight(); - - // copy the icon to the bitmap - ScreenHDC hdcScreen; - HDC hdc = ::CreateCompatibleDC(hdcScreen); - HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height); - HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap); - - ::DrawIcon(hdc, 0, 0, GetHiconOf(icon)); - - ::SelectObject(hdc, hbmpOld); - ::DeleteDC(hdc); - - wxBitmapRefData *refData = new wxBitmapRefData; - m_refData = refData; - - refData->m_width = width; - refData->m_height = height; - refData->m_depth = wxDisplayDepth(); - - refData->m_hBitmap = (WXHBITMAP)hbitmap; - - return TRUE; -#else // Win32 return CopyFromIconOrCursor(icon); -#endif // Win16/Win32 } -#if wxUSE_WXDIB +#ifndef NEVER_USE_DIB + bool wxBitmap::CopyFromDIB(const wxDIB& dib) { wxCHECK_MSG( dib.IsOk(), FALSE, _T("invalid DIB in CopyFromDIB") ); +#ifdef SOMETIMES_USE_DIB HBITMAP hbitmap = dib.CreateDDB(); if ( !hbitmap ) return FALSE; +#else // ALWAYS_USE_DIB + HBITMAP hbitmap = ((wxDIB &)dib).Detach(); // const_cast +#endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB UnRef(); @@ -356,7 +337,8 @@ bool wxBitmap::CopyFromDIB(const wxDIB& dib) return TRUE; } -#endif + +#endif // NEVER_USE_DIB wxBitmap::~wxBitmap() { @@ -429,7 +411,11 @@ wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) } // Create from XPM data +#if wxUSE_IMAGE && wxUSE_XPM bool wxBitmap::CreateFromXpm(const char **data) +#else +bool wxBitmap::CreateFromXpm(const char **WXUNUSED(data)) +#endif { #if wxUSE_IMAGE && wxUSE_XPM Init(); @@ -496,9 +482,9 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc) GetBitmapData()->m_width = w; GetBitmapData()->m_height = h; - HBITMAP hbmp; + HBITMAP hbmp wxDUMMY_INITIALIZE(0); -#if wxUSE_WXDIB +#ifndef NEVER_USE_DIB if ( wxShouldCreateDIB(w, h, d, hdc) ) { if ( d == -1 ) @@ -518,13 +504,9 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc) GetBitmapData()->m_depth = d; } else // create a DDB -#endif +#endif // NEVER_USE_DIB { - if ( d == -1 ) - d = wxDisplayDepth(); - - GetBitmapData()->m_depth = d; - +#ifndef ALWAYS_USE_DIB #ifndef __WXMICROWIN__ if ( d > 0 ) { @@ -533,8 +515,10 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc) { wxLogLastError(wxT("CreateBitmap")); } + + GetBitmapData()->m_depth = d; } - else + else // d == 0, create bitmap compatible with the screen #endif // !__WXMICROWIN__ { ScreenHDC dc; @@ -546,6 +530,7 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc) GetBitmapData()->m_depth = wxDisplayDepth(); } +#endif // !ALWAYS_USE_DIB } SetHBITMAP((WXHBITMAP)hbmp); @@ -746,6 +731,8 @@ wxImage wxBitmap::ConvertToImage() const // wxImage to/from conversions // ---------------------------------------------------------------------------- +#if wxUSE_WXDIB + bool wxBitmap::CreateFromImage(const wxImage& image, int depth) { return CreateFromImage(image, depth, 0); @@ -759,9 +746,8 @@ bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc) return CreateFromImage(image, -1, dc.GetHDC()); } -bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc ) +bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc) { -#if wxUSE_WXDIB wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") ); UnRef(); @@ -785,7 +771,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc ) // next either store DIB as is or create a DDB from it - HBITMAP hbitmap; + HBITMAP hbitmap wxDUMMY_INITIALIZE(0); // are we going to use DIB? // @@ -798,12 +784,14 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc ) refData->m_isDIB = TRUE; refData->m_depth = dib.GetDepth(); } +#ifndef ALWAYS_USE_DIB else // we need to convert DIB to DDB { hbitmap = dib.CreateDDB((HDC)hdc); - refData->m_depth = depth == -1 ? wxDisplayDepth() : depth; + refData->m_depth = depth == -1 ? dib.GetDepth() : depth; } +#endif // !ALWAYS_USE_DIB // validate this object SetHBITMAP((WXHBITMAP)hbitmap); @@ -817,306 +805,96 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc ) } return TRUE; -#else - // FIXME: wxWinCE doesn't support wxDIB yet - return FALSE; -#endif } wxImage wxBitmap::ConvertToImage() const { - // FIXME: this is untested code for WinCE, and - // the mask is not yet handled. - // For tips, see: - // http://www.codeproject.com/bitmap/dibsection.asp?print=true - -#ifdef __WXWINCE__ - // the colour used as transparent one in wxImage and the one it is replaced - // with when it really occurs in the bitmap - static const int MASK_RED = 1; - static const int MASK_GREEN = 2; - static const int MASK_BLUE = 3; - static const int MASK_BLUE_REPLACEMENT = 2; - - wxImage image; + wxDIB dib(*this); - 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; - } - - // calc the number of bytes per scanline and padding in the DIB - int bytePerLine = width*3; - int sizeDWORD = sizeof( DWORD ); - int lineBoundary = bytePerLine % sizeDWORD; - int padding = 0; - if( lineBoundary > 0 ) - { - padding = sizeDWORD - lineBoundary; - bytePerLine += padding; - } - - // create a DIB header - int headersize = sizeof(BITMAPINFOHEADER); - BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize ); - if( !lpDIBh ) - { - wxFAIL_MSG( wxT("could not allocate data for DIB header") ); - free( data ); - return wxNullImage; - } - // Fill in the DIB header - lpDIBh->bmiHeader.biSize = headersize; - lpDIBh->bmiHeader.biWidth = width; - lpDIBh->bmiHeader.biHeight = -height; - lpDIBh->bmiHeader.biSizeImage = bytePerLine * height; - lpDIBh->bmiHeader.biPlanes = 1; - lpDIBh->bmiHeader.biBitCount = 24; - lpDIBh->bmiHeader.biCompression = BI_RGB; - lpDIBh->bmiHeader.biClrUsed = 0; - // These seem not really needed for our purpose here. - lpDIBh->bmiHeader.biClrImportant = 0; - lpDIBh->bmiHeader.biXPelsPerMeter = 0; - lpDIBh->bmiHeader.biYPelsPerMeter = 0; - - // memory for DIB data is allocated by CreateDIBSection - void *lpBits = NULL; - - // copy data from the device-dependent bitmap to the DIB - HDC hdc = ::GetDC(NULL); - HBITMAP hBitmap = (HBITMAP) GetHBITMAP(); - - HBITMAP hBitmapSection = ::CreateDIBSection( hdc, lpDIBh, DIB_RGB_COLORS, & lpBits, NULL, 0 ); - if (!hBitmapSection) + if ( !dib.IsOk() ) { - wxFAIL_MSG( wxT("could not create a DIB section") ); return wxNullImage; } - // Copy the image from the DDB to the DIBSection - // Need to copy the supplied bitmap onto the newly created DIBsection - HDC hMemDC = CreateCompatibleDC(hdc); - HDC hCopyDC = CreateCompatibleDC(hdc); - - if (! hMemDC || ! hCopyDC) + wxImage image = dib.ConvertToImage(); + if ( !image.Ok() ) { - wxFAIL_MSG( wxT("unable to create compatible DCs") ); return wxNullImage; } -#if 0 - if (m_hPal) - { - SelectPalette(hMemDC, m_hPal, FALSE); RealizePalette(hMemDC); - SelectPalette(hCopyDC, m_hPal, FALSE); RealizePalette(hCopyDC); - } -#endif - - HBITMAP hOldMemBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap); - HBITMAP hOldCopyBitmap = (HBITMAP) SelectObject(hCopyDC, hBitmapSection); - - BitBlt(hCopyDC, 0, 0, GetWidth(), GetHeight(), hMemDC, 0, 0, SRCCOPY); + // set mask - SelectObject(hMemDC, hOldMemBitmap); - SelectObject(hCopyDC, hOldCopyBitmap); - DeleteDC(hMemDC); - DeleteDC(hCopyDC); + // TODO: WinCE mask-copying support and use wxDIB +#ifndef __WXWINCE__ -#if 0 - if (m_hPal) + if( GetMask() && GetMask()->GetMaskBitmap() ) { - HGDIOBJ hObj = ::GetStockObject(DEFAULT_PALETTE); - SelectObject(hMemDC, hObj); - SelectObject(hCopyDC, hObj); - } -#endif + static const int MASK_RED = 1; + static const int MASK_GREEN = 2; + static const int MASK_BLUE = 3; + static const int MASK_BLUE_REPLACEMENT = 2; - ReleaseDC(NULL, hdc); + HBITMAP hbitmap = (HBITMAP) GetMask()->GetMaskBitmap(); + int width = GetWidth(); + int height = GetHeight(); - // copy DIB data into the wxImage object - int i, j; - unsigned char *ptdata = data; - unsigned char *ptbits = (unsigned char*) lpBits; - for( i=0; i 0 ) { - *(ptdata++) = *(ptbits+2); - *(ptdata++) = *(ptbits+1); - *(ptdata++) = *(ptbits ); - ptbits += 3; + padding = sizeDWORD - lineBoundary; + bytesPerLine += padding; } - ptbits += padding; - } - // TODO -#if 0 - // similarly, set data according to the possible mask bitmap - if( GetMask() && GetMask()->GetMaskBitmap() ) - { - hbitmap = (HBITMAP) GetMask()->GetMaskBitmap(); - // memory DC created, color set, data copied, and memory DC deleted - HDC memdc = ::CreateCompatibleDC( hdc ); - ::SetTextColor( memdc, RGB( 0, 0, 0 ) ); - ::SetBkColor( memdc, RGB( 255, 255, 255 ) ); - ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS ); - ::DeleteDC( memdc ); - ptdata = data; - ptbits = lpBits; - for( i=0; ibmiHeader.biSize = headersize; + lpDIBh->bmiHeader.biWidth = width; + lpDIBh->bmiHeader.biHeight = -height; + lpDIBh->bmiHeader.biSizeImage = bytesPerLine * height; + lpDIBh->bmiHeader.biPlanes = 1; + lpDIBh->bmiHeader.biBitCount = 24; + lpDIBh->bmiHeader.biCompression = BI_RGB; + lpDIBh->bmiHeader.biClrUsed = 0; + // These seem not really needed for our purpose here. + lpDIBh->bmiHeader.biClrImportant = 0; + lpDIBh->bmiHeader.biXPelsPerMeter = 0; + lpDIBh->bmiHeader.biYPelsPerMeter = 0; - 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; - } - - // calc the number of bytes per scanline and padding in the DIB - int bytePerLine = width*3; - int sizeDWORD = sizeof( DWORD ); - int lineBoundary = bytePerLine % sizeDWORD; - int padding = 0; - if( lineBoundary > 0 ) - { - padding = sizeDWORD - lineBoundary; - bytePerLine += padding; - } - - // create a DIB header - int headersize = sizeof(BITMAPINFOHEADER); - BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize ); - if( !lpDIBh ) - { - wxFAIL_MSG( wxT("could not allocate data for DIB header") ); - free( data ); - return wxNullImage; - } - // Fill in the DIB header - lpDIBh->bmiHeader.biSize = headersize; - lpDIBh->bmiHeader.biWidth = width; - lpDIBh->bmiHeader.biHeight = -height; - lpDIBh->bmiHeader.biSizeImage = bytePerLine * height; - lpDIBh->bmiHeader.biPlanes = 1; - lpDIBh->bmiHeader.biBitCount = 24; - lpDIBh->bmiHeader.biCompression = BI_RGB; - lpDIBh->bmiHeader.biClrUsed = 0; - // These seem not really needed for our purpose here. - lpDIBh->bmiHeader.biClrImportant = 0; - lpDIBh->bmiHeader.biXPelsPerMeter = 0; - lpDIBh->bmiHeader.biYPelsPerMeter = 0; - // memory for DIB data - unsigned char *lpBits; - lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage ); - if( !lpBits ) - { - wxFAIL_MSG( wxT("could not allocate data for DIB") ); - free( data ); - free( lpDIBh ); - return wxNullImage; - } - - // copy data from the device-dependent bitmap to the DIB - HDC hdc = ::GetDC(NULL); - HBITMAP hbitmap; - hbitmap = (HBITMAP) GetHBITMAP(); - ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS ); + // memory DC created, color set, data copied, and memory DC deleted - // copy DIB data into the wxImage object - int i, j; - unsigned char *ptdata = data; - unsigned char *ptbits = lpBits; - for( i=0; ibmiHeader.biSizeImage ); + if( !lpBits ) { - *(ptdata++) = *(ptbits+2); - *(ptdata++) = *(ptbits+1); - *(ptdata++) = *(ptbits ); - ptbits += 3; + wxFAIL_MSG( wxT("could not allocate data for DIB") ); + free( lpDIBh ); + return wxNullImage; } - ptbits += padding; - } - // similarly, set data according to the possible mask bitmap - if( GetMask() && GetMask()->GetMaskBitmap() ) - { - hbitmap = (HBITMAP) GetMask()->GetMaskBitmap(); - // memory DC created, color set, data copied, and memory DC deleted + + HDC hdc = ::GetDC(NULL); + HDC memdc = ::CreateCompatibleDC( hdc ); ::SetTextColor( memdc, RGB( 0, 0, 0 ) ); ::SetBkColor( memdc, RGB( 255, 255, 255 ) ); ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS ); ::DeleteDC( memdc ); - ptdata = data; - ptbits = lpBits; + unsigned char *ptdata = image.GetData();//data; + unsigned char *ptbits = lpBits; + int i, j; for( i=0; iLoadFile(this, filename, type, -1, -1); } #if wxUSE_IMAGE - else + else // no bitmap handler found { wxImage image; if ( image.LoadFile( filename, type ) && image.Ok() ) @@ -1224,7 +1001,7 @@ bool wxBitmap::SaveFile(const wxString& filename, return handler->SaveFile(this, filename, type, palette); } #if wxUSE_IMAGE - else + else // no bitmap handler found { // FIXME what about palette? shouldn't we use it? wxImage image = ConvertToImage(); @@ -1510,20 +1287,20 @@ void wxBitmap::UngetRawData(wxPixelDataBase& dataBase) p = rowStart; p.OffsetY(data, 1); } + } - // if we're a DDB we need to convert DIB back to DDB now to make the - // changes made via raw bitmap access effective - if ( !GetBitmapData()->m_isDIB ) - { - wxDIB *dib = GetBitmapData()->m_dib; - GetBitmapData()->m_dib = NULL; + // if we're a DDB we need to convert DIB back to DDB now to make the + // changes made via raw bitmap access effective + if ( !GetBitmapData()->m_isDIB ) + { + wxDIB *dib = GetBitmapData()->m_dib; + GetBitmapData()->m_dib = NULL; - // TODO: convert + // TODO: convert - delete dib; - } + delete dib; } -#endif +#endif // wxUSE_WXDIB } #endif // #ifdef wxHAVE_RAW_BITMAP @@ -1762,14 +1539,11 @@ bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel, HPALETTE hPal, LPBITMAPINFO* lpDIBHeader) { unsigned long i, headerSize; - LPBITMAPINFO lpDIBheader = NULL; - LPPALETTEENTRY lpPe = NULL; - // Allocate space for a DIB header headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY))); - lpDIBheader = (BITMAPINFO *) malloc(headerSize); - lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER)); + LPBITMAPINFO lpDIBheader = (BITMAPINFO *) malloc(headerSize); + LPPALETTEENTRY lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER)); GetPaletteEntries(hPal, 0, 256, lpPe); @@ -1910,8 +1684,8 @@ HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h) wxLogLastError(wxT("CreateBitmap")); } - ::SelectObject(hdcSrc, hbmpMask); - ::SelectObject(hdcDst, hbmpInvMask); + HGDIOBJ srcTmp = ::SelectObject(hdcSrc, hbmpMask); + HGDIOBJ dstTmp = ::SelectObject(hdcDst, hbmpInvMask); if ( !::BitBlt(hdcDst, 0, 0, w, h, hdcSrc, 0, 0, NOTSRCCOPY) ) @@ -1919,6 +1693,10 @@ HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h) wxLogLastError(wxT("BitBlt")); } + // Deselect objects + SelectObject(hdcSrc,srcTmp); + SelectObject(hdcDst,dstTmp); + ::DeleteDC(hdcSrc); ::DeleteDC(hdcDst);