X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/6faac21b0bf815c01b1d98cf3ea3310bb5238f6f..84bdb0d8d7d7fe3d7cd6a2e5f50ee247ce2af5df:/src/msw/bitmap.cpp?ds=sidebyside diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index b174fdf9de..3df871c59f 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -67,6 +67,7 @@ class WXDLLEXPORT wxBitmapRefData : public wxGDIImageRefData { public: wxBitmapRefData(); + wxBitmapRefData(const wxBitmapRefData& data); virtual ~wxBitmapRefData() { Free(); } virtual void Free(); @@ -120,7 +121,9 @@ private: // optional mask for transparent drawing wxMask *m_bitmapMask; - DECLARE_NO_COPY_CLASS(wxBitmapRefData) + + // not implemented + wxBitmapRefData& operator=(const wxBitmapRefData&); }; // ---------------------------------------------------------------------------- @@ -197,7 +200,24 @@ wxBitmapRefData::wxBitmapRefData() #endif m_isDIB = - m_hasAlpha = FALSE; + m_hasAlpha = false; +} + +wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData& data) + : wxGDIImageRefData(data) +{ +#ifdef __WXDEBUG__ + m_selectedInto = NULL; +#endif + + // can't copy the mask as the other bitmap destroys it + m_bitmapMask = NULL; + + wxASSERT_MSG( !data.m_isDIB, + _T("can't copy bitmap locked for raw access!") ); + m_isDIB = false; + + m_hasAlpha = data.m_hasAlpha; } void wxBitmapRefData::Free() @@ -236,6 +256,32 @@ wxGDIImageRefData *wxBitmap::CreateData() const return new wxBitmapRefData; } +wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *dataOrig) const +{ + const wxBitmapRefData * + data = wx_static_cast(const wxBitmapRefData *, dataOrig); + if ( !data ) + return NULL; + + wxBitmap *self = wx_const_cast(wxBitmap *, this); + +#if wxUSE_WXDIB + // copy the other bitmap + if ( data->m_hBitmap ) + { + wxDIB dib((HBITMAP)(data->m_hBitmap)); + self->CopyFromDIB(dib); + } + else +#endif // wxUSE_WXDIB + { + // don't copy the bitmap data, but do copy the size, depth, ... + self->m_refData = new wxBitmapRefData(*data); + } + + return m_refData; +} + #ifdef __WIN32__ bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon) @@ -286,7 +332,7 @@ bool wxBitmap::CopyFromCursor(const wxCursor& cursor) UnRef(); if ( !cursor.Ok() ) - return FALSE; + return false; return CopyFromIconOrCursor(cursor); } @@ -296,7 +342,7 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon) UnRef(); if ( !icon.Ok() ) - return FALSE; + return false; return CopyFromIconOrCursor(icon); } @@ -305,12 +351,12 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon) bool wxBitmap::CopyFromDIB(const wxDIB& dib) { - wxCHECK_MSG( dib.IsOk(), FALSE, _T("invalid DIB in CopyFromDIB") ); + wxCHECK_MSG( dib.IsOk(), false, _T("invalid DIB in CopyFromDIB") ); #ifdef SOMETIMES_USE_DIB HBITMAP hbitmap = dib.CreateDDB(); if ( !hbitmap ) - return FALSE; + return false; #else // ALWAYS_USE_DIB HBITMAP hbitmap = ((wxDIB &)dib).Detach(); // const_cast #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB @@ -336,7 +382,7 @@ bool wxBitmap::CopyFromDIB(const wxDIB& dib) delete palette; #endif // wxUSE_PALETTE - return TRUE; + return true; } #endif // NEVER_USE_DIB @@ -380,7 +426,7 @@ wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) for ( int bits = 0; bits < 8; bits++) { reversed <<= 1; - reversed |= (val & 0x01); + reversed |= (unsigned char)(val & 0x01); val >>= 1; } *dst++ = reversed; @@ -412,25 +458,22 @@ 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(); - wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") ) + 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") ) + wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") ) *this = wxBitmap(img); - return TRUE; + return true; #else - return FALSE; + wxUnusedVar(data); + return false; #endif } @@ -469,7 +512,7 @@ bool wxBitmap::Create(int width, int height, int depth) bool wxBitmap::Create(int width, int height, const wxDC& dc) { - wxCHECK_MSG( dc.Ok(), FALSE, _T("invalid HDC in wxBitmap::Create()") ); + wxCHECK_MSG( dc.Ok(), false, _T("invalid HDC in wxBitmap::Create()") ); return DoCreate(width, height, -1, dc.GetHDC()); } @@ -496,12 +539,12 @@ bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc) wxDIB dib(w, h, d); if ( !dib.IsOk() ) - return FALSE; + return false; // don't delete the DIB section in dib object dtor hbmp = dib.Detach(); - GetBitmapData()->m_isDIB = TRUE; + GetBitmapData()->m_isDIB = true; GetBitmapData()->m_depth = d; } else // create a DDB @@ -576,7 +619,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc) if (hBitmap == NULL) { ::ReleaseDC(NULL, hScreenDC); - return FALSE; + return false; } HDC hMemDC = ::CreateCompatibleDC(hScreenDC); @@ -602,7 +645,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc) if (!hMaskBitmap) { - hasMask = FALSE; + hasMask = false; } else { @@ -613,7 +656,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc) hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap); } #else - hasMask = FALSE; + hasMask = false; #endif } @@ -660,7 +703,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc) SetPalette(image.GetPalette()); #endif // wxUSE_PALETTE - return TRUE; + return true; } wxImage wxBitmap::ConvertToImage() const @@ -741,7 +784,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth) bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc) { - wxCHECK_MSG( dc.Ok(), FALSE, + wxCHECK_MSG( dc.Ok(), false, _T("invalid HDC in wxBitmap::CreateFromImage()") ); return CreateFromImage(image, -1, dc.GetHDC()); @@ -749,7 +792,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc) bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc) { - wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") ); + wxCHECK_MSG( image.Ok(), false, wxT("invalid image") ); UnRef(); @@ -759,7 +802,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc) wxDIB dib(image); if ( !dib.IsOk() ) - return FALSE; + return false; // store the bitmap parameters @@ -782,7 +825,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc) // don't delete the DIB section in dib object dtor hbitmap = dib.Detach(); - refData->m_isDIB = TRUE; + refData->m_isDIB = true; refData->m_depth = dib.GetDepth(); } #ifndef ALWAYS_USE_DIB @@ -800,12 +843,45 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc) // finally also set the mask if we have one if ( image.HasMask() ) { - SetMask(new wxMask(*this, wxColour(image.GetMaskRed(), - image.GetMaskGreen(), - image.GetMaskBlue()))); + const size_t len = 2*((w+15)/16); + BYTE *src = image.GetData(); + BYTE *data = new BYTE[h*len]; + memset(data, 0, h*len); + BYTE r = image.GetMaskRed(), + g = image.GetMaskGreen(), + b = image.GetMaskBlue(); + BYTE *dst = data; + for ( int y = 0; y < h; y++, dst += len ) + { + BYTE *dstLine = dst; + BYTE mask = 0x80; + for ( int x = 0; x < w; x++, src += 3 ) + { + if (src[0] != r || src[1] != g || src[2] != b) + *dstLine |= mask; + + if ( (mask >>= 1) == 0 ) + { + dstLine++; + mask = 0x80; + } + } + } + + hbitmap = ::CreateBitmap(w, h, 1, 1, data); + if ( !hbitmap ) + { + wxLogLastError(_T("CreateBitmap(mask)")); + } + else + { + SetMask(new wxMask((WXHBITMAP)hbitmap)); + } + + delete data; } - return TRUE; + return true; } wxImage wxBitmap::ConvertToImage() const @@ -919,12 +995,12 @@ bool wxBitmap::LoadFile(const wxString& filename, long type) { *this = wxBitmap(image); - return TRUE; + return true; } } #endif // wxUSE_IMAGE - return FALSE; + return false; } bool wxBitmap::Create(void *data, long type, int width, int height, int depth) @@ -937,7 +1013,7 @@ bool wxBitmap::Create(void *data, long type, int width, int height, int depth) { wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type); - return FALSE; + return false; } m_refData = new wxBitmapRefData; @@ -967,7 +1043,7 @@ bool wxBitmap::SaveFile(const wxString& filename, } #endif // wxUSE_IMAGE - return FALSE; + return false; } // ---------------------------------------------------------------------------- @@ -982,11 +1058,13 @@ wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const (rect.y+rect.height <= GetHeight()), wxNullBitmap, wxT("Invalid bitmap or bitmap region") ); - wxBitmap ret( rect.width, rect.height ); + wxBitmap ret( rect.width, rect.height, GetDepth() ); wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); #ifndef __WXMICROWIN__ - // TODO: copy alpha channel data if any + // handle alpha channel, if any + if (HasAlpha()) + ret.UseAlpha(); // copy bitmap data MemoryHDC dcSrc, @@ -1094,7 +1172,7 @@ void wxBitmap::SetSelectedInto(wxDC *dc) void wxBitmap::SetPalette(const wxPalette& palette) { - EnsureHasData(); + AllocExclusive(); GetBitmapData()->m_bitmapPalette = palette; } @@ -1103,7 +1181,7 @@ void wxBitmap::SetPalette(const wxPalette& palette) void wxBitmap::SetMask(wxMask *mask) { - EnsureHasData(); + AllocExclusive(); GetBitmapData()->SetMask(mask); } @@ -1180,7 +1258,11 @@ void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) data.m_height = h; // remember that DIBs are stored in top to bottom order! - const LONG bytesPerRow = ds.dsBm.bmWidthBytes; + // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a + // multiple of 2, as required by the documentation. So we use the official + // formula, which we already use elsewhere.) + const LONG bytesPerRow = + wxDIB::GetLineSize(ds.dsBm.bmWidth, ds.dsBm.bmBitsPixel); data.m_stride = -bytesPerRow; char *bits = (char *)ds.dsBm.bmBits; @@ -1201,12 +1283,7 @@ void wxBitmap::UngetRawData(wxPixelDataBase& dataBase) 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 ) + if ( !&dataBase ) { // invalid data, don't crash -- but don't assert neither as we're // called automatically from wxPixelDataBase dtor and so there is no @@ -1214,36 +1291,6 @@ void wxBitmap::UngetRawData(wxPixelDataBase& dataBase) 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 ) @@ -1301,7 +1348,7 @@ wxMask::~wxMask() bool wxMask::Create(const wxBitmap& bitmap) { #ifndef __WXMICROWIN__ - wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE, + wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, false, _T("can't create mask from invalid or not monochrome bitmap") ); if ( m_maskBitmap ) @@ -1324,9 +1371,10 @@ bool wxMask::Create(const wxBitmap& bitmap) DeleteDC(srcDC); SelectObject(destDC, 0); DeleteDC(destDC); - return TRUE; + return true; #else - return FALSE; + wxUnusedVar(bitmap); + return false; #endif } @@ -1352,7 +1400,7 @@ bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) } #endif // wxUSE_PALETTE - return FALSE; + return false; } // Create a mask from a bitmap and a colour indicating @@ -1360,7 +1408,7 @@ bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) { #ifndef __WXMICROWIN__ - wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") ); + wxCHECK_MSG( bitmap.Ok(), false, _T("invalid bitmap in wxMask::Create") ); if ( m_maskBitmap ) { @@ -1383,7 +1431,7 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) wxLogLastError(wxT("CreateCompatibleDC")); } - bool ok = TRUE; + bool ok = true; // SelectObject() will fail wxASSERT_MSG( !bitmap.GetSelectedInto(), @@ -1394,7 +1442,7 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) { wxLogLastError(wxT("SelectObject")); - ok = FALSE; + ok = false; } HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap); @@ -1402,7 +1450,7 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) { wxLogLastError(wxT("SelectObject")); - ok = FALSE; + ok = false; } if ( ok ) @@ -1421,7 +1469,9 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) return ok; #else // __WXMICROWIN__ - return FALSE; + wxUnusedVar(bitmap); + wxUnusedVar(colour); + return false; #endif // __WXMICROWIN__/!__WXMICROWIN__ } @@ -1436,7 +1486,7 @@ bool wxBitmapHandler::Create(wxGDIImage *image, { wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); - return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE; + return bitmap ? Create(bitmap, data, flags, width, height, depth) : false; } bool wxBitmapHandler::Load(wxGDIImage *image, @@ -1446,7 +1496,7 @@ bool wxBitmapHandler::Load(wxGDIImage *image, { wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); - return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE; + return bitmap ? LoadFile(bitmap, name, flags, width, height) : false; } bool wxBitmapHandler::Save(wxGDIImage *image, @@ -1455,7 +1505,7 @@ bool wxBitmapHandler::Save(wxGDIImage *image, { wxBitmap *bitmap = wxDynamicCast(image, wxBitmap); - return bitmap ? SaveFile(bitmap, name, type) : FALSE; + return bitmap ? SaveFile(bitmap, name, type) : false; } bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap), @@ -1465,7 +1515,7 @@ bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap), int WXUNUSED(height), int WXUNUSED(depth)) { - return FALSE; + return false; } bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap), @@ -1474,7 +1524,7 @@ bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap), int WXUNUSED(desiredWidth), int WXUNUSED(desiredHeight)) { - return FALSE; + return false; } bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap), @@ -1482,7 +1532,7 @@ bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap), int WXUNUSED(type), const wxPalette *WXUNUSED(palette)) { - return FALSE; + return false; } // ---------------------------------------------------------------------------- @@ -1527,7 +1577,7 @@ bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel, *lpDIBHeader = lpDIBheader; - return TRUE; + return true; } void wxFreeDIB(LPBITMAPINFO lpDIBHeader) @@ -1604,12 +1654,12 @@ HICON wxBitmapToIconOrCursor(const wxBitmap& bmp, HICON wxBitmapToHICON(const wxBitmap& bmp) { - return wxBitmapToIconOrCursor(bmp, TRUE, 0, 0); + return wxBitmapToIconOrCursor(bmp, true, 0, 0); } HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY) { - return (HCURSOR)wxBitmapToIconOrCursor(bmp, FALSE, hotSpotX, hotSpotY); + return (HCURSOR)wxBitmapToIconOrCursor(bmp, false, hotSpotX, hotSpotY); } HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h) @@ -1651,7 +1701,7 @@ HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h) // Deselect objects SelectObject(hdcSrc,srcTmp); SelectObject(hdcDst,dstTmp); - + ::DeleteDC(hdcSrc); ::DeleteDC(hdcDst);