这是我参照其他人的程序,写的一段读取BMP文件的程序,可是运行出来的图像发生了扭曲和色彩的变化,不知道是什么问题,下面是程序代码:
#include"iostream.h"
#include "stdio.h"
#include "stdlib.h"
typedef struct { // 长度12字节
// short bftype; // 固定为"BM",VC不能读取一个short,故省略
int bfSize; // 位图文件的大小,以字节为单位
short bfReserved1; // 位图文件保留字,必须为0
short bfReserved2; // 位图文件保留字,必须为0
int bfOffBits; // 位图数据的起始位置,以相对于位图文件头的偏移量表示,以字节为单位
} BITMAPFILEHEADER;
typedef struct { // 长度40字节
int biSize; // 本结构所占用字节数,长度40字节
int biWidth; // 位图的宽度,以像素为单位
int biHeight; // 位图的高度,以像素为单位
short biPlanes; // 目标设备的级别,必须为1
short biBitCount; // 每个像素所需的位数,必须是1(双色),4(16色),8(256色)或24(真彩色)之一
int biCompression; // 位图压缩类型,必须是0(不压缩),1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一
int biSizeImage; // 位图的大小,以字节为单位
int biXPelsPerMeter; // 位图水平分辨率,每米像素数
int biYPelsPerMeter; // 位图垂直分辨率,每米像素数
int biClrUsed; // 位图实际使用的颜色表中的颜色数
int biClrImportant; // 位图显示过程中重要的颜色数
} BITMAPINFOHEADER;
void bitmapfileheadinitialize(BITMAPFILEHEADER &fh) // 初始化
{
fh.bfOffBits=0; fh.bfReserved1=0; fh.bfReserved2=0; fh.bfSize=0;
}
void bitmapimforheadinitialize(BITMAPINFOHEADER &ih) //初始化
{
ih.biBitCount=0;ih.biClrImportant=0;ih.biClrUsed=0;ih.biCompression=0;
ih.biHeight=0;ih.biPlanes=0;ih.biSize=0;ih.biSizeImage=0;ih.biWidth=0;
ih.biXPelsPerMeter=0;
}
void main()
{
FILE *rfile; //读入文件指针
FILE *wfile; //写入文件指针
short bmp_header=0x4D42; // bmp文件头字
short bmp_end=0x0000; // bmp文件尾字
BITMAPFILEHEADER fh, *pointfh; //文件头指针
BITMAPINFOHEADER ih, *pointih; //数据头指针
char *pointimage;// 图区域头指针
pointfh=&fh;pointih=&ih;
bitmapfileheadinitialize(fh); //初始化文件头
bitmapimforheadinitialize(ih);
而且读取的图像的水平分辨率和垂直分辨率是3780,但是我点击图片右键看图片的“摘要”“属性”的时候上面的水平和垂直分辨率是96,出现不一致的现象,这又是怎么回事呢????谢谢大侠了,感激涕零
if( ( rfile=fopen( "d:\\huojian256.bmp", "r" )) == NULL )
printf( "The file 'd:\\huojian256.bmp' was not opened\n" );
else
printf( "The file 'd:\\huojian256.bmp' was opened\n" );
if( ( wfile=fopen( "d:\\huojian.bmp", "w" )) == NULL )
printf( "The file 'd:\\huojian.bmp' was not opened\n" );
else
printf( "The file 'd:\\huojian.bmp' was opened\n" );
//读取头文件
fseek( rfile , sizeof(short) ,SEEK_SET );
fread( pointfh ,sizeof(BITMAPFILEHEADER) , 1 , rfile );
//读取信息头文件
fseek( rfile , sizeof(BITMAPFILEHEADER)+sizeof(short) ,SEEK_SET );
fread( pointih ,sizeof(BITMAPINFOHEADER),1, rfile );
//读位图信息
pointimage=(char*)calloc(ih.biSizeImage*3, sizeof(char));
fseek(rfile,fh.bfOffBits,SEEK_SET);//头指针移动到位图区
fread(pointimage,sizeof(char),ih.biSizeImage*3,rfile);