📄 dib.cpp
字号:
// Dib.cpp : implementation of the CDIB class
// written by Xu
////////////////////////////////////////////////
#include "stdafx.h"
#include "dib.h"
CDIB::CDIB()
{
m_hDrawDib=NULL;
m_pDIB=NULL;
m_Colors=0;
}
CDIB::~CDIB()
{
Close();
}
void CDIB:: Draw (CDC * pDC, int nWidth, int nHeight)
{
ASSERT (IsValid());
DrawDibRealize (m_hDrawDib, pDC->GetSafeHdc (), TRUE);
DrawDibDraw (m_hDrawDib, pDC->GetSafeHdc (),
0, // dest left
0, // dest top
nWidth,
nHeight,
(BITMAPINFOHEADER *) m_pDIB,
(LPVOID) GetBits (),
0, // source left
0, // source top
((BITMAPINFOHEADER * ) m_pDIB)->biWidth,
((BITMAPINFOHEADER * ) m_pDIB)->biHeight,
DDF_BACKGROUNDPAL);
}
CSize CDIB:: GetSize()
{
return CSize (((BITMAPINFOHEADER *) m_pDIB) ->biWidth,
((BITMAPINFOHEADER *) m_pDIB) ->biHeight);
}
void CDIB:: Close()
{
if (m_hDrawDib != NULL)
{
DrawDibClose (m_hDrawDib);
m_hDrawDib=NULL;
}
if (m_pDIB != NULL)
delete m_pDIB;
}
BOOL CDIB:: Open (const char * pzFileName)
{
BITMAPFILEHEADER bmpfh;
CFile file;
int nHeaderSize;
int i;
RGBQUAD quad;
RGBQUAD *quads;
BITMAPINFOHEADER * pDib;
Close ();
// DrawDibOpen initializes the DrawDib library and
// return a handle for all DrawDib operations
if (! (m_hDrawDib=DrawDibOpen ()))
goto fail;
// Open and read the DIB file header
nHeaderSize=sizeof (BITMAPFILEHEADER);
if (! file.Open (pzFileName, CFile:: modeRead | CFile:: typeBinary))
goto fail;
if (file.Read ( (void * ) &bmpfh, nHeaderSize) != (UINT) nHeaderSize)
goto fail;
// Validate the DIB file header by checking the first
// two characters for the signature " BM "
if (bmpfh.bfType != * ( (WORD *) "BM"))
goto fail;
FileSize=bmpfh.bfSize;
// Allocate a big chunk of global memory to store the DIB
m_pDIB= (BYTE *) new char [bmpfh.bfSize-nHeaderSize];
if (! m_pDIB)
goto fail;
// Read the DIB into the buffer 32K at a time using ReadHuge
file.ReadHuge (m_pDIB, bmpfh.bfSize-nHeaderSize);
pDib= (BITMAPINFOHEADER *) m_pDIB;
if ( ( (BITMAPINFOHEADER *) m_pDIB) ->biSizeImage==0)
{
// The application that created this bitmap didn't fill in
// the biSizeImage field (Photoshop 3.0x does this) . Let's
// fill it in even though the DrawDib * functions don't need it.
// Scan lines must be DWORD aligned, hence the strange bit stuff
pDib->biSizeImage=
((((pDib->biWidth * pDib->biBitCount)+31)&~31)>>3) * pDib->biHeight;
}
m_nBitcount=pDib->biBitCount;
if(pDib->biClrUsed!=0)
m_Colors=pDib->biClrUsed;
else if(pDib->biBitCount<=8)
m_Colors=(1<<pDib->biBitCount);
if(m_Colors>0&&m_Colors<=256)
{// 填入调色板数组索引
quads=(RGBQUAD*)(m_pDIB+sizeof(BITMAPINFOHEADER));
m_Quads.SetSize(m_Colors,-1);
for(i=0;i<m_Colors;i++)
{
quad.rgbRed=quads[i].rgbRed;
quad.rgbGreen=quads[i].rgbGreen;
quad.rgbBlue=quads[i].rgbBlue;
m_Quads[i]=quad;
}
}
file.Close ();
return TRUE;
fail:
Close ();
return FALSE;
}
BYTE * CDIB:: GetBits ()
{
// The size of the color map is determined by the number of
// RGBQUAD structures present. It also depends on the bit-depth
// of the DIB.
DWORD dwNumColors, dwColorTableSize;
BITMAPINFOHEADER * lpDib= (BITMAPINFOHEADER *) m_pDIB;
WORD wBitCount=lpDib->biBitCount;
if (lpDib->biSize>=36)
dwNumColors=lpDib->biClrUsed;
else
dwNumColors=0;
if (dwNumColors==0)
{
if (wBitCount !=24)
dwNumColors=1L<<wBitCount;
else
dwNumColors=0;
}
dwColorTableSize=dwNumColors * sizeof (RGBQUAD);
return m_pDIB+lpDib->biSize+dwColorTableSize;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -