int ReadFileHeader(char *filepath,BITMAPFILEHEADER *bmfh)
{
FILE *fp;
fp = fopen(filepath,"rb");
if(!fp)
{
printf("Can not open the file:%s\n",filepath);
return -1;
}
if(fread(&bmfh->bfType, sizeof(WORD),1,fp) != 1)
{
printf("Can not read bfType in the file header.\n");
fclose(fp);
return -1;
}
if(fread(&bmfh->bfSize,sizeof(DWORD),1,fp) != 1)
{
printf("Can not read bfSize in the file header.\n");
fclose(fp);
return -1;
}
if(fread(&bmfh->bfReserved1,sizeof(WORD),1,fp) != 1)
{
printf("Can not read bfReserved1 in the file header.\n");
fclose(fp);
return -1;
}
if(fread(&bmfh->bfReserved2,sizeof(WORD),1,fp) != 1)
{
printf("Can not read bfReserved2 in the file header.\n");
fclose(fp);
return -1;
}
if(fread(&bmfh->bfOffBits,sizeof(DWORD),1,fp) != 1)
{
printf("Can not read bfOffBits in the file header.\n");
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
int ReadInfoHeader(char *filepath, BITMAPINFOHEADER *bmih)
{
FILE *fp;
fp = fopen(filepath, "rb");
if(!fp)
{
printf("Can not open the file:%s\n",filepath);
return -1;
}
fseek(fp,14,SEEK_SET);
if(fread(&bmih->biSize,sizeof(DWORD),1,fp) != 1)
{
printf("Can not read biSize in the info header.\n");
fclose(fp);
return -1;
}
if(fread(&bmih->biWidth,sizeof(LONG),1,fp) != 1)
{
printf("Can not read biWidth in the info header.\n");
fclose(fp);
return -1;
}
if(fread(&bmih->biHeight,sizeof(LONG),1,fp) != 1)
{
printf("Can not read biHeight in the info header.\n");
fclose(fp);
return -1;
}
if(fread(&bmih->biPlanes,sizeof(WORD),1,fp) != 1)
{
printf("Can not read biPlanes in the info header.\n");
fclose(fp);
return -1;
}
if(fread(&bmih->biBitCount,sizeof(WORD),1,fp) != 1)
{
printf("Can not read biBitCount in the info header.\n");
fclose(fp);
return -1;
}
if(fread(&bmih->biCompression,sizeof(DWORD),1,fp)!=1)
{
printf("Can not read biCompression in the info header.\n");
fclose(fp);
return -1;
}
if(fread(&bmih->biSizeImage,sizeof(DWORD),1,fp)!=1)
{
printf("Can not read biSizeImage in the info header.\n");
fclose(fp);
return -1;
}
if(fread(&bmih->biXPelsPerMeter,sizeof(LONG),1,fp)!=1)
{
printf("Can not read biXPelsPerMeter in the info header.\n");
fclose(fp);
return -1;
}
if(fread(&bmih->biYPelsPerMeter,sizeof(LONG),1,fp)!=1)
{
printf("Can not read biYPelsPerMeter in the info header.\n");
fclose(fp);
return -1;
}
if(fread(&bmih->biClrUsed,sizeof(DWORD),1,fp)!=1)
{
printf("Can not read biClrUsed in the info header.\n");
fclose(fp);
return -1;
}
if(fread(&bmih->biClrImportant,sizeof(DWORD),1,fp)!=1)
{
printf("Can not read biClrImportant in the info header.\n");
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
LONG GetLineBytes(int imgWidth,int bitCount)
{
return (imgWidth*bitCount+31)/32*4;
}
int ReadPixelData(char *filepath,BYTE *imgData)
{
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
BYTE *data;
FILE *fp;
int n;
int width;
int height;
int bitCount;
DWORD dwLineBytes;
n = ReadFileHeader(filepath,&bmfh);
if(n == -1)
{
printf("Can not read the file header of the BMP file.\n");
return -1;
}
n = ReadInfoHeader(filepath,&bmih);
if(n == -1)
{
printf("Can not read the info header of the BMP file.\n");
return -1;
}
width = bmih.biWidth;
height = bmih.biHeight;
bitCount = bmih.biBitCount;
dwLineBytes = GetLineBytes(width, bitCount);
if(_msize(imgData) != (dwLineBytes * height))
{
printf("The size you allocate for the pixel data is not right.\n");
printf("Fittable size:%ldbytes.\n",(dwLineBytes*height));
printf("Your size :%ld bytes.\n",sizeof(imgData));
return -1;
}
data = (BYTE *)malloc(dwLineBytes*height*sizeof(BYTE));
if(!data)
{
printf("Can allocate memory for pixel data.\n");
return -1;
}
fp = fopen(filepath,"rb");
if(!fp)
{
printf("Can not oprn the file :%s.\n",filepath);
free(data);
return -1;
}
int main()
{
FILE *fp;
char filepath[256];
int i,j;
float m,n,d;
int x1=0,x2=0;
int k1=0,k2=0;
int sum1=0,sum2=0;
int a[ROW][COL],b[COL],c[COL];
int width;
int height;
int bitCount;
DWORD dwLineBytes;
printf("Input the path of the BMP file:\n");
gets(filepath);
fp = fopen(filepath,"r");
if(!fp)
{
printf("Error: The path is not correct.\n");
return -1;
}
//读文件头
i = ReadFileHeader(filepath,&bmfh);
if(i != -1)
{
printf("Read the file header successfully.\n");
bReadBMFH = true;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef long LONG;
typedef unsigned char BYTE;
typedef struct tagBITMAPFILEHEADER
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
}BITMAPINFOHEADER;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef long LONG;
typedef unsigned char BYTE;
typedef struct tagBITMAPFILEHEADER
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
}BITMAPINFOHEADER;