I seem to be having problems with rounding numbers in these two functions. I am making sure to work with floats and I'm using the round() function so I'm stuck scratching my head at the minute! Grayscale is really baffling me as the code is so simple I can't see where I'm going wrong with it. The single pixel without whole number average is resulting in 27 rather than the expected 28.
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
float avg = round(image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3;
image[i][j].rgbtRed = avg;
image[i][j].rgbtGreen = avg;
image[i][j].rgbtBlue = avg;
}
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE imagecopy[height][width];
float totalRed;
float totalGreen;
float totalBlue;
float counter;
for (int i = 0; i < height; i++) //loop through rows
{
for (int j = 0; j < width; j++) //loop through columns
{
totalRed = 0;
totalGreen = 0;
totalBlue = 0;
counter = 0;
//loop through all possible pixels in 3 x 3 box
for (int k = -1; k < 2; k++ )
{
{
for(int l = -1; l < 2; l++)
{
if((i + k) >= 0 && (i + k) <= height && (j + l) >= 0 && (j + l) < width)
{
counter++;
totalRed += image[i + k][j + l].rgbtRed;
totalGreen += image[i + k][j + l].rgbtGreen;
totalBlue += image[i + k][j + l].rgbtBlue;
}
}
}
}
imagecopy[i][j].rgbtRed = round(totalRed / counter);
imagecopy[i][j].rgbtGreen = round(totalGreen / counter);
imagecopy[i][j].rgbtBlue = round(totalBlue / counter);
}
}
//copy blurred value pixels into image array
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = imagecopy[i][j].rgbtRed;
image[i][j].rgbtGreen = imagecopy[i][j].rgbtGreen;
image[i][j].rgbtBlue = imagecopy[i][j].rgbtBlue;
}
}
return;
}