'CS50 pset4 Blur (how to solve)
been stuck at blur for the longest time every (even more than tideman) and i do not know what is wrong with my code. It is returning the same image back to me when i do Check50. Even tried to malloc but not sure why it is not working. Here is my code
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
int sumRed = 0;
int sumBlue = 0;
int sumGreen = 0;
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// discuss exceptions
//4 corners
if (i == 0 && j == 0)
{
sumRed = image[0][0].rgbtRed + image[0][1].rgbtRed + image[1][0].rgbtRed;
sumBlue = image[0][0].rgbtBlue + image[0][1].rgbtBlue + image[1][0].rgbtBlue;
sumGreen = image[0][0].rgbtGreen + image[0][1].rgbtGreen + image[1][0].rgbtGreen;
temp[0][0].rgbtRed = round(sumRed/3);
temp[0][0].rgbtBlue = round(sumBlue/3);
temp[0][0].rgbtGreen = round(sumGreen/3);
}
else if (i == height - 1 && j == 0)
{
sumRed = image[height - 1][0].rgbtRed + image[height - 2][0].rgbtRed + image[height - 1][1].rgbtRed;
sumBlue = image[height - 1][0].rgbtBlue + image[height - 2][0].rgbtBlue + image[height - 1][1].rgbtBlue;
sumGreen = image[height - 1][0].rgbtGreen + image[height - 2][0].rgbtGreen + image[height - 1][1].rgbtGreen;
temp[height - 1][0].rgbtRed = round(sumRed/3);
temp[height - 1][0].rgbtBlue = round(sumBlue/3);
temp[height - 1][0].rgbtGreen = round(sumGreen/3);
}
else if ( i == 0 && j == width - 1)
{
sumRed = image[0][width - 1].rgbtRed + image[0][width - 2].rgbtRed + image[1][width - 1].rgbtRed;
sumBlue = image[0][width - 1].rgbtBlue+ image[0][width - 2].rgbtBlue + image[1][width - 1].rgbtBlue;
sumGreen = image[0][width - 1].rgbtGreen + image[0][width - 2].rgbtGreen + image[1][width - 1].rgbtGreen;
temp[0][width - 1].rgbtRed = round(sumRed/3);
temp[0][width - 1].rgbtBlue = round(sumBlue/3);
temp[0][width - 1].rgbtGreen = round(sumGreen/3);
}
else if ( i == height - 1 && j == width - 1)
{
sumRed = image[height - 1][width - 1].rgbtRed + image[height - 1][width - 2].rgbtRed + image[height - 2][width - 1].rgbtRed;
sumBlue = image[height - 1][width - 1].rgbtBlue+ image[height - 1][width - 2].rgbtBlue + image[height - 2][width - 1].rgbtBlue;
sumGreen = image[height - 1][width - 1].rgbtGreen + image[height - 1][width - 2].rgbtGreen + image[height - 2][width - 1].rgbtGreen;
temp[height - 1][width - 1].rgbtRed = round(sumRed/3);
temp[height - 1][width - 1].rgbtBlue = round(sumBlue/3);
temp[height - 1][width - 1].rgbtGreen = round(sumGreen/3);
}
//4 sides
else if (i == 0 && (j != 0) && (j != width -1)) // (top row)
{
for (int a = i; a < i + 2 ; a++)
{
for (int b = j - 1; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else if (i == height - 1 && (j != 0) && (j != width -1)) // (bottom row)
{
for (int a = i - 1; a < i + 1 ; a++)
{
for (int b = j - 1; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else if (j == 0 && (i != 0) && (i != height -1)) // (left column)
{
for (int a = i - 1; a < i + 2 ; a++)
{
for (int b = j; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else if (j == width - 1 && (i != 0) && (i != height - 1)) // (right column)
{
for (int a = i - 1; a < i + 2 ; a++)
{
for (int b = j - 1; b < j + 1 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/6);
temp[i][j].rgbtBlue = round(sumBlue/6);
temp[i][j].rgbtGreen = round(sumGreen/6);
}
}
}
else
{
// Add everything around a pixel (9)
for (int a = i - 1; a < i + 2 ; a++)
{
for (int b = j - 1; b < j + 2 ; b++)
{
sumRed += image[a][b].rgbtRed;
sumBlue += image[a][b].rgbtBlue;
sumGreen += image[a][b].rgbtGreen;
temp[i][j].rgbtRed = round(sumRed/9);
temp[i][j].rgbtBlue = round(sumBlue/9);
temp[i][j].rgbtGreen = round(sumGreen/9);
}
}
}
}
}
return;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = temp[i][j].rgbtRed;
image[i][j].rgbtGreen = temp[i][j].rgbtGreen;
image[i][j].rgbtBlue = temp[i][j].rgbtBlue;
}
}
}
I understand that it is abit tedious + there are ways to improve it, but given my logic of solving this pset, are there any errors? Thanks everyone in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
