X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cc37ebe0908a89e62c400df586293df42310fe2c..876262caf1dd81b0e3d9a0c81c10879f6fb256e8:/src/msw/bitmap.cpp diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index 797f755a14..dd6423fff7 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -50,7 +50,9 @@ #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,18 @@ public: wxDC *m_selectedInto; #endif // __WXDEBUG__ + // 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; + // 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; @@ -162,7 +172,11 @@ wxBitmapRefData::wxBitmapRefData() m_selectedInto = NULL; #endif m_bitmapMask = NULL; + m_hBitmap = (WXHBITMAP) NULL; + m_dib = NULL; + + m_isDIB = m_hasAlpha = FALSE; } @@ -171,6 +185,8 @@ void wxBitmapRefData::Free() wxASSERT_MSG( !m_selectedInto, wxT("deleting bitmap still selected into wxMemoryDC") ); + wxASSERT_MSG( !m_dib, _T("forgot to call wxBitmap::UngetRawData()!") ); + if ( m_hBitmap) { if ( !::DeleteObject((HBITMAP)m_hBitmap) ) @@ -493,6 +509,7 @@ 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; } else // create a DDB @@ -773,11 +790,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 @@ -1074,11 +1094,13 @@ wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const // wxBitmap accessors // ---------------------------------------------------------------------------- +#if wxUSE_PALETTE wxPalette* wxBitmap::GetPalette() const { return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette : (wxPalette *) NULL; } +#endif wxMask *wxBitmap::GetMask() const { @@ -1169,72 +1191,138 @@ 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 ( !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) != sizeof(DIBSECTION) ) + if ( ::GetObject(hDIB, sizeof(ds), &ds) != sizeof(DIBSECTION) ) { - return FALSE; + wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") ); + + return NULL; } - // ok, store the relevant info in wxRawBitmapData + // 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 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; } -void wxBitmap::UngetRawData(wxRawBitmapData *data) +void wxBitmap::UngetRawData(wxPixelDataBase& dataBase) { - wxCHECK_RET( data, _T("NULL pointer in wxBitmap::UngetRawData()") ); + if ( !Ok() ) + return; - // AlphaBlend() wants to have premultiplied source alpha but wxRawBitmap - // API uses normal, not premultiplied, colours, so adjust them here now - wxRawBitmapIterator p(*data); + // 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; - const int w = data->GetWidth(); - const int h = data->GetHeight(); + 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; + } - for ( int y = 0; y < h; y++ ) + if ( GetBitmapData()->m_hasAlpha ) { - wxRawBitmapIterator rowStart = p; + // 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 x = 0; x < w; x++ ) + for ( int y = 0; y < h; y++ ) { - const unsigned alpha = p.Alpha(); + 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.Red() = (p.Red() * alpha) / 255; - p.Blue() = (p.Blue() * alpha) / 255; - p.Green() = (p.Green() * alpha) / 255; + ++p; + } - ++p; + p = rowStart; + p.OffsetY(data, 1); } - p = rowStart; - p.OffsetY(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 // #ifdef wxHAVE_RAW_BITMAP // ---------------------------------------------------------------------------- // wxMask