0

I have this code:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE tempImage[height][width];
    int sumR = 0;
    int sumG = 0;
    int sumB = 0;

    int numPixels = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            tempImage[i][j] = image[i][j];
            for (int h = 1; h > -2; h--)
            {
                for (int w = 1; h > -2; h--)
                {
                    if (h >= 0 &&  w >= 0 && h < height && w < width)
                    {
                        sumR += image[i][j].rgbtRed;
                        sumG += image[i][j].rgbtGreen;
                        sumB += image[i][j].rgbtBlue;
                        numPixels ++;
                    }
                }
            }
            tempImage[i][j].rgbtRed = (sumR / numPixels);
            tempImage[i][j].rgbtGreen = (sumG / numPixels);
            tempImage[i][j].rgbtBlue = (sumB / numPixels);
        }
    }
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < height; j++)
        {
            image[i][j].rgbtRed = tempImage[i][j].rgbtRed;
            image[i][j].rgbtGreen = tempImage[i][j].rgbtGreen;
            image[i][j].rgbtBlue = tempImage[i][j].rgbtBlue;
        }
    }
    return;
}

When I run check50, I get every single one of the blur ones wrong:

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "70 84 94\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "25 35 45\n"
:( blur correctly filters pixel in corner
    expected "70 85 95\n", not "10 20 30\n"
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "10 20 30\n25 3..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "10 20 30\n25 3..."

If I change these loops:

for (int h = 1; h > -2; h--)
{
    for (int w = 1; h > -2; h--)
    {

to this

for (int h = -1; h < 2; h++)
{
    for (int w = -1; h < 2; h++)
    {

It creates a divide by zero error (which I fully understand). How can I fix this issue?

EDIT: I may have not made it clear, I want to be able to fix all the errors in check50

You must log in to answer this question.

Browse other questions tagged .