📄 bmpfile.cpp
字号:
// bmpfile.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "bmpfile.h"
HXLBMPFILE::HXLBMPFILE()
{
Imagedata=NULL;
for (int i = 0; i< 256;i++)
{
palette [i].rgbBlue =
palette [i].rgbGreen =
palette [i].rgbRed = i;
palette [i].rgbReserved = 0;
}
iRGBnum = 0;
imagew = imageh = 0;
}
HXLBMPFILE::~HXLBMPFILE()
{
if (Imagedata) delete Imagedata;
}
UCHAR *HXLBMPFILE::GetByteatH(int height, int RGB)
{
if (iRGBnum <= RGB) return NULL;
int w=imagew *height + RGB * imagew * imageh ;
return Imagedata+w;
}
BOOL HXLBMPFILE::AllocateMem()
{
int w = imagew * imageh * iRGBnum;
if (Imagedata)
{
delete Imagedata;Imagedata=NULL;
}
Imagedata=new BYTE [w];
if (Imagedata) memset(Imagedata,0,w);
return (Imagedata!=NULL);
}
BOOL HXLBMPFILE::LoadBMPFILE (char *cFilename)
{
FILE *f;
if (strlen(cFilename)<1) return FALSE;
f=fopen(cFilename,"r+b");
if (f==NULL) return FALSE;
BITMAPFILEHEADER fh;
BITMAPINFOHEADER ih;
fread(&fh,sizeof(BITMAPFILEHEADER),1,f);
if (fh.bfType!=0x4d42) {fclose(f);return FALSE;}//"BM"
fread (&ih,sizeof(BITMAPINFOHEADER),1,f);
if ( (ih.biBitCount != 8)&&(ih.biBitCount != 24) )
{
fclose (f);
return FALSE;
}
iRGBnum = ih.biBitCount/8;
imagew = ih.biWidth ;
imageh = ih.biHeight ;
if(!AllocateMem()) {fclose (f);return FALSE;}
if ( iRGBnum == 1) fread (palette,sizeof(RGBQUAD),256,f);
fseek(f,fh.bfOffBits,SEEK_SET);
int w4b = (imagew * iRGBnum + 3 )/4 *4, i, j;
BYTE *ptr;
if (iRGBnum == 1)
{
ptr = new BYTE [10];
w4b -= imagew;
for (i=imageh -1; i>=0; i--)
{
fread(GetByteatH (i),imagew ,1,f);
if (w4b > 0) fread(ptr,w4b,1,f);
}
delete ptr;
}
if ( iRGBnum == 3)
{
ptr = new BYTE [ w4b];
for ( i = imageh - 1; i >= 0; i--)
{
fread(ptr,w4b,1,f);
for ( j = 0; j < imagew; j++)
{
GetByteatH(i,0)[j] = ptr[j*3 + 0];
GetByteatH(i,1)[j] = ptr[j*3 + 1];
GetByteatH(i,2)[j] = ptr[j*3 + 2];
}
}
delete ptr;
}
fclose(f);
return TRUE;
}
BOOL HXLBMPFILE::SaveBMPFILE (char *cFilename)
{
if (!Imagedata) return FALSE;
FILE *f;
if (strlen(cFilename)<1) return FALSE;
f=fopen(cFilename,"w+b");
if (f==NULL) return FALSE;
BITMAPFILEHEADER fh;
BITMAPINFOHEADER ih;
memset(&ih,0,sizeof(BITMAPINFOHEADER));
fh.bfType = 0x4d42;
fh.bfReserved1 = 0;
fh.bfReserved2 = 0;
fh.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) +( (iRGBnum == 1)?256*sizeof(RGBQUAD):0);
ih.biWidth = imagew;
ih.biHeight = imageh;
ih.biSize = 40;
ih.biPlanes = 1;
ih.biBitCount = 8 * iRGBnum;
int w4b = (imagew*iRGBnum +3)/4*4;
ih.biSizeImage = fh.bfOffBits + ih.biHeight *w4b;
fwrite(&fh,sizeof(BITMAPFILEHEADER),1,f);
fwrite(&ih,sizeof(BITMAPINFOHEADER),1,f);
if ( iRGBnum == 1) fwrite(palette,sizeof(RGBQUAD),256,f);
BYTE* ptr;
int i,j;
if (iRGBnum == 1)
{
ptr = new BYTE [10];
memset(ptr,0,10);
w4b -= ih.biWidth ;
for ( i=ih.biHeight -1;i>=0;i--)
{
fwrite(GetByteatH(i),ih.biWidth,1,f);
if (w4b>0) fwrite(ptr,w4b,1,f);
}
delete ptr;
}
if (iRGBnum == 3)
{
ptr = new BYTE [w4b];
memset(ptr,0,w4b);
for ( i=ih.biHeight -1;i>=0;i--)
{
for ( j = 0; j < ih.biWidth ; j++)
{
ptr[j*3 +0 ] = GetByteatH(i,0)[j];
ptr[j*3 +1 ] = GetByteatH(i,1)[j];
ptr[j*3 +2 ] = GetByteatH(i,2)[j];
}
fwrite(ptr,w4b,1,f);
}
delete ptr;
}
fclose(f);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -