www.gusucode.com > VC++位图操作相关源码-源码程序 > VC++位图操作相关源码-源码程序/code/chap1_4View.cpp

    //Download by http://www.NewXing.com
// chap1_4View.cpp : implementation of the CChap1_4View class
//

#include "stdafx.h"
#include "chap1_4.h"

#include "chap1_4Doc.h"
#include "chap1_4View.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChap1_4View

IMPLEMENT_DYNCREATE(CChap1_4View, CView)

BEGIN_MESSAGE_MAP(CChap1_4View, CView)
	//{{AFX_MSG_MAP(CChap1_4View)
	ON_COMMAND(id_DataAccess, OnDataAccess)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChap1_4View construction/destruction

CChap1_4View::CChap1_4View()
{
	// TODO: add construction code here

}

CChap1_4View::~CChap1_4View()
{
}

BOOL CChap1_4View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CChap1_4View drawing

void CChap1_4View::OnDraw(CDC* pDC)
{
	//获取文档类句柄
	CChap1_4Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if(pDoc->m_pDib==NULL)
		return;

	//定义infoHead变量指向DIB中的BITMAPINFOHEADER结构
	BITMAPINFOHEADER *infoHead=(BITMAPINFOHEADER*)pDoc->m_pDib;

	//获取DIB的宽、高、所占位数
	int width=infoHead->biWidth;
	int height=infoHead->biHeight;
	int biBitCount=infoHead->biBitCount;

	//求颜色表的长度,彩色图像颜色表长度为0,非彩色图像(灰度图像)
	// 颜色表长度为pow(2,biBitCount)
	int colorTableLng;
	if(biBitCount!=24)
		colorTableLng=pow(2,biBitCount);
	else
		colorTableLng=0;

	//如果有颜色表,则创建调色板,hPalette为新创建的调色板句柄,
	//hOldPal旧的调色板句柄
	HPALETTE hPalette=0, hOldPal;
	if(colorTableLng!=0){

		//定义颜色表指针pColorTable,指向DIB的颜色表
		RGBQUAD *pColorTable=(RGBQUAD *)(pDoc->m_pDib+
			sizeof(BITMAPINFOHEADER));

		//申请空间,生成LOGPALETTE结构
		LPLOGPALETTE pLogPal = (LPLOGPALETTE)new char[2*sizeof(WORD)
			+colorTableLng * sizeof(PALETTEENTRY)];
		pLogPal->palVersion = 0x300;
		pLogPal->palNumEntries =colorTableLng;
		for(int i = 0; i < colorTableLng; i++) {
			pLogPal->palPalEntry[i].peRed = pColorTable[i].rgbRed;
			pLogPal->palPalEntry[i].peGreen =pColorTable[i].rgbGreen;
			pLogPal->palPalEntry[i].peBlue = pColorTable[i].rgbBlue;
			pLogPal->palPalEntry[i].peFlags = 0;
		}

		//创建逻辑调色板
		hPalette = ::CreatePalette(pLogPal);

		//将调色板选入系统
		hOldPal=::SelectPalette(pDC->GetSafeHdc(), hPalette, TRUE);

		//实现调色板
		pDC->RealizePalette();

		//清理空间
		delete []pLogPal;
	}

	//DIB显示所需要的模式
	pDC->SetStretchBltMode(COLORONCOLOR);

	//pImgData指向DIB的位图数据
	unsigned char* pImgData=(unsigned char*)(pDoc->m_pDib+
		sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD) * colorTableLng);

	//显示DIB到显示器
	::StretchDIBits(pDC->GetSafeHdc(), 0, 0, width, height,
		0, 0,  width, height, pImgData,
		 (LPBITMAPINFO)(pDoc->m_pDib), DIB_RGB_COLORS, SRCCOPY);

	//恢复原调色板
	if(hOldPal!=NULL)
		::SelectPalette(pDC->GetSafeHdc(), hOldPal, TRUE);
}

/////////////////////////////////////////////////////////////////////////////
// CChap1_4View diagnostics

#ifdef _DEBUG
void CChap1_4View::AssertValid() const
{
	CView::AssertValid();
}

void CChap1_4View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CChap1_4Doc* CChap1_4View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CChap1_4Doc)));
	return (CChap1_4Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChap1_4View message handlers

void CChap1_4View::OnDataAccess() 
{
		//获取文档类句柄
	CChap1_4Doc* pDoc = GetDocument();

	//如果DIB为空,则返回
	if(pDoc->m_pDib==NULL)
		return;

	//定义infoHead变量指向DIB中的BITMAPINFOHEADER结构
	BITMAPINFOHEADER *infoHead=(BITMAPINFOHEADER*)pDoc->m_pDib;

	//获取DIB的宽、高、所占位数
	int width=infoHead->biWidth;
	int height=infoHead->biHeight;
	int biBitCount=infoHead->biBitCount;

	//每行像素所占字节数,必须是4的倍数
	int lineByte=(width*biBitCount/8+3)/4*4;

	//求颜色表的长度,彩色图像颜色表长度为0,非彩色图像(灰度图像)
	// 颜色表长度为pow(2,biBitCount)
	int colorTableLng;
	if(biBitCount!=24)
		colorTableLng=pow(2,biBitCount);
	else
		colorTableLng=0;

	//pImgData指向DIB的位图数据
	unsigned char* pImgData=(unsigned char*)(pDoc->m_pDib+
		sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD) * colorTableLng);

	//以下将图像数据左下角1/4置成黑色
	//循环变量,图像的坐标
	int i, j;
	if(biBitCount==8){//灰度图像
		for(i=0;i<height/2;i++){
			for(j=0;j<width/2;j++){
				*(pImgData+i*lineByte+j)=0;
			}
		}
	}
	else{//彩色图像
		int k;
		for(i=0;i<height/2;i++){
			for(j=0;j<width/2;j++){
				for(k=0;k<3;k++)//彩色图像,每像素三个分量都置0
					*(pImgData+i*lineByte+j*3+k)=0;
			}
		}
	}
	
	//刷新显示
	Invalidate();
}