Reputation: 362
Is there a simple way to get an rgba int[]
from an argb BufferedImage
? I need it to be converted for opengl, but I don't want to have to iterate through the pixel array and convert it myself.
Upvotes: 0
Views: 1499
Reputation: 43319
GL_BGRA
pixel format and reversed packed pixels.On the surface BGRA
does not sound like what you want, but let me explain.
Calls like glTexImage2D (...)
do what is known as pixel transfer, which involves packing and unpacking image data. During the process of pixel transfer, data conversion may be performed, special alignment rules may be followed, etc. The data conversion step is what we are particularly interested in here; you can transfer pixels in a number of different layouts besides the obvious RGBA
component order.
If you reverse the byte order (e.g. data type = GL_UNSIGNED_INT_8_8_8_8_REV
) together with a GL_BGRA
format, you will effectively transfer ARGB pixels without any real effort on your part.
glTexImage2D (...)
call:glTexImage2D (..., GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, image);
The usual use-case for _REV
packed data types is handling endian differences between different processors, but it also comes in handy when you want to reverse the order of components in an image (since there is no such thing as GL_ARGB
).
Do not convert things for OpenGL - it is perfectly capable of doing this by itself.
Upvotes: 2
Reputation: 942
In order to transition between argb and rgba you can apply "bit-wise shifts" in order to convert them back and forth in a fast and concise format.
argb = rgba <<< 8
rgba = argb <<< 24
If you have any further questions, this topic might should give you a more in-depth answer on converting between rgba and argb.
Also, if you'd like to learn more about java's bitwise operators check out this link
Upvotes: 0