I created a for loop checking each pixel around the one to be blurred. If the pixel was in range, its RGB values would be added to a total variable to then be averaged. My program works for all requirements except the 3x3 and 4x4 images. Here are my results: https://submit.cs50.io/check50/b0fce8b4cfdbb28d3a22b6f7c93c885a8fc60b8a
I'm not sure why it successfully filters all the pixels in an image but 3x3 and 4x4. I tried playing with some of the if statements to make it < width
and < height
instead of <= width
and <= height
but that still didn't work. I don't know what else to try.
Here is my code:
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE new_image[height][width];
// fully duplicate image to avoid blurring already blurred pixels
for (int i = 0; i < height; i++)
{
for (int x = 0; x < width; x++)
{
new_image[i][x].rgbtRed = image[i][x].rgbtRed;
new_image[i][x].rgbtGreen = image[i][x].rgbtGreen;
new_image[i][x].rgbtBlue = image[i][x].rgbtBlue;
}
}
for (int i = 0; i < height; i++)
{
for (int x = 0; x < width; x++)
{
/* center bottom middle top middle center right center left bottom right top left bottom left top right
totalColor = image[i][x] + image[i+1][x] + image[i-1][x] + image[i][x + 1] + image[i][x - 1] + image[i + 1][x + 1] + image[i - 1][x - 1] + image[i + 1][x - 1] + image[i - 1][x + 1]
*/
int pixels = 1;
int totalRed = 0, totalGreen = 0, totalBlue = 0;
// top left
if (i - 1 >= 0 && x - 1 >= 0)
{
pixels++;
totalRed += new_image[i - 1][x - 1].rgbtRed;
totalGreen += new_image[i - 1][x - 1].rgbtGreen;
totalBlue += new_image[i - 1][x - 1].rgbtBlue;
}
// top middle
if (i - 1 >= 0)
{
pixels++;
totalRed += new_image[i - 1][x].rgbtRed;
totalGreen += new_image[i - 1][x].rgbtGreen;
totalBlue += new_image[i - 1][x].rgbtBlue;
}
// top right
if (i - 1 >= 0 && x + 1 <= width)
{
pixels++;
totalRed += new_image[i - 1][x + 1].rgbtRed;
totalGreen += new_image[i - 1][x + 1].rgbtGreen;
totalBlue += new_image[i - 1][x + 1].rgbtBlue;
}
// center left
if (x - 1 >= 0)
{
pixels++;
totalRed += new_image[i][x - 1].rgbtRed;
totalGreen += new_image[i][x - 1].rgbtGreen;
totalBlue += new_image[i][x - 1].rgbtBlue;
}
// center right
if (x + 1 <= width)
{
pixels++;
totalRed += new_image[i][x + 1].rgbtRed;
totalGreen += new_image[i][x + 1].rgbtGreen;
totalBlue += new_image[i][x + 1].rgbtBlue;
}
// bottom left
if (i + 1 <= height && x - 1 >= 0)
{
pixels++;
totalRed += new_image[i + 1][x - 1].rgbtRed;
totalGreen += new_image[i + 1][x - 1].rgbtGreen;
totalBlue += new_image[i + 1][x - 1].rgbtBlue;
}
// bottom middle
if (i + 1 <= height)
{
pixels++;
totalRed += new_image[i + 1][x].rgbtRed;
totalGreen += new_image[i + 1][x].rgbtGreen;
totalBlue += new_image[i + 1][x].rgbtBlue;
}
// bottom right
if (i + 1 <= height && x + 1 <= width)
{
pixels++;
totalRed += new_image[i + 1][x + 1].rgbtRed;
totalGreen += new_image[i + 1][x + 1].rgbtGreen;
totalBlue += new_image[i + 1][x + 1].rgbtBlue;
}
image[i][x].rgbtRed = round((float)(totalRed + new_image[i][x].rgbtRed) / pixels);
image[i][x].rgbtGreen = round((float)(totalGreen + new_image[i][x].rgbtGreen) / pixels);
image[i][x].rgbtBlue = round((float)(totalBlue + new_image[i][x].rgbtBlue) / pixels);
}
}
return;
}```