LWJGL Forum

Programming => OpenGL => Topic started by: adam_1212 on May 06, 2010, 03:53:46

Title: How can i get the real color in 2d game?
Post by: adam_1212 on May 06, 2010, 03:53:46
How can i get the real color in 2d game
(http://lwjgl.org/forum/index.php?action=dlattach;topic=3332.0;attach=385;image)
Title: Re: How can i get the real color in 2d game?
Post by: wondersonic on May 06, 2010, 10:25:27
Hey, I see you tried the wonderful Propotyp project.

In fact, you just suffer from the correction of BUG http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6879279 (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6879279) from JDK 1.6.0_18.

Indeed they changed the type for PNG images so you have to convert back RGBA components.

For example, I do:

buffImage = ImageIO.read(cl.getResource(path));
final byte[] data = ((DataBufferByte) buffImage.getRaster().getDataBuffer()).getData();
                switch (buffImage.getType()) {
                    case BufferedImage.TYPE_4BYTE_ABGR:
                        convertFromARGBToBGRA(data);
                        break;
                    case BufferedImage.TYPE_3BYTE_BGR:
                        convertFromBGRToRGB(data);
                        break;
                }
...

    private static void convertFromARGBToBGRA(final byte[] data) {
        final int size = data.length;
        for (int i = 0; i < size; i += 4) {
            final int a = data[i] & 0x000000FF;
            final int r = data[i + 1] & 0x000000FF;
            final int g = data[i + 2] & 0x000000FF;
            final int b = data[i + 3] & 0x000000FF;

            data[i] = (byte) b;
            data[i + 1] = (byte) g;
            data[i + 2] = (byte) r;
            data[i + 3] = (byte) a;
        }
    }

    private static void convertFromBGRToRGB(final byte[] data) {
        final int size = data.length;
        for (int i = 0; i < size; i += 3) {
            final int b = data[i] & 0xFF;
            final int g = data[i + 1] & 0xFF;
            final int r = data[i + 2] & 0xFF;

            data[i] = (byte) r;
            data[i + 1] = (byte) g;
            data[i + 2] = (byte) b;
        }
    }


WS