This is my first post; I hope I get the netiquette right. :-) I've been stuck on pset4 / filters / blur for days. The code compiles.
When I run it through check50, these two points are correct: :) blur correctly filters pixel on edge :) blur correctly filters pixel in corner
The rest are incorrect.
I've also been getting the same runtime errors at the same spot in the code for days and I can't figure out why.
- helpers.c:145:85 (and every other "middle of loop" line where w = w +1): runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
- helpers.c:245:32 (always at round(green) and I can't fathom how that could be a negative number): runtime error: -2 is outside the range of representable values of type 'unsigned char'
- The picture always comes out purple with dots of green (RGB green at 255, blue and red 0)
I would really appreciate any tips on where to look! I am completely flummoxed.
void blur(int height, int width, RGBTRIPLE image[height][width])
{
//make a temp image for storing values
RGBTRIPLE imagetemp[height][width];
//make a copy for reference
RGBTRIPLE imagecopy[height][width];
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
imagecopy[h][w] = image[h][w];
}
}
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
BYTE avgred, avggreen, avgblue;
float red, green, blue;
//top edge case
if (h == 0)
{
//left edge case
if (w == 0)
{
red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w + 1].rgbtRed) / 4);
green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h + 1][w].rgbtGreen + imagecopy[h + 1][w + 1].rgbtGreen) / 4);
blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w + 1].rgbtBlue) / 4);
}
//right edge case
if (w == width)
{
red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w - 1].rgbtRed) / 4);
green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h + 1][w].rgbtGreen + imagecopy[h + 1][w - 1].rgbtGreen) / 4);
blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w - 1].rgbtBlue) / 4);
}
//middle of image
if (w > 0 && w < width)
{
red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w + 1].rgbtRed + imagecopy[h + 1][w - 1].rgbtRed) / 6);
green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h + 1][w].rgbtGreen + imagecopy[h + 1][w + 1].rgbtGreen + imagecopy[h + 1][w - 1].rgbtGreen) / 6);
blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w + 1].rgbtBlue + imagecopy[h + 1][w - 1].rgbtBlue) / 6);
}
}
//bottom edge case
if (h == height)
{
//left edge case
if (w == 0)
{
red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w + 1].rgbtRed) / 4);
green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen + imagecopy[h - 1][w + 1].rgbtGreen) / 4);
blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w + 1].rgbtBlue) / 4);
}
//right edge case
if (w == width)
{
red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w - 1].rgbtRed) / 4);
green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen + imagecopy[h - 1][w - 1].rgbtGreen) / 4);
blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w - 1].rgbtBlue) / 4);
}
if (w != 0 && w != width)
{
red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w + 1].rgbtRed + imagecopy[h - 1][w - 1].rgbtRed) / 6);
green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen - imagecopy[h - 1][w + 1].rgbtGreen + imagecopy[h - 1][w - 1].rgbtGreen) / 6);
blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w + 1].rgbtBlue + imagecopy[h - 1][w - 1].rgbtBlue) / 6);
}
}
if (h != 0 && h != height)
// middle of image when 0 < h < height
{
//left edge case
if (w == 0)
{
red = ((imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w + 1].rgbtRed + imagecopy[h][w].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w + 1].rgbtRed) / 6);
green = ((imagecopy[h + 1][w].rgbtGreen + imagecopy[h + 1][w + 1].rgbtGreen + imagecopy[h][w].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen + imagecopy[h - 1][w + 1].rgbtGreen) / 6);
blue = ((imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w + 1].rgbtBlue + imagecopy[h][w].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w + 1].rgbtBlue) / 6);
}
//right edge case
if (w == width)
{
red = ((imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w - 1].rgbtRed + imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w - 1].rgbtRed) / 6);
green = ((imagecopy[h + 1][w].rgbtGreen + imagecopy[h + 1][w - 1].rgbtGreen + imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen + imagecopy[h - 1][w - 1].rgbtGreen) / 6);
blue = ((imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w - 1].rgbtBlue + imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w - 1].rgbtBlue) / 6);
}
//middle of image
if (w > 0 && w < width)
{
red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w + 1].rgbtRed + imagecopy[h - 1][w - 1].rgbtRed + imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w + 1].rgbtRed + imagecopy[h + 1][w - 1].rgbtRed) / 9);
green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen - imagecopy[h - 1][w + 1].rgbtGreen + imagecopy[h - 1][w - 1].rgbtGreen + imagecopy[h + 1][w].rgbtGreen - imagecopy[h + 1][w + 1].rgbtGreen + imagecopy[h + 1][w - 1].rgbtGreen) / 9);
blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w + 1].rgbtBlue + imagecopy[h - 1][w - 1].rgbtBlue + imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w + 1].rgbtBlue + imagecopy[h + 1][w - 1].rgbtBlue) / 9);
}
}
avgred = round(red);
avggreen = round(green);
avgblue = round(blue);
imagetemp[h][w].rgbtRed = avgred;
imagetemp[h][w].rgbtGreen = avggreen;
imagetemp[h][w].rgbtBlue = avgblue;
}
}
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
image[h][w] = imagetemp[h][w];
}
}
return;
}