From: Vadim Zeitlin Date: Mon, 24 Mar 2003 22:58:50 +0000 (+0000) Subject: wxDIB::Create(wxBitmap) shouldn't do any conversions if the bitmap is already a DIB... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/82ac3b0a423b755c6329c2ae2faaab9576a3f647 wxDIB::Create(wxBitmap) shouldn't do any conversions if the bitmap is already a DIB section git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@19785 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/msw/dib.h b/include/wx/msw/dib.h index f4f0bf0a86..6a2a40a020 100644 --- a/include/wx/msw/dib.h +++ b/include/wx/msw/dib.h @@ -151,30 +151,10 @@ public: private: // common part of all ctors - void Init() - { - m_handle = 0; - - m_data = NULL; - - m_width = - m_height = - m_depth = 0; - } + void Init(); // free resources - void Free() - { - if ( m_handle ) - { - if ( !::DeleteObject(m_handle) ) - { - wxLogLastError(wxT("DeleteObject(hDIB)")); - } - - Init(); - } - } + void Free(); // the DIB section handle, 0 if invalid HBITMAP m_handle; @@ -198,6 +178,11 @@ private: m_height, m_depth; + // in some cases we could be using a handle which we didn't create and in + // this case we shouldn't free it neither -- this flag tell us if this is + // the case + bool m_ownsHandle; + // DIBs can't be copied wxDIB(const wxDIB&); @@ -208,6 +193,33 @@ private: // inline functions implementation // ---------------------------------------------------------------------------- +inline +void wxDIB::Init() +{ + m_handle = 0; + m_ownsHandle = true; + + m_data = NULL; + + m_width = + m_height = + m_depth = 0; +} + +inline +void wxDIB::Free() +{ + if ( m_handle && m_ownsHandle ) + { + if ( !::DeleteObject(m_handle) ) + { + wxLogLastError(wxT("DeleteObject(hDIB)")); + } + + Init(); + } +} + inline wxDIB::~wxDIB() { Free(); diff --git a/src/msw/dib.cpp b/src/msw/dib.cpp index a3e9b49a45..0adbe93ad4 100644 --- a/src/msw/dib.cpp +++ b/src/msw/dib.cpp @@ -132,26 +132,48 @@ bool wxDIB::Create(const wxBitmap& bmp) { wxCHECK_MSG( bmp.Ok(), false, _T("wxDIB::Create(): invalid bitmap") ); - const int w = bmp.GetWidth(); - const int h = bmp.GetHeight(); - int d = bmp.GetDepth(); - if ( d == -1 ) - d = wxDisplayDepth(); + // this bitmap could already be a DIB section in which case we don't need + // to convert it to DIB + HBITMAP hbmp = GetHbitmapOf(bmp); - if ( !Create(w, h, d) ) - return false; + DIBSECTION ds; + if ( ::GetObject(hbmp, sizeof(ds), &ds) == sizeof(ds) ) + { + m_handle = hbmp; - // we could have used GetDIBits() too but GetBitmapBits() is simpler - if ( !::GetBitmapBits - ( - GetHbitmapOf(bmp), // the source DDB - GetLineSize(w, d)*h, // the number of bytes to copy - m_data // the pixels will be copied here - ) ) + // wxBitmap will free it, not we + m_ownsHandle = false; + + // copy all the bitmap parameters too as we have them now anyhow + m_width = ds.dsBm.bmWidth; + m_height = ds.dsBm.bmHeight; + m_depth = ds.dsBm.bmBitsPixel; + + m_data = ds.dsBm.bmBits; + } + else // no, it's a DDB -- convert it to DIB { - wxLogLastError(wxT("GetDIBits()")); + const int w = bmp.GetWidth(); + const int h = bmp.GetHeight(); + int d = bmp.GetDepth(); + if ( d == -1 ) + d = wxDisplayDepth(); + + if ( !Create(w, h, d) ) + return false; + + // we could have used GetDIBits() too but GetBitmapBits() is simpler + if ( !::GetBitmapBits + ( + GetHbitmapOf(bmp), // the source DDB + GetLineSize(w, d)*h, // the number of bytes to copy + m_data // the pixels will be copied here + ) ) + { + wxLogLastError(wxT("GetDIBits()")); - return 0; + return 0; + } } return true;