0

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.

name
  • 362
  • 2
  • 12
  • 1
    Why do you even want to convert the data at all? R,G,B,A are at this point just arbitrary labels for the 1st, 2nd. 3rd and fourth channel - the data can be swizzled around as needed when accessing the texture, with no extra cost. – derhass Jan 23 '15 at 22:12

2 Answers2

2

OpenGL 1.2+ supports a 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.

Example 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.

Community
  • 1
  • 1
Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
  • In fact, on many GPU / CPU combinations, `GL_BGRA` + `GL_UNSIGNED_INT_8_8_8_8_REV` is the highest performance pixel transfer available _("fast path")_ because that is the order the GPU wants the data in to begin with and passing the data this way means the driver does not have to do any conversion ;) – Andon M. Coleman Jan 24 '15 at 01:35
0

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

Community
  • 1
  • 1
Alec
  • 942
  • 5
  • 11