Screenshot image is too dark

Started by napier, January 01, 2005, 14:55:54

Previous topic - Next topic

napier

I'm using glReadPixels to grab framebuffer or pbuffer pixels, convert to BufferedImage, and save as PNG.  The code below produces an image that is noticeably darker than the screen image.  

Any ideas why the BufferedImage would darken the image?

   public void screenShot(int width, int height)
     {
         // allocate space for RBG pixels
         ByteBuffer fb = GLApp.allocBytes(width * height * 3);
         int[] pixels = new int[width * height];
         int bindex;
         // grab a copy of the current frame contents as RGB
         GL11.glReadPixels(0, 0, width, height, GL11.GL_RGB, GL11.GL_BYTE, fb);
         // convert RGB data in ByteBuffer to integer array
         for (int i=0; i < pixels.length; i++) {
             bindex = i * 3;
             pixels[i] =
                 ((fb.get(bindex) << 16))  +
                 ((fb.get(bindex+1) << 8))  +
                 ((fb.get(bindex+2) << 0));
         }
         // Create a BufferedImage with the RGB pixels then save as PNG
         try {
             BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
             image.setRGB(0, 0, width, height, pixels, 0, width);
             javax.imageio.ImageIO.write(image, "png", new File("screenshot_test.png"));
         }
         catch (Exception e) {
             System.out.println("ScreenShot() exception: " +e);
         }
     }


Running on Win32, lwjgl .92,  Nvidia GeForce4
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl

napier

D'oh!

2 minutes after posting I found the answer: use GL_UNSIGNED_BYTE in readpixels.

GL11.glReadPixels(0, 0, width, height, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, fb);


copies pixels with no color distortion.
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl

WiESi

Look here, I'm saving the image with DevIL, much faster than ImageIO: http://lwjgl.org/forum/viewtopic.php?t=861

WiESi

napier

Thanks for the DevIL link.  I considered going that route, and may still, but for now I don't want to include another libe.
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl