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;
}