0

Can someone help me out with this? This code duplicates the images not reflecting it.

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int* r = malloc(sizeof(int));
    int* g = malloc(sizeof(int));
    int* b = malloc(sizeof(int));

    for (int i = 0; i < height - 1; i++)
    {
        for (int j = 0, z = round(((width - 1) / 2)); j < z; j++)
        {
            for (int k = width - 1; k > j; k--)
            {
                //store first pixel values in rgb malloc memories

                *r = image[i][j].rgbtRed;
                *g = image[i][j].rgbtGreen;
                *b = image[i][j].rgbtBlue;

                //change first pixel values to the last pixel values

                image[i][j].rgbtRed = image[i][k].rgbtRed;
                image[i][j].rgbtGreen = image[i][k].rgbtGreen;
                image[i][j].rgbtBlue = image[i][k].rgbtBlue;

                //change last pixel values to the values that were stored in rgb malloc

                image[i][k].rgbtRed = *r;
                image[i][k].rgbtGreen = *g;
                image[i][k].rgbtBlue = *b;
            }
        }
    }
    free(r);
    free(g);
    free(b);
    return;
}

#update 1

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

Try to use 2 loops instead of 3 and instead of using k, use (width-1)-i. you don't need to use pointers in this set because you are working with structures. try to use a temporary structure (RGBTRIPLE temp) inside the loop to swap the structures (e.g. temp = image[i][j]) instead of swapping the red, green and blue values.

2
  • Hello! Thanks for answering! I just revised my code and updated above but still not working :(
    – James Lee
    May 11 '20 at 16:45
  • Got it! Thanks!
    – James Lee
    May 12 '20 at 18:10
2

Have a closer look on the "i"s when swapping the pixels.

Greetz

1
  • Works! Thanks!!
    – James Lee
    May 12 '20 at 18:11

You must log in to answer this question.

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