bitmap.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,776 行 · 第 1/4 页
CPP
1,776 行
if ( !GetBitmapData()->m_isDIB )
{
wxDIB *dib = GetBitmapData()->m_dib;
GetBitmapData()->m_dib = NULL;
// TODO: convert
delete dib;
}
#endif // wxUSE_WXDIB
}
#endif // #ifdef wxHAVE_RAW_BITMAP
// ----------------------------------------------------------------------------
// wxMask
// ----------------------------------------------------------------------------
wxMask::wxMask()
{
m_maskBitmap = 0;
}
// Construct a mask from a bitmap and a colour indicating
// the transparent area
wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
{
m_maskBitmap = 0;
Create(bitmap, colour);
}
// Construct a mask from a bitmap and a palette index indicating
// the transparent area
wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
{
m_maskBitmap = 0;
Create(bitmap, paletteIndex);
}
// Construct a mask from a mono bitmap (copies the bitmap).
wxMask::wxMask(const wxBitmap& bitmap)
{
m_maskBitmap = 0;
Create(bitmap);
}
wxMask::~wxMask()
{
if ( m_maskBitmap )
::DeleteObject((HBITMAP) m_maskBitmap);
}
// 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") );
if ( m_maskBitmap )
{
::DeleteObject((HBITMAP) m_maskBitmap);
m_maskBitmap = 0;
}
m_maskBitmap = (WXHBITMAP) CreateBitmap(
bitmap.GetWidth(),
bitmap.GetHeight(),
1, 1, 0
);
HDC srcDC = CreateCompatibleDC(0);
SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
HDC destDC = CreateCompatibleDC(0);
SelectObject(destDC, (HBITMAP) m_maskBitmap);
BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
SelectObject(srcDC, 0);
DeleteDC(srcDC);
SelectObject(destDC, 0);
DeleteDC(destDC);
return true;
#else
wxUnusedVar(bitmap);
return false;
#endif
}
// Create a mask from a bitmap and a palette index indicating
// the transparent area
bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
{
if ( m_maskBitmap )
{
::DeleteObject((HBITMAP) m_maskBitmap);
m_maskBitmap = 0;
}
#if wxUSE_PALETTE
if (bitmap.Ok() && bitmap.GetPalette()->Ok())
{
unsigned char red, green, blue;
if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
{
wxColour transparentColour(red, green, blue);
return Create(bitmap, transparentColour);
}
}
#endif // wxUSE_PALETTE
return false;
}
// Create a mask from a bitmap and a colour indicating
// 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 )
{
::DeleteObject((HBITMAP) m_maskBitmap);
m_maskBitmap = 0;
}
int width = bitmap.GetWidth(),
height = bitmap.GetHeight();
// scan the bitmap for the transparent colour and set the corresponding
// pixels in the mask to BLACK and the rest to WHITE
COLORREF maskColour = wxColourToPalRGB(colour);
m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
HDC srcDC = ::CreateCompatibleDC(NULL);
HDC destDC = ::CreateCompatibleDC(NULL);
if ( !srcDC || !destDC )
{
wxLogLastError(wxT("CreateCompatibleDC"));
}
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 )
{
wxLogLastError(wxT("SelectObject"));
ok = false;
}
HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
if ( !hbmpDstOld )
{
wxLogLastError(wxT("SelectObject"));
ok = false;
}
if ( ok )
{
// 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(srcDC);
::SelectObject(destDC, hbmpDstOld);
::DeleteDC(destDC);
return ok;
#else // __WXMICROWIN__
wxUnusedVar(bitmap);
wxUnusedVar(colour);
return false;
#endif // __WXMICROWIN__/!__WXMICROWIN__
}
// ----------------------------------------------------------------------------
// wxBitmapHandler
// ----------------------------------------------------------------------------
bool wxBitmapHandler::Create(wxGDIImage *image,
void *data,
long flags,
int width, int height, int depth)
{
wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
return bitmap ? Create(bitmap, data, flags, width, height, depth) : false;
}
bool wxBitmapHandler::Load(wxGDIImage *image,
const wxString& name,
long flags,
int width, int height)
{
wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
return bitmap ? LoadFile(bitmap, name, flags, width, height) : false;
}
bool wxBitmapHandler::Save(wxGDIImage *image,
const wxString& name,
int type)
{
wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
return bitmap ? SaveFile(bitmap, name, type) : false;
}
bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
void *WXUNUSED(data),
long WXUNUSED(type),
int WXUNUSED(width),
int WXUNUSED(height),
int WXUNUSED(depth))
{
return false;
}
bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
const wxString& WXUNUSED(name),
long WXUNUSED(type),
int WXUNUSED(desiredWidth),
int WXUNUSED(desiredHeight))
{
return false;
}
bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
const wxString& WXUNUSED(name),
int WXUNUSED(type),
const wxPalette *WXUNUSED(palette))
{
return false;
}
// ----------------------------------------------------------------------------
// DIB functions
// ----------------------------------------------------------------------------
#ifndef __WXMICROWIN__
bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
{
unsigned long i, headerSize;
// Allocate space for a DIB header
headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
LPBITMAPINFO lpDIBheader = (BITMAPINFO *) malloc(headerSize);
LPPALETTEENTRY lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
GetPaletteEntries(hPal, 0, 256, lpPe);
memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
// Fill in the static parts of the DIB header
lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpDIBheader->bmiHeader.biWidth = xSize;
lpDIBheader->bmiHeader.biHeight = ySize;
lpDIBheader->bmiHeader.biPlanes = 1;
// this value must be 1, 4, 8 or 24 so PixelDepth can only be
lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
lpDIBheader->bmiHeader.biCompression = BI_RGB;
lpDIBheader->bmiHeader.biSizeImage = (xSize * abs(ySize) * bitsPerPixel) >> 3;
lpDIBheader->bmiHeader.biClrUsed = 256;
// Initialize the DIB palette
for (i = 0; i < 256; i++) {
lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
}
*lpDIBHeader = lpDIBheader;
return true;
}
void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
{
free(lpDIBHeader);
}
#endif
// ----------------------------------------------------------------------------
// global helper functions implemented here
// ----------------------------------------------------------------------------
// 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;
}
if ( bmp.HasAlpha() )
{
// Create an empty mask bitmap.
// it doesn't seem to work if we mess with the mask at all.
HBITMAP hMonoBitmap = CreateBitmap(bmp.GetWidth(),bmp.GetHeight(),1,1,NULL);
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 = hMonoBitmap;
iconInfo.hbmColor = GetHbitmapOf(bmp);
HICON hicon = ::CreateIconIndirect(&iconInfo);
::DeleteObject(hMonoBitmap);
return hicon;
}
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() && !bmp.HasAlpha() )
{
// 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") );
// get width/height from the bitmap if not given
if ( !w || !h )
{
BITMAP bm;
::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
w = bm.bmWidth;
h = bm.bmHeight;
}
HDC hdcSrc = ::CreateCompatibleDC(NULL);
HDC hdcDst = ::CreateCompatibleDC(NULL);
if ( !hdcSrc || !hdcDst )
{
wxLogLastError(wxT("CreateCompatibleDC"));
}
HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
if ( !hbmpInvMask )
{
wxLogLastError(wxT("CreateBitmap"));
}
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);
return hbmpInvMask;
#else
return 0;
#endif
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?