ScreenShotHelper .java

net.minecraft.util.ScreenShotHelper (class, 153 lines): members: saveScreenshot, pixelBuffer, pixelValues, dateFormat, logger. strings: "screenshots".

net.minecraft.utilclass153 linescommon5.7 KB

API Surface (2)

IChatComponent saveScreenshot(File gameDirectory, int width, int height, Framebuffer buffer)
IChatComponent saveScreenshot(File gameDirectory, String screenshotName, int width, int height, Framebuffer buffer)

Fields & Constants (4)

logger
dateFormat
pixelBuffer
pixelValues

String literals (6)

"yyyy-MM-dd_HH.mm.ss"
"screenshots"
"screenshot.success"
"Couldn\'t save screenshot"
"screenshot.failure"
".png"

Code Graph: Dependencies (6)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

ScreenShotHelper.java153 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.util;
2
3
import java.awt.image.BufferedImage;
4
import java.io.File;
5
import java.nio.IntBuffer;
6
import java.text.DateFormat;
7
import java.text.SimpleDateFormat;
8
import java.util.Date;
9
import javax.imageio.ImageIO;
10
import net.minecraft.client.renderer.GlStateManager;
11
import net.minecraft.client.renderer.OpenGlHelper;
12
import net.minecraft.client.renderer.texture.TextureUtil;
13
import net.minecraft.client.shader.Framebuffer;
14
import net.minecraft.event.ClickEvent;
15
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.Logger;
17
import org.lwjgl.BufferUtils;
18
import org.lwjgl.opengl.GL11;
19
import org.lwjgl.opengl.GL12;
20
21
public class ScreenShotHelper
22
{
23
    private static final Logger logger = LogManager.getLogger();
24
    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
25
26
    /** A buffer to hold pixel values returned by OpenGL. */
27
    private static IntBuffer pixelBuffer;
28
29
    /**
30
     * The built-up array that contains all the pixel values returned by OpenGL.
31
     */
32
    private static int[] pixelValues;
33
34
    /**
35
     * Saves a screenshot in the game directory with a time-stamped filename.  Args: gameDirectory,
36
     * requestedWidthInPixels, requestedHeightInPixels, frameBuffer
37
     */
38
    public static IChatComponent saveScreenshot(File gameDirectory, int width, int height, Framebuffer buffer)
39
    {
40
        return saveScreenshot(gameDirectory, (String)null, width, height, buffer);
41
    }
42
43
    /**
44
     * Saves a screenshot in the game directory with the given file name (or null to generate a time-stamped name).
45
     * Args: gameDirectory, fileName, requestedWidthInPixels, requestedHeightInPixels, frameBuffer
46
     */
47
    public static IChatComponent saveScreenshot(File gameDirectory, String screenshotName, int width, int height, Framebuffer buffer)
48
    {
49
        try
50
        {
51
            File file1 = new File(gameDirectory, "screenshots");
52
            file1.mkdir();
53
54
            if (OpenGlHelper.isFramebufferEnabled())
55
            {
56
                width = buffer.framebufferTextureWidth;
57
                height = buffer.framebufferTextureHeight;
58
            }
59
60
            int i = width * height;
61
62
            if (pixelBuffer == null || pixelBuffer.capacity() < i)
63
            {
64
                pixelBuffer = BufferUtils.createIntBuffer(i);
65
                pixelValues = new int[i];
66
            }
67
68
            GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
69
            GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
70
            pixelBuffer.clear();
71
72
            if (OpenGlHelper.isFramebufferEnabled())
73
            {
74
                GlStateManager.bindTexture(buffer.framebufferTexture);
75
                GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, (IntBuffer)pixelBuffer);
76
            }
77
            else
78
            {
79
                GL11.glReadPixels(0, 0, width, height, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, (IntBuffer)pixelBuffer);
80
            }
81
82
            pixelBuffer.get(pixelValues);
83
            TextureUtil.processPixelValues(pixelValues, width, height);
84
            BufferedImage bufferedimage = null;
85
86
            if (OpenGlHelper.isFramebufferEnabled())
87
            {
88
                bufferedimage = new BufferedImage(buffer.framebufferWidth, buffer.framebufferHeight, 1);
89
                int j = buffer.framebufferTextureHeight - buffer.framebufferHeight;
90
91
                for (int k = j; k < buffer.framebufferTextureHeight; ++k)
92
                {
93
                    for (int l = 0; l < buffer.framebufferWidth; ++l)
94
                    {
95
                        bufferedimage.setRGB(l, k - j, pixelValues[k * buffer.framebufferTextureWidth + l]);
96
                    }
97
                }
98
            }
99
            else
100
            {
101
                bufferedimage = new BufferedImage(width, height, 1);
102
                bufferedimage.setRGB(0, 0, width, height, pixelValues, 0, width);
103
            }
104
105
            File file2;
106
107
            if (screenshotName == null)
108
            {
109
                file2 = getTimestampedPNGFileForDirectory(file1);
110
            }
111
            else
112
            {
113
                file2 = new File(file1, screenshotName);
114
            }
115
116
            ImageIO.write(bufferedimage, "png", (File)file2);
117
            IChatComponent ichatcomponent = new ChatComponentText(file2.getName());
118
            ichatcomponent.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_FILE, file2.getAbsolutePath()));
119
            ichatcomponent.getChatStyle().setUnderlined(Boolean.valueOf(true));
120
            return new ChatComponentTranslation("screenshot.success", new Object[] {ichatcomponent});
121
        }
122
        catch (Exception exception)
123
        {
124
            logger.warn((String)"Couldn\'t save screenshot", (Throwable)exception);
125
            return new ChatComponentTranslation("screenshot.failure", new Object[] {exception.getMessage()});
126
        }
127
    }
128
129
    /**
130
     * Creates a unique PNG file in the given directory named by a timestamp.  Handles cases where the timestamp alone
131
     * is not enough to create a uniquely named file, though it still might suffer from an unlikely race condition where
132
     * the filename was unique when this method was called, but another process or thread created a file at the same
133
     * path immediately after this method returned.
134
     */
135
    private static File getTimestampedPNGFileForDirectory(File gameDirectory)
136
    {
137
        String s = dateFormat.format(new Date()).toString();
138
        int i = 1;
139
140
        while (true)
141
        {
142
            File file1 = new File(gameDirectory, s + (i == 1 ? "" : "_" + i) + ".png");
143
144
            if (!file1.exists())
145
            {
146
                return file1;
147
            }
148
149
            ++i;
150
        }
151
    }
152
}
153

Deobfuscated MCP 1.8.9 source of ScreenShotHelper from Marcelektro/MavenMCP-1.8.9 (net/minecraft/util/ScreenShotHelper.java), stored verbatim.