Reflect Filter-less CS50

1.8k Views Asked by At

Currently, I am doing pset4 filter-less, reflect and struggling with the code I wrote. It compiles fine, but the output picture looks exactly like the input picture. I am trying to first store the reflected image in a temporary array and transfer it to the image array. I was unable to find anyone who has tried something similar. This is what I have written so far.

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int i;                              //row
    int j;                              //column for img
    int z;                              //column of tmp img
    RGBTRIPLE tmpi[height][width];      //tmp img
    for (i = 0; i < height; i++)
    {
        for (j = 0, z = width; j > z; j++, z--)
        {
            image[i][j].rgbtRed = tmpi[i][z].rgbtRed;
            image[i][j].rgbtBlue = tmpi[i][z].rgbtBlue;
            image[i][j].rgbtGreen = tmpi[i][z].rgbtGreen;
        }
    }
    for (i = 0; i < height; i ++)
    {
        for (j = 0; j < width; j++)
        {
            tmpi[i][j].rgbtRed = image[i][j].rgbtRed;
            tmpi[i][j].rgbtBlue = image[i][j].rgbtBlue;
            tmpi[i][j].rgbtGreen = image[i][j].rgbtGreen;
        }
    }
    return;
}

Can you please help me out?

3

There are 3 best solutions below

2
Gerhardh On BEST ANSWER

You have 2 errors:

  1. You first copy uninitialized memory.
    for (i = 0; i < height; i++)
    {
        for (j = 0, z = width; j > z; j++, z--)
        {
            image[i][j].rgbtRed = tmpi[i][z].rgbtRed;

The array tmpi contains indetermined values as you never assign anything to it.

You first need to copy the array to tmpi before you can copy it back.

  1. Your limits for the loop are wrong:
        for (j = 0, z = width; j > z; j++, z--)
        {
            image[i][j].rgbtRed = tmpi[i][z].rgbtRed;
            ...
  • Here, z may only go up to width-1.
  • j>z is wrong condition. It will never be true.
  • Proper condition would be j<width.
  • Not an error but you don't really need z. Just use width-j-1
  • Also not an error but there is no need to copy each member of a struct. You can just assign the whole struct at once.

A fixed version looks like this:

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int i;                              //row
    int j;                              //column for img

    RGBTRIPLE tmpi[height][width];      //tmp img

    for (i = 0; i < height; i ++)
    {
        for (j = 0; j < width; j++)
        {
            tmpi[i][j] = image[i][j];
        }
    }


    for (i = 0; i < height; i++)
    {
        for (j = 0; j < width; j++)
        {
            image[i][j] = tmpi[i][width - j - 1];
        }
    }
    return;
}

Or you could even combine the outer loops:

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int i;                              //row
    int j;                              //column for img

    RGBTRIPLE tmpi[height][width];      //tmp img

    for (i = 0; i < height; i ++)
    {
        for (j = 0; j < width; j++)
        {
            tmpi[i][j] = image[i][j];
        }

        for (j = 0; j < width; j++)
        {
            image[i][j] = tmpi[i][width - j - 1];
        }
    }
    return;
}

Or you could swap the values in place:

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    int i;                              //row
    int j;                              //column for img

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

Feel free to think about more optimizations, once it is working correctly.

0
Prachi Jain On

You are reinitializing the color bytes whereas we need to swap out the pixels here, I suggest making a swap function first like this:

void swap (RGBTRIPLE *a, RGBTRIPLE *b)
{
    RGBTRIPLE temp = *a;
    *a = *b;
    *b = temp;
}

Then swap the pixels, as we are swapping halfway make sure to run j for width/2

Here's the code for your reference:

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        int k = width - 1;
        for (int j = 0; j < width/2; j++)
        {
            swap(&image[i][j], &image[i][k]);
            k--;
        }
    }
    return;
}
2
Lucysicecream On

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width]) 
{
    for (int i = 0; i < height; i++) /*per column*/
    {
        RGBTRIPLE temp[height][width]; 
        int n = width;
        int j = 0;
        do  /*copy in temp file*/
        {
            temp[i][width - 1] = image[i][j];
            j++;
            width--;
        }
        while (width != 0);
        
        j = 0;
        width = n;
        do
        {
            image[i][j] = temp[i][j];
            j++;
        }
        while (j != width);
    }
    return;
}