大家看看问题出在哪?
程序代码:#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//#include <math.h>
int p[512][512],q[512][512],l[9];
int height=512,width=512;
void main()
{
FILE *fid, *newraw,*newhead,*newbmp;
char *ph=(char *)malloc(sizeof(char)*1078);
int i,j,m,n,b;int a=0;
//打开lena_gray.bmp
if((fid=fopen("lena_sp.bmp","rb+"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
if((newraw=fopen("lena.raw","wb+"))==NULL)
{
printf("cannot create lena.raw\n");
exit(0);
}
//把目标文件的数据信息写到待转换的矩阵p中
fseek(fid,1078,SEEK_SET);
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
fread(&p[i][j],sizeof(char),1,fid);
}
}
//因为.bmp文件和.raw文件对于个像素的扫描顺序不同,所以在这里进行光标转换。
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
q[i][j]=p[height-i-1][j];
}
}
//把转换后的q中的信息写入newraw
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
fwrite(&q[i][j],sizeof(char),1,newraw);
}
}
//新建head文件(用于记录文件头和调色板)
if((newhead=fopen("newhead","wb+"))==NULL)
{
printf("cannot create newhead\n");
exit(0);
}
fseek(fid,0L,SEEK_SET);
fread(ph,1078,1,fid);
fwrite(ph,1078,1,newhead);
//新建bmp文件
if((newbmp=fopen("lena2.bmp","wb+"))==NULL)
{
printf("cannot create file\n");
exit(0);
}
//打开newhead文件
if((newhead=fopen("newhead","rb+"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
//打开lena.raw
if((newraw=fopen("lena.raw","rb+"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
//向lena2.bmp写入文件头
fread(ph,1078,1,newhead);
fwrite(ph,1078,1,newbmp);
//newraw导入变换数组
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
fread(&p[i][j],sizeof(char),1,newraw);
}
}
//转换坐标
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
q[i][j]=p[height-i-1][j];
}
}
//////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
///
for (i=1;i<height-1;i++){
for (j=1;j<width-1;j++){
/* l[0]=q[i-1][j-1];
l[1]=q[i-1][j];
l[2]=q[i-1][j+1];
l[3]=q[i][j-1];
l[4]=q[i][j];
l[5]=q[i][j+1];
l[6]=q[i+1][j-1];
l[7]=q[i+1][j];
l[8]=q[i+1][j+1];*/这个很笨的办法是可以做出来,但是改成下边的for循环,不能得到正确结果。
for (m=i-1;m<i+2;m++)
{
for (n=j-1;n<j+2;n++)
{
l[a++]=q[m][n];
}
//依次取出九个方阵的元素付给数组l
}
//冒泡法对取出的九个元素进行排序
for (m=0;m<8;m++)
{
for (n=0;n<8-m;n++)
{if (l[n]<l[n+1])
{b=l[n];
l[n]=l[n+1];
l[n+1]=b;}
}
}
//取出中间大小的数值付给循环当前元素
q[i][j]=l[4];
}
}
/////////////////////////////////////////////
//////////////////////////////////////////////
/////////////////////////////////////////////
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
fwrite(&q[i][j],sizeof(char),1,newbmp);
}
}
fclose(newbmp);
fclose(newraw);
fclose(newhead);
fclose(fid);
}







