www.pudn.com > mfcopentree.rar > MyBitmap.cpp


// MyBitmap.cpp: implementation of the CMyBitmap class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "mfcopen.h" 
#include "MyBitmap.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CMyBitmap::CMyBitmap(void) 
{ 
	data = 0; 
	width=height=0; 
} 
 
 
bool CMyBitmap::create(int x, int y) 
{	 
	width  = x; 
	height = y; 
	data = new unsigned char[(width*height)*3]; 
	 
	if(!data)  
		return false; 
	else 
		return true; 
} 
 
 
void CMyBitmap::getcolor(int x, int y, BYTE *r, BYTE *g, BYTE *b)  
{ 
	if((x < width) && (y < height)) { 
		*r = data[(x + (y*width))*3 + 0]; 
		*g = data[(x + (y*width))*3 + 1]; 
		*b = data[(x + (y*width))*3 + 2]; 
	} 
} 
 
 
void CMyBitmap::setcolor(int x, int y, BYTE r, BYTE g, BYTE b)  
{ 
	if((x < width) && (y < height)) { 
		data[(x + (y*width))*3 + 0] = r; 
		data[(x + (y*width))*3 + 1] = g; 
		data[(x + (y*width))*3 + 2] = b; 
	} 
} 
 
 
bool CMyBitmap::load(char *filename) 
{ 
	PAL color; 
	int x, y, i; 
	bmpFHEAD h1; 
	bmpIHEAD h2; 
	FILE *fp = fopen(filename, "rb"); 
 
	fread(&h1.bfType, sizeof(h1.bfType), 1, fp); 
	fread(&h1.bfSize, sizeof(h1.bfSize), 1, fp); 
	fread(&h1.bfReserved1, sizeof(h1.bfReserved1), 1, fp); 
	fread(&h1.bfReserved2, sizeof(h1.bfReserved2), 1, fp); 
	fread(&h1.bfOffBits, sizeof(h1.bfOffBits), 1, fp); 
 
	fread(&h2.biSize, sizeof(h2.biSize), 1, fp); 
	fread(&h2.biWidth, sizeof(h2.biWidth), 1, fp); 
	fread(&h2.biHeight, sizeof(h2.biHeight), 1, fp); 
	fread(&h2.biPlanes, sizeof(h2.biPlanes), 1, fp); 
	fread(&h2.biBitCount, sizeof(h2.biBitCount), 1, fp); 
	fread(&h2.biCompression, sizeof(h2.biCompression), 1, fp); 
	fread(&h2.biSizeImage, sizeof(h2.biSizeImage), 1, fp); 
	fread(&h2.biXPelsPerMeter, sizeof(h2.biXPelsPerMeter), 1, fp); 
	fread(&h2.biYPelsPerMeter, sizeof(h2.biYPelsPerMeter), 1, fp); 
	fread(&h2.biClrUsed, sizeof(h2.biClrUsed), 1, fp); 
	fread(&h2.biClrImportant, sizeof(h2.biClrImportant), 1, fp); 
 
	width  = h2.biWidth; 
	height = h2.biHeight; 
	int w = width*3; 
	width = (w+3)&~3;//~3==0, x&~3一定能被4整除! 
	data = new unsigned char[(width*height)*3]; 
 
	if(!data)  
		return false; 
 
	i = 0; 
 
	for(x=0; x