0

I'm testing the greyscale function for the filter problem in the Memory problem set. The compiler doesn't seem to have a problem with the code, but when I type in the command (./filter -g yard.bmp out.bmp) in the compiler,, it returns "Could not open yard.bmp."

As far as I can see the file name match those in the directory and the infile is the same directroy as the outfile (see below).

enter image description here

Some pointers in the right direction would be appreciated, below is my code:

void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++) {

        int red = image[i][j].rgbtRed;
        int green = image[i][j].rgbtGreen;
        int blue = image[i][j].rgbtBlue;

        int average = (red + green + blue) / 3.0;
        int new_value = round((double)average);

        red = 255 - new_value;
        green = 255 - new_value;
        blue = 255 - new_value;
        }

    }

I typed in the command expecting the out.bmp to appear in greyscale.

1 Answer 1

0

Your images are in the images directory, you need to include the directory in your command.

./filter -g images/yard.bmp out.bmp

This should help the program locate the yard.bmp file correctly.

Updated Grayscale function

void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int red = image[i][j].rgbtRed;
            int green = image[i][j].rgbtGreen;
            int blue = image[i][j].rgbtBlue;

            int average = round((red + green + blue) / 3.0);

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

The average should be an integer, so double is not required. You also should set the RGB values to the average and not subtract it from 255.

Note: out.bmp must not be used by another program or must not be opened during the execution.

Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.