0

I'm working through pset4 filter and the code for the reflect function. I'm still getting errors so clearly there's some kind of issue with my logic. When I try to apply filter to an actual image, I get this error: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'

My thinking was to iterate through the array of pixels in the bitmap with two loops, but only go halfway for the loop going horizontally. And then use a helper function to swap the RGB color values with each pixel with its opposite pixel (for the opposite pixel - the position with regards to height is the same but the width is the total width minus the width value for that pixel). What am I getting wrong here?

void swap(int *a, int *b);

void reflect(int height, int width, RGBTRIPLE image[height][width]) // TODO explore more with swap function and malloc from lesson
{
    for (int i = 0; i < height; i++)
    {
        for (int k = 0; k < (width / 2); k++)
        {
            int x = image[i][k].rgbtBlue;
            int y = image[i][width - k].rgbtBlue;
            int g = image[i][k].rgbtGreen;
            int h = image[i][width - k].rgbtGreen;
            int l = image[i][k].rgbtRed;
            int j = image[i][width - k].rgbtRed;
            swap(&x, &y);
            swap(&g, &h);
            swap(&l, &j);
        }
    }
    return;
}

void swap(int *a, int *b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

UPDATE:

I tried another solution here, where I adjusted the bounds of the array and also ditched the local variables, but it's still not working. What am I missing? Do I need to copy the entire image and not just pixel by pixel somehow?

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

2 Answers 2

1

There are two issues. First is the one that @Himanshi brought up - It's doing a great job of swapping x/y, g/h and i/j. But at what point is anything actually being changed in the image array? Did you notice that the output file contains the original image?

Second, have you looked carefully at the maximum indexes that you're using for the array? Is [width -k] a valid index when k = 0???

Programming tip: While all of the reassigning and swapping of individual struct elements rgbtRed, rgbtGreen and rgbtBlue are correct, the whole thing can be streamlined just by swapping the entire pixel struct. Instead of 9 lines plus an added swap function, the whole thing can be done in just 3 lines. Say that you had two pixels that you wanted to swap, pxl1 and pxl2.

        RGBTRIPLE temp = pxl1 ;
        pxl1 = pxl2;
        pxl2 = temp;

It's not necessary to copy each element of a struct when you want to copy an entire struct.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • so is iterating through the array the way that i am one of the issues? that I'm not correctly going through the array to swap?
    – getsendy
    Commented Jul 23, 2020 at 1:32
  • It's not that you're iteration through the array is wrong. You're swapping temp variables x,y,g,h,i,j and not the actual data in the array, image[i][k], image[i][width-k]. Remember, those temp vars only contain copies of the original data. you can do anything you want to them and it won't change the original data in the image.
    – Cliff B
    Commented Jul 23, 2020 at 1:40
  • Look at this: image[i][width - k] Say that k=0. That means it's really image[i][width] Is width a valid value for an array index?
    – Cliff B
    Commented Jul 24, 2020 at 4:20
1

when you are writing this 'int y = image[i][width - k].rgbtBlue;' think exactly what pixel is this thing swapping?..

3
  • it should swap with the opposite pixel right? say width = 100. image[i][1].rgbtBlue would swap with image[i][99].rgbtBlue? What am I misunderstanding here?
    – getsendy
    Commented Jul 22, 2020 at 4:18
  • 2
    If width is 100, then the last element in an array is 99. So, 0 and 99 should swap and 1 and 98 ... etc.
    – Hrvoje T
    Commented Jul 22, 2020 at 5:14
  • This is one of the problems. Remember, arrays start at 0, not 1, and stop at the number of elements - 1, not number of elements. So, if you have width = 100, it runs from 0 to 99, not 1 to 100.
    – Cliff B
    Commented Jul 22, 2020 at 17:47

You must log in to answer this question.

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