4

So i'm doing filter and I noticed that its not rounding up correctly.

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    // Iterate over height
    for (int h = 0; h < height; h++)
    {
        // Iterare over width
        for (int w = 0; w < width; w++)
        {
            // Calculate the average of rgbt
            int k = round((image[h][w].rgbtBlue + image[h][w].rgbtGreen + image[h][w].rgbtRed)/3);

            // Update rgbt
            image[h][w].rgbtBlue = k;
            image[h][w].rgbtGreen = k;
            image[h][w].rgbtRed = k;
        }
    }
    return;
}

:) helpers.c exists

:) filter compiles

:) grayscale correctly filters single pixel with whole number average

:( grayscale correctly filters single pixel without whole number average expected "28 28 28\n", not "27 27 27\n"

:) grayscale leaves alone pixels that are already gray

:) grayscale correctly filters simple 3x3 image

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

:( grayscale correctly filters 4x4 image expected "20 20 20\n50 5...", not "20 20 20\n50 5..."

So I tried to run the file to se what was happening and

$ ./filter -g yard.bmp test.bmp

Could not open yard.bmp.

1 Answer 1

23

You are dividing an integer by an integer. This is integer division, and will produce an integer again, ignoring the fractional part.

You could make it a floating point division by making at least one operand floating point, like replacing 3 with 3.0.

8
  • do you have any idea why I can't run the program?
    – Chris
    Commented Jan 20, 2020 at 10:54
  • That part is trivial: You have no file named yard.bmp in the current directory. Maybe you have to specify its path? (try ./filter -g images/yard.bmp test.bmp)
    – Blauelf
    Commented Jan 20, 2020 at 10:56
  • That is odd because I have a file on pset4/filter called image with a bunch of images. and I used the code given from the edX website to try to run it. But the check comes out fine now. It says it works
    – Chris
    Commented Jan 20, 2020 at 10:59
  • 1
    filter does not automatically add the images, you have to specify the full path (like images/yard.bmp or ~/pset4/resize/images/yard.bmp)
    – Blauelf
    Commented Jan 20, 2020 at 11:01
  • okey so I needed to add images/yard.bmp and not just yard.bmp into the command line. Thank you
    – Chris
    Commented Jan 20, 2020 at 11:05

You must log in to answer this question.

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