X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f2bb16493d89e72ba4cfb0149c5ae1e40a068e82..e58989615283771efcaa9e798568772ef5fcfb0c:/src/msw/bitmap.cpp diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index 1e88064683..dbb6114bee 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -43,14 +43,16 @@ #include "wx/msw/private.h" #include "wx/log.h" -#if !defined(__WXMICROWIN__) +#if wxUSE_WXDIB #include "wx/msw/dib.h" #endif #include "wx/image.h" #include "wx/xpmdecod.h" +#ifdef wxHAVE_RAW_BITMAP #include "wx/rawbmp.h" +#endif // missing from mingw32 header #ifndef CLR_INVALID @@ -100,10 +102,20 @@ public: wxDC *m_selectedInto; #endif // __WXDEBUG__ +#if wxUSE_WXDIB + // when GetRawData() is called for a DDB we need to convert it to a DIB + // first to be able to provide direct access to it and we cache that DIB + // here and convert it back to DDB when UngetRawData() is called + wxDIB *m_dib; +#endif + // true if we have alpha transparency info and can be drawn using // AlphaBlend() bool m_hasAlpha; + // true if our HBITMAP is a DIB section, false if it is a DDB + bool m_isDIB; + private: // optional mask for transparent drawing wxMask *m_bitmapMask; @@ -131,6 +143,7 @@ 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 @@ -150,6 +163,9 @@ static bool wxShouldCreateDIB(int w, int h, int d, WXHDC hdc) (d >= 24 || (d == -1 && wxDIB::GetLineSize(w, wxDisplayDepth())*h > 16*1024*1024)); +#else + return FALSE; +#endif } // ---------------------------------------------------------------------------- @@ -162,7 +178,13 @@ wxBitmapRefData::wxBitmapRefData() m_selectedInto = NULL; #endif m_bitmapMask = NULL; + m_hBitmap = (WXHBITMAP) NULL; +#if wxUSE_WXDIB + m_dib = NULL; +#endif + + m_isDIB = m_hasAlpha = FALSE; } @@ -171,6 +193,10 @@ void wxBitmapRefData::Free() wxASSERT_MSG( !m_selectedInto, wxT("deleting bitmap still selected into wxMemoryDC") ); +#if wxUSE_WXDIB + wxASSERT_MSG( !m_dib, _T("forgot to call wxBitmap::UngetRawData()!") ); +#endif + if ( m_hBitmap) { if ( !::DeleteObject((HBITMAP)m_hBitmap) ) @@ -202,7 +228,7 @@ wxGDIImageRefData *wxBitmap::CreateData() const bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon) { -#ifndef __WXMICROWIN__ +#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__) // it may be either HICON or HCURSOR HICON hicon = (HICON)icon.GetHandle(); @@ -306,6 +332,7 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon) #endif // Win16/Win32 } +#if wxUSE_WXDIB bool wxBitmap::CopyFromDIB(const wxDIB& dib) { wxCHECK_MSG( dib.IsOk(), FALSE, _T("invalid DIB in CopyFromDIB") ); @@ -337,6 +364,7 @@ bool wxBitmap::CopyFromDIB(const wxDIB& dib) return TRUE; } +#endif wxBitmap::~wxBitmap() { @@ -434,6 +462,13 @@ wxBitmap::wxBitmap(int w, int h, int d) (void)Create(w, h, d); } +wxBitmap::wxBitmap(int w, int h, const wxDC& dc) +{ + Init(); + + (void)Create(w, h, dc); +} + wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth) { Init(); @@ -471,6 +506,7 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc) HBITMAP hbmp; +#if wxUSE_WXDIB if ( wxShouldCreateDIB(w, h, d, hdc) ) { if ( d == -1 ) @@ -486,10 +522,11 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc) // don't delete the DIB section in dib object dtor hbmp = dib.Detach(); + GetBitmapData()->m_isDIB = TRUE; GetBitmapData()->m_depth = d; - GetBitmapData()->m_hasAlpha = d == 32; // 32bpp DIBs have alpha channel } else // create a DDB +#endif { if ( d == -1 ) d = wxDisplayDepth(); @@ -741,6 +778,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc) bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc ) { +#if wxUSE_WXDIB wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") ); UnRef(); @@ -767,11 +805,14 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc ) HBITMAP hbitmap; // are we going to use DIB? - if ( wxShouldCreateDIB(w, h, depth, hdc) ) + // + // NB: DDBs don't support alpha so if we have alpha channel we must use DIB + if ( image.HasAlpha() || wxShouldCreateDIB(w, h, depth, hdc) ) { // don't delete the DIB section in dib object dtor hbitmap = dib.Detach(); + refData->m_isDIB = TRUE; refData->m_depth = dib.GetDepth(); } else // we need to convert DIB to DDB @@ -797,10 +838,213 @@ 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; + + 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) + { + 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) + { + 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); + + SelectObject(hMemDC, hOldMemBitmap); + SelectObject(hCopyDC, hOldCopyBitmap); + DeleteDC(hMemDC); + DeleteDC(hCopyDC); + +#if 0 + if (m_hPal) + { + HGDIOBJ hObj = ::GetStockObject(DEFAULT_PALETTE); + SelectObject(hMemDC, hObj); + SelectObject(hCopyDC, hObj); + } +#endif + + ReleaseDC(NULL, hdc); + + // copy DIB data into the wxImage object + int i, j; + unsigned char *ptdata = data; + unsigned char *ptbits = (unsigned char*) lpBits; + for( i=0; iGetMaskBitmap() ) + { + 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; im_bitmapPalette : (wxPalette *) NULL; } +#endif wxMask *wxBitmap::GetMask() const { @@ -1097,6 +1354,12 @@ int wxBitmap::GetQuality() const #endif // WXWIN_COMPATIBILITY_2_4 +void wxBitmap::UseAlpha() +{ + if ( GetBitmapData() ) + GetBitmapData()->m_hasAlpha = true; +} + bool wxBitmap::HasAlpha() const { return GetBitmapData() && GetBitmapData()->m_hasAlpha; @@ -1157,41 +1420,144 @@ void wxBitmap::SetQuality(int WXUNUSED(quality)) // raw bitmap access support // ---------------------------------------------------------------------------- -bool wxBitmap::GetRawData(wxRawBitmapData *data) +#ifdef wxHAVE_RAW_BITMAP +void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) { - wxCHECK_MSG( data, FALSE, _T("NULL pointer in wxBitmap::GetRawData") ); - +#if wxUSE_WXDIB if ( !Ok() ) { // no bitmap, no data (raw or otherwise) - return FALSE; + return NULL; + } + + // if we're already a DIB we can access our data directly, but if not we + // need to convert this DDB to a DIB section and use it for raw access and + // then convert it back + HBITMAP hDIB; + if ( !GetBitmapData()->m_isDIB ) + { + wxCHECK_MSG( !GetBitmapData()->m_dib, FALSE, + _T("GetRawData() may be called only once") ); + + wxDIB *dib = new wxDIB(*this); + if ( !dib->IsOk() ) + { + delete dib; + + return NULL; + } + + // we'll free it in UngetRawData() + GetBitmapData()->m_dib = dib; + + hDIB = dib->GetHandle(); + } + else // we're a DIB + { + hDIB = GetHbitmap(); } - // we only support raw access to the DIBs, so check if we have one DIBSECTION ds; - if ( !::GetObject(GetHbitmap(), sizeof(ds), &ds) ) + if ( ::GetObject(hDIB, sizeof(ds), &ds) != sizeof(DIBSECTION) ) { - return FALSE; + wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") ); + + return NULL; + } + + // check that the bitmap is in correct format + if ( ds.dsBm.bmBitsPixel != bpp ) + { + wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") ); + + return NULL; } - // ok, store the relevant info in wxRawBitmapData + // ok, store the relevant info in wxPixelDataBase const LONG h = ds.dsBm.bmHeight; - data->m_width = ds.dsBm.bmWidth; - data->m_height = h; - data->m_bypp = ds.dsBm.bmBitsPixel / 8; + data.m_width = ds.dsBm.bmWidth; + data.m_height = h; // remember that DIBs are stored in top to bottom order! const LONG bytesPerRow = ds.dsBm.bmWidthBytes; - data->m_stride = -bytesPerRow; - data->m_pixels = (unsigned char *)ds.dsBm.bmBits; + data.m_stride = -bytesPerRow; + + char *bits = (char *)ds.dsBm.bmBits; if ( h > 1 ) { - data->m_pixels += (h - 1)*bytesPerRow; + bits += (h - 1)*bytesPerRow; } - return TRUE; + return bits; +#else + return NULL; +#endif +} + +void wxBitmap::UngetRawData(wxPixelDataBase& dataBase) +{ +#if wxUSE_WXDIB + if ( !Ok() ) + return; + + // the cast is ugly but we can't do without it and without making this + // function template (and hence inline) unfortunately + typedef wxPixelData PixelData; + PixelData& data = (PixelData &)dataBase; + + if ( !data ) + { + // invalid data, don't crash -- but don't assert neither as we're + // called automatically from wxPixelDataBase dtor and so there is no + // way to prevent this from happening + return; + } + + if ( GetBitmapData()->m_hasAlpha ) + { + // AlphaBlend() wants to have premultiplied source alpha but + // wxRawBitmap API uses normal, not premultiplied, colours, so adjust + // them here now + PixelData::Iterator p(data); + + const int w = data.GetWidth(); + const int h = data.GetHeight(); + + for ( int y = 0; y < h; y++ ) + { + PixelData::Iterator rowStart = p; + + for ( int x = 0; x < w; x++ ) + { + const unsigned alpha = p.Alpha(); + + p.Red() = (p.Red() * alpha + 127) / 255; + p.Blue() = (p.Blue() * alpha + 127) / 255; + p.Green() = (p.Green() * alpha + 127) / 255; + + ++p; + } + + 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; + + // TODO: convert + + delete dib; + } + } +#endif } +#endif // #ifdef wxHAVE_RAW_BITMAP // ---------------------------------------------------------------------------- // wxMask