自从上一次写了,后来是准备马上就写程序了,谁知后来就忘了,昨天突然想起来就将其写了出来。
程序的功能是将彩色图转灰度图,中间彩色和灰度的文件header都用到了,程序的流程我想我写的还是比较清楚的。没有用到Windows下的某些函数,在Linux下应该也能运行。
#include#include using namespace std;#pragma pack(2) //两字节对齐,否则bmp_fileheader会占16Bytestruct bmp_fileheader{ unsigned short bfType; //若不对齐,这个会占4Byte unsigned long bfSize; unsigned short bfReverved1; unsigned short bfReverved2; unsigned long bfOffBits;};struct bmp_infoheader{ unsigned long biSize; unsigned long biWidth; unsigned long biHeight; unsigned short biPlanes; unsigned short biBitCount; unsigned long biCompression; unsigned long biSizeImage; unsigned long biXPelsPerMeter; unsigned long biYpelsPerMeter; unsigned long biClrUsed; unsigned long biClrImportant;};fstream input_file;fstream output_file;struct bmp_fileheader bfh;struct bmp_infoheader bih;unsigned char *src_buff;unsigned char *dst_buff;void read_bmp_fileheader(){ input_file.read((char*)&bfh,sizeof(struct bmp_fileheader));}void read_bmp_infoheader(){ input_file.read((char*)&bih,sizeof(struct bmp_infoheader));}void read_bmp_data(){ src_buff=new unsigned char[bih.biHeight*bih.biWidth*3]; input_file.read((char*)src_buff,sizeof(unsigned char)*bih.biHeight*bih.biWidth*3); }void bmp_rgb2gray(){ dst_buff=new unsigned char[bih.biHeight*bih.biWidth]; int tmp; unsigned long j=0; for (unsigned long i=0;i