0

The following code keeps on giving me bugs about something I can't figure out when checking with check50 and I'm not sure what's wrong.

here's the code:

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

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < width; i++)
    {
        for (int d = 0; d < height; d++)
        {
            int originalRed = image[d][i].rgbtRed;
            int originalBlue = image[d][i].rgbtBlue;
            int originalGreen = image[d][i].rgbtGreen;

            int newRed =  (int)((float)round(.393 * originalRed + .769 * originalGreen + .189 * originalBlue));
            int newGreen = (int)((float)round(.349 * originalRed + .686 * originalGreen + .168 * originalBlue));
            int newBlue = (int)((float)round(.272 * originalRed + .534 * originalGreen + .131 * originalBlue));

            image[d][i].rgbtRed = fmin(255, newRed);
            image[d][i].rgbtGreen = fmin(255, newGreen);
            image[d][i].rgbtBlue = fmin(255, newBlue);

            return;
        }
    }
}

And here are the frowny faces:

for grayscale:

:( grayscale correctly filters more complex 3x3 image
expected "20 20 20\n50 5...", not "20 20 20\n50 5..."

for sepia:

:( sepia correctly filters simple 3x3 image
expected "100 89 69\n100...", not "100 89 69\n255..."
:( sepia correctly filters more complex 3x3 image
expected "25 22 17\n66 5...", not "25 22 17\n40 5..."
:( sepia correctly filters 4x4 image
expected "25 22 17\n66 5...", not "25 22 17\n40 5..."

Any help is much appreciated. Thank you very much :)

1

If you look at the extended check50 results in the link at the end of the message, there are more clues to be found.

For the greyscale issue, the last pixel in the 3x3 image is off by 1, to the high direction. The problem lies here:

int average = (int) ceilf(avg);

The code should be using a rounding function, not a ceiling function. The ceiling function adjusts to the next higher integer if there's a fractional part of the input number. It's the opposite of a truncate function. Neither ceiling nor truncate functions make a decision based on the value of the fraction. That's why the rounding function is needed.

For the Sepia function, the problem is totally different. The first pixel is correctly calculated, but the remaining pixels are unchanged. The instinct might be to check the calculations, looking for a flaw, but they are fine.

The problem lies in the placement of the return statement. The loop will only run once because of that placement. I'll leave it to you to figure out the simple fix. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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