LWJGL Forum

Programming => OpenGL => Topic started by: mimhof on March 31, 2006, 12:08:19

Title: Image drawing: Wrong colors
Post by: mimhof on March 31, 2006, 12:08:19
I try to draw an org.eclipse.swt.graphics.Image on a GLCanvas (lwjgl binding for java). But the image (The eclipse.gif) is reddish instead of blue.
It's seems I'm reading the wrong format (GL_RGB). But what's the
format of an image??

Here's the code:


public void drawImage(Image image, int x, int y) throws LWJGLException {
   int width = image.getBounds().width;
   int height = image.getBounds().height;

   ByteBuffer byteBuffer = ByteBuffer.allocateDirect(width * height * 3)
       .order(ByteOrder.nativeOrder()).put(image.getImageData().data);
   byteBuffer.flip();
   
   GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
   GL11.glRasterPos2f(x, y);
   GL11.glPixelZoom(1.0f, -1.0f); // Flip image
   GL11.glDrawPixels(width, height, GL11.GL_RGB,         GL11.GL_UNSIGNED_BYTE, byteBuffer);
Title: Image drawing: Wrong colors
Post by: napier on March 31, 2006, 15:10:28
Probably the image format in Java is ARGB.  Unless you specified otherwise, that's the default.  

You can convert the java pixels to RGBA, then use GL11.GL_RGBA in your drawpixels() call.  Here's some code I use to convert Java pixels (int) to RGBA (bytes).


   public static byte[] convertARGBtoRGBA(int[] jpixels)
   {
       byte[] bytes = new byte[jpixels.length*4];  // will hold pixels as RGBA bytes
       int p, r, g, b, a;
       int j=0;
       for (int i = 0; i < jpixels.length; i++) {
           p = jpixels[i];
           a = (p >> 24) & 0xFF;  // get pixel bytes in ARGB order
           r = (p >> 16) & 0xFF;
           g = (p >> 8) & 0xFF;
           b = (p >> 0) & 0xFF;
           bytes[j+0] = (byte)r;  // fill in bytes in RGBA order
           bytes[j+1] = (byte)g;
           bytes[j+2] = (byte)b;
           bytes[j+3] = (byte)a;
           j += 4;
       }
       return bytes;
   }


Also if you want good performance, drawPixels() is not best.  It's better to make a texture with the image and texture it onto a quad.
Title: Image drawing: Wrong colors
Post by: mimhof on April 05, 2006, 06:25:49
I tried to use the convertion from ARGB to RGBA but then I get
white vertical stripes!! That's not the problem.
Is there something wrong with the color set?
[/img]