Saving Screenshots

Started by CodeBunny, August 10, 2011, 21:14:28

Previous topic - Next topic

CodeBunny

The buffer place isn't off, the pixels are in the correct locations. The color values are just 1/2 what they should be.

For example, if I replace this:

int color = (0xFF << 24) | // For a completely opaque image.
			(red << 16) |
			(green << 8) |
			(blue);
	image.setRGB(i, height - (j + 1), color);


With this:

image.setRGB(i, height - (j + 1), color << ((0xFF << 24) |
									(red << 16) |
									(green << 8) |
									(blue)) << 1);


It works. Basically, by bit-shifting, the color values work.

To me this looks like a sign issue. Unfortunately, when I try to use glReadPixels with a GL_UNSIGNED_INT, I get the opposite problem - it's too bright.

I would use this code since because it works fine; however, I want to be sure that this bug isn't machine-dependent. This feels a little "hackish."

jediTofu

Try this:

int color = (0xFF << 24) | // For a completely opaque image.
			((red & 0xFF) << 16) |
			((green & 0xFF) << 8) |
			(blue & 0xFF);
	image.setRGB(i, height - (j + 1), color);


If this doesn't work, I'd try using longs (but type would still be GL_UNSIGNED_INT).

EDIT:  also try "& 0xFF" after the shifts instead.
cool story, bro

CodeBunny

Doesn't work.

What do you mean I should do with longs?

jediTofu

This code worked for me:

        // read
        int bpp = 4;
        int height = HEIGHT;
        int width = WIDTH;
        ByteBuffer screenBuffer = BufferUtils.createByteBuffer(width * height * bpp);
        glReadPixels(0,0,width,height,GL_RGBA,GL_UNSIGNED_BYTE,screenBuffer);

        // write
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
        for(int y = 0; y < height; ++y) {
          for(int x = 0; x < width; ++x) {
            int i = y * width * bpp + x * bpp;
            int r = screenBuffer.get(i) & 0xFF;
            int g = screenBuffer.get(i + 1) & 0xFF;
            int b = screenBuffer.get(i + 2) & 0xFF;
            image.setRGB(x,y,(0xFF << 24) | (r << 16) | (g << 8) | b);
          }
        }

        // save
        try {
          ImageIO.write(image,"PNG",new File("test.png"));
        }
        catch(Throwable t) {
          t.printStackTrace();
        }
cool story, bro

CodeBunny

Sweet, that worked! Thank you very much! :D

I feel like this should be put up in the wiki as a tutorial. Does anyone else want to do it, or should I? I'm perfectly willing to, I just don't have a wiki account.

jediTofu

I can when I get some free time from work/life :)  but I'm sure they are perfectly fine with giving you a wiki account.  maybe message Kappa?  I think he edits the wiki the most
cool story, bro

CodeBunny


CodeBunny


kappa

oh nice work, pretty well written tutorial.

CodeBunny

Haha I saw the tweet. Thanks!