glDrawPixels and mangled colors

Started by ravenshadow, February 08, 2007, 07:58:16

Previous topic - Next topic

ravenshadow

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

Matzon

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 ?

Fool Running

Can you paste the BufferedImage creation code? To get mine to work (before I started using DevIL) I had to do this:
        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

ravenshadow

according to (BufferedImage).getColorModel().hasAlpha() there aren't any alpha values values in the image.
and I'm creating the buffered image with...
        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 :(

Fool Running

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

ravenshadow

yeah, all image viewers show them correctly.

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

jhelp

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 :
imageBuffer.flip();

by
imageBuffer.rewind();

I hopes it's could help you

KenRussell

Did you try using GL_UNSIGNED_BYTE in place of GL_BYTE?

DrAnonymous

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>

Sardtok

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:
GL11.glDrawPixels(
                tex.getTexWidth(), 
                tex.getTexHeight(), 
                GL11.GL_RGB, 
                GL11.GL_BYTE, 
                tex.data);

To this:
GL11.glDrawPixels(
                tex.getTexWidth(), 
                tex.getTexHeight(), 
                GL11.GL_RGB, 
                GL11.GL_UNSIGNED_BYTE, 
                tex.data);
igg -- Take me off for great justice?