// 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
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()
// ----------------------------------------------------------------------------
// 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)
-{
- // 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));
-}
+//
+// NB: we always use DIBs under Windows CE as this is much simpler (even if
+// also less efficient...) and we obviously can't use them if there is no
+// DIB support compiled in at all
+#ifdef __WXWINCE__
+ static inline bool wxShouldCreateDIB(int, int, int, WXHDC) { return true; }
+
+ #define ALWAYS_USE_DIB
+#elif !wxUSE_WXDIB
+ // no sense in defining wxShouldCreateDIB() as we can't compile code
+ // executed if it is true, so we have to use #if's anyhow
+ #define NEVER_USE_DIB
+#else // wxUSE_WXDIB && !__WXWINCE__
+ static inline bool wxShouldCreateDIB(int w, int h, int d, WXHDC hdc)
+ {
+ // 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));
+ }
+
+ #define SOMETIMES_USE_DIB
+#endif // different DIB usage scenarious
// ----------------------------------------------------------------------------
// wxBitmapRefData
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)
{
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;
if ( !cursor.Ok() )
return FALSE;
-#ifdef __WIN16__
- wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
-
- return FALSE;
-#else
return CopyFromIconOrCursor(cursor);
-#endif // Win16
}
bool wxBitmap::CopyFromIcon(const wxIcon& icon)
if ( !icon.Ok() )
return FALSE;
- // GetIconInfo() doesn't exist under Win16 and I don't know any other way
- // to create a bitmap from icon there - but using this way we won't have
- // the mask (FIXME)
-#ifdef __WIN16__
- int width = icon.GetWidth(),
- height = icon.GetHeight();
-
- // copy the icon to the bitmap
- ScreenHDC hdcScreen;
- HDC hdc = ::CreateCompatibleDC(hdcScreen);
- HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
- HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
-
- ::DrawIcon(hdc, 0, 0, GetHiconOf(icon));
-
- ::SelectObject(hdc, hbmpOld);
- ::DeleteDC(hdc);
-
- wxBitmapRefData *refData = new wxBitmapRefData;
- m_refData = refData;
-
- refData->m_width = width;
- refData->m_height = height;
- refData->m_depth = wxDisplayDepth();
-
- 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
}
+#ifndef NEVER_USE_DIB
+
bool wxBitmap::CopyFromDIB(const wxDIB& dib)
{
wxCHECK_MSG( dib.IsOk(), FALSE, _T("invalid DIB in CopyFromDIB") );
+#ifdef SOMETIMES_USE_DIB
HBITMAP hbitmap = dib.CreateDDB();
if ( !hbitmap )
return FALSE;
+#else // ALWAYS_USE_DIB
+ HBITMAP hbitmap = ((wxDIB &)dib).Detach(); // const_cast
+#endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
UnRef();
return TRUE;
}
+#endif // NEVER_USE_DIB
+
wxBitmap::~wxBitmap()
{
}
}
// 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();
GetBitmapData()->m_width = w;
GetBitmapData()->m_height = h;
- HBITMAP hbmp;
+ HBITMAP hbmp wxDUMMY_INITIALIZE(0);
+#ifndef NEVER_USE_DIB
if ( wxShouldCreateDIB(w, h, d, hdc) )
{
if ( d == -1 )
GetBitmapData()->m_depth = d;
}
else // create a DDB
+#endif // NEVER_USE_DIB
{
- if ( d == -1 )
- d = wxDisplayDepth();
-
- GetBitmapData()->m_depth = d;
-
+#ifndef ALWAYS_USE_DIB
#ifndef __WXMICROWIN__
if ( d > 0 )
{
{
wxLogLastError(wxT("CreateBitmap"));
}
+
+ GetBitmapData()->m_depth = d;
}
- else
+ else // d == 0, create bitmap compatible with the screen
#endif // !__WXMICROWIN__
{
ScreenHDC dc;
GetBitmapData()->m_depth = wxDisplayDepth();
}
+#endif // !ALWAYS_USE_DIB
}
SetHBITMAP((WXHBITMAP)hbmp);
-#if WXWIN_COMPATIBILITY_2
- GetBitmapData()->m_ok = hbmp != 0;
-#endif // WXWIN_COMPATIBILITY_2
-
return Ok();
}
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
// ----------------------------------------------------------------------------
+#if wxUSE_WXDIB
+
bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
{
return CreateFromImage(image, depth, 0);
return CreateFromImage(image, -1, dc.GetHDC());
}
-bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc )
+bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc)
{
wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
// next either store DIB as is or create a DDB from it
- HBITMAP hbitmap;
+ HBITMAP hbitmap wxDUMMY_INITIALIZE(0);
// 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();
}
+#ifndef ALWAYS_USE_DIB
else // we need to convert DIB to DDB
{
hbitmap = dib.CreateDDB((HDC)hdc);
refData->m_depth = depth == -1 ? wxDisplayDepth() : depth;
}
+#endif // !ALWAYS_USE_DIB
// 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() )
{
wxImage wxBitmap::ConvertToImage() const
{
- 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;
- }
+ wxDIB dib(*this);
- // 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
- unsigned char *lpBits;
- lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
- if( !lpBits )
- {
- wxFAIL_MSG( wxT("could not allocate data for DIB") );
- free( data );
- free( lpDIBh );
- return wxNullImage;
- }
-
- // copy data from the device-dependent bitmap to the DIB
- HDC hdc = ::GetDC(NULL);
- HBITMAP hbitmap;
- hbitmap = (HBITMAP) GetHBITMAP();
- ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
-
- // copy DIB data into the wxImage object
- int i, j;
- unsigned char *ptdata = data;
- unsigned char *ptbits = 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;
- }
-
- // similarly, set data according to the possible mask bitmap
- if( GetMask() && GetMask()->GetMaskBitmap() )
+ wxImage image;
+ if ( dib.IsOk() )
{
- 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 );
- // 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++ )
+ image = dib.ConvertToImage();
+ if ( image.Ok() )
{
- for( j=0; j<width; j++ )
- {
- if( *ptbits != 0 )
- ptdata += 3;
- else
- {
- *(ptdata++) = r;
- *(ptdata++) = g;
- *(ptdata++) = b;
- }
- ptbits += 3;
- }
- ptbits += padding;
+ // TODO: set mask
}
- image.SetMaskColour( r, g, b );
- image.SetMask( TRUE );
}
- else
- {
- image.SetMask( FALSE );
- }
- // free allocated resources
- ::ReleaseDC(NULL, hdc);
- free(lpDIBh);
- free(lpBits);
return image;
}
+#endif // wxUSE_WXDIB
+
#endif // wxUSE_IMAGE
// ----------------------------------------------------------------------------
return handler->LoadFile(this, filename, type, -1, -1);
}
#if wxUSE_IMAGE
- else
+ else // no bitmap handler found
{
wxImage image;
if ( image.LoadFile( filename, type ) && image.Ok() )
return handler->SaveFile(this, filename, type, palette);
}
#if wxUSE_IMAGE
- else
+ else // no bitmap handler found
{
// FIXME what about palette? shouldn't we use it?
wxImage image = ConvertToImage();
// wxBitmap accessors
// ----------------------------------------------------------------------------
+#if wxUSE_PALETTE
wxPalette* wxBitmap::GetPalette() const
{
return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
: (wxPalette *) NULL;
}
+#endif
wxMask *wxBitmap::GetMask() const
{
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))
// 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
{
delete dib;
- return FALSE;
+ return NULL;
}
// we'll free it in UngetRawData()
{
wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
- return FALSE;
+ 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(wxRawBitmapData *data)
+void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
{
- wxCHECK_RET( data, _T("NULL pointer in wxBitmap::UngetRawData()") );
-
+#if wxUSE_WXDIB
if ( !Ok() )
return;
- if ( !*data )
+ // 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 wxRawBitmapData dtor and so there is no
+ // called automatically from wxPixelDataBase dtor and so there is no
// way to prevent this from happening
return;
}
- // AlphaBlend() wants to have premultiplied source alpha but wxRawBitmap
- // API uses normal, not premultiplied, colours, so adjust them here now
- wxRawBitmapIterator p(*data);
-
- const int w = data->GetWidth();
- const int h = data->GetHeight();
-
- 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);
- for ( int x = 0; x < w; x++ )
+ const int w = data.GetWidth();
+ const int h = data.GetHeight();
+
+ for ( int y = 0; y < h; y++ )
{
- const unsigned alpha = p.Alpha();
+ PixelData::Iterator rowStart = p;
- p.Red() = (p.Red() * alpha + 127) / 255;
- p.Blue() = (p.Blue() * alpha + 127) / 255;
- p.Green() = (p.Green() * alpha + 127) / 255;
+ for ( int x = 0; x < w; x++ )
+ {
+ const unsigned alpha = p.Alpha();
- ++p;
- }
+ p.Red() = (p.Red() * alpha + 127) / 255;
+ p.Blue() = (p.Blue() * alpha + 127) / 255;
+ p.Green() = (p.Green() * alpha + 127) / 255;
- p = rowStart;
- p.OffsetY(1);
+ ++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 wxRawBitmapData effective
+ // changes made via raw bitmap access effective
if ( !GetBitmapData()->m_isDIB )
{
wxDIB *dib = GetBitmapData()->m_dib;
delete dib;
}
+#endif // wxUSE_WXDIB
}
+#endif // #ifdef wxHAVE_RAW_BITMAP
// ----------------------------------------------------------------------------
// wxMask
HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
{
unsigned long i, headerSize;
- LPBITMAPINFO lpDIBheader = NULL;
- LPPALETTEENTRY lpPe = NULL;
-
// Allocate space for a DIB header
headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
- lpDIBheader = (BITMAPINFO *) malloc(headerSize);
- lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
+ LPBITMAPINFO lpDIBheader = (BITMAPINFO *) malloc(headerSize);
+ LPPALETTEENTRY lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
GetPaletteEntries(hPal, 0, 256, lpPe);
}
ICONINFO iconInfo;
+ wxZeroMemory(iconInfo);
iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
if ( !iconWanted )
{
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);