#include "wx/icon.h"
#endif
+//#include "device.h"
+
#include "wx/msw/private.h"
#include "wx/log.h"
+#if !defined(__WXMICROWIN__)
#include "wx/msw/dib.h"
+#endif
+
#include "wx/image.h"
+#include "wx/xpmdecod.h"
// missing from mingw32 header
#ifndef CLR_INVALID
if ( m_hBitmap)
{
+ // printf("About to delete bitmap %d\n", (int) (HBITMAP) m_hBitmap);
+#if 1
if ( !::DeleteObject((HBITMAP)m_hBitmap) )
{
wxLogLastError(wxT("DeleteObject(hbitmap)"));
}
+#endif
}
delete m_bitmapMask;
{
// m_refData = NULL; done in the base class ctor
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
}
#ifdef __WIN32__
bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
{
+#ifndef __WXMICROWIN__
// it may be either HICON or HCURSOR
HICON hicon = (HICON)icon.GetHandle();
refData->m_bitmapMask = new wxMask((WXHBITMAP)
wxInvertMask(iconInfo.hbmMask, w, h));
+
+ // 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;
+#endif
}
#endif // Win32
wxBitmap::~wxBitmap()
{
- if (wxTheBitmapList)
- wxTheBitmapList->DeleteObject(this);
}
wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
{
Init();
+#ifndef __WXMICROWIN__
wxBitmapRefData *refData = new wxBitmapRefData;
m_refData = refData;
// we assume that it is in XBM format which is not quite the same as
// the format CreateBitmap() wants because the order of bytes in the
// line is inversed!
- static const size_t bytesPerLine = (width + 7) / 8;
- static const size_t padding = bytesPerLine % 2;
- static const size_t len = height * ( padding + bytesPerLine );
+ const size_t bytesPerLine = (width + 7) / 8;
+ const size_t padding = bytesPerLine % 2;
+ const size_t len = height * ( padding + bytesPerLine );
data = (char *)malloc(len);
const char *src = bits;
char *dst = data;
}
SetHBITMAP((WXHBITMAP)hbmp);
+#endif
}
// Create from XPM data
bool wxBitmap::CreateFromXpm(const char **data)
{
+#if wxUSE_IMAGE && wxUSE_XPM
Init();
- return Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
+ 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") )
+
+ *this = wxBitmap(img);
+ return TRUE;
+#else
+ return FALSE;
+#endif
}
wxBitmap::wxBitmap(int w, int h, int d)
(void)Create(data, type, width, height, depth);
}
-wxBitmap::wxBitmap(const wxString& filename, long type)
+wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
{
Init();
GetBitmapData()->m_depth = d;
HBITMAP hbmp;
-
+#ifndef __WXMICROWIN__
if ( d > 0 )
{
hbmp = ::CreateBitmap(w, h, 1, d, NULL);
}
}
else
+#endif
{
ScreenHDC dc;
hbmp = ::CreateCompatibleBitmap(dc, w, h);
#if WXWIN_COMPATIBILITY_2
GetBitmapData()->m_ok = hbmp != 0;
#endif // WXWIN_COMPATIBILITY_2
-
return Ok();
}
+// ----------------------------------------------------------------------------
+// wxImage to/from conversions
+// ----------------------------------------------------------------------------
+
+#if wxUSE_IMAGE
+
bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
{
+#ifdef __WXMICROWIN__
+
+ // Set this to 1 to experiment with mask code,
+ // which currently doesn't work
+#define USE_MASKS 0
+
+ m_refData = new wxBitmapRefData();
+
+ // Initial attempt at a simple-minded implementation.
+ // The bitmap will always be created at the screen depth,
+ // so the 'depth' argument is ignored.
+
+ HDC hScreenDC = ::GetDC(NULL);
+ // printf("Screen planes = %d, bpp = %d\n", hScreenDC->psd->planes, hScreenDC->psd->bpp);
+ int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
+
+ HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
+ HBITMAP hMaskBitmap = NULL;
+ HBITMAP hOldMaskBitmap = NULL;
+ HDC hMaskDC = NULL;
+ unsigned char maskR = 0;
+ unsigned char maskG = 0;
+ unsigned char maskB = 0;
+
+ // printf("Created bitmap %d\n", (int) hBitmap);
+ if (hBitmap == NULL)
+ {
+ ::ReleaseDC(NULL, hScreenDC);
+ return FALSE;
+ }
+ HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
+
+ HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
+ ::ReleaseDC(NULL, hScreenDC);
+
+ // created an mono-bitmap for the possible mask
+ bool hasMask = image.HasMask();
+
+ if ( hasMask )
+ {
+#if USE_MASKS
+ // FIXME: we should be able to pass bpp = 1, but
+ // GdBlit can't handle a different depth
+#if 0
+ hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL );
+#else
+ hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight());
+#endif
+ maskR = image.GetMaskRed();
+ maskG = image.GetMaskGreen();
+ maskB = image.GetMaskBlue();
+
+ if (!hMaskBitmap)
+ {
+ hasMask = FALSE;
+ }
+ else
+ {
+ hScreenDC = ::GetDC(NULL);
+ hMaskDC = ::CreateCompatibleDC(hScreenDC);
+ ::ReleaseDC(NULL, hScreenDC);
+
+ hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap);
+ }
+#else
+ hasMask = FALSE;
+#endif
+ }
+
+ int i, j;
+ for (i = 0; i < image.GetWidth(); i++)
+ {
+ for (j = 0; j < image.GetHeight(); j++)
+ {
+ unsigned char red = image.GetRed(i, j);
+ unsigned char green = image.GetGreen(i, j);
+ unsigned char blue = image.GetBlue(i, j);
+
+ ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
+
+ if (hasMask)
+ {
+ // scan the bitmap for the transparent colour and set the corresponding
+ // pixels in the mask to BLACK and the rest to WHITE
+ if (maskR == red && maskG == green && maskB == blue)
+ ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0));
+ else
+ ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255));
+ }
+ }
+ }
+
+ ::SelectObject(hMemDC, hOldBitmap);
+ ::DeleteDC(hMemDC);
+ if (hasMask)
+ {
+ ::SelectObject(hMaskDC, hOldMaskBitmap);
+ ::DeleteDC(hMaskDC);
+
+ ((wxBitmapRefData*)m_refData)->m_bitmapMask = new wxMask((WXHBITMAP) hMaskBitmap);
+ }
+
+ SetWidth(image.GetWidth());
+ SetHeight(image.GetHeight());
+ SetDepth(screenDepth);
+ SetHBITMAP( (WXHBITMAP) hBitmap );
+
+#if wxUSE_PALETTE
+ // Copy the palette from the source image
+ SetPalette(image.GetPalette());
+#endif // wxUSE_PALETTE
+
+#if WXWIN_COMPATIBILITY_2
+ // check the wxBitmap object
+ GetBitmapData()->SetOk();
+#endif // WXWIN_COMPATIBILITY_2
+
+ return TRUE;
+
+#else
wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
m_refData = new wxBitmapRefData();
}
// set bitmap parameters
- wxCHECK_MSG( Ok(), *this, wxT("invalid image") );
+ wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
SetWidth( width );
SetHeight( bmpHeight );
if (depth == -1) depth = wxDisplayDepth();
SetDepth( depth );
+#if wxUSE_PALETTE
+ // Copy the palette from the source image
+ SetPalette(image.GetPalette());
+#endif // wxUSE_PALETTE
+
// create a DIB header
int headersize = sizeof(BITMAPINFOHEADER);
BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
- wxCHECK_MSG( lpDIBh, bitmap, wxT("could not allocate memory for DIB header") );
+ wxCHECK_MSG( lpDIBh, FALSE, wxT("could not allocate memory for DIB header") );
// Fill in the DIB header
lpDIBh->bmiHeader.biSize = headersize;
lpDIBh->bmiHeader.biWidth = (DWORD)width;
hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
::SelectObject( memdc, hbitmap);
+#if wxUSE_PALETTE
HPALETTE hOldPalette = 0;
if (image.GetPalette().Ok())
{
hOldPalette = ::SelectPalette(memdc, (HPALETTE) image.GetPalette().GetHPALETTE(), FALSE);
::RealizePalette(memdc);
}
+#endif // wxUSE_PALETTE
// copy image data into DIB data and then into DDB (in a loop)
unsigned char *data = image.GetData();
}
SetHBITMAP( (WXHBITMAP) hbitmap );
+#if wxUSE_PALETTE
if (hOldPalette)
SelectPalette(memdc, hOldPalette, FALSE);
+#endif // wxUSE_PALETTE
// similarly, created an mono-bitmap for the possible mask
if( image.HasMask() )
// check the wxBitmap object
GetBitmapData()->SetOk();
#endif // WXWIN_COMPATIBILITY_2
-
- if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
return TRUE;
+#endif
}
wxImage wxBitmap::ConvertToImage() const
{
+#ifdef __WXMICROWIN__
+ // Initial attempt at a simple-minded implementation.
+ // The bitmap will always be created at the screen depth,
+ // so the 'depth' argument is ignored.
+ // TODO: transparency (create a mask image)
+
+ if (!Ok())
+ {
+ wxFAIL_MSG( wxT("bitmap is invalid") );
+ return wxNullImage;
+ }
+
+ 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;
+ }
+
+ HDC hScreenDC = ::GetDC(NULL);
+
+ HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
+ ::ReleaseDC(NULL, hScreenDC);
+
+ HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
+
+ HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
+
+ int i, j;
+ for (i = 0; i < GetWidth(); i++)
+ {
+ for (j = 0; j < GetHeight(); j++)
+ {
+ COLORREF color = ::GetPixel(hMemDC, i, j);
+ unsigned char red = GetRValue(color);
+ unsigned char green = GetGValue(color);
+ unsigned char blue = GetBValue(color);
+
+ image.SetRGB(i, j, red, green, blue);
+ }
+ }
+
+ ::SelectObject(hMemDC, hOldBitmap);
+ ::DeleteDC(hMemDC);
+
+#if wxUSE_PALETTE
+ // Copy the palette from the source image
+ if (GetPalette())
+ image.SetPalette(* GetPalette());
+#endif // wxUSE_PALETTE
+
+ return image;
+
+#else // __MICROWIN__
+
wxImage image;
-
+
wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
// create an wxImage object
free(lpBits);
return image;
+#endif
}
+#endif // wxUSE_IMAGE
+
bool wxBitmap::LoadFile(const wxString& filename, long type)
{
UnRef();
return handler->LoadFile(this, filename, type, -1, -1);
}
+#if wxUSE_IMAGE
else
{
wxImage image;
- if ( !image.LoadFile( filename, type ) || !image.Ok() )
- return FALSE;
-
- *this = image.ConvertToBitmap();
+ if ( image.LoadFile( filename, type ) && image.Ok() )
+ {
+ *this = image.ConvertToBitmap();
- return TRUE;
+ return TRUE;
+ }
}
+#endif // wxUSE_IMAGE
+
+ return FALSE;
}
bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
return handler->Create(this, data, type, width, height, depth);
}
-bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette)
+bool wxBitmap::SaveFile(const wxString& filename,
+ int type,
+ const wxPalette *palette)
{
wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
{
return handler->SaveFile(this, filename, type, palette);
}
+#if wxUSE_IMAGE
else
{
// FIXME what about palette? shouldn't we use it?
wxImage image( *this );
- if (!image.Ok())
- return FALSE;
-
- return image.SaveFile( filename, type );
+ if ( image.Ok() )
+ {
+ return image.SaveFile(filename, type);
+ }
}
+#endif // wxUSE_IMAGE
+
+ return FALSE;
}
// ----------------------------------------------------------------------------
wxBitmap ret( rect.width, rect.height, GetDepth() );
wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
+#ifndef __WXMICROWIN__
// copy bitmap data
- HDC dcSrc = ::CreateCompatibleDC(NULL);
- HDC dcDst = ::CreateCompatibleDC(NULL);
- SelectObject(dcSrc, (HBITMAP) GetHBITMAP());
- SelectObject(dcDst, (HBITMAP) ret.GetHBITMAP());
- BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
+ MemoryHDC dcSrc, dcDst;
+
+ {
+ SelectInHDC selectSrc(dcSrc, GetHbitmap()),
+ selectDst(dcDst, GetHbitmapOf(ret));
+
+ if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
+ dcSrc, rect.x, rect.y, SRCCOPY) )
+ {
+ wxLogLastError(_T("BitBlt"));
+ }
+ }
// copy mask if there is one
- if (GetMask())
+ if ( GetMask() )
{
HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
- SelectObject(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap());
- SelectObject(dcDst, (HBITMAP) hbmpMask);
- BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
+ SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()),
+ selectDst(dcDst, hbmpMask);
+
+ if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
+ dcSrc, rect.x, rect.y, SRCCOPY) )
+ {
+ wxLogLastError(_T("BitBlt"));
+ }
wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
ret.SetMask(mask);
}
-
- SelectObject(dcDst, NULL);
- SelectObject(dcSrc, NULL);
- DeleteDC(dcDst);
- DeleteDC(dcSrc);
+#endif // !__WXMICROWIN__
return ret;
}
}
#endif // WXWIN_COMPATIBILITY_2
+#if wxUSE_PALETTE
+
void wxBitmap::SetPalette(const wxPalette& palette)
{
EnsureHasData();
GetBitmapData()->m_bitmapPalette = palette;
}
+#endif // wxUSE_PALETTE
+
void wxBitmap::SetMask(wxMask *mask)
{
EnsureHasData();
// Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
{
+#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_PALETTE
if( GetPalette() && GetPalette()->Ok() )
{
tmpBitmap.SetPalette(*GetPalette());
memDC.SelectObject(tmpBitmap);
memDC.SetPalette( palette );
}
+#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
wxFreeDIB(lpDib);
return tmpBitmap;
+#endif
}
// ----------------------------------------------------------------------------
// Create a mask from a mono bitmap (copies the bitmap).
bool wxMask::Create(const wxBitmap& bitmap)
{
+#ifndef __WXMICROWIN__
wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
_T("can't create mask from invalid or not monochrome bitmap") );
SelectObject(destDC, 0);
DeleteDC(destDC);
return TRUE;
+#else
+ return FALSE;
+#endif
}
// Create a mask from a bitmap and a palette index indicating
::DeleteObject((HBITMAP) m_maskBitmap);
m_maskBitmap = 0;
}
+
+#if wxUSE_PALETTE
if (bitmap.Ok() && bitmap.GetPalette()->Ok())
{
unsigned char red, green, blue;
return Create(bitmap, transparentColour);
}
}
+#endif // wxUSE_PALETTE
+
return FALSE;
}
// the transparent area
bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
{
+#ifndef __WXMICROWIN__
wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
if ( m_maskBitmap )
// scan the bitmap for the transparent colour and set the corresponding
// pixels in the mask to BLACK and the rest to WHITE
- COLORREF maskColour = wxColourToRGB(colour);
+ COLORREF maskColour = RGB(colour.Red(), colour.Green(), colour.Blue());
m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
HDC srcDC = ::CreateCompatibleDC(NULL);
bool ok = TRUE;
+ // SelectObject() will fail
+ wxASSERT_MSG( !bitmap.GetSelectedInto(),
+ _T("bitmap can't be selected in another DC") );
+
HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
if ( !hbmpSrcOld )
{
ok = FALSE;
}
- // this is not very efficient, but I can't think of a better way of doing
- // it
- for ( int w = 0; ok && (w < width); w++ )
+ if ( ok )
{
- for ( int h = 0; ok && (h < height); h++ )
- {
- COLORREF col = GetPixel(srcDC, w, h);
- if ( col == CLR_INVALID )
- {
- wxLogLastError(wxT("GetPixel"));
-
- // doesn't make sense to continue
- ok = FALSE;
-
- break;
- }
-
- if ( col == maskColour )
- {
- ::SetPixel(destDC, w, h, RGB(0, 0, 0));
- }
- else
- {
- ::SetPixel(destDC, w, h, RGB(255, 255, 255));
- }
- }
+ // this will create a monochrome bitmap with 0 points for the pixels
+ // which have the same value as the background colour and 1 for the
+ // others
+ ::SetBkColor(srcDC, maskColour);
+ ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY);
}
::SelectObject(srcDC, hbmpSrcOld);
::DeleteDC(destDC);
return ok;
+#else // __WXMICROWIN__
+ return FALSE;
+#endif // __WXMICROWIN__/!__WXMICROWIN__
}
// ----------------------------------------------------------------------------
// DIB functions
// ----------------------------------------------------------------------------
+#ifndef __WXMICROWIN__
bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
{
{
free(lpDIBHeader);
}
+#endif
// ----------------------------------------------------------------------------
// other helper functions
extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
{
+#ifndef __WXMICROWIN__
wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
// get width/height from the bitmap if not given
::DeleteDC(hdcDst);
return hbmpInvMask;
+#else
+ return 0;
+#endif
}