I would like to know if someone can think about a better solution for doing this stuff.
I have an array of byte raw bitmap data in 32bit format (RGBA)
I need to transform this array in an 24bit format by eliminating the alpha component.
Additionally i need to transpose the data by aligning the red component in the first part of the result array, the green in after the first 1/3 and the blue after the 2/3 because i'm doing an RLE compression after.
So instead of having an array that look like R,G,B,R,G,B,R.... i will have an array that look like R,R,R,R....G,G,G,G.....B,B,B,B
By doing this alignment, i have a better RLE compression because there is more chance to have alignment in each component between pixels.
I'm using unsafe and fixed pointer to do that. I'm getting something like 10ms (~31000 ticks) to process for a 8.3M byte array length. It seems to be low but i m doing a realtime screengrabbing and every ms is counting...
byte[] d1 = new Byte[width*height*4];
.....
byte[] datain24format = new byte[width*heigth * 3];
unsafe
{
fixed(byte* pd1=d1)
{
fixed(byte* pd24b=datain24format)
{
int* actualpointeur = (int*)pd1 + (datalenght / 4) - 1;
byte* redindexp = (pd24b + (datain24format.Length / 3)) - 1;
byte* greenindexp = (pd24b + (datain24format.Length / 3) * 2) - 1;
byte* blueindexp = (pd24b + datain24format.Length) - 1;
for (int i = datalenght - 1; i >= 0; i -= 4, actualpointeur--, redindexp--, greenindexp--, blueindexp--)
{
*redindexp = (byte)*(actualpointeur);
*greenindexp = (byte)(*(actualpointeur) >> 8);
*blueindexp = (byte)(*(actualpointeur) >> 16);
}
}
}
}
Update
i tried the loop unrolling (didn t know this tips before) by 16 operation (more doesn't make better performance)
My result is now 5ms (~15000 ticks) :)
here is my new loop :
for (int i = _DataLenght - 1; i >= 0; i -= 64, actualpointeur-=16,redindexp-=16,greenindexp-=16,blueindexp-=16)
{
*redindexp = (byte)*(actualpointeur);
*greenindexp = (byte)(*(actualpointeur) >> 8);
*blueindexp = (byte)(*(actualpointeur) >> 16);
*(redindexp-1) = (byte)*(actualpointeur-1);
*(greenindexp-1) = (byte)(*(actualpointeur-1) >> 8);
*(blueindexp-1) = (byte)(*(actualpointeur-1) >> 16);
......
*(redindexp - 15) = (byte)*(actualpointeur - 15);
*(greenindexp - 15) = (byte)(*(actualpointeur - 15) >> 8);
*(blueindexp - 15) = (byte)(*(actualpointeur - 15) >> 16);
}