📄 bitmap.cpp
字号:
//////////////////////////////////////////////////////////////////////
// bitmap.cpp by Tobias Franke (webmaster@cyberhead.de) 2001 //
//////////////////////////////////////////////////////////////////////
#include "bitmap.h"
bitmap::bitmap(void)
{
data = 0;
width=height=0;
}
bool bitmap::create(int x, int y)
{
width = x;
height = y;
data = new unsigned char[(width*height)*3];
if(!data)
return false;
else
return true;
}
void bitmap::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 bitmap::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 bitmap::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;
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 bitmap::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; }
bitsize = (width * 24 + 7) / 8 * abs(height);
//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!
fwrite (&color, sizeof (color), 1, fp);
}
//BITMAP DONE
fclose (fp);
return true;
}
bitmap::~bitmap(void)
{
if (data)
delete data;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -