0

My reflect function doesn't seem to do anything to the outputted image and my blur image fails check50 even though the resulting image looks visually blurred. Any insight into why this is the case is appreciated.

Reflect

For the reflect function the test fails as follows:

:( reflect correctly filters 1x2 image
    expected "0 0 255\n255 0...", not "255 0 0\n0 0 2..."
:( reflect correctly filters 1x3 image
    expected "0 0 255\n0 255...", not "255 0 0\n0 255..."
:) reflect correctly filters image that is its own mirror image
:( reflect correctly filters 3x3 image
    expected "70 80 90\n40 5...", not "10 20 30\n40 5..."
:( reflect correctly filters 4x4 image
    expected "100 110 120\n7...", not "10 20 30\n40 5..."

Logically it should work. All i'm doing it finding out the opposite key and swapping the data from one to the other. However, the image remains unchanged for this.

// 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; j++)
        {
            // the opposite of this one will be
            int ant = (width - 1) - j;

            // create a new temp var
            RGBTRIPLE row = {};

            // save the currents values into tmp
            row = image[i][j];

            // replace the currents value with the ant
            image[i][j] = image[i][ant];

            //transfer the values from tmp to the ant
            image[i][ant] = row;
        }
    }

    return;
}

When I try to debug by showing the values of row, image[i][j] and image[i][ant] it is getting swapped correctly but nothing ends up happening to the outputted image.

Blur

The image comes out looking blurry as expected. However the test fails on:

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

I'm assuming there is some problem in my logic but I am unable to see it.

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // create a tmp image because otherwise the colors will be off
    RGBTRIPLE tmp[height][width];

    // loop through each row
    for (int i = 0; i < height; i++)
    {
        // loop through each column
        for (int j = 0; j < width; j++)
        {
            // store the colors
            float red = 0;
            float green = 0;
            float blue = 0;

            // keep track of neighbours for later
            int neighbour = 0;

            // start from the top
            for (int k = -1; k < 2; k++)
            {
                // then to the left
                for (int l = -1; l < 2; l++)
                {
                    // is row and top greater than or eq to 0, row and top less than or equal to the height,
                    // column and left greater than or equal to 0, column and left less than the width
                    if ((i + k) >= 0 && (i + k) <= height && (j + l) >= 0 && (j + l) < width)
                    {
                        red += image[i + k][j + l].rgbtRed;
                        green += image[i + k][j + l].rgbtGreen;
                        blue += image[i + k][j + l].rgbtBlue;
                        neighbour++;
                    }
                }
            }

            tmp[i][j].rgbtRed = round(red / neighbour);
            tmp[i][j].rgbtGreen = round(green / neighbour);
            tmp[i][j].rgbtBlue = round(blue / neighbour);
        }

    }

    // transfer the tmp image to the real image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j] = tmp[i][j];
        }
    }

    return;
}
1

For reflect a question: How many swaps do you need to do to flip the image horizontaly? Think about it.

For blur you got it rigth, except for a little mispell in your 'if' line.

Edited because I didn't realize that you could copy an entire struct by simply:

 RGBTRIPLE row = {};
row = image[i][j];
1
  • How did I not see this! I had to divide width/2 because otherwise it was just swapping those pixels again. As for the if line i had to check if (i + k) were less than the height not less than and equal to. Thanks! May 24 '20 at 21:42

You must log in to answer this question.

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