Im doing the filter-less problem in CS50, i did pretty much all the other filter correctly but im a little struggling with the blur.
I don't really get how malloc works (i just ctrl+c ctrl+v how they allocated space for 'image' to do 'copy') and how to do the average of the surrounding pixels, i tried something with the average function i thought it would work but it tells me 'floating point exception (core dumped)' and i think the problem is in the for loop but idk how to fix it honestly.
Usually i like to found solutions to my problems by myself but here i really don't understand im on this one for almost 2 weeks and i don't wanna see videos.
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE (*copy)[width] = calloc(height, width * sizeof(RGBTRIPLE));
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
copy[i][j] = image[i][j];
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = average(i, j, height, width, copy);
}
}
free(copy);
return;
}
RGBTRIPLE average (int pxlheight, int pxlwidth, int height, int width, RGBTRIPLE
copy[height][width])
{
RGBTRIPLE caca;
int x = 0;
int y = 0;
for (int i = pxlheight - 1; i <= pxlheight + 1 && i >= 0 && i <= height - 1; i++)
{
y++;
for (int j = pxlwidth - 1; j <= pxlwidth + 1 && j >= 0 && j <= width - 1; j++)
{
x++;
caca.rgbtRed += copy[i][j].rgbtRed;
caca.rgbtGreen += copy[i][j].rgbtGreen;
caca.rgbtBlue += copy[i][j].rgbtBlue;
}
}
caca.rgbtRed = round(caca.rgbtRed / (x * y));
caca.rgbtGreen = round(caca.rgbtGreen / (x * y));
caca.rgbtBlue = round(caca.rgbtBlue / (x * y));
return caca;
}