views:

1077

answers:

4

Hi sorry for being annoying by rephrasing my question but I am just on the point of discovering my answer.

I have an array of int composed of RGB values, I need to decompose that int array into a byte array, but it should be in BGR order.

The array of int composed of RGB values is being created like so:

pix[index++] = (255 << 24) | (red << 16) | blue;
A: 
#define N something
unsigned char bytes[N*3];
unsigned int  ints[N];

for(int i=0; i<N; i++) {
    bytes[i*3]   = ints[i];       // Blue
    bytes[i*3+1] = ints[i] >> 8;  // Green
    bytes[i*3+2] = ints[i] >> 16; // Red
}
Can Berk Güder
Wrong langauge; magic numbers are evil
Jay Bazuzi
The question doesn't state a specific language, and I only saw the c# tag now.
Can Berk Güder
A: 

r = (pix[index] >> 16) & 0xFF

the rest is similar, just change 16 to 8 or 24.

Baczek
Can Berk Güder
+1  A: 
A: 

Using Linq:

        pix.SelectMany(i => new byte[] { 
            (byte)(i >> 0),
            (byte)(i >> 8),
            (byte)(i >> 16),
        }).ToArray();

Or

        return (from i in pix
                from x in new[] { 0, 8, 16 }
                select (byte)(i >> x)
               ).ToArray();
Jay Bazuzi