//对真彩色(24位或8位或32位)图象处理 完成图象的分析与图象的生成
#include #include //--------- struct bmp_pal{ //24位 图象数据区结构 unsigned char blue ; unsigned char green; unsigned char red ; }bmp_pal; struct bmp_pal256 { //调色板结构 unsigned char blue ; unsigned char green; unsigned char red ; unsigned char reserved; }bmp_pal256,bmp32; struct BMP_file{ unsigned int Biftype; unsigned long Bifsize; unsigned int Reserved1; unsigned int reserved2; unsigned long Bifoffset; }bitmapfile, *mapfile; struct BMP_info{ unsigned long bisize; unsigned long biWidth; unsigned long biHeight; unsigned int biPlanes; unsigned int biBitcount; unsigned long biCompression; unsigned long biSizeimage; //文件数据大小 unsigned long biXplospermeter;//水平方向的分辨率 unsigned long biYplospermeter;//垂直方向的分辨率 unsigned long biClrused;//位图实际使用的彩色表中的颜色索引数(设为0的话,则说明使用所有调色板项) unsigned long biClrimportant; }bitmapinfo,*mapinfo; int Line; int Row; int Dw; int TT=0x0; //用于对齐的标志 void main() {int i; int j; int k; int Temp_color; char file[40]; FILE *fp; //原文件 FILE *new_file; //新文件 mapfile=&bitmapfile; mapinfo=&bitmapinfo; printf("请输入文件名!\n"); gets(file); printf("您输入的文件名是:\n"); puts(file); fp=fopen(file,"rb"); if (fp==NULL) {printf("文件未能打开!\n"); exit(0); } fread(&bitmapfile.Biftype,2,1,fp); fread(&bitmapfile.Bifsize,4,1,fp); fread(&bitmapfile.Reserved1,2,1,fp); fread(&bitmapfile.reserved2,2,1,fp); fread(&bitmapfile.Bifoffset,4,1,fp); //--------------- fread(&bitmapinfo.bisize,4,1,fp); fread(&bitmapinfo.biWidth,4,1,fp); Line=bitmapinfo.biWidth; //图象扫描行 fread(&bitmapinfo.biHeight,4,1,fp); Row=bitmapinfo.biHeight; //图象扫描列 fread(&bitmapinfo.biPlanes,2,1,fp); fread(&bitmapinfo.biBitcount,2,1,fp); fread(&bitmapinfo.biCompression,4,1,fp); fread(&bitmapinfo.biSizeimage,4,1,fp); fread(&bitmapinfo.biXplospermeter,4,1,fp); fread(&bitmapinfo.biYplospermeter,4,1,fp); fread(&bitmapinfo.biClrused,4,1,fp); fread(&bitmapinfo.biClrimportant,4,1,fp); printf("文件信息读取完成!\n"); printf("已完成文件分析!\n"); //----------- printf("文件类型:%c\n",bitmapfile.Biftype); printf("文件长度:%d\n",bitmapfile.Bifsize); printf("文件描述区长度:%d\n",bitmapfile.Bifoffset); printf("文件是否是压缩格式:%d\n",bitmapinfo.biCompression); printf("图形尺寸:%d\n",bitmapinfo.bisize); printf("图形宽度:%d\n",bitmapinfo.biWidth); printf("图形高度:%d\n",bitmapinfo.biHeight); printf("图形数据大小:%d\n",bitmapinfo.biSizeimage); printf("一个象素占有位:%d\n",bitmapinfo.biBitcount); printf("是否使用所有调色板项:%d\n",bitmapinfo.biClrused); printf("图形水平方向的分辨率:%d\n",bitmapinfo.biXplospermeter); printf("图形垂直方向的分辨率:%d\n",bitmapinfo.biYplospermeter); //*************************************************************** //拒绝处理的图象格式 //***************************************************************if (bitmapfile[0].Biftype!=19778 && bitmapfile[1].Biftype!=19778) { printf("不是bitmap文件 拒绝处理!\n"); exit(1); } if (bitmapinfo[0].biBitcount!=24 && bitmapinfo[1].biBitcount!=32) { printf("不是24位 或32位 拒绝处理!\n"); exit(1); } if (bitmapinfo[0].biCompression!=0 && bitmapinfo[0].biCompression!=0) {printf("压缩格式! 拒绝处理!\n"); exit (1); } //*************************************************************** printf("开始生成新文件\n"); new_file=fopen("d:\\new_bmp.bmp","wb"); fwrite(&bitmapfile[0].Biftype,2,1,new_file); fwrite(&bitmapfile[0].Bifsize,4,1,new_file); fwrite(&bitmapfile[0].Reserved1,2,1,new_file); fwrite(&bitmapfile[0].reserved2,2,1,new_file); fwrite(&bitmapfile[0].Bifoffset,4,1,new_file); fwrite(&bitmapinfo[0].bisize,4,1,new_file); fwrite(&bitmapinfo[0].biWidth,4,1,new_file); fwrite(&bitmapinfo[0].biHeight,4,1,new_file); fwrite(&bitmapinfo[0].biPlanes,2,1,new_file); fwrite(&bitmapinfo[0].biBitcount,2,1,new_file); fwrite(&bitmapinfo[0].biCompression,4,1,new_file); fwrite(&bitmapinfo[0].biSizeimage,4,1,new_file); fwrite(&bitmapinfo[0].biXplospermeter,4,1,new_file); fwrite(&bitmapinfo[0].biYplospermeter,4,1,new_file); fwrite(&bitmapinfo[0].biClrused,4,1,new_file); fwrite(&bitmapinfo[0].biClrimportant,4,1,new_file); printf("新文件头完成\n"); //****************************************************************** //处理24位的 //****************************************************************** Dw=(bitmapinfo[0].biWidth*(bitmapinfo[0].biBitcount/8))%4; //用于对齐的字节 if (Dw!=0) Dw=4-Dw; k=Dw; Dw_copy=(bitmapinfo[1].biWidth*(bitmapinfo[1].biBitcount/8))%4; //用于对齐的字节 if (Dw_copy!=0) Dw_copy=4-Dw_copy; k_copy=Dw_copy; printf("Dw== %d\n",Dw); printf("Dw_copy==%d\n",Dw_copy); if (bitmapinfo[0].biBitcount==24 && bitmapinfo[1].biBitcount==24) { for(i=0;i