威望0
积分7946
贡献0
在线时间763 小时
UID1
注册时间2021-4-14
最后登录2024-11-21
管理员
- UID
- 1
- 威望
- 0
- 积分
- 7946
- 贡献
- 0
- 注册时间
- 2021-4-14
- 最后登录
- 2024-11-21
- 在线时间
- 763 小时
|
- #include <stdio.h>
- #include <malloc.h>
- #include <iostream.h>
- #include <stdlib.h>
- void main()
- {
- FILE *fid, *newraw,*newhead,*newbmp;
- char *ph=new char[1078];
- long int width;
- long int height;
- int a,b,c,d,m,n,i,j;
-
- //打开lena_gray.bmp
- if((fid=fopen("LENA2.bmp","rb+"))==NULL)
- {
- cout<<"cannot open file\n"<<endl;
- exit(0);
- }
-
- //读取宽度和高度
- fseek(fid,18,SEEK_SET);
- fread(&width,sizeof(long),1,fid);
- fread(&height,sizeof(long),1,fid);
- cout<<"width="<<width<<endl;
- cout<<"height="<<height<<endl;
-
- //////////////////转化成表头和RAW文件/////////////////////////
- //新建head文件(用于记录文件头和调色板)
- if((newhead=fopen("newhead","wb+"))==NULL)
- {
- cout<<"cannot create newhead\n"<<endl;
- exit(0);
- }
- //新建lena_gray.raw
- if((newraw=fopen("lena_gray.raw","wb+"))==NULL)
- {
- cout<<"cannot create lena_gray.raw\n"<<endl;
- exit(0);
- }
- fseek(fid,0L,SEEK_SET);
- fread(ph,1078,1,fid);
- fwrite(ph,1078,1,newhead);
- fclose(newhead);
- //对图像宽度进行处理:
- if(width%4!=0)
- {
- width=(width*8+31)/32*4;
- }
- //申请二维数组的空间
- char **p=new char*[height];
- for(i=0;i<height;i++)
- p[i]=new char[width];
- char **q=new char*[height];
- for(j=0;j<height;j++)
- q[j]=new char[width];
- fseek(fid,1078,SEEK_SET);
- for(a=0;a<height;a++)
- {
- for(b=0;b<width;b++)
- {
- fread(&p[a][b],sizeof(char),1,fid);
- }
- }
- //坐标转换
- for(m=0;m<height;m++)
- {
- for(n=0;n<width;n++)
- {
- q[m][n]=p[height-m-1][n];
- }
-
- }
-
- for(c=0;c<height;c++)
- {
- for(d=0;d<width;d++)
- {
- fwrite(&q[c][d],sizeof(char),1,newraw);
- }
- }
-
- fclose(newraw);
- fclose(fid);
- ////////////////////////转化成BMP文件///////////////////
- //新建bmp文件
- if((newbmp=fopen("lena_nn.bmp","wb+"))==NULL)
- {
- cout<<"cannot create file\n"<<endl;
- exit(0);
- }
- //打开newhead文件
- if((newhead=fopen("newhead","rb+"))==NULL)
- {
- cout<<"cannot open file\n"<<endl;
- exit(0);
- }
- //打开lena_gray.raw
- if((newraw=fopen("lena_gray.raw","rb+"))==NULL)
- {
- cout<<"cannot open file\n"<<endl;
- exit(0);
- }
- //向lena_nn.bmp写入文件头
- fread(ph,1078,1,newhead);
- fwrite(ph,1078,1,newbmp);
-
- for(a=0;a<height;a++)
- {
- for(b=0;b<width;b++)
- {
- fread(&p[a][b],sizeof(char),1,newraw);
- }
- }
- //转换坐标
- for(m=0;m<height;m++)
- {
- for(n=0;n<width;n++)
- {
- q[m][n]=p[height-m-1][n];
- }
-
- }
- //向lenan.bmp文件写入图像数据
-
- for(c=0;c<height;c++)
- {
- for(d=0;d<width;d++)
- {
- fwrite(&q[c][d],sizeof(char),1,newbmp);
- }
- }
-
- fclose(newbmp);
- fclose(newraw);
- fclose(newhead);
- //释放二维数组占用的空间
- for(i=0;i<height;i++)
- delete(p[i]);
- delete(p);
- for(j=0;j<height;j++)
- delete(q[j]);
- delete(q);
-
- }
复制代码 |
|