I'm trying to load textures to use with NDK OpenGL from Java with the Bitmap class. It works, but I'm having problems with the pixel format.
First, in Java, I load a bitmap from the assets folder like this:
Bitmap bitmap = BitmapFactory.decodeStream(amgr.open(path));
return bitmap.copy(Bitmap.Config.ARGB_8888, false);
the bitmap config does not have an option for RGBA channel order.
[JNI things happen here]
Using GLES 1, I then buffer the texture like so:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
// `pixels` is the pixel buffer I produced earlier.
As you can see, there is a problem with the pixel format. glTexImage2D
does not have an option for ARGB, but the Java Bitmap class does not have an option to create a buffer in RGBA. So I end up with messed up color channels. I do need the alpha channel by the way.
The question is: how do I most efficiently produce a pixel buffer in RGBA8888 format from the Java bitmap class, or, how do I load a GL texture in ARGB8888 format?
Surely there is a way other than manually swapping bytes pixel-by-pixel?
I am currently doing this:
void pxl::swap_channels_ARGB_to_RGBA(void *pixBuf, const int len)
{
jint *pixels = (jint *)pixBuf;
for(int i = 0; i < len; i++)
{
jint pixel = pixels[i];
jint a = (pixel >> 24) & 0xFF;
jint r = (pixel >> 16) & 0xFF;
jint g = (pixel >> 8) & 0xFF;
jint b = (pixel >> 0) & 0xFF;
pixels[i] = (jint)(a | (r << 24 ) | (g << 16) | (b << 8));
}
}
Or maybe there is another error? Not exactly sure about the glTexImage2D
options to be honest.
Thanks!