/*---------------------------------------------------------------------------------
dibkernel.cpp, version 1.0, June 1997
Ren� Dencker Eriksen (http://www.imada.ou.dk/~edr) and
Hans-Martin Br�ndmose Jensen (http://www.mip.ou.dk/~hansm)
---------------------------------------------------------------------------------*/
#include "stdafx.h" //standard precompiled headers
#include "dibkernel.h"
//default color table for 8 bit color depth
const BYTE def256pal_r[256] = {0,128,0,128,0,128,0,192,192,166,64,96,128,160,192,
224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,
192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,
160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,
128,160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,192,224,0,32,64,
96,128,160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,192,224,0,32,
64,96,128,160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,192,224,0,
32,64,96,128,160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,192,
224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,
192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,
160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,128,160,192,224,0,32,64,96,
128,160,255,160,128,255,0,255,0,255,0,255};
const BYTE def256pal_g[256] = {0,0,128,128,0,0,128,192,220,202,32,32,32,32,32,32,
64,64,64,64,64,64,64,64,96,96,96,96,96,96,96,96,128,128,128,128,128,128,128,
128,160,160,160,160,160,160,160,160,192,192,192,192,192,192,192,192,224,224,
224,224,224,224,224,224,0,0,0,0,0,0,0,0,32,32,32,32,32,32,32,32,64,64,64,64,
64,64,64,64,96,96,96,96,96,96,96,96,128,128,128,128,128,128,128,128,160,160,
160,160,160,160,160,160,192,192,192,192,192,192,192,192,224,224,224,224,224,
224,224,224,0,0,0,0,0,0,0,0,32,32,32,32,32,32,32,32,64,64,64,64,64,64,64,64,
96,96,96,96,96,96,96,96,128,128,128,128,128,128,128,128,160,160,160,160,160,
160,160,160,192,192,192,192,192,192,192,192,224,224,224,224,224,224,224,224,
0,0,0,0,0,0,0,0,32,32,32,32,32,32,32,32,64,64,64,64,64,64,64,64,96,96,96,96,
96,96,96,96,128,128,128,128,128,128,128,128,160,160,160,160,160,160,160,160,
192,192,192,192,192,192,251,160,128,0,255,255,0,0,255,255};
const BYTE def256pal_b[256] = {0,0,0,0,128,128,128,192,192,240,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,128,128,128,128,128,128,128,
128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,
128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,
128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,
192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,
192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,
192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,192,240,164,128,
0,0,0,255,255,255,255};
IMPLEMENT_SERIAL(CDib,CObject,0);
CDib::CDib()
{
m_pntOrigo = CPoint(0,0); //default origo
m_lpBMIH = NULL;
m_lpImage = NULL;
m_hPalette = NULL;
Empty();
}
CDib::CDib(CSize size, int nBitCount, BOOL bInitialize)
{
m_pntOrigo = CPoint(0,0); //default origo
m_hPalette = NULL;
m_lpBMIH = NULL;
m_lpImage = NULL;
Empty();
ComputePaletteSize(nBitCount);
m_lpBMIH = (LPBITMAPINFOHEADER) new
char[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*m_nColorTableEntries];
m_lpBMIH->biSize = sizeof(BITMAPINFOHEADER);
m_lpBMIH->biWidth = size.cx;
m_lpBMIH->biHeight = size.cy;
m_lpBMIH->biPlanes = 1;
m_lpBMIH->biBitCount = nBitCount;
m_lpBMIH->biCompression = BI_RGB;
m_lpBMIH->biSizeImage = 0;
m_lpBMIH->biXPelsPerMeter = 0;
m_lpBMIH->biYPelsPerMeter = 0;
m_lpBMIH->biClrUsed = m_nColorTableEntries;
m_lpBMIH->biClrImportant = m_nColorTableEntries;
ComputeMetrics();
m_lpImage=(LPBYTE) new char[m_dwSizeImage];
if (bInitialize) {
//make default palette from def256pal_x constants
int i;
switch (nBitCount) {
case 1:
SetPaletteEntry(0,0); //black
SetPaletteEntry(1,RGB(255,255,255)); //white
break;
case 8:
for (i=0; i<=255; i++) {
SetPaletteEntry(i,RGB(def256pal_r[i],def256pal_g[i],def256pal_b[i]));
}
break;
case 24:
break;
}
memset(m_lpImage, 0, m_dwSizeImage); //initialize image bits to zero
}
MakePalette();
}
CDib::CDib(const CDib& dib,CRect rect)
//copy area of interest from dib
{
int headerSize = sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*dib.m_nColorTableEntries;
//copy header
m_lpBMIH = (LPBITMAPINFOHEADER) new char[headerSize];
memcpy(m_lpBMIH,dib.m_lpBMIH,headerSize);
//correct size
m_lpBMIH->biWidth=rect.Width();
m_lpBMIH->biHeight=rect.Height();
m_lpBMIH->biSizeImage = 0;
//compute member variables
ComputeMetrics();
ComputePaletteSize(dib.m_lpBMIH->biBitCount);
MakePalette();
m_pntOrigo = CPoint(0,0); //default origo
//copy bits
m_lpImage = (LPBYTE)new char[m_dwSizeImage];
for(int y=rect.top; y<=rect.bottom; y++){
for(int x=rect.left; x<=rect.right; x++){
SetPixel(CPoint(x-rect.left,y-rect.top),dib.GetPixel(CPoint(x,y)));
}
}
}
CDib& CDib::operator=(const CDib& dib)
{
ASSERT(this != &dib); //beware of dib=dib
if (dib.m_lpBMIH) {
Empty(); //clear left operand (dib we are coping to)
int headerSize = sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*dib.m_nColorTableEntries;
//copy header
m_lpBMIH = (LPBITMAPINFOHEADER) new char[headerSize];
memcpy(m_lpBMIH,dib.m_lpBMIH,headerSize);
//copy member variables
ComputeMetrics();
ComputePaletteSize(dib.m_lpBMIH->biBitCount);
MakePalette();
m_pntOrigo = dib.m_pntOrigo;
//copy bits
m_lpImage = (LPBYTE)new char[dib.m_dwSizeImage];
memcpy(m_lpImage,dib.m_lpImage,dib.m_dwSizeImage);
}
return *this;
}
CDib::~CDib()
{
Empty(); //clean up memory
}
CSize CDib::GetDimensions()
{
if(m_lpBMIH == NULL) return CSize(0, 0);
return CSize((int) m_lpBMIH->biWidth, (int) m_lpBMIH->biHeight);
}
BOOL CDib::Draw(CDC* pDC,CRect rcDest,CPoint pntSrc)
/*-----------------------------------------------------------------------------
pDC: Device context pointer to do output to.
rcDest: Rectangle on DC to do output to.
pntSrc: Coordinate of the lower-left corner of the DIB to output into rcDest.
-----------------------------------------------------------------------------*/
{
/* Check for valid DIB handle */
if ((m_lpBMIH == NULL) /*|| (m_hPalette == NULL)*/) return FALSE;
BOOL bSuccess=FALSE; //Success/fail flag
HPALETTE hOldPal=NULL; //Previous palette (our DIB's palette is m_hPalette)
CSize dibsize = GetDimensions(); //Get DIB's dimensions
// Select as background since we have already realized in forground if needed
hOldPal = ::SelectPalette(pDC->GetSafeHdc(), m_hPalette, TRUE);
if (pDC->IsPrinting()) // printer DC
{
// get size of printer page (in pixels)
int cxPage = pDC->GetDeviceCaps(HORZRES);
int cyPage = pDC->GetDeviceCaps(VERTRES);
// get printer pixels per inch
int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
// Best Fit case -- create a rectangle which preserves
// the DIB's aspect ratio, and fills the page horizontally.
//
// The formula in the "->bottom" field below calculates the Y
// position of the printed bitmap, based on the size of the
// bitmap, the width of the page, and the relative size of
// a printed pixel (cyInch / cxInch).
rcDest.top = rcDest.left = 0;
rcDest.bottom = (int)(((double)dibsize.cy*cxPage*cyInch)
/((double)dibsize.cx*cxInch));
rcDest.right = cxPage;
没有合适的资源?快使用搜索试试~ 我知道了~
dibkernel10.zip
共2个文件
h:1个
cpp:1个
5星 · 超过95%的资源 需积分: 9 21 下载量 144 浏览量
2008-12-31
15:23:01
上传
评论
收藏 6KB ZIP 举报
温馨提示
这个方法比较烦琐,并且只能显示bmp,但它不依赖特定的系统,也比较实用。这个类在MSDN提供的例子中有,名字为ex10c.dsw,找到cdib.cpp和cdib.h,加入你的工程即可。
资源详情
资源评论
资源推荐
收起资源包目录
dibkernel10.zip (2个子文件)
dibkernel.h 3KB
dibkernel.cpp 19KB
共 2 条
- 1
WHJ500
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1