# How to conserve original values in blur function? (pset 4)

Here's my code inside the blur function (in pset 4, Filter-Less):

``````int redSum;
int greenSum;
int blueSum;
int n;

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
redSum = 0;
greenSum = 0;
blueSum = 0;
n = 0;

for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if (i + x >= 0 && j + y >= 0)
{
redSum += image[i + x][j + y].rgbtRed;
greenSum += image[i + x][j + y].rgbtGreen;
blueSum += image[i + x][j + y].rgbtBlue;
n++;
}
}
}
image[i][j].rgbtRed = redSum / n;
image[i][j].rgbtGreen = greenSum / n;
image[i][j].rgbtBlue = blueSum / n;
}
}
return;
``````

It only passes one of the tests (correctly filters pixel in corner) and I think I know what the issue is: it works correctly for the pixel at image, on the top-left, but then once this value is modified all other values are off because they no longer calculate the averages of the original values (hope this is clear enough, English isn't my first language). So I think the solution would be to find a way to conserve the original values for each pixel, but how?

Of course, I'm not looking for the exact answer, just for some hints in the right direction ;)

EDIT: I've updated my code and this is working better:

``````int redSum;
int greenSum;
int blueSum;
float n;
RGBTRIPLE tmp[height][width];

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
redSum = 0;
greenSum = 0;
blueSum = 0;
n = 0;
tmp[i][j] = image[i][j];

for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if (i + x >= 0 && j + y >= 0)
{
redSum += image[i + x][j + y].rgbtRed;
greenSum += image[i + x][j + y].rgbtGreen;
blueSum += image[i + x][j + y].rgbtBlue;
n++;
}
}
}
tmp[i][j].rgbtRed = round(redSum / n);
tmp[i][j].rgbtGreen = round(greenSum / n);
tmp[i][j].rgbtBlue = round(blueSum / n);
}
}

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = tmp[i][j].rgbtRed;
image[i][j].rgbtGreen = tmp[i][j].rgbtGreen;
image[i][j].rgbtBlue = tmp[i][j].rgbtBlue;
}
}
return;
``````

Now it passes the first 3 tests (correctly filters middle pixel, pixel on edge, and pixel in corner) but not the last 2 (correctly filters 3x3 image and 4x4 image), and I'm not sure why...

``````tmp[i][j].rgbtRed = round(redSum / n);