4,446
社区成员
发帖
与我相关
我的任务
分享
/*
#define BYTE (unsigned char)
#define WORD (unsigned short)
#define DWORD (unsigned int)
*/
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef struct tagBITMAPFILEHEADER{
WORD bfType;//文件类型,必须是0x424D,即字符“BM”
DWORD bfSize;//文件大小
WORD bfReserved1;//保留字
WORD bfReserved2;//保留字
DWORD bfOffBits;//从文件头到实际位图数据的偏移字节数
//WORD aa;
}BITMAPFILEHEADER;//位图文件头定义;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;//信息头大小
DWORD biWidth;//图像宽度
DWORD biHeight;//图像高度
WORD biPlanes;//位平面数,必须为1
WORD biBitCount;//每像素位数
DWORD biCompression; //压缩类型
DWORD biSizeImage; //压缩图像大小字节数
DWORD biXPelsPerMeter; //水平分辨率
DWORD biYPelsPerMeter; //垂直分辨率
DWORD biClrUsed; //位图实际用到的色彩数
DWORD biClrImportant; //本位图中重要的色彩数
}BITMAPINFOHEADER; //位图信息头定义
typedef struct tagRGBQUAD{
BYTE rgbBlue; //该颜色的蓝色分量
BYTE rgbGreen; //该颜色的绿色分量
BYTE rgbRed; //该颜色的红色分量
BYTE rgbReserved; //保留值
}RGBQUAD;//调色板定义
#include "ReadBmp.h"
#include "stdlib.h"
#include <iostream>
using namespace std;
//变量定义
BITMAPFILEHEADER strHead;
RGBQUAD strPla[256];//256色调色板
BITMAPINFOHEADER strInfo;
int main(){
char filename[30];//bmp文件名
WORD type;
FILE *fpi,*fpw;
//cout<<"sizeof(strHead)"<<sizeof(strHead)<<endl;
//cout<<"sizeof(strInfo)"<<sizeof(strInfo)<<endl;
cout<<"请输入所要读取的文件名:"<<endl;
cin>>filename;
if((fpi=fopen(filename,"rb"))==NULL){
cout<<"open the bmp file error!"<<endl;
exit(0);
}
//读取bmp文件的文件头和信息头
fread((char *)&strHead,1,sizeof(strHead),fpi);
fread((char *)&strInfo,1,sizeof(strInfo),fpi);
//读取调色板
for(int nCounti=0;nCounti<strInfo.biClrUsed;nCounti++){
fread((char *)&strPla[nCounti],1,sizeof(RGBQUAD),fpi);
}
fclose(fpi);
type=strHead.bfType;
cout<<"bfType value is "<<hex<<type<<endl;
cout<<"bfSize value is "<<hex<<strHead.bfSize<<endl;
cout<<hex<<strPla[0].rgbRed<<"---"<<hex<<strPla[0].rgbGreen<<"---"<<hex<<strPla[0].rgbBlue<<endl;
if((fpw=fopen("b.bmp","w"))==NULL){
cout<<"create the bmp file error!"<<endl;
exit(0);
}
fwrite((char *)&strHead,1,sizeof(strHead),fpw);
fwrite((char *)&strInfo,1,sizeof(strInfo),fpw);
for(int nCounti=0;nCounti<strInfo.biClrUsed;nCounti++){
fwrite((char *)&strPla[nCounti],1,sizeof(RGBQUAD),fpw);
}
fclose(fpw);
system("pause");
}