程序代码:
//read image
FILE *fp;
if((fp=fopen(name.c_str(),"rb"))==NULL) {
printf("cannot open bmp.name\n");
return ;
}
fread(&bfType,sizeof(WORD),1,fp);
if(bfType!=0x4d42) {//该值必需是0x4D42,也就是字符'BM'
printf("the input map is not bmp type");
return ;
}
fread(&bfSize,sizeof(DWORD),1,fp);
fread(&bfReserved1,sizeof(WORD),1,fp);
fread(&bfReserved2,sizeof(WORD),1,fp);
fread(&bfOffBits,sizeof(DWORD),1,fp);
fread(&bih,sizeof(BITMAPINFOHEADER),1,fp);
width=bih.biWidth ;
height=bih.biHeight;
if(width % 4 !=0)
width=width+ (4-width % 4);
DWORD size=width*(bih.biBitCount/8)*height;
unsigned char* pData=new unsigned char[size];
fread(pData,size,1,fp);
int bmWidthBytes=width*bih.biBitCount /8;
int bmBitsPixel=bih.biBitCount ;
int nBit=bmBitsPixel/8;
int z;
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
z=height-y-1;
*(r+(y*width+x))=pData[x*nBit+2+z*bmWidthBytes];
*(g+(y*width+x))=pData[x*nBit+1+z*bmWidthBytes];
*(b+(y*width+x))=pData[x*nBit+z*bmWidthBytes];
}
}
[
本帖最后由 njkido 于 2011-3-21 12:38 编辑 ]