-2

I have a vector of RGB data.

vector<int> image({1,1,1, 2,2,2, 3,3,3 , 4,4,4}); 

I want to flip this image horizontally (about the center column).

to perform flip operation, I have created a function

flip(void* image, int rows, int cols)

where i would like to perform flip, but keep individual triplets of RGB intact

So I created a

struct color{
    char r;
    char g;
    char b;
};

and calling the function as

int main(){
vector<int> image({1,1,1, 2,2,2, 3,3,3 , 4,4,4});
flip(&image[0],rows,cols);
}

but casting void* to struct color[] raises compilation error. How can I proceed around this ?

Akshay Mathur
  • 560
  • 1
  • 4
  • 14

2 Answers2

1

You can't cast to an array in C++. You'll have to use a pointer.

struct color* image = (struct color*)arr;

And for your purposes, this won't work right away. You'll have to start using a vector of char not int. This is because and int is usually 4 bytes and a char is 1 byte. Trying to assign an int to your struct (which is 3 bytes) wouldn't give you the result you expect.

Then you can access the pointer as an array. For example image[0].r would return the first value in the vector, and image[1].r would return the fourth. Just be careful to not go beyond the size of the vector.

devil0150
  • 1,350
  • 3
  • 13
  • 36
0

i changed the struct as

struct color{
int rgb[3];
};

and

struct color* image = (struct color*)arr;

provides an array of struct color which can be flipped keeping the rgb data intact.

Akshay Mathur
  • 560
  • 1
  • 4
  • 14