-
Notifications
You must be signed in to change notification settings - Fork 79
/
ScreenShotEncoder.java
224 lines (180 loc) · 5.42 KB
/
ScreenShotEncoder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package mod.chiselsandbits.share;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.IntBuffer;
import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.client.shader.Framebuffer;
public class ScreenShotEncoder
{
public static BufferedImage getScreenshot()
{
int width = Minecraft.getMinecraft().displayWidth;
int height = Minecraft.getMinecraft().displayHeight;
final Framebuffer buffer = Minecraft.getMinecraft().getFramebuffer();
if ( OpenGlHelper.isFramebufferEnabled() )
{
width = buffer.framebufferTextureWidth;
height = buffer.framebufferTextureHeight;
}
final int pixels = width * height;
final IntBuffer pixelBuffer = BufferUtils.createIntBuffer( pixels );
final int[] pixelValues = new int[pixels];
GL11.glPixelStorei( GL11.GL_PACK_ALIGNMENT, 1 );
GL11.glPixelStorei( GL11.GL_UNPACK_ALIGNMENT, 1 );
pixelBuffer.clear();
if ( OpenGlHelper.isFramebufferEnabled() )
{
GlStateManager.bindTexture( buffer.framebufferTexture );
GL11.glGetTexImage( GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, pixelBuffer );
}
else
{
GL11.glReadPixels( 0, 0, width, height, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, pixelBuffer );
}
pixelBuffer.get( pixelValues );
TextureUtil.processPixelValues( pixelValues, width, height );
BufferedImage screenshotImage = null;
if ( OpenGlHelper.isFramebufferEnabled() )
{
screenshotImage = new BufferedImage( buffer.framebufferWidth, buffer.framebufferHeight, BufferedImage.TYPE_INT_RGB );
final int textureFrameDiff = buffer.framebufferTextureHeight - buffer.framebufferHeight;
for ( int y = textureFrameDiff; y < buffer.framebufferTextureHeight; ++y )
{
for ( int x = 0; x < buffer.framebufferWidth; ++x )
{
screenshotImage.setRGB( x, y - textureFrameDiff, pixelValues[y * buffer.framebufferTextureWidth + x] );
}
}
}
else
{
screenshotImage = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
screenshotImage.setRGB( 0, 0, width, height, pixelValues, 0, width );
}
return screenshotImage;
}
static public void encodeScreenshot(
final BufferedImage screenshot,
final byte[] modelData,
final OutputStream outfile ) throws IOException
{
final int sizeOfData = 4 + modelData.length;
final int extraStoragePerPixel = 3;
final int storagePerImagePixel = 1;
final int width = screenshot.getWidth();
final int height = screenshot.getHeight();
final int innerImageStorage = width * height * storagePerImagePixel;
int extraXStorage = 0;
int extraYStorage = 0;
int newXData = 0;
int newYData = 0;
while ( sizeOfData > innerImageStorage + extraXStorage + extraYStorage )
{
if ( newYData < newXData )
{
newYData += 2;
extraYStorage += extraStoragePerPixel * ( width + newXData );
}
else
{
newXData += 2;
extraXStorage += extraStoragePerPixel * ( height + newYData );
}
}
final BufferedImage output = new BufferedImage( width + newXData, height + newYData, BufferedImage.TYPE_4BYTE_ABGR );
final int zeros[] = new int[output.getWidth() * output.getHeight() * 4];
output.setRGB( 0, 0, output.getWidth(), output.getHeight(), zeros, 0, output.getWidth() * 4 );
// transfer screenshot to output...
final Graphics g = output.getGraphics();
g.drawImage( screenshot, newXData / 2, newYData / 2, null );
g.dispose();
final int size = modelData.length;
final ImageWriter writer = new ImageWriter( output, storagePerImagePixel, extraStoragePerPixel );
// time to encode data..
writer.writeByte( size >> 24 );
writer.writeByte( size >> 16 & 0xff );
writer.writeByte( size >> 8 & 0xff );
writer.writeByte( size & 0xff );
for ( int x = 0; x < size; ++x )
{
writer.writeByte( modelData[x] & 0xff );
}
ImageIO.write( output, "png", outfile );
outfile.close();
}
private static class ImageWriter
{
int x = 0;
int y = 0;
int innerPixel = 0;
int width;
final BufferedImage image;
public ImageWriter(
final BufferedImage output,
final int storagePerImagePixel,
final int extraStoragePerPixel )
{
image = output;
width = image.getWidth();
if ( extraStoragePerPixel != 3 )
{
throw new RuntimeException( "requires 3!" );
}
}
private void writeByte(
final int iByte )
{
int rgba = image.getRGB( x, y );
int alpha = rgba >>> 24;
// extra...
if ( alpha < 128 )
{
rgba |= iByte << 16 - innerPixel;
image.setRGB( x, y, rgba );
innerPixel += 8;
if ( innerPixel >= 24 )
{
innerPixel = 0;
++x;
if ( x >= width )
{
x = 0;
++y;
}
}
}
else // inner image
{
int red = rgba >>> 16 & 0xfc;
int green = rgba >>> 8 & 0xfc;
int blue = rgba & 0xfc;
alpha &= 0xfc;
final int p1 = iByte >>> 6 & 3;
final int p2 = iByte >>> 4 & 3;
final int p3 = iByte >>> 2 & 3;
final int p4 = iByte & 3;
alpha |= p1;
red |= p2;
green |= p3;
blue |= p4;
rgba = alpha << 24 | red << 16 | green << 8 | blue;
image.setRGB( x, y, rgba );
++x;
if ( x >= width )
{
x = 0;
++y;
}
}
}
};
}