0

my helpers doesn't pass check50 and I don't know why. This is the code:

#include "helpers.h"
#include <math.h>

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i ++)
{
    for (int j = 0; j < width; j ++)
    {
        float average = round((image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / (float)3);
        image[i][j].rgbtBlue = average;
        image[i][j].rgbtGreen = average;
        image[i][j].rgbtRed = average;
    }
}
return;
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i ++)
{
    for (int j = 0; j < width; j ++)
    {
        float sepiaRed = ((float)0.393 * image[i][j].rgbtRed) + ((float)0.769 * image[i][j].rgbtGreen) + ((float)0.189 * image[i][j].rgbtBlue);
        float sepiaGreen = ((float)0.349 * image[i][j].rgbtRed) + ((float)0.686 * image[i][j].rgbtGreen) + ((float)0.168 * image[i][j].rgbtBlue);
        float sepiaBlue = ((float)0.272 * image[i][j].rgbtRed) + ((float)0.534 * image[i][j].rgbtGreen) + ((float)0.131 * image[i][j].rgbtBlue);

        if (sepiaRed > 255)
        {
            sepiaRed = 255;
        }
        if (sepiaGreen > 255)
        {
            sepiaGreen = 255;
        }
        if (sepiaBlue > 255)
        {
            sepiaBlue = 255;
        }

        image[i][j].rgbtRed = round(sepiaRed);
        image[i][j].rgbtGreen = round(sepiaGreen);
        image[i][j].rgbtBlue = round(sepiaBlue);
    }
}
return;
 }

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i ++)
{
    for (int j = 0; j < width / 2; j ++)
    {
        RGBTRIPLE temp =  image[i][j];
        image[i][j] =  image[i][(width - 1) - j];
        image[i][(width - 1) - j] = temp;
    }
}
return;
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
float sum_red = 0, sum_green = 0, sum_blue = 0;
int count = 0;

RGBTRIPLE copy_image[height][width];

for (int i = 0; i < height; i ++)
{
    for (int j = 0; j < width; j ++)
    {
        copy_image[i][j] = image[i][j];

        //check if pixel has top left pixel
        if (i - 1 >= 0 && j - 1 >= 0)
        {
            sum_red  += image[i - 1][j - 1].rgbtRed;
            sum_green += image[i - 1][j - 1].rgbtGreen;
            sum_blue += image[i - 1][j - 1].rgbtBlue;
            count ++;
        }

        //check for top right pixel
        if (i - 1 >= 0 && j + 1 <= width - 1)
        {
            sum_red  += image[i - 1][j + 1].rgbtRed;
            sum_green += image[i - 1][j + 1].rgbtGreen;
            sum_blue += image[i - 1][j + 1].rgbtBlue;
            count ++;
        }

        //check for top pixel
        if (i - 1 >= 0)
        {
            sum_red  += image[i - 1][j].rgbtRed;
            sum_green += image[i - 1][j].rgbtGreen;
            sum_blue += image[i - 1][j].rgbtBlue;
            count ++;
        }

        //check for left pixel
        if (j - 1 >= 0)
        {
            sum_red  += image[i][j - 1].rgbtRed;
            sum_green += image[i][j - 1].rgbtGreen;
            sum_blue += image[i][j - 1].rgbtBlue;
            count ++;
        }

        //check for right pixel
        if (j + 1 <= width - 1)
        {
            sum_red  += image[i][j + 1].rgbtRed;
            sum_green += image[i][j + 1].rgbtGreen;
            sum_blue += image[i][j + 1].rgbtBlue;
            count ++;
        }

        //check for bottom left pixel
        if (i + 1 <= height - 1 && j - 1 >= 0)
        {
            sum_red  += image[i + 1][j - 1].rgbtRed;
            sum_green += image[i + 1][j - 1].rgbtGreen;
            sum_blue += image[i + 1][j - 1].rgbtBlue;
            count ++;
        }

        //check for bottom right pixel
        if (i + 1 <= height - 1 && j + 1 <= width - 1)
        {
            sum_red  += image[i + 1][j + 1].rgbtRed;
            sum_green += image[i + 1][j + 1].rgbtGreen;
            sum_blue += image[i + 1][j + 1].rgbtBlue;
            count ++;
        }

        //check for bottom pixel
        if (i + 1 <= height - 1)
        {
            sum_red  += image[i + 1][j].rgbtRed;
            sum_green += image[i + 1][j].rgbtGreen;
            sum_blue += image[i + 1][j].rgbtBlue;
            count ++;
        }

        //middle pixel
        sum_red  += image[i][j].rgbtRed;
        sum_green += image[i][j].rgbtGreen;
        sum_blue += image[i][j].rgbtBlue;
        count ++;

        copy_image[i][j].rgbtRed = round(sum_red / count);
        copy_image[i][j].rgbtGreen = round(sum_green / count);
        copy_image[i][j].rgbtBlue = round(sum_blue / count);

        image[i][j].rgbtRed = copy_image[i][j].rgbtRed;
        image[i][j].rgbtGreen = copy_image[i][j].rgbtGreen;
        image[i][j].rgbtBlue = copy_image[i][j].rgbtBlue;
    }
}
return;
}

this is the message it shows:

:( blur correctly filters middle pixel expected "127 140 149\n", not "113 128 138\n"

:( blur correctly filters pixel on edge expected "80 95 105\n", not "82 98 108\n"

:) blur correctly filters pixel in corner

:( blur correctly filters 3x3 image expected "70 85 95\n80 9...", not "70 85 95\n82 9..."

:( blur correctly filters 4x4 image expected "70 85 95\n80 9...", not "70 85 95\n82 9..."

1 Answer 1

1

This is happening because your loop is summing values from neighbors to the left and up of the current pixel that have already been blurred by previous iterations of the same loop. You need to check the values from one of your two copies of the image, and write the blurred values to another copy. Right now, you're writing to both copies at the end of the body of your loop. You're almost there!

If this answers your question, please click on the check mark to accept. Don't be shy to ask another question the next time you have an issue!

You must log in to answer this question.

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