    001package net.minecraft.util;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.awt.image.BufferedImage;
    006import java.io.File;
    007import java.nio.IntBuffer;
    008import java.text.DateFormat;
    009import java.text.SimpleDateFormat;
    010import java.util.Date;
    011import javax.imageio.ImageIO;
    012import org.lwjgl.BufferUtils;
    013import org.lwjgl.opengl.GL11;
    014import org.lwjgl.opengl.GL12;
    015
    016@SideOnly(Side.CLIENT)
    017public class ScreenShotHelper
    018{
    019    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
    020    private static IntBuffer field_74293_b;
    021    private static int[] field_74294_c;
    022
    023    /**
    024     * Takes a screenshot and saves it to the screenshots directory. Returns the filename of the screenshot.
    025     */
    026    public static String saveScreenshot(File par0File, int par1, int par2)
    027    {
    028        return func_74292_a(par0File, (String)null, par1, par2);
    029    }
    030
    031    public static String func_74292_a(File par0File, String par1Str, int par2, int par3)
    032    {
    033        try
    034        {
    035            File file2 = new File(par0File, "screenshots");
    036            file2.mkdir();
    037            int k = par2 * par3;
    038
    039            if (field_74293_b == null || field_74293_b.capacity() < k)
    040            {
    041                field_74293_b = BufferUtils.createIntBuffer(k);
    042                field_74294_c = new int[k];
    043            }
    044
    045            GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
    046            GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
    047            field_74293_b.clear();
    048            GL11.glReadPixels(0, 0, par2, par3, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, field_74293_b);
    049            field_74293_b.get(field_74294_c);
    050            func_74289_a(field_74294_c, par2, par3);
    051            BufferedImage bufferedimage = new BufferedImage(par2, par3, 1);
    052            bufferedimage.setRGB(0, 0, par2, par3, field_74294_c, 0, par2);
    053            File file3;
    054
    055            if (par1Str == null)
    056            {
    057                file3 = func_74290_a(file2);
    058            }
    059            else
    060            {
    061                file3 = new File(file2, par1Str);
    062            }
    063
    064            ImageIO.write(bufferedimage, "png", file3);
    065            return "Saved screenshot as " + file3.getName();
    066        }
    067        catch (Exception exception)
    068        {
    069            exception.printStackTrace();
    070            return "Failed to save: " + exception;
    071        }
    072    }
    073
    074    private static File func_74290_a(File par0File)
    075    {
    076        String s = dateFormat.format(new Date()).toString();
    077        int i = 1;
    078
    079        while (true)
    080        {
    081            File file2 = new File(par0File, s + (i == 1 ? "" : "_" + i) + ".png");
    082
    083            if (!file2.exists())
    084            {
    085                return file2;
    086            }
    087
    088            ++i;
    089        }
    090    }
    091
    092    private static void func_74289_a(int[] par0ArrayOfInteger, int par1, int par2)
    093    {
    094        int[] aint1 = new int[par1];
    095        int k = par2 / 2;
    096
    097        for (int l = 0; l < k; ++l)
    098        {
    099            System.arraycopy(par0ArrayOfInteger, l * par1, aint1, 0, par1);
    100            System.arraycopy(par0ArrayOfInteger, (par2 - 1 - l) * par1, par0ArrayOfInteger, l * par1, par1);
    101            System.arraycopy(aint1, 0, par0ArrayOfInteger, (par2 - 1 - l) * par1, par1);
    102        }
    103    }
    104}