Hello Guest

Saving Screenshots

  • 24 Replies
  • 21737 Views
Re: Saving Screenshots
« Reply #15 on: August 13, 2011, 12:22:13 »
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:

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

With this:

Code: [Select]
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."

Re: Saving Screenshots
« Reply #16 on: August 13, 2011, 23:14:54 »
Try this:

Code: [Select]
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

Re: Saving Screenshots
« Reply #17 on: August 14, 2011, 11:42:20 »
Doesn't work.

What do you mean I should do with longs?

Re: Saving Screenshots
« Reply #18 on: August 15, 2011, 05:29:51 »
This code worked for me:

Code: [Select]
        // 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

Re: Saving Screenshots
« Reply #19 on: August 15, 2011, 12:06:45 »
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.

Re: Saving Screenshots
« Reply #20 on: August 16, 2011, 02:50:47 »
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

Re: Saving Screenshots
« Reply #21 on: August 16, 2011, 12:35:29 »
Okay, I will.

Re: Saving Screenshots
« Reply #22 on: August 16, 2011, 23:14:56 »

*

Offline kappa

  • *****
  • 1319
Re: Saving Screenshots
« Reply #23 on: August 17, 2011, 08:44:59 »
oh nice work, pretty well written tutorial.

Re: Saving Screenshots
« Reply #24 on: August 17, 2011, 12:13:12 »
Haha I saw the tweet. Thanks!