name
name

Reputation: 362

Get RGBA pixels from ARGB BufferedImage?

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

Answers (2)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

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.

Upvotes: 2

Alec
Alec

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

Related Questions