how to decompose integer array to a byte array (pixel codings)

妖精的绣舞 提交于 2020-01-23 17:47:05

问题


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;

回答1:


C# code


        // convert integer array representing [argb] values to byte array representing [bgr] values
        private byte[] convertArray(int[] array)
        {
            byte[] newarray = new byte[array.Length * 3];
for (int i = 0; i < array.Length; i++) {
newarray[i * 3] = (byte)array[i]; newarray[i * 3 + 1] = (byte)(array[i] >> 8); newarray[i * 3 + 2] = (byte)(array[i] >> 16);
} return newarray; }



回答2:


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



回答3:


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();



回答4:


Try to use Buffer Class

byte[] bytes = new byte[ints.Length*4];
Buffer.BlockCopy(ints, 0, bytes, 0, ints.Length * 4);



回答5:


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

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



来源:https://stackoverflow.com/questions/392371/how-to-decompose-integer-array-to-a-byte-array-pixel-codings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!