// headers
// ----------------------------------------------------------------------------
-#ifdef __GNUG__
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "bitmap.h"
#endif
#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
#define CLR_INVALID ((COLORREF)-1)
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;
// implementation
// ============================================================================
+// ----------------------------------------------------------------------------
+// helper functions
+// ----------------------------------------------------------------------------
+
+// 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
+}
+
// ----------------------------------------------------------------------------
// wxBitmapRefData
// ----------------------------------------------------------------------------
m_selectedInto = NULL;
#endif
m_bitmapMask = NULL;
+
m_hBitmap = (WXHBITMAP) NULL;
+#if wxUSE_WXDIB
+ m_dib = NULL;
+#endif
+
+ m_isDIB =
m_hasAlpha = FALSE;
}
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) )
void wxBitmap::Init()
{
// m_refData = NULL; done in the base class ctor
-
}
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();
// delete the old one now as we don't need it any more
::DeleteObject(iconInfo.hbmMask);
-#if WXWIN_COMPATIBILITY_2
- refData->m_ok = TRUE;
-#endif // WXWIN_COMPATIBILITY_2
-
return TRUE;
#else
return FALSE;
refData->m_hBitmap = (WXHBITMAP)hbitmap;
-#if WXWIN_COMPATIBILITY_2
- refData->m_ok = TRUE;
-#endif // WXWIN_COMPATIBILITY_2
-
return TRUE;
#else // Win32
return CopyFromIconOrCursor(icon);
#endif // Win16/Win32
}
+#if wxUSE_WXDIB
+bool wxBitmap::CopyFromDIB(const wxDIB& dib)
+{
+ wxCHECK_MSG( dib.IsOk(), FALSE, _T("invalid DIB in CopyFromDIB") );
+
+ HBITMAP hbitmap = dib.CreateDDB();
+ if ( !hbitmap )
+ return FALSE;
+
+ UnRef();
+
+ wxBitmapRefData *refData = new wxBitmapRefData;
+ m_refData = refData;
+
+ refData->m_width = dib.GetWidth();
+ refData->m_height = dib.GetHeight();
+ refData->m_depth = dib.GetDepth();
+
+ refData->m_hBitmap = (WXHBITMAP)hbitmap;
+
+#if wxUSE_PALETTE
+ wxPalette *palette = dib.CreatePalette();
+ if ( palette )
+ {
+ refData->m_bitmapPalette = *palette;
+ }
+
+ delete palette;
+#endif // wxUSE_PALETTE
+
+ return TRUE;
+}
+#endif
+
wxBitmap::~wxBitmap()
{
}
(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();
LoadFile(filename, (int)type);
}
-bool wxBitmap::Create(int w, int h, int d)
+bool wxBitmap::Create(int width, int height, int depth)
+{
+ return DoCreate(width, height, depth, 0);
+}
+
+bool wxBitmap::Create(int width, int height, const wxDC& dc)
+{
+ wxCHECK_MSG( dc.Ok(), FALSE, _T("invalid HDC in wxBitmap::Create()") );
+
+ return DoCreate(width, height, -1, dc.GetHDC());
+}
+
+bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
{
UnRef();
m_refData = new wxBitmapRefData;
-#if wxUSE_DIB_FOR_BITMAP
- if ( w && h && d >= 16 )
+ GetBitmapData()->m_width = w;
+ GetBitmapData()->m_height = h;
+
+ HBITMAP hbmp;
+
+#if wxUSE_WXDIB
+ if ( wxShouldCreateDIB(w, h, d, hdc) )
{
- if ( !CreateDIB(w, h, d) )
+ if ( d == -1 )
+ {
+ // create DIBs without alpha channel by default
+ d = 24;
+ }
+
+ wxDIB dib(w, h, d);
+ if ( !dib.IsOk() )
return FALSE;
+
+ // don't delete the DIB section in dib object dtor
+ hbmp = dib.Detach();
+
+ GetBitmapData()->m_isDIB = TRUE;
+ GetBitmapData()->m_depth = d;
}
- else
-#endif // wxUSE_DIB_FOR_BITMAP
+ else // create a DDB
+#endif
{
- GetBitmapData()->m_width = w;
- GetBitmapData()->m_height = h;
+ if ( d == -1 )
+ d = wxDisplayDepth();
+
GetBitmapData()->m_depth = d;
- HBITMAP hbmp;
#ifndef __WXMICROWIN__
if ( d > 0 )
{
GetBitmapData()->m_depth = wxDisplayDepth();
}
-
- SetHBITMAP((WXHBITMAP)hbmp);
-
-#if WXWIN_COMPATIBILITY_2
- GetBitmapData()->m_ok = hbmp != 0;
-#endif // WXWIN_COMPATIBILITY_2
}
+ SetHBITMAP((WXHBITMAP)hbmp);
+
return Ok();
}
// make sense to use #ifdefs inside the function bodies
#ifdef __WXMICROWIN__
-bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
+bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc)
{
// Set this to 1 to experiment with mask code,
// which currently doesn't work
SetPalette(image.GetPalette());
#endif // wxUSE_PALETTE
-#if WXWIN_COMPATIBILITY_2
- // check the wxBitmap object
- GetBitmapData()->SetOk();
-#endif // WXWIN_COMPATIBILITY_2
-
return TRUE;
}
// wxImage to/from conversions
// ----------------------------------------------------------------------------
-bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
+bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
+{
+ return CreateFromImage(image, depth, 0);
+}
+
+bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc)
+{
+ wxCHECK_MSG( dc.Ok(), FALSE,
+ _T("invalid HDC in wxBitmap::CreateFromImage()") );
+
+ return CreateFromImage(image, -1, dc.GetHDC());
+}
+
+bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc )
{
+#if wxUSE_WXDIB
wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
UnRef();
wxBitmapRefData *refData = new wxBitmapRefData;
refData->m_width = w;
refData->m_height = h;
- refData->m_depth = dib.GetDepth();
refData->m_hasAlpha = image.HasAlpha();
m_refData = refData;
// next either store DIB as is or create a DDB from it
HBITMAP hbitmap;
- // TODO: if we're ready to use DIB as is, we can just do this:
- // if ( ... )
- // hbitmap = dib.Detach();
- // else
- {
- // create and set the device-dependent bitmap
- //
- // VZ: why don't we just use SetDIBits() instead? because of the
- // palette or is there some other reason?
- hbitmap = ::CreateCompatibleBitmap(ScreenHDC(), w, h);
- if ( !hbitmap )
- {
- wxLogLastError(_T("CreateCompatibleBitmap()"));
-
- return FALSE;
- }
-
- MemoryHDC hdcMem;
- SelectInHDC select(hdcMem, hbitmap);
- if ( !select )
- {
- wxLogLastError(_T("SelectObjct(hBitmap)"));
- }
-
-#if wxUSE_PALETTE
- const wxPalette& palette = image.GetPalette();
-
- HPALETTE hOldPalette;
- if ( palette.Ok() )
- {
- SetPalette(palette);
-
- hOldPalette = ::SelectPalette
- (
- hdcMem,
- GetHpaletteOf(palette),
- FALSE // ignored for hdcMem
- );
-
- if ( !hOldPalette )
- {
- wxLogLastError(_T("SelectPalette()"));
- }
-
- if ( ::RealizePalette(hdcMem) == GDI_ERROR )
- {
- wxLogLastError(_T("RealizePalette()"));
- }
- }
- else // no valid palette
- {
- hOldPalette = 0;
- }
-#endif // wxUSE_PALETTE
-
- DIBSECTION ds;
- if ( !::GetObject(dib.GetHandle(), sizeof(ds), &ds) )
- {
- wxLogLastError(_T("GetObject(hDIB)"));
- }
-
- if ( ::StretchDIBits(hdcMem,
- 0, 0, w, h,
- 0, 0, w, h,
- dib.GetData(),
- (BITMAPINFO *)&ds.dsBmih,
- DIB_RGB_COLORS,
- SRCCOPY) == GDI_ERROR )
- {
- wxLogLastError(_T("StretchDIBits()"));
+ // are we going to use DIB?
+ //
+ // 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();
- return FALSE;
- }
+ refData->m_isDIB = TRUE;
+ refData->m_depth = dib.GetDepth();
+ }
+ else // we need to convert DIB to DDB
+ {
+ hbitmap = dib.CreateDDB((HDC)hdc);
-#if wxUSE_PALETTE
- if ( hOldPalette )
- {
- ::SelectPalette(hdcMem, hOldPalette, FALSE);
- }
-#endif // wxUSE_PALETTE
+ refData->m_depth = depth == -1 ? wxDisplayDepth() : depth;
}
// validate this object
SetHBITMAP((WXHBITMAP)hbitmap);
-#if WXWIN_COMPATIBILITY_2
- m_refData->m_ok = TRUE;
-#endif // WXWIN_COMPATIBILITY_2
-
// finally also set the mask if we have one
if ( image.HasMask() )
{
}
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; i<height; i++ )
+ {
+ for( j=0; j<width; j++ )
+ {
+ *(ptdata++) = *(ptbits+2);
+ *(ptdata++) = *(ptbits+1);
+ *(ptdata++) = *(ptbits );
+ ptbits += 3;
+ }
+ 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; i<height; i++ )
+ {
+ for( j=0; j<width; j++ )
+ {
+ // is this pixel transparent?
+ if ( *ptbits != 0 )
+ {
+ if ( (ptdata[0] == MASK_RED) &&
+ (ptdata[1] == MASK_GREEN) &&
+ (ptdata[2] == MASK_BLUE) )
+ {
+ // we have to fudge the colour a bit to prevent this
+ // pixel from appearing transparent
+ ptdata[2] = MASK_BLUE_REPLACEMENT;
+ }
+ ptdata += 3;
+ }
+ else // masked pixel
+ {
+ *(ptdata++) = MASK_RED;
+ *(ptdata++) = MASK_GREEN;
+ *(ptdata++) = MASK_BLUE;
+ }
+ ptbits += 3;
+ }
+ ptbits += padding;
+ }
+
+ image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
+ image.SetMask( TRUE );
+ }
+ else
+#endif
+ {
+ image.SetMask( FALSE );
+ }
+
+ // free allocated resources
+ ::ReleaseDC(NULL, hdc);
+ free(lpDIBh);
+
+ // Delete the DIB section
+ ::DeleteObject(hBitmapSection);
+
+ return image;
+#else
+ // 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") );
::SetBkColor( memdc, RGB( 255, 255, 255 ) );
::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
::DeleteDC( memdc );
- // background color set to RGB(16,16,16) in consistent with wxGTK
- unsigned char r=16, g=16, b=16;
ptdata = data;
ptbits = lpBits;
for( i=0; i<height; i++ )
{
for( j=0; j<width; j++ )
{
- if( *ptbits != 0 )
+ // is this pixel transparent?
+ if ( *ptbits != 0 )
+ {
+ if ( (ptdata[0] == MASK_RED) &&
+ (ptdata[1] == MASK_GREEN) &&
+ (ptdata[2] == MASK_BLUE) )
+ {
+ // we have to fudge the colour a bit to prevent this
+ // pixel from appearing transparent
+ ptdata[2] = MASK_BLUE_REPLACEMENT;
+ }
ptdata += 3;
- else
+ }
+ else // masked pixel
{
- *(ptdata++) = r;
- *(ptdata++) = g;
- *(ptdata++) = b;
+ *(ptdata++) = MASK_RED;
+ *(ptdata++) = MASK_GREEN;
+ *(ptdata++) = MASK_BLUE;
}
ptbits += 3;
}
ptbits += padding;
}
- image.SetMaskColour( r, g, b );
+
+ image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
image.SetMask( TRUE );
}
else
free(lpBits);
return image;
+#endif
}
#endif // wxUSE_IMAGE
// wxBitmap accessors
// ----------------------------------------------------------------------------
+#if wxUSE_PALETTE
wxPalette* wxBitmap::GetPalette() const
{
return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
: (wxPalette *) NULL;
}
+#endif
wxMask *wxBitmap::GetMask() const
{
#endif // WXWIN_COMPATIBILITY_2_4
+void wxBitmap::UseAlpha()
+{
+ if ( GetBitmapData() )
+ GetBitmapData()->m_hasAlpha = true;
+}
+
bool wxBitmap::HasAlpha() const
{
return GetBitmapData() && GetBitmapData()->m_hasAlpha;
GetBitmapData()->SetMask(mask);
}
-#if WXWIN_COMPATIBILITY_2
-
-void wxBitmap::SetOk(bool isOk)
-{
- EnsureHasData();
-
- GetBitmapData()->m_ok = isOk;
-}
-
-#endif // WXWIN_COMPATIBILITY_2
-
#if WXWIN_COMPATIBILITY_2_4
void wxBitmap::SetQuality(int WXUNUSED(quality))
#endif // WXWIN_COMPATIBILITY_2_4
// ----------------------------------------------------------------------------
-// TODO: to be replaced by something better
+// raw bitmap access support
// ----------------------------------------------------------------------------
-// Creates a bitmap that matches the device context, from
-// an arbitray bitmap. At present, the original bitmap must have an
-// associated palette. TODO: use a default palette if no palette exists.
-// Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
-wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
+#ifdef wxHAVE_RAW_BITMAP
+void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
{
-#ifdef __WXMICROWIN__
- return *this;
-#else
- wxMemoryDC memDC;
- wxBitmap tmpBitmap(GetWidth(), GetHeight(), dc.GetDepth());
- HPALETTE hPal = (HPALETTE) NULL;
- LPBITMAPINFO lpDib;
- void *lpBits = (void*) NULL;
+#if wxUSE_WXDIB
+ if ( !Ok() )
+ {
+ // no bitmap, no data (raw or otherwise)
+ return NULL;
+ }
-#if wxUSE_PALETTE
- if( GetPalette() && GetPalette()->Ok() )
+ // 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 )
{
- tmpBitmap.SetPalette(*GetPalette());
- memDC.SelectObject(tmpBitmap);
- memDC.SetPalette(*GetPalette());
- hPal = (HPALETTE)GetPalette()->GetHPALETTE();
+ 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
+ else // we're a DIB
+ {
+ hDIB = GetHbitmap();
+ }
+
+ DIBSECTION ds;
+ if ( ::GetObject(hDIB, sizeof(ds), &ds) != sizeof(DIBSECTION) )
{
- hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
- wxPalette palette;
- palette.SetHPALETTE( (WXHPALETTE)hPal );
- tmpBitmap.SetPalette( palette );
- memDC.SelectObject(tmpBitmap);
- memDC.SetPalette( palette );
+ wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
+
+ return NULL;
}
-#else // !wxUSE_PALETTE
- hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
-#endif // wxUSE_PALETTE/!wxUSE_PALETTE
- // set the height negative because in a DIB the order of the lines is
- // reversed
- if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) )
+ // check that the bitmap is in correct format
+ if ( ds.dsBm.bmBitsPixel != bpp )
{
- return wxNullBitmap;
+ wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
+
+ return NULL;
}
- lpBits = malloc(lpDib->bmiHeader.biSizeImage);
+ // ok, store the relevant info in wxPixelDataBase
+ const LONG h = ds.dsBm.bmHeight;
- ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits);
+ data.m_width = ds.dsBm.bmWidth;
+ data.m_height = h;
- ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0,
- GetWidth(), GetHeight(),
- 0, 0, 0, GetHeight(),
- lpBits, lpDib, DIB_RGB_COLORS);
+ // remember that DIBs are stored in top to bottom order!
+ const LONG bytesPerRow = ds.dsBm.bmWidthBytes;
+ data.m_stride = -bytesPerRow;
- free(lpBits);
+ char *bits = (char *)ds.dsBm.bmBits;
+ if ( h > 1 )
+ {
+ bits += (h - 1)*bytesPerRow;
+ }
+
+ 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<wxBitmap, wxAlphaPixelFormat> 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;
- wxFreeDIB(lpDib);
+ 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);
+ }
- return tmpBitmap;
+ // 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
#endif
// ----------------------------------------------------------------------------
-// other helper functions
+// global helper functions implemented here
// ----------------------------------------------------------------------------
-extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
+// helper of wxBitmapToHICON/HCURSOR
+static
+HICON wxBitmapToIconOrCursor(const wxBitmap& bmp,
+ bool iconWanted,
+ int hotSpotX,
+ int hotSpotY)
+{
+ if ( !bmp.Ok() )
+ {
+ // we can't create an icon/cursor form nothing
+ return 0;
+ }
+
+ wxMask *mask = bmp.GetMask();
+ if ( !mask )
+ {
+ // we must have a mask for an icon, so even if it's probably incorrect,
+ // do create it (grey is the "standard" transparent colour)
+ mask = new wxMask(bmp, *wxLIGHT_GREY);
+ }
+
+ ICONINFO iconInfo;
+ wxZeroMemory(iconInfo);
+ iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
+ if ( !iconWanted )
+ {
+ iconInfo.xHotspot = hotSpotX;
+ iconInfo.yHotspot = hotSpotY;
+ }
+
+ iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
+ iconInfo.hbmColor = GetHbitmapOf(bmp);
+
+ // black out the transparent area to preserve background colour, because
+ // Windows blits the original bitmap using SRCINVERT (XOR) after applying
+ // the mask to the dest rect.
+ {
+ MemoryHDC dcSrc, dcDst;
+ SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
+ selectBitmap(dcDst, iconInfo.hbmColor);
+
+ if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
+ dcSrc, 0, 0, SRCAND) )
+ {
+ wxLogLastError(_T("BitBlt"));
+ }
+ }
+
+ HICON hicon = ::CreateIconIndirect(&iconInfo);
+
+ if ( !bmp.GetMask() )
+ {
+ // we created the mask, now delete it
+ delete mask;
+ }
+
+ // delete the inverted mask bitmap we created as well
+ ::DeleteObject(iconInfo.hbmMask);
+
+ return hicon;
+}
+
+HICON wxBitmapToHICON(const wxBitmap& bmp)
+{
+ return wxBitmapToIconOrCursor(bmp, TRUE, 0, 0);
+}
+
+HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY)
+{
+ return (HCURSOR)wxBitmapToIconOrCursor(bmp, FALSE, hotSpotX, hotSpotY);
+}
+
+HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
{
#ifndef __WXMICROWIN__
wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
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) )
wxLogLastError(wxT("BitBlt"));
}
+ // Deselect objects
+ SelectObject(hdcSrc,srcTmp);
+ SelectObject(hdcDst,dstTmp);
+
::DeleteDC(hdcSrc);
::DeleteDC(hdcDst);