Hello Guest

glDrawPixels and mangled colors

  • 9 Replies
  • 7902 Views
glDrawPixels and mangled colors
« on: February 08, 2007, 07:58:16 »
greetings. I've been struggling with this all day and would appreciate it if someone could point me in the right direction.
I'm trying to draw a bufferedimage using glDrawPixels (just to make sure my image loading is working)

It loads fine and draws but the colors get mangled. At first I thought it was due to the image maybe being in a BGR format instead of an RGB so I wrote a function to swap the R and B in the ByteBuffer of the image data but that just messed up the colors in a different way.

heres a screenshot before the color component swap:


and this is after the swap:


here's how I'm getting the ByteBuffer from the image.
Code: [Select]
    public static void setData(BufferedImage image, Texture tex)
    {
        ByteBuffer imageBuffer = null;
        byte[] data = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
       
        System.err.println("BI CM:"+image.getColorModel());
       
        imageBuffer = ByteBuffer.allocateDirect(data.length);
        imageBuffer.order(ByteOrder.nativeOrder());
        imageBuffer.put(data, 0, data.length);
        imageBuffer.flip();
       
        System.err.println(" CALC:"+tex.getTexWidth() * tex.getTexHeight());
        convertBGRtoRGB(imageBuffer);
       
        tex.data = imageBuffer;
    }

here's how I tried converting...
Code: [Select]
    public static void convertBGRtoRGB(ByteBuffer in)
    {
        int pixels = in.limit() / 3;
       
        System.err.println("PIXELS:"+pixels);
       
        for (int i = 0; i < in.limit(); i += 3)
        {
            byte b = in.get(i);
            byte g = in.get(i + 1);
            byte r = in.get(i + 2);
            in.put(i, r);
            in.put(i + 1, g);
            in.put(i + 2, b);
        }
    }

and here's how I'm drawing...
Code: [Select]
        GL11.glDrawPixels(
                tex.getTexWidth(),
                tex.getTexHeight(),
                GL11.GL_RGB,
                GL11.GL_BYTE,
                tex.data);

If anyone has any ideas what I'm doing wrong I'ld sure like to here it.

Thanks,
Ravenshadow

*

Offline Matzon

  • *****
  • 2242
Re: glDrawPixels and mangled colors
« Reply #1 on: February 08, 2007, 10:12:03 »
as much as I would like to see the bikini-clad girl in real colors (there's a way to get help, eh? - make sure your images contain something "interesting"), I can't really see anything wrong...
Are there any alpha values in the BufferedImage ?

Re: glDrawPixels and mangled colors
« Reply #2 on: February 08, 2007, 14:27:27 »
Can you paste the BufferedImage creation code? To get mine to work (before I started using DevIL) I had to do this:
Code: [Select]
        Image image = (new ImageIcon(file.getPath())).getImage();
        BufferedImage tex = new BufferedImage(image.getWidth(null), image.getHeight(null)
                                              , BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = (Graphics2D)tex.getGraphics();
        g.drawImage(image, null, null);
        g.dispose();
        tex = flipImage(tex);
        System.out.println("JPG/PNG/GIF file");
        return tex;
Probably not the most efficient way to load, but it worked ;D
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: glDrawPixels and mangled colors
« Reply #3 on: February 08, 2007, 18:26:23 »
according to (BufferedImage).getColorModel().hasAlpha() there aren't any alpha values values in the image.
and I'm creating the buffered image with...
Code: [Select]
        BufferedImage bufferedImage = ImageIO.read(new File(ref));

so I don't think I'm doing anything silly

I tried loading the image with DevIL and (other than the image is right side up the color defects are still there. It looks to me as if the color bytes aren't in the order I think they are, but I have no way of checking (other than drawing what's there and getting what I'm getting).

I'll try loading the image like mentioned above and see if that changes anything.

EDIT: just tried it... same thing :(
« Last Edit: February 08, 2007, 18:30:55 by ravenshadow »

Re: glDrawPixels and mangled colors
« Reply #4 on: February 08, 2007, 18:55:09 »
What image format is it in? (jpeg, bmp, tga, etc.) If you load it in an image viewer, are the colors correct?
Ummmmm... other than that I'm out of ideas ???
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: glDrawPixels and mangled colors
« Reply #5 on: February 08, 2007, 20:45:42 »
yeah, all image viewers show them correctly.

I think I'll try a different approach. Thanks for the help guys.

Re: glDrawPixels and mangled colors
« Reply #6 on: February 15, 2007, 09:06:35 »
Hello,
May be it didn't work for you, but for me it's worked when I have had a prblem like this.
Try to replace :
Code: [Select]
imageBuffer.flip();by
Code: [Select]
imageBuffer.rewind();I hopes it's could help you

Re: glDrawPixels and mangled colors
« Reply #7 on: February 17, 2007, 09:23:49 »
Did you try using GL_UNSIGNED_BYTE in place of GL_BYTE?

Re: glDrawPixels and mangled colors
« Reply #8 on: February 20, 2007, 05:04:45 »
I cropped just the area of the girl in an image program and looked at the colors used in the image.  There aren't any reds at all.  Only greens, blues, and blacks.  I also tried to roll the colors assuming RGB -> BRG -> GBR.  Nothing looked correct.

Hope this helps.

Dr.A>

Re: glDrawPixels and mangled colors
« Reply #9 on: February 21, 2007, 22:52:36 »
Like KenRussel said, have you tried using GL_UNSIGNED_BYTE when drawing it?
Not sure if it could be loaded as a texture with unsigned bytes or not, but worth a shot...
Changing this:
Code: [Select]
GL11.glDrawPixels(
                tex.getTexWidth(),
                tex.getTexHeight(),
                GL11.GL_RGB,
                GL11.GL_BYTE,
                tex.data);
To this:
Code: [Select]
GL11.glDrawPixels(
                tex.getTexWidth(),
                tex.getTexHeight(),
                GL11.GL_RGB,
                GL11.GL_UNSIGNED_BYTE,
                tex.data);
igg -- Take me off for great justice?