bitmap.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,776 行 · 第 1/4 页
CPP
1,776 行
for ( int rows = 0; rows < height; rows++ )
{
for ( size_t cols = 0; cols < bytesPerLine; cols++ )
{
unsigned char val = *src++;
unsigned char reversed = 0;
for ( int bits = 0; bits < 8; bits++)
{
reversed <<= 1;
reversed |= (unsigned char)(val & 0x01);
val >>= 1;
}
*dst++ = reversed;
}
if ( padding )
*dst++ = 0;
}
}
else
{
// bits should already be in Windows standard format
data = (char *)bits; // const_cast is harmless
}
HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
if ( !hbmp )
{
wxLogLastError(wxT("CreateBitmap"));
}
if ( data != bits )
{
free(data);
}
SetHBITMAP((WXHBITMAP)hbmp);
#endif
}
// Create from XPM data
bool wxBitmap::CreateFromXpm(const char **data)
{
#if wxUSE_IMAGE && wxUSE_XPM && wxUSE_WXDIB
Init();
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
wxUnusedVar(data);
return false;
#endif
}
wxBitmap::wxBitmap(int w, int h, int d)
{
Init();
(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();
(void)Create(data, type, width, height, depth);
}
wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
{
Init();
LoadFile(filename, (int)type);
}
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;
GetBitmapData()->m_width = w;
GetBitmapData()->m_height = h;
HBITMAP hbmp wxDUMMY_INITIALIZE(0);
#ifndef NEVER_USE_DIB
if ( wxShouldCreateDIB(w, h, d, hdc) )
{
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 // create a DDB
#endif // NEVER_USE_DIB
{
#ifndef ALWAYS_USE_DIB
#ifndef __WXMICROWIN__
if ( d > 0 )
{
hbmp = ::CreateBitmap(w, h, 1, d, NULL);
if ( !hbmp )
{
wxLogLastError(wxT("CreateBitmap"));
}
GetBitmapData()->m_depth = d;
}
else // d == 0, create bitmap compatible with the screen
#endif // !__WXMICROWIN__
{
ScreenHDC dc;
hbmp = ::CreateCompatibleBitmap(dc, w, h);
if ( !hbmp )
{
wxLogLastError(wxT("CreateCompatibleBitmap"));
}
GetBitmapData()->m_depth = wxDisplayDepth();
}
#endif // !ALWAYS_USE_DIB
}
SetHBITMAP((WXHBITMAP)hbmp);
return Ok();
}
#if wxUSE_IMAGE
// ----------------------------------------------------------------------------
// wxImage to/from conversions for Microwin
// ----------------------------------------------------------------------------
// 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, const wxDC& dc)
{
// 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);
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)->SetMask(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
return true;
}
wxImage wxBitmap::ConvertToImage() const
{
// 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;
}
#endif // __WXMICROWIN__
// ----------------------------------------------------------------------------
// wxImage to/from conversions
// ----------------------------------------------------------------------------
#if wxUSE_WXDIB
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)
{
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;
if (depth == -1)
depth = dib.GetDepth(); // Get depth from image if none specified
// store the bitmap parameters
wxBitmapRefData *refData = new wxBitmapRefData;
refData->m_width = w;
refData->m_height = h;
refData->m_hasAlpha = image.HasAlpha();
m_refData = refData;
// next either store DIB as is or create a DDB from it
HBITMAP hbitmap wxDUMMY_INITIALIZE(0);
// 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();
refData->m_isDIB = true;
refData->m_depth = depth;
}
#ifndef ALWAYS_USE_DIB
else // we need to convert DIB to DDB
{
hbitmap = dib.CreateDDB((HDC)hdc);
refData->m_depth = depth;
}
#endif // !ALWAYS_USE_DIB
// validate this object
SetHBITMAP((WXHBITMAP)hbitmap);
// finally also set the mask if we have one
if ( image.HasMask() )
{
const size_t len = 2*((w+15)/16);
BYTE *src = image.GetData();
BYTE *data = new BYTE[h*len];
memset(data, 0, h*len);
BYTE r = image.GetMaskRed(),
g = image.GetMaskGreen(),
b = image.GetMaskBlue();
BYTE *dst = data;
for ( int y = 0; y < h; y++, dst += len )
{
BYTE *dstLine = dst;
BYTE mask = 0x80;
for ( int x = 0; x < w; x++, src += 3 )
{
if (src[0] != r || src[1] != g || src[2] != b)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?