Logic Used:
Insert Pixel Data into a struct containing RGB values of the pixels
read Data in a 3x3 array: | BGR(-1,-1) | BGR(-1,0) | BGR(-1,1) |
| BGR(0,-1) | BGR(0,0) | BGR(0,1) |
| BGR(1,-1) | BGR(1,0) | BGR(1,1) |
Assign Average of all Blue,Green,Red components respectively to another struct containing RGB values and write the same
to the output file
Problem:Unexpected output,as seen in the bmp files attached in mail.The output image is supposed to be almost the same,except a little blurred.
Input File: o.bmp
Output File: q.bmp
#define MASKSIZE 3
#define INFILE "o.bmp"
#define OUTFILE "q.bmp"
/*------------Algorithm(Using Mask 3x3)---------------------------*/
fp1=fopen(INFILE,"rb");
fp3=fopen(OUTFILE2,"wb");
fread((BYTE *)&bmfh,sizeof(BITMAPFILEHEADER),1,fp1);
fread((BYTE *)&bmih,sizeof(BITMAPINFOHEADER),1,fp1);
fwrite((BYTE *)&bmfh,sizeof(BITMAPFILEHEADER),1,fp3);
fwrite((BYTE *)&bmih,sizeof(BITMAPINFOHEADER),1,fp3);
fseek(fp1,bmfh.bfOffsetBits,SEEK_SET); /*Set Position to Start of Pixel Data*/
row=0; /*Offset for Row of Bitmap Array*/
/*Begin Loop*/
while(row<bmih.biHeight)
{
col=0; /*Offset for Column of Bitmap Array*/
while(col<bmih.biWidth)
{
fseek(fp1,(bmfh.bfOffsetBits)+(row * (bmih.biWidth * 3))+(col),SEEK_SET); /*Move to Next Pixel (central pixel on which mask coincides)*/
pos=ftell(fp1);
pos=pos-((bmih.biWidth *3) + MASKSIZE);
if(row==0)
{
if(col==0)
{
fseek(fp1,0,SEEK_CUR); /*for poition (0,0) read pixel from (0,0)*/
}
else
{
fseek(fp1,ftell(fp1)-3,SEEK_CUR); /*for position(0,col) read pixel into array from previous pixel( - 3Bytes)*/
}
}
else
{
if(col==0)
{
fseek(fp1,ftell(fp1)-(bmih.biWidth *3),SEEK_CUR); /*for position(row,0) start reading into array from the pixel directly above current position*/
}
else
{
if(col == (bmih.biWidth)-1)
{
fseek(fp1,ftell(fp1)-((bmih.biWidth+1)*3),SEEK_CUR); /*for poition(row,Width-1) read pixel data into array at position (-1,-1) in a 3x3 array*/
}
else
{
fseek(fp1,ftell(fp1)-((bmih.biWidth+1)*3),SEEK_CUR); /*for position(row,col)*/
}
}
}
/*Begin Reading Pixel Data Into Array */
for(rowoff=-1;rowoff<2;rowoff++)
{
for(coloff=-1;coloff<2;coloff++)
{
if(row==0) /*Padding*/
{
rgb[-1][-1].rgbtBlue=rgb[-1][0].rgbtBlue=rgb[-1][2].rgbtBlue=0;
rgb[-1][-1].rgbtGreen=rgb[-1][0].rgbtGreen=rgb[-1][2].rgbtGreen=0;
rgb[-1][-1].rgbtRed=rgb[-1][0].rgbtRed=rgb[-1][2].rgbtRed=0;
rowoff++;
}
if(col==0) /*Padding*/
{
rgb[-1][-1].rgbtBlue=rgb[0][-1].rgbtBlue=rgb[1][-1].rgbtBlue=0;
rgb[-1][-1].rgbtGreen=rgb[0][-1].rgbtGreen=rgb[1][-1].rgbtGreen=0;
rgb[-1][-1].rgbtRed=rgb[0][-1].rgbtRed=rgb[1][-1].rgbtRed=0;
coloff++;
}
fread(pixel,1,sizeof(RGBTRIPLE),fp1);
rgb[rowoff][coloff].rgbtBlue=pixel->rgbtBlue;
rgb[rowoff][coloff].rgbtGreen=pixel->rgbtGreen;
rgb[rowoff][coloff].rgbtRed=pixel->rgbtRed;
if(col==(bmih.biWidth-1)) /*Padding*/
{
rgb[-1][1].rgbtBlue=rgb[0][1].rgbtBlue=rgb[1][1].rgbtBlue=0;
rgb[-1][1].rgbtGreen=rgb[0][1].rgbtGreen=rgb[1][1].rgbtGreen=0;
rgb[-1][1].rgbtRed=rgb[0][1].rgbtRed=rgb[1][1].rgbtRed=0;
coloff++;
}
if(row==(bmih.biHeight)) /*Padding*/
{
rgb[1][-1].rgbtBlue=rgb[1][0].rgbtBlue=rgb[1][1].rgbtBlue=0;
rgb[1][-1].rgbtGreen=rgb[1][0].rgbtGreen=rgb[1][1].rgbtGreen=0;
rgb[1][-1].rgbtRed=rgb[1][0].rgbtRed=rgb[1][1].rgbtRed=0;
rowoff++;
}
}
fseek(fp1,((bmih.biWidth)*3)+(MASKSIZE),SEEK_CUR);
}
/*Finish Read*/
/*Begin Averaging Array color value*/
for(i=-1;i<2;i++)
{
for(j=-1;j<2;j++)
{
Blue=(Blue+(rgb[i][j].rgbtBlue))/9;
Green=(Green+(rgb[i][j].rgbtGreen))/9;
Red=(Red+(rgb[i][j].rgbtRed))/9;
}
}
/*Averaging Finish*/
/*Write Averaged Values Into Fil*/
pixel->rgbtBlue=Blue;
pixel->rgbtGreen=Green;
pixel->rgbtRed=Red;
fwrite(pixel,1,sizeof(RGBTRIPLE),fp3);
/*Finish Writing*/
col++;
}
row++;
}
/*End Loop*/
Input Image:
[IMG]http://www.daniweb.com/forums/attachment.php?attachmentid=12733&stc=1&d=1259307546[/IMG]
Output Image:
[IMG]http://www.daniweb.com/forums/attachment.php?attachmentid=12734&stc=1&d=1259307546[/IMG]