I'm using the WinAPI to grab a bitmap of your monitor and then add that data to a texture. The problem however is that the GetDIBits function writes its color-data (lpPixels parameter) in BGR-format as opposed to RGB. Thus, assigning this data to a texture, which uses a RGB-formatted byte array, makes the order wrong. One can easily just make a for/while loop and just iterate through the arrays and fix the order, however, this process is extremely slow. You could also just directly assign the BGR-array to the RGB-array but this results in a texture which has the wrong RGB-order! On the other hand, the second method is extremely fast, which is exactly what I am looking for.
I suppose there might be ways to use bitmasks within the WinAPIs BITMAPINFO struct, but I do not know how or even if that would be possible. There might even be a variable of some sort which determines the color-order used by the WinAPI, either way, I haven't been able to find such a variable. Hence, my question: How do I quickly assign a BGR-formatted array to a RGB-formatted one?
Slow way of converting BGR to RGB:
for (size_t i = 0; i < myWidth * myHeight; i++)
{
size_t tempIndex = (i * 4);
// The colors are in BGR-format (windows format)
// Hence, the order is reversed
rgbPixels[tempIndex] = bgrPixels[tempIndex + 2]; // r
rgbPixels[tempIndex + 1] = bgrPixels[tempIndex + 1]; // g
rgbPixels[tempIndex + 2] = bgrPixels[tempIndex]; // b
rgbPixels[tempIndex + 3] = 255; // a (always 255)
}