1、 LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant;BITMAPINFOHEADER;typedef struct tagRGBQUAD / rgbq BYTE rgbBlue; BYTE rgbGreen; BYTE rgbRed; BYTE rgbReserved;RGBQUAD;typedef struc
2、t tagBITMAPINFO BITMAPINFOHEADER bmiHeader; RGBQUAD bmiColors1;BITMAPINFO;#endif / BMP_H_INCLUDED 主程序:main.c #include stdlib.hstring.hmalloc.hctype.hprocess.h#include BMP.hBITMAPFILEHEADER bmfh;BITMAPINFOHEADER bmih;BYTE *imgData;bool bReadBMFH=false;bool bReadBMIH=false;bool bReadPixel=false;/检查路径是
3、否合法:文件能打开;以 bmp 为后缀名 int CheckFilePath(char *filepath);/读入位图的文件头 int ReadFileHeader(char *filepath,BITMAPFILEHEADER *bmfh);/打印位图的文件头 void PrintFileHeader(BITMAPFILEHEADER *bmfh);/读入位图的信息头 int ReadInfoHeader(char *filepath,BITMAPINFOHEADER *bmih);/打印位图的信息头 void PrintInfoHeader(BITMAPINFOHEADER *bmih)
4、;/创建 8 位位图的调色板 int CreatePalette(RGBQUAD pal);/读入位图的像素数据 int ReadPixelData(char *filepath,BYTE *imgData);/计算每行像素所占的字节数 LONG GetLineBytes(int imgWidth,int bitCount);/打印位图的像素数据 void PrintPixelData(BYTE *imgData,int width,int height,int bitCount);/打印菜单选项 void PrintMenu();/另存为位图 int SaveAsImage(char *fi
5、lepath);/显示位图 void ShowImage(char * filepath);/保存文件头 int SaveFileHeader(FILE* fp);/保存信息头 int SaveInfoHeader(FILE* fp);/保存调色板 int SaveColorPalette(FILE *fp);/保存像素数据 int SavePixelData(FILE* fp);int main() char filepath256; char saveasfilepath256; int i; int width; int height; int bitCount; DWORD dwLin
6、eBytes; int select; int q=0;system(echo off);color 2printf(-TIMimage-nInput the path of the BMP file:ngets(filepath);i=CheckFilePath(filepath);if(i=-1) return -1; do PrintMenu(); scanf(%u,&select); switch(select) case 0: printf(%s,filepath); CheckFilePath(filepath); break; case 1: i=ReadFileHeader(f
7、ilepath,&bmfh); if(i!=-1) Read the file header successfully.n bReadBMFH=true; else Read the file header failed.n bReadBMFH=false; q=1;case 2: i=ReadInfoHeader(filepath,&bmih);Read the info header successfully.n bReadBMIH=true;Read the info header failed.n bReadBMIH=false;case 3: if(!bReadBMIH) Pleas
8、e read the info header at first.n height=bmih.biHeight; width=bmih.biWidth; bitCount=bmih.biBitCount; dwLineBytes=GetLineBytes(width,bitCount); imgData=(BYTE*)malloc(dwLineBytes*height*sizeof(BYTE);imgData) Can not allocate memory for the image.n i=ReadPixelData(filepath,imgData); if(i=-1) Read the
9、pixel data failed.n bReadPixel=false;Read the pixel data successfully.n bReadPixel=true;case 4: if(bReadBMFH) PrintFileHeader(&Please read the file header at first.ncase 5: if(bReadBMIH) PrintInfoHeader(&case 6: if(bReadPixel) PrintPixelData(imgData,width,height,bitCount);Please read the pixel data
10、at first.n case 7: ShowImage(filepath); case 8:Input the path(ex. d:/poon.bmp) you want to save:,saveasfilepath); i=SaveAsImage(saveasfilepath);Error: failed to save the image.n default: select=9527; while (q=0); return 0;int ReadFileHeader(char *filepath,BITMAPFILEHEADER *bmfh) FILE *fp; fp=fopen(f
11、ilepath,rbfp) Can not open the file:%sn if(fread(&bmfh-bfType,sizeof(WORD),1,fp)!=1) Can not read bfType in the file header.n fclose(fp);bfSize,sizeof(DWORD),1,fp)!Can not read bfSize in the file header.nbfReserved1,sizeof(WORD),1,fp)!Can not read bfReserved1 in the file header.nbfReserved2,sizeof(W
12、ORD),1,fp)!Can not read bfReserved2 in the file header.nbfOffBits,sizeof(DWORD),1,fp)!Can not read bfOffBits in the file header.nint ReadInfoHeader(char *filepath,BITMAPINFOHEADER *bmih) fseek(fp,14,SEEK_SET);if(fread(&bmih-biSize,sizeof(DWORD),1,fp)!Can not read biSize in the info header.nbiWidth,s
13、izeof(LONG),1,fp)!Can not read biWidth in the info header.nbiHeight,sizeof(LONG),1,fp)!Can not read biHeight in the info header.nbiPlanes,sizeof(WORD),1,fp)!Can not read biPlanes in the info header.nbiBitCount,sizeof(WORD),1,fp)!Can not read biBitCount in the info header.nbiCompression,sizeof(DWORD)
14、,1,fp)!Can not read biCompression in the info header.nbiSizeImage,sizeof(DWORD),1,fp)!Can not read biSizeImage in the info header.nbiXPelsPerMeter,sizeof(LONG),1,fp)!Can not read biXPelsPerMeter in the info header.nbiYPelsPerMeter,sizeof(LONG),1,fp)!Can not read biYPelsPerMeter in the info header.nb
15、iClrUsed,sizeof(DWORD),1,fp)!Can not read biClrUsed in the info header.nbiClrImportant,sizeof(DWORD),1,fp)!Can not read biClrImportant in the info header.nint CreatePalette(RGBQUAD pal) if(sizeof(pal)/sizeof(RGBQUAD)!=256) The size of the palette must be 256.n for(i=0;ibfOffBits);bfReserved1:bfReserved1); printf(