读取bmp头信息出错、、??
先贴部分代码吧、、、
自己定义的头文件结构体:
typedef struct BMP_file //位图文件头,14个字节
{
unsigned int bfType; //文件类型
unsigned long bfSize; //bmp文件长度
unsigned int Reserved1;
unsigned int Reserved2;
unsigned long bfOffset; //文件描述区长度,16色为118,256色为1078
}bitmapfile;
typedef struct BMP_info //位图信息头,40个字节
{
unsigned long biSize;
unsigned long biWidth; //图像宽度
unsigned long biHeight; //图像高度
unsigned int biPlanes; //设备级别,必须是1
unsigned int biBitCount; //每个像素所需位数,必须是1(黑白),4(16色),8(256色)或24(真彩色)之一
unsigned long biCompression;
unsigned long biSizeImage;
unsigned long biXplosPerMeter;
unsigned long biYplosPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
}bitmapinfo;
在主函数main()中实现:
bitmapfile bmpfilehead;
bitmapinfo bmpfileinfo;
in_file = fopen("1.bmp", "rb");
if (in_file == NULL )
{
perror("fopen() failed");
exit(0);
}
fread(&bmpfilehead,1,14,in_file);
fread(&bmpfileinfo,1,40,in_file);
if(bmpfilehead.bfType != 0x4d42)
{
printf("Not a BMP file!\n");
exit(1);
}
cols = bmpfileinfo.biWidth;
rows = bmpfileinfo.biHeight;
i = bmpfileinfo.biSize;
j = bmpfileinfo.biBitCount;
fsize = bmpfilehead.bfSize;
foffsize = bmpfilehead.bfOffset;
问题:
我选用的是1.bmp是512*512的标准灰度Lena图像,结果识别“Not a BMP file!”,为什么啊、、?
如果我跳过判别bmp那段,后面的cols是512,rows是512,i是40,都是对的;后面的j竟然是0,fsize文件总大小才为4,后面的foffsize偏移量更离谱、、、
是什么原因啊、、?我觉得我定义的结构体应该没问题啊,读取信息顺序也应该是对的啊,怎么搞的啊、、、