LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: napier on January 01, 2005, 14:55:54

Title: Screenshot image is too dark
Post by: napier on January 01, 2005, 14:55:54
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
Title: Screenshot image is too dark
Post by: napier on January 01, 2005, 15:11:49
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.
Title: Screenshot image is too dark
Post by: WiESi on January 01, 2005, 17:22:32
Look here, I'm saving the image with DevIL, much faster than ImageIO: http://lwjgl.org/forum/viewtopic.php?t=861

WiESi
Title: Screenshot image is too dark
Post by: napier on January 02, 2005, 09:45:22
Thanks for the DevIL link.  I considered going that route, and may still, but for now I don't want to include another libe.