-/////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////
// Name: bitmap.cpp
// Purpose: wxBitmap
// Author: Julian Smart
#include "wx/icon.h"
#endif
-//#include "device.h"
-
#include "wx/msw/private.h"
#include "wx/log.h"
#define CLR_INVALID ((COLORREF)-1)
#endif // no CLR_INVALID
+// ----------------------------------------------------------------------------
+// Bitmap data
+// ----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxBitmapRefData : public wxGDIImageRefData
+{
+public:
+ wxBitmapRefData();
+ virtual ~wxBitmapRefData() { Free(); }
+
+ virtual void Free();
+
+ // set the mask object to use as the mask, we take ownership of it
+ void SetMask(wxMask *mask)
+ {
+ delete m_bitmapMask;
+ m_bitmapMask = mask;
+ }
+
+ // set the HBITMAP to use as the mask
+ void SetMask(HBITMAP hbmpMask)
+ {
+ SetMask(new wxMask((WXHBITMAP)hbmpMask));
+ }
+
+ // return the mask
+ wxMask *GetMask() const { return m_bitmapMask; }
+
+public:
+#if wxUSE_PALETTE
+ wxPalette m_bitmapPalette;
+#endif // wxUSE_PALETTE
+
+ // MSW-specific
+ // ------------
+
+#ifdef __WXDEBUG__
+ // this field is solely for error checking: we detect selecting a bitmap
+ // into more than one DC at once or deleting a bitmap still selected into a
+ // DC (both are serious programming errors under Windows)
+ wxDC *m_selectedInto;
+#endif // __WXDEBUG__
+
+ // true if we have alpha transparency info and can be drawn using
+ // AlphaBlend()
+ bool m_hasAlpha;
+
+private:
+ // optional mask for transparent drawing
+ wxMask *m_bitmapMask;
+
+ DECLARE_NO_COPY_CLASS(wxBitmapRefData)
+};
+
// ----------------------------------------------------------------------------
// macros
// ----------------------------------------------------------------------------
wxBitmapRefData::wxBitmapRefData()
{
- m_quality = 0;
m_selectedInto = NULL;
- m_numColors = 0;
m_bitmapMask = NULL;
m_hBitmap = (WXHBITMAP) NULL;
+ m_hasAlpha = FALSE;
}
void wxBitmapRefData::Free()
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;
}
+wxGDIImageRefData *wxBitmap::CreateData() const
+{
+ return new wxBitmapRefData;
+}
+
#ifdef __WIN32__
bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
// the mask returned by GetIconInfo() is inversed compared to the usual
// wxWin convention
- refData->m_bitmapMask = new wxMask((WXHBITMAP)
- wxInvertMask(iconInfo.hbmMask, w, h));
+ refData->SetMask(wxInvertMask(iconInfo.hbmMask, w, h));
// delete the old one now as we don't need it any more
refData->m_width = width;
refData->m_height = height;
refData->m_depth = depth;
- refData->m_numColors = 0;
- refData->m_selectedInto = NULL;
char *data;
if ( depth == 1 )
m_refData = new wxBitmapRefData;
- GetBitmapData()->m_width = w;
- GetBitmapData()->m_height = h;
- GetBitmapData()->m_depth = d;
-
- HBITMAP hbmp;
-#ifndef __WXMICROWIN__
- if ( d > 0 )
+#if wxUSE_DIB_FOR_BITMAP
+ if ( w && h && d >= 16 )
{
- hbmp = ::CreateBitmap(w, h, 1, d, NULL);
- if ( !hbmp )
- {
- wxLogLastError(wxT("CreateBitmap"));
- }
+ if ( !CreateDIB(w, h, d) )
+ return FALSE;
}
else
-#endif
+#endif // wxUSE_DIB_FOR_BITMAP
{
- ScreenHDC dc;
- hbmp = ::CreateCompatibleBitmap(dc, w, h);
- if ( !hbmp )
+ GetBitmapData()->m_width = w;
+ GetBitmapData()->m_height = h;
+ GetBitmapData()->m_depth = d;
+
+ HBITMAP hbmp;
+#ifndef __WXMICROWIN__
+ if ( d > 0 )
{
- wxLogLastError(wxT("CreateCompatibleBitmap"));
+ hbmp = ::CreateBitmap(w, h, 1, d, NULL);
+ if ( !hbmp )
+ {
+ wxLogLastError(wxT("CreateBitmap"));
+ }
}
+ else
+#endif // !__WXMICROWIN__
+ {
+ ScreenHDC dc;
+ hbmp = ::CreateCompatibleBitmap(dc, w, h);
+ if ( !hbmp )
+ {
+ wxLogLastError(wxT("CreateCompatibleBitmap"));
+ }
- GetBitmapData()->m_depth = wxDisplayDepth();
- }
+ GetBitmapData()->m_depth = wxDisplayDepth();
+ }
- SetHBITMAP((WXHBITMAP)hbmp);
+ SetHBITMAP((WXHBITMAP)hbmp);
#if WXWIN_COMPATIBILITY_2
- GetBitmapData()->m_ok = hbmp != 0;
+ GetBitmapData()->m_ok = hbmp != 0;
#endif // WXWIN_COMPATIBILITY_2
+ }
+
return Ok();
}
+#if wxUSE_IMAGE
+
// ----------------------------------------------------------------------------
-// wxImage to/from conversions
+// wxImage to/from conversions for Microwin
// ----------------------------------------------------------------------------
-#if wxUSE_IMAGE
+// Microwin versions are so different from normal ones that it really doesn't
+// make sense to use #ifdefs inside the function bodies
+#ifdef __WXMICROWIN__
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
+ // Set this to 1 to experiment with mask code,
+ // which currently doesn't work
+ #define USE_MASKS 0
m_refData = new wxBitmapRefData();
// 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());
::SelectObject(hMaskDC, hOldMaskBitmap);
::DeleteDC(hMaskDC);
- ((wxBitmapRefData*)m_refData)->m_bitmapMask = new wxMask((WXHBITMAP) hMaskBitmap);
+ ((wxBitmapRefData*)m_refData)->SetMask(hMaskBitmap);
}
SetWidth(image.GetWidth());
#endif // WXWIN_COMPATIBILITY_2
return TRUE;
-
-#else
- wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
-
- m_refData = new wxBitmapRefData();
-
- // sizeLimit is the MS upper limit for the DIB size
-#ifdef WIN32
- int sizeLimit = 1024*768*3;
-#else
- int sizeLimit = 0x7fff ;
-#endif
-
- // width and height of the device-dependent bitmap
- int width = image.GetWidth();
- int bmpHeight = image.GetHeight();
-
- // calc the number of bytes per scanline and padding
- int bytePerLine = width*3;
- int sizeDWORD = sizeof( DWORD );
- int lineBoundary = bytePerLine % sizeDWORD;
- int padding = 0;
- if( lineBoundary > 0 )
- {
- padding = sizeDWORD - lineBoundary;
- bytePerLine += padding;
- }
- // calc the number of DIBs and heights of DIBs
- int numDIB = 1;
- int hRemain = 0;
- int height = sizeLimit/bytePerLine;
- if( height >= bmpHeight )
- height = bmpHeight;
- else
- {
- numDIB = bmpHeight / height;
- hRemain = bmpHeight % height;
- if( hRemain >0 ) numDIB++;
- }
-
- // set bitmap parameters
- 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, FALSE, wxT("could not allocate memory for DIB header") );
- // Fill in the DIB header
- lpDIBh->bmiHeader.biSize = headersize;
- lpDIBh->bmiHeader.biWidth = (DWORD)width;
- lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
- lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
- // the general formula for biSizeImage:
- // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * 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 memory for DIB") );
- free( lpDIBh );
- return FALSE;
- }
-
- // create and set the device-dependent bitmap
- HDC hdc = ::GetDC(NULL);
- HDC memdc = ::CreateCompatibleDC( hdc );
- HBITMAP hbitmap;
- 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();
- int i, j, n;
- int origin = 0;
- unsigned char *ptdata = data;
- unsigned char *ptbits;
-
- for( n=0; n<numDIB; n++ )
- {
- if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
- {
- // redefine height and size of the (possibly) last smaller DIB
- // memory is not reallocated
- height = hRemain;
- lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
- lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
- }
- ptbits = lpBits;
-
- for( j=0; j<height; j++ )
- {
- for( i=0; i<width; i++ )
- {
- *(ptbits++) = *(ptdata+2);
- *(ptbits++) = *(ptdata+1);
- *(ptbits++) = *(ptdata );
- ptdata += 3;
- }
- for( i=0; i< padding; i++ ) *(ptbits++) = 0;
- }
- ::StretchDIBits( memdc, 0, origin, width, height,\
- 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
- origin += height;
- // if numDIB = 1, lines below can also be used
- // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
- // The above line is equivalent to the following two lines.
- // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
- // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
- // or the following lines
- // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
- // HDC memdc = ::CreateCompatibleDC( hdc );
- // ::SelectObject( memdc, hbitmap);
- // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
- // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
- // ::SelectObject( memdc, 0 );
- // ::DeleteDC( memdc );
- }
- 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() )
- {
- hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
- HGDIOBJ hbmpOld = ::SelectObject( memdc, hbitmap);
- if( numDIB == 1 ) height = bmpHeight;
- else height = sizeLimit/bytePerLine;
- lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
- lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
- origin = 0;
- unsigned char r = image.GetMaskRed();
- unsigned char g = image.GetMaskGreen();
- unsigned char b = image.GetMaskBlue();
- unsigned char zero = 0, one = 255;
- ptdata = data;
- for( n=0; n<numDIB; n++ )
- {
- if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
- {
- // redefine height and size of the (possibly) last smaller DIB
- // memory is not reallocated
- height = hRemain;
- lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
- lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
- }
- ptbits = lpBits;
- for( int j=0; j<height; j++ )
- {
- for(i=0; i<width; i++ )
- {
- // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
- unsigned char cr = (*(ptdata++)) ;
- unsigned char cg = (*(ptdata++)) ;
- unsigned char cb = (*(ptdata++)) ;
-
- if( ( cr !=r) || (cg!=g) || (cb!=b) )
- {
- *(ptbits++) = one;
- *(ptbits++) = one;
- *(ptbits++) = one;
- }
- else
- {
- *(ptbits++) = zero;
- *(ptbits++) = zero;
- *(ptbits++) = zero;
- }
- }
- for( i=0; i< padding; i++ ) *(ptbits++) = zero;
- }
- ::StretchDIBits( memdc, 0, origin, width, height,\
- 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
- origin += height;
- }
- // create a wxMask object
- wxMask *mask = new wxMask();
- mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
- SetMask( mask );
- // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
- /* The following can also be used but is slow to run
- wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
- wxMask *mask = new wxMask( *this, colour );
- SetMask( mask );
- */
-
- ::SelectObject( memdc, hbmpOld );
- }
-
- // free allocated resources
- ::DeleteDC( memdc );
- ::ReleaseDC(NULL, hdc);
- free(lpDIBh);
- free(lpBits);
-
-#if WXWIN_COMPATIBILITY_2
- // check the wxBitmap object
- GetBitmapData()->SetOk();
-#endif // WXWIN_COMPATIBILITY_2
-
- 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.
#endif // wxUSE_PALETTE
return image;
+}
+
+#endif // __WXMICROWIN__
+
+// ----------------------------------------------------------------------------
+// wxImage to/from conversions
+// ----------------------------------------------------------------------------
+
+bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
+{
+ wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
+
+ UnRef();
+
+ // first convert the image to DIB
+ const int h = image.GetHeight();
+ const int w = image.GetWidth();
+
+ wxDIB dib(image);
+ if ( !dib.IsOk() )
+ return FALSE;
+
+
+ // store the bitmap parameters
+ 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()"));
-#else // __MICROWIN__
+ return FALSE;
+ }
+
+#if wxUSE_PALETTE
+ if ( hOldPalette )
+ {
+ ::SelectPalette(hdcMem, hOldPalette, FALSE);
+ }
+#endif // wxUSE_PALETTE
+ }
+
+ // 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() )
+ {
+ SetMask(new wxMask(*this, wxColour(image.GetMaskRed(),
+ image.GetMaskGreen(),
+ image.GetMaskBlue())));
+ }
+
+ return TRUE;
+}
+wxImage wxBitmap::ConvertToImage() const
+{
wxImage image;
wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
free(lpBits);
return image;
-#endif
}
#endif // wxUSE_IMAGE
+// ----------------------------------------------------------------------------
+// loading and saving bitmaps
+// ----------------------------------------------------------------------------
+
bool wxBitmap::LoadFile(const wxString& filename, long type)
{
UnRef();
if ( !handler )
{
- wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type);
+ wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type);
return FALSE;
}
(rect.y+rect.height <= GetHeight()),
wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
- wxBitmap ret( rect.width, rect.height, GetDepth() );
+ wxBitmap ret( rect.width, rect.height );
wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
#ifndef __WXMICROWIN__
+ // TODO: copy alpha channel data if any
+
// copy bitmap data
- MemoryHDC dcSrc, dcDst;
+ MemoryHDC dcSrc,
+ dcDst;
{
SelectInHDC selectSrc(dcSrc, GetHbitmap()),
selectDst(dcDst, GetHbitmapOf(ret));
+ if ( !selectSrc || !selectDst )
+ {
+ wxLogLastError(_T("SelectObjct(hBitmap)"));
+ }
+
if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
dcSrc, rect.x, rect.y, SRCCOPY) )
{
// wxBitmap accessors
// ----------------------------------------------------------------------------
-void wxBitmap::SetQuality(int q)
+wxPalette* wxBitmap::GetPalette() const
{
- EnsureHasData();
+ return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
+ : (wxPalette *) NULL;
+}
- GetBitmapData()->m_quality = q;
+wxMask *wxBitmap::GetMask() const
+{
+ return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask *) NULL;
}
-#if WXWIN_COMPATIBILITY_2
-void wxBitmap::SetOk(bool isOk)
+wxDC *wxBitmap::GetSelectedInto() const
{
- EnsureHasData();
+ return GetBitmapData() ? GetBitmapData()->m_selectedInto : (wxDC *) NULL;
+}
- GetBitmapData()->m_ok = isOk;
+#if WXWIN_COMPATIBILITY_2_4
+
+int wxBitmap::GetQuality() const
+{
+ return 0;
+}
+
+#endif // WXWIN_COMPATIBILITY_2_4
+
+bool wxBitmap::HasAlpha() const
+{
+ return GetBitmapData() && GetBitmapData()->m_hasAlpha;
+}
+
+// ----------------------------------------------------------------------------
+// wxBitmap setters
+// ----------------------------------------------------------------------------
+
+void wxBitmap::SetSelectedInto(wxDC *dc)
+{
+ if ( GetBitmapData() )
+ GetBitmapData()->m_selectedInto = dc;
}
-#endif // WXWIN_COMPATIBILITY_2
#if wxUSE_PALETTE
{
EnsureHasData();
- GetBitmapData()->m_bitmapMask = mask;
+ 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
+// ----------------------------------------------------------------------------
+
// 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.