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<width; x++)  
		for(y=0; y<height; y++)  
		{ 
			fread(&color, sizeof(color), 1, fp); 
			data[i+0] = color.b; 
			data[i+1] = color.g; 
			data[i+2] = color.r; 
			i += 3; 
    	} 
 
	fclose(fp); 
 
	if(!data) 
		return false; 
	else 
		return true; 
} 
 
 
bool CMyBitmap::save(char *filename) 
{ 
	bmpFHEAD filehead;					//File Header 
	bmpIHEAD infohead;					//Info Header 
	bmpPAL color; 
	long bitsize;						//Size of the bitmap 
	int i=0; 
	FILE *fp = fopen(filename, "wb"); 
 
	if (!data || !fp)					//If there's no data nor a file, don't even start writing 
		{ fclose (fp); return false; } 
 
	int w = width*3; 
	w = (w+3)&~3; 
	bitsize = w*height; 
//	bitsize = (width * 24 + 7) / 8 * abs(height); 
//	bitsize = (128*24+7)/8*128;   
	//FILEHEADER 
	filehead.bfType      = 'MB'; 
    filehead.bfSize      = sizeof(bmpFHEAD) + sizeof(bmpIHEAD) + bitsize; 
    filehead.bfReserved1 = 0; 
    filehead.bfReserved2 = 0; 
    filehead.bfOffBits   = sizeof(bmpFHEAD) + sizeof(bmpIHEAD); 
 
    fwrite(&filehead.bfType, sizeof(filehead.bfType), 1, fp); 
	fwrite(&filehead.bfSize, sizeof(filehead.bfSize), 1, fp); 
	fwrite(&filehead.bfReserved1, sizeof(filehead.bfReserved1), 1, fp); 
	fwrite(&filehead.bfReserved2, sizeof(filehead.bfReserved2), 1, fp); 
	fwrite(&filehead.bfOffBits, sizeof(filehead.bfOffBits), 1, fp); 
	//FILEHEADER DONE 
 
	//HEADER 
	infohead.biSize			= sizeof (bmpIHEAD); 
	infohead.biWidth		= width; 
	infohead.biHeight		= height; 
	infohead.biPlanes		= 1; 
	infohead.biBitCount		= 24;			//24bit 
	infohead.biCompression	= 0;			//RGB 
	infohead.biSizeImage	= bitsize; 
	infohead.biXPelsPerMeter= 2952; 
	infohead.biYPelsPerMeter= 2952; 
	infohead.biClrUsed		= 0; 
	infohead.biClrImportant = 0; 
 
	fwrite(&infohead.biSize, sizeof(infohead.biSize), 1, fp); 
	fwrite(&infohead.biWidth, sizeof(infohead.biWidth), 1, fp); 
	fwrite(&infohead.biHeight, sizeof(infohead.biHeight), 1, fp); 
	fwrite(&infohead.biPlanes, sizeof(infohead.biPlanes), 1, fp); 
	fwrite(&infohead.biBitCount, sizeof(infohead.biBitCount), 1, fp); 
	fwrite(&infohead.biCompression, sizeof(infohead.biCompression), 1, fp); 
	fwrite(&infohead.biSizeImage, sizeof(infohead.biSizeImage), 1, fp); 
	fwrite(&infohead.biXPelsPerMeter, sizeof(infohead.biXPelsPerMeter), 1, fp); 
	fwrite(&infohead.biYPelsPerMeter, sizeof(infohead.biYPelsPerMeter), 1, fp); 
	fwrite(&infohead.biClrUsed, sizeof(infohead.biClrUsed), 1, fp); 
	fwrite(&infohead.biClrImportant, sizeof(infohead.biClrImportant), 1, fp); 
	//HEADER DONE 
 
 
	//BITMAP 
	for(int y=0; y<height; y++)  
		for(int x=0; x<width; x++)  
		{ 
			getcolor(x, y, &color.g, &color.r, &color.b); //Dunno why, but it works!&color.g, &color.r, &color.b 
			fwrite (&color, sizeof (color), 1, fp); 
    	} 
	//BITMAP DONE 
	 
	fclose (fp); 
 
	return true; 
} 
 
 
CMyBitmap::~CMyBitmap(void) 
{ 
	if (data)  
		delete data; 
}