0

I have been working on the blur function for PSET4 and when i test the function, the provided picture is blurred but i still get a few errors when i run check50. See link to check50's results:

Check50: https://submit.cs50.io/check50/953fd88ff79766cfdccb1552ded6e7f3a9eccf92

Sorry for length of the function, i couldn't find a more efficient way to solve the problem.

void blur(int height, int width, RGBTRIPLE image[height][width])
{
int new_red , new_blue , new_green , counter;
///copying images into a new array
RGBTRIPLE imageO[height][width];

for (int i = 0 ; i < height ; i++)

for (int j = 0 ; j < width; j++)
{
      imageO[i][j] = image[i][j];
}
///looping over copied imaged and storing values into variables: new_red , new_blue , new_green
for(int i = 0 ; i < height ; i++)

for(int j = 0; j < width ; j++)
{
counter = 0;
new_red = 0;
new_blue = 0;
new_green = 0;
///adding right pixel
if(j == 0)
{
new_red = imageO[i][j + 1].rgbtRed + imageO[i][j].rgbtRed;
new_green =  imageO[i][j + 1].rgbtGreen + imageO[i][j].rgbtGreen;
new_blue = imageO[i][j + 1].rgbtBlue + imageO[i][j].rgbtBlue;
counter =  2.0;
}
// adding left pixel
if(j == width - 1 )
{
new_red = imageO[i][j - 1].rgbtRed + imageO[i][j].rgbtRed;
new_green = imageO[i][j - 1].rgbtGreen  + imageO[i][j].rgbtRed;
new_blue = imageO[i][j - 1].rgbtBlue  + imageO[i][j].rgbtRed;
counter =  2.0;
}
// adding right and left pixel
if((j > 0 ) && (j < width -1))
{
new_red = imageO[i][j + 1 ].rgbtRed +  imageO[i][j - 1 ].rgbtRed + imageO[i][j].rgbtRed;
new_green = imageO[i][j + 1 ].rgbtGreen +  imageO[i][j - 1 ].rgbtGreen +  imageO[i][j].rgbtGreen;
new_blue = imageO[i][j + 1 ].rgbtBlue +  imageO[i][j - 1 ].rgbtBlue +  imageO[i][j].rgbtBlue;
counter =  3.0;
}
//adding pixels above and to the right
if((i > 0) && (j == 0))
{
new_red += imageO[i - 1][j].rgbtRed + imageO[i - 1][j + 1].rgbtRed;
new_green += imageO[i - 1][j].rgbtGreen + imageO[i - 1][j + 1].rgbtGreen;
new_blue += imageO[i - 1][j].rgbtBlue + imageO[i - 1][j + 1].rgbtBlue;
counter +=   2.0 ;
}
//adding pixels above and to the left
if((i > 0) && (j == width - 1 ))
{
new_red += imageO[i - 1][j].rgbtRed + imageO[i - 1][j - 1].rgbtRed;
new_green += imageO[i - 1][j].rgbtGreen + imageO[i - 1][j - 1].rgbtGreen;
new_blue += imageO[i - 1][j].rgbtBlue + imageO[i - 1][j - 1].rgbtBlue;
counter += 2.0;
}
//adding pixels above to the right and left
if((i > 0 ) && (j > 0 && j < width - 1))
{
new_red += imageO[i - 1][j].rgbtRed + imageO[i - 1][j - 1].rgbtRed + imageO[i - 1][j + 1].rgbtRed;
new_green += imageO[i - 1][j].rgbtGreen + imageO[i -  1][j - 1].rgbtGreen + imageO[i - 1][j + 1].rgbtGreen;
new_blue += imageO[i - 1][j].rgbtBlue + imageO[i -  1][j - 1].rgbtBlue +  imageO[i - 1][j + 1].rgbtBlue;
counter +=  3.0;
}
///adding pixels below  and to the right and left
if((i < height - 1) && (j > 0 && j < width - 1))
{
new_red += imageO[i + 1][j].rgbtRed + imageO[i + 1][j - 1].rgbtRed + imageO[i + 1][j + 1].rgbtRed;
new_green += imageO[i + 1][j].rgbtGreen + imageO[i + 1][j - 1].rgbtGreen + imageO[i + 1][j + 1].rgbtGreen;
new_blue += imageO[i + 1][j].rgbtBlue + imageO[i + 1][j - 1].rgbtBlue +  imageO[i + 1][j + 1].rgbtBlue;
counter +=  3.0;
}
///adding pixels below and to the right
if((i < height - 1) && (j == 0))
{
new_red += imageO[i + 1][j].rgbtRed +imageO[i + 1][j + 1].rgbtRed;
new_green += imageO[i + 1][j].rgbtGreen +imageO[i + 1][j + 1].rgbtGreen;
new_blue += imageO[i + 1][j].rgbtBlue +imageO[i + 1][j + 1].rgbtBlue;
counter  +=   2.0;
}
//adding pixles below and to the left
if((i < height - 1) && (j == width - 1))
{
new_red += imageO[i + 1][j].rgbtRed +imageO[i + 1][j - 1].rgbtRed;
new_green += imageO[i + 1][j].rgbtGreen +imageO[i + 1][j - 1].rgbtGreen;
new_blue += imageO[i + 1][j].rgbtBlue +imageO[i + 1][j - 1].rgbtBlue;
counter +=   2.0;
}


image[i][j].rgbtBlue =  round(new_blue/counter);
image[i][j].rgbtGreen = round(new_green/counter);
image[i][j].rgbtRed =  round(new_red/counter);


}

 return;

}

1 Answer 1

0

It's relatively useless to do counter = 2.0;, since you declared counter to be an integer, the 2.0 is converted to 2. Either declare it float, or convert in your division, like new_green / (float)counter or (float)new_green / counter.

Also, something is wrong here:

new_green = imageO[i][j - 1].rgbtGreen  + imageO[i][j].rgbtRed;
new_blue = imageO[i][j - 1].rgbtBlue  + imageO[i][j].rgbtRed;

To get rid of all the if/else, you could have two inner loops inside your loops, nesting a total of four. Those could then go from i-1 to i+1 and j-1 to j+1 (or something equivalent), either restricting the range to existing pixels in the loop or with a single if with a more complex condition in the loop.

1
  • The folly of copy and past, lesson learned. Thanks again!!
    – mo_coder
    Commented Jan 30, 2020 at 14:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .