📄 cdib.cpp
字号:
pLogPal->palVersion = 0x300;
pLogPal->palNumEntries = nEntries;
::GetSystemPaletteEntries(hDC, 0, nEntries,
(LPPALETTEENTRY) ((LPBYTE) pLogPal + 2 * sizeof(WORD)));
m_hPalette = ::CreatePalette(pLogPal);
delete pLogPal;
return TRUE;
}
HBITMAP CDib::CreateBitmap(CDC* pDC)
{
if (m_dwSizeImage == 0) return NULL;
HBITMAP hBitmap = ::CreateDIBitmap(pDC->GetSafeHdc(), m_lpBMIH,
CBM_INIT, m_lpImage, (LPBITMAPINFO) m_lpBMIH, DIB_RGB_COLORS);
ASSERT(hBitmap);
return hBitmap;
}
BOOL CDib::Compress(CDC* pDC, BOOL bCompress /* = TRUE */)
{
// 1. makes GDI bitmap from existing DIB
// 2. makes a new DIB from GDI bitmap with compression
// 3. cleans up the original DIB
// 4. puts the new DIB in the object
if((m_lpBMIH->biBitCount != 4) && (m_lpBMIH->biBitCount != 8)) return FALSE;
// compression supported only for 4 bpp and 8 bpp DIBs
if(m_hBitmap) return FALSE; // can't compress a DIB Section!
TRACE("Compress: original palette size = %d\n", m_nColorTableEntries);
HDC hdc = pDC->GetSafeHdc();
HPALETTE hOldPalette = ::SelectPalette(hdc, m_hPalette, FALSE);
HBITMAP hBitmap; // temporary
if((hBitmap = CreateBitmap(pDC)) == NULL) return FALSE;
int nSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_nColorTableEntries;
LPBITMAPINFOHEADER lpBMIH = (LPBITMAPINFOHEADER) new char[nSize];
memcpy(lpBMIH, m_lpBMIH, nSize); // new header
if(bCompress) {
switch (lpBMIH->biBitCount) {
case 4:
lpBMIH->biCompression = BI_RLE4;
break;
case 8:
lpBMIH->biCompression = BI_RLE8;
break;
default:
ASSERT(FALSE);
}
// calls GetDIBits with null data pointer to get size of compressed DIB
if(!::GetDIBits(pDC->GetSafeHdc(), hBitmap, 0, (UINT) lpBMIH->biHeight,
NULL, (LPBITMAPINFO) lpBMIH, DIB_RGB_COLORS)) {
AfxMessageBox("Unable to compress this DIB");
// probably a problem with the color table
::DeleteObject(hBitmap);
delete [] lpBMIH;
::SelectPalette(hdc, hOldPalette, FALSE);
return FALSE;
}
if (lpBMIH->biSizeImage == 0) {
AfxMessageBox("Driver can't do compression");
::DeleteObject(hBitmap);
delete [] lpBMIH;
::SelectPalette(hdc, hOldPalette, FALSE);
return FALSE;
}
else {
m_dwSizeImage = lpBMIH->biSizeImage;
}
}
else {
lpBMIH->biCompression = BI_RGB; // decompress
// figure the image size from the bitmap width and height
DWORD dwBytes = ((DWORD) lpBMIH->biWidth * lpBMIH->biBitCount) / 32;
if(((DWORD) lpBMIH->biWidth * lpBMIH->biBitCount) % 32) {
dwBytes++;
}
dwBytes *= 4;
m_dwSizeImage = dwBytes * lpBMIH->biHeight; // no compression
lpBMIH->biSizeImage = m_dwSizeImage;
}
// second GetDIBits call to make DIB
LPBYTE lpImage = (LPBYTE) new char[m_dwSizeImage];
VERIFY(::GetDIBits(pDC->GetSafeHdc(), hBitmap, 0, (UINT) lpBMIH->biHeight,
lpImage, (LPBITMAPINFO) lpBMIH, DIB_RGB_COLORS));
TRACE("dib successfully created - height = %d\n", lpBMIH->biHeight);
::DeleteObject(hBitmap);
Empty();
m_nBmihAlloc = m_nImageAlloc = crtAlloc;
m_lpBMIH = lpBMIH;
m_lpImage = lpImage;
ComputePaletteSize(m_lpBMIH->biBitCount);
ComputeMetrics();
MakePalette();
::SelectPalette(hdc, hOldPalette, FALSE);
TRACE("Compress: new palette size = %d\n", m_nColorTableEntries);
return TRUE;
}
BOOL CDib::Read(CFile* pFile)
{
// 1. read file header to get size of info hdr + color table
// 2. read info hdr (to get image size) and color table
// 3. read image
// can't use bfSize in file header
Empty();
int nCount, nSize;
BITMAPFILEHEADER bmfh;
TRY {
nCount = pFile->Read((LPVOID) &bmfh, sizeof(BITMAPFILEHEADER));
if(nCount != sizeof(BITMAPFILEHEADER)) {
AfxMessageBox("read error 1");
return FALSE;
}
if(bmfh.bfType != 0x4d42) {
AfxMessageBox("Invalid bitmap file");
return FALSE;
}
nSize = bmfh.bfOffBits - sizeof(BITMAPFILEHEADER);
m_lpBMIH = (LPBITMAPINFOHEADER) new char[nSize];
m_nBmihAlloc = m_nImageAlloc = crtAlloc;
nCount = pFile->Read(m_lpBMIH, nSize); // info hdr & color table
ComputeMetrics();
m_lpImage = (LPBYTE) new char[m_dwSizeImage];
nCount = pFile->Read(m_lpImage, m_dwSizeImage); // image only
}
CATCH (CException, e) {
AfxMessageBox("Read error 1");
return FALSE;
}
END_CATCH
ComputePaletteSize(m_lpBMIH->biBitCount);
MakePalette();
return TRUE;
}
BOOL CDib::Write(CFile* pFile)
{
BITMAPFILEHEADER bmfh;
bmfh.bfType = 0x4d42; // 'BM'
int nSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_nColorTableEntries + m_dwSizeImage;
bmfh.bfSize = nSize + sizeof(BITMAPFILEHEADER);
// meaning of bfSize open to interpretation (bytes, words, dwords?) -- we won't use it
bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * m_nColorTableEntries;
TRY {
pFile->Write((LPVOID) &bmfh, sizeof(BITMAPFILEHEADER));
pFile->Write((LPVOID) m_lpBMIH, nSize);
}
CATCH (CException, e) {
AfxMessageBox("write error");
return FALSE;
}
END_CATCH
return TRUE;
}
void CDib::Serialize(CArchive& ar)
{
ar.Flush();
if(ar.IsStoring()) {
Write(ar.GetFile());
}
else {
Read(ar.GetFile());
}
}
// helper functions
void CDib::ComputePaletteSize(int nBitCount)
{
if(m_lpBMIH == 0 ||
m_lpBMIH->biClrUsed == 0) {
switch(nBitCount) {
case 1:
m_nColorTableEntries = 2;
break;
case 4:
m_nColorTableEntries = 16;
break;
case 8:
m_nColorTableEntries = 256;
break;
case 16:
case 24:
case 32:
m_nColorTableEntries = 0;
break;
default:
ASSERT(FALSE);
}
}
else {
m_nColorTableEntries = m_lpBMIH->biClrUsed;
}
ASSERT(m_nColorTableEntries <= 256);
}
void CDib::ComputeMetrics()
{
m_dwSizeImage = m_lpBMIH->biSizeImage;
if(m_dwSizeImage == 0) {
DWORD dwBytes = ((DWORD) m_lpBMIH->biWidth * m_lpBMIH->biBitCount) / 32;
if(((DWORD) m_lpBMIH->biWidth * m_lpBMIH->biBitCount) % 32) {
dwBytes++;
}
dwBytes *= 4;
m_dwSizeImage = dwBytes * m_lpBMIH->biHeight; // no compression
}
m_lpvColorTable = (LPBYTE) m_lpBMIH + sizeof(BITMAPINFOHEADER);
}
void CDib::Empty()
{
// this is supposed to clean up whatever is in the DIB
DetachMapFile();
if(m_nBmihAlloc == crtAlloc) {
delete [] m_lpBMIH;
}
else if(m_nBmihAlloc == heapAlloc) {
::GlobalUnlock(m_hGlobal);
::GlobalFree(m_hGlobal);
}
if(m_nImageAlloc == crtAlloc) delete [] m_lpImage;
if(m_hPalette != NULL) ::DeleteObject(m_hPalette);
if(m_hBitmap != NULL) ::DeleteObject(m_hBitmap);
m_nBmihAlloc = m_nImageAlloc = noAlloc;
m_hGlobal = NULL;
m_lpBMIH = NULL;
m_lpImage = NULL;
m_lpvColorTable = NULL;
m_nColorTableEntries = 0;
m_dwSizeImage = 0;
m_lpvFile = NULL;
m_hMap = NULL;
m_hFile = NULL;
m_hBitmap = NULL;
m_hPalette = NULL;
}
void CDib::DetachMapFile()
{
if(m_hFile == NULL) return;
::UnmapViewOfFile(m_lpvFile);
::CloseHandle(m_hMap);
::CloseHandle(m_hFile);
m_hFile = NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -