• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.jme3.util;
2 
3 import java.awt.image.BufferedImage;
4 import java.awt.image.DataBufferByte;
5 import java.awt.image.DataBufferInt;
6 import java.awt.image.WritableRaster;
7 import java.nio.ByteBuffer;
8 import java.nio.IntBuffer;
9 
10 public final class Screenshots {
11 
convertScreenShot2(IntBuffer bgraBuf, BufferedImage out)12     public static void convertScreenShot2(IntBuffer bgraBuf, BufferedImage out){
13         WritableRaster wr = out.getRaster();
14         DataBufferInt db = (DataBufferInt) wr.getDataBuffer();
15 
16         int[] cpuArray = db.getData();
17 
18         bgraBuf.clear();
19         bgraBuf.get(cpuArray);
20 
21 //        int width  = wr.getWidth();
22 //        int height = wr.getHeight();
23 //
24 //        // flip the components the way AWT likes them
25 //        for (int y = 0; y < height / 2; y++){
26 //            for (int x = 0; x < width; x++){
27 //                int inPtr  = (y * width + x);
28 //                int outPtr = ((height-y-1) * width + x);
29 //                int pixel = cpuArray[inPtr];
30 //                cpuArray[inPtr] = cpuArray[outPtr];
31 //                cpuArray[outPtr] = pixel;
32 //            }
33 //        }
34     }
35 
convertScreenShot(ByteBuffer bgraBuf, BufferedImage out)36     public static void convertScreenShot(ByteBuffer bgraBuf, BufferedImage out){
37         WritableRaster wr = out.getRaster();
38         DataBufferByte db = (DataBufferByte) wr.getDataBuffer();
39 
40         byte[] cpuArray = db.getData();
41 
42         // copy native memory to java memory
43         bgraBuf.clear();
44         bgraBuf.get(cpuArray);
45         bgraBuf.clear();
46 
47         int width  = wr.getWidth();
48         int height = wr.getHeight();
49 
50         // flip the components the way AWT likes them
51         for (int y = 0; y < height / 2; y++){
52             for (int x = 0; x < width; x++){
53                 int inPtr  = (y * width + x) * 4;
54                 int outPtr = ((height-y-1) * width + x) * 4;
55 
56                 byte b1 = cpuArray[inPtr+0];
57                 byte g1 = cpuArray[inPtr+1];
58                 byte r1 = cpuArray[inPtr+2];
59                 byte a1 = cpuArray[inPtr+3];
60 
61                 byte b2 = cpuArray[outPtr+0];
62                 byte g2 = cpuArray[outPtr+1];
63                 byte r2 = cpuArray[outPtr+2];
64                 byte a2 = cpuArray[outPtr+3];
65 
66                 cpuArray[outPtr+0] = a1;
67                 cpuArray[outPtr+1] = b1;
68                 cpuArray[outPtr+2] = g1;
69                 cpuArray[outPtr+3] = r1;
70 
71                 cpuArray[inPtr+0] = a2;
72                 cpuArray[inPtr+1] = b2;
73                 cpuArray[inPtr+2] = g2;
74                 cpuArray[inPtr+3] = r2;
75             }
76         }
77     }
78 }
79