BGRA to RGBA

If you want to use juce-loaded images for OpenGL textures, you will want to preprocess them to swap the red and blue channels if you’re using Windows.

The code is obvious, but it might save someone a minute:

	static void exchangeRedAndBlue(Image* image) {
		int lineStride, pixelStride;

		uint8* pixels = image->lockPixelDataReadWrite(
				0, 0, image->getWidth(),
				image->getHeight(),
				lineStride,
				pixelStride);

		uint8 temp;

		int index = 0;
		for(int i = 0; i < image->getWidth(); i++) {
			for(int j = 0; j < image->getHeight(); j++) {
				index = (i * image->getHeight() + j) * pixelStride;
				temp = pixels[index + 0];
				pixels[index + 0] = pixels[index + 2];
				pixels[index + 2] = temp;
			}
		}
		image->releasePixelDataReadWrite(pixels);
	}

I am using 1.46 - I know there was a change to images recently so hopefully this still works.

Daven,

Just so you know, OpenGL can do this for you too. You just tag the format of the pixels.

Bruce

Do you mean the BGRA_EXT? I played around with that without success.

Also had a quick look at OpenGLPixelFormat but I can’t see anything there that lets me choose the order of colours.

PixelFormat is more to do with the context. You can use GL_BGRA as the pixel format when you upload textures. It worka fine - in fact it’s quicker, generally, it’s often RGBA that needs to be swizzled.

Bruce