I've posted this program several other times, but am different troubles with it. I want to write an algorithm to blur an image, and so far have gotten every pixel value of a grayscale image into a **char
array. From there, my plan is to go through each pixel, look to its immediate neighboring pixels, sum the values of them AND the pixel I am currently looking at, and get the average of all of those pixels.
After that is done, I want to set the pixel I'm currently looking at to that average value. I've written partial code for this, and I believe I'm adding it up correctly, but still do not get a resulting image. Must I provide a cast to my sum before assigning it back to the pixel?
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fin, *fout;
char path_in[64], path_out[64];
unsigned char **rev, px;
int sum, width, height, read, row, i, j;
printf("Input file name: ");
scanf("%s", path_in);
printf("Output file name: ");
scanf("%s", path_out);
printf("Width of image (in pixels): ");
scanf("%d", &width);
printf("Height of image (in pixels): ");
scanf("%d", &height);
fin = fopen(path_in, "rb");
fout = fopen(path_out, "wb");
rev = (unsigned char **)malloc(height * sizeof(unsigned char *));
for(i = 0; i < height; i++)
rev[i] = (unsigned char *)malloc(width * sizeof(unsigned char));
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
read = fread(&px, sizeof(char), 1, fin);
rev[i][j] = px;
}
}
sum = 0;
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
//Top row of image
if(i == 0)
{
if(j == 0)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j + 1] +
(unsigned)rev[i + 1][j] + (unsigned)rev[i + 1][j + 1]) / 4;
else if(j == width - 1)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j - 1] +
(unsigned)rev[i + 1][j] + (unsigned)rev[i + 1][j - 1]) / 4;
else
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j - 1] + (unsigned)rev[i][j + 1] +
(unsigned)rev [i + 1][j] + (unsigned)rev[i + 1][j - 1] + (unsigned)rev[i + 1][j + 1]) / 6;
}
//Bottom row of image
else if(i == height - 1)
{
if(j == 0)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j + 1] +
(unsigned)rev[i - 1][j] + (unsigned)rev[i - 1][j + 1]) / 4;
else if(j == width - 1)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j - 1] +
(unsigned)rev[i - 1][j] + (unsigned)rev[i - 1][j - 1]) / 4;
else
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j - 1] + (unsigned)rev[i][j + 1] +
(unsigned)rev[i - 1][j] + (unsigned)rev[i - 1][j - 1] + (unsigned)rev[i - 1][j + 1]) / 6;
}
//Left side of image (excluding top or bottom row)
else if(j == 0)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i - 1][j] + (unsigned)rev[i + 1][j] +
(unsigned)rev[i][j + 1] + (unsigned)rev[i - 1][j + 1] + (unsigned)rev[i + 1][j + 1]) / 6;
//Right side of image (excluding top or bottom row)
else if(j == width - 1)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i - 1][j] + (unsigned)rev[i + 1][j] +
(unsigned)rev[i][j - 1] + (unsigned)rev[i - 1][j - 1] + (unsigned)rev[i + 1][j - 1]) / 6;
rev[i][j] = (unsigned char)sum;
}
}
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
if(j < width && i < height)
fwrite(&rev[i][j], sizeof(char), 1, fout);
}
}
fclose(fout);
fclose(fin);
return 0;
}
Note: this code only works with .raw
grayscale images.
EDIT: I've implemented blurring to the rest of the image, but am still not getting a readable output file. I'm not sure if my arithmetic is correct, as I'm not too familiar with adding bytes together.