给你C++的代码吧呵呵,C的也差不多,稍微修改一下就行了
主要是你要了解BMP文件的结构,就是文件头那里所包含的信息,这里用了BITMAPINFOHEADER等现成的结构体来处理,BMP的文件头百度一下就知道的啦,很多资料
#include "fstream.h"//24bit bitmap
bool CBitmapWindow::LoadFile (char *bmpFile)
{
FILE *fp;
if((fp=fopen(bmpFile,"rb"))==NULL)
return false;
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
BITMAPINFOHEADER bih;
fread(&bfType,sizeof(WORD),1,fp);
if(bfType!=0x4d42)
return false;
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(BITMAPINFOHEADER1),1,fp);
nWidth=bih.biWidth ;
nHeight=bih.biHeight;
if(nWidth % 4 !=0)
nWidth=nWidth+ (4-nWidth % 4); //修正位图宽度值
DWORD size=nWidth*bih.biBitCount/8*nHeight;
arrayColor=new ZafLogicalColor[nWidth*nHeight];
// LPSTR pData=new char[size];
unsigned char* pData=new unsigned char[size];
fread(pData,size,1,fp);
int bmWidthBytes=nWidth*bih.biBitCount /8;
int bmBitsPixel=bih.biBitCount ;
int nBit=bmBitsPixel/8;
colorTableBitmap* clrTableBmp;
clrTableBmp=new colorTableBitmap[nWidth*nHeight];
char cBit[40];
int z;
for (int y=0;y
for (int x=0;x
z=nHeight-y-1;
clrTableBmp[y*nWidth+x].nIndex =y*nWidth+x;
clrTableBmp[y*nWidth+x].iBlue =pData[x*nBit+z*bmWidthBytes];
clrTableBmp[y*nWidth+x].iGreen =pData[x*nBit+1+z*bmWidthBytes];
clrTableBmp[y*nWidth+x].iRed=pData[x*nBit+2+z*bmWidthBytes];
sprintf(cBit,",%d:%d:%d:%d",y*nWidth+x,pData[x*nBit+z*bmWidthBytes],
pData[x*nBit+1+z*bmWidthBytes],
pData[x*nBit+2+z*bmWidthBytes]);
}
}
delete []clrTableBmp;
delete []pData;
fclose(fp);
return true;
}