#include <stdio.h>
#include <math.h>
#include <memory.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include "stdlib.h"
#include "string.h"
//原图象的宽度和高度
#define width 256
#define higth 256
int lvbo(unsigned char D[9])
{
return (D[0]+D[1]+D[2]+D[3]+D[4]+D[5]+D[6]+D[7]+D[8])/9;
}
void main()
{
FILE *fp,*newfp;
int i,j;
if(!(fp=fopen("D:\\Program Files\\Microsoft Visual Studio\\MyProjects\\jzlb\\Debug\\xrk.jpg","rb")))
{
printf("Open file1 error!\n");
return ;
}
if(!(newfp=fopen("D:\\Program Files\\Microsoft Visual Studio\\MyProjects\\jzlb\\Debug\\2.jpg","wb")))
{
printf("Open file2 error!\n");
return ;
}
unsigned char buffer[54+1024];//定义原图像头缓冲区
fread(buffer,1,54+1024,fp);//读取文件头54个字节
unsigned long length=width*higth;//图像的总象素个数
unsigned char readData[higth][width]; //用于存储原图数据的数组
unsigned char writeData[higth][width]; //用于存储原图数据的数组
fread(&readData[0][0], sizeof(unsigned char),length, fp);//从原图读入数据
for(i=0;i<higth;i++)
{
for(j=0;j<width;j++)
{
writeData[i][j]=readData[i][j];
}
}
unsigned char D[9]; //定义选取框
for(i=1;i<higth-1;i++)
{
for(j=1;j<width-1;j++)
{
D[0]=readData[i-1][j+1];
D[1]=readData[i][j+1];
D[2]=readData[i+1][j+1];
D[3]=readData[i-1][j];
D[4]=readData[i][j];
D[5]=readData[i+1][j];
D[6]=readData[i-1][j-1];
D[7]=readData[i][j-1];
D[8]=readData[i+1][j-1];
writeData[i][j]=lvbo(D);
}
}
fwrite(buffer,sizeof(unsigned char),54+1024,newfp);
fwrite(writeData,sizeof(unsigned char),length,newfp);
fclose(newfp);
fclose(fp);
printf("success\n");
return ;
}