You are on page 1of 2

/* program to demonstrate high pass filtering using

-1 [ 1 1 1 ]
--- [ 1 1 1 ]
9 [ 1 1 1 ]
*/
#include <string.h>
#include <stdio.h>
#include <conio.h>
FILE *stream,*cptr;
int width,height;
unsigned char b[2];
int value(int row,int col)
{
// 54th byte onwards
int s;
double r=(double)row*((width)*3)+54+(col*3);
fseek(stream,0,SEEK_SET);
while(r>=32760)
{
fseek(stream,32760,SEEK_CUR);
r=r-32760;}
fseek(stream,(int)r,SEEK_CUR);
fread(b,1, 1, stream);
fread(b,1, 1, stream);
fread(b,1, 1, stream);
return b[0];
}
int main(void)
{
int dimension;
int i,j,k,l,prod,temp1;
int dim;
float temp;
clrscr();
if ((stream = fopen("c:\\try.bmp", "rb+"))
== NULL)
{
fprintf(stderr, "Cannot open input file.\n");
return 1;
}
if ((cptr = fopen("c:\\m.bmp", "wb+")) == NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
//copy the header to the output file as it is.
for(i=0;i<=53;i++)
{
fread(b,1, 1, stream);
fwrite(b,1,1,cptr);
}
//to get the width of the image
fseek(stream,18,SEEK_SET);
width=0;
for(j=0,prod=1;j<3;j++)
{
fread(b,1, 1, stream);
width=width+b[0]*prod;
prod=prod*255;
}
//to get the height of the image
fseek(stream,22,SEEK_SET);
height=0;
for(j=0,prod=1;j<3;j++)
{
fread(b,1, 1, stream);
height=height+b[0]*prod;
prod=prod*255;
}
printf("Enter the dimension of the high pass filter mask to be applied");
printf("\n Note the dimension should be an odd number");
scanf("%d",&dimension);
dim=dimension;
dimension/=2;
//height contains the height of the image
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
temp=0;
temp1=value(i,j);
for(k=i-dimension;k<=i+dimension;k++)
{
for(l=j-dimension;l<=j+dimension;l++)
{
if(k>=0&&l>=0&&k<height&&l<width)
temp+=value(k,l);
}
}
b[0]=temp/(dim*dim);
b[0]=abs(temp1-b[0]);
fwrite(b,1,1,cptr);
fwrite(b,1,1,cptr);
fwrite(b,1,1,cptr);
}
}
fclose(stream);
fclose(cptr);
return 0;
}