    001    package net.minecraft.util;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.awt.image.BufferedImage;
    006    import java.io.File;
    007    import java.nio.IntBuffer;
    008    import java.text.DateFormat;
    009    import java.text.SimpleDateFormat;
    010    import java.util.Date;
    011    import javax.imageio.ImageIO;
    012    import org.lwjgl.BufferUtils;
    013    import org.lwjgl.opengl.GL11;
    014    import org.lwjgl.opengl.GL12;
    015
    016    @SideOnly(Side.CLIENT)
    017    public 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 var4 = new File(par0File, "screenshots");
    036                var4.mkdir();
    037                int var5 = par2 * par3;
    038
    039                if (field_74293_b == null || field_74293_b.capacity() < var5)
    040                {
    041                    field_74293_b = BufferUtils.createIntBuffer(var5);
    042                    field_74294_c = new int[var5];
    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 var6 = new BufferedImage(par2, par3, 1);
    052                var6.setRGB(0, 0, par2, par3, field_74294_c, 0, par2);
    053                File var7;
    054
    055                if (par1Str == null)
    056                {
    057                    var7 = func_74290_a(var4);
    058                }
    059                else
    060                {
    061                    var7 = new File(var4, par1Str);
    062                }
    063
    064                ImageIO.write(var6, "png", var7);
    065                return "Saved screenshot as " + var7.getName();
    066            }
    067            catch (Exception var8)
    068            {
    069                var8.printStackTrace();
    070                return "Failed to save: " + var8;
    071            }
    072        }
    073
    074        private static File func_74290_a(File par0File)
    075        {
    076            String var2 = dateFormat.format(new Date()).toString();
    077            int var3 = 1;
    078
    079            while (true)
    080            {
    081                File var1 = new File(par0File, var2 + (var3 == 1 ? "" : "_" + var3) + ".png");
    082
    083                if (!var1.exists())
    084                {
    085                    return var1;
    086                }
    087
    088                ++var3;
    089            }
    090        }
    091
    092        private static void func_74289_a(int[] par0ArrayOfInteger, int par1, int par2)
    093        {
    094            int[] var3 = new int[par1];
    095            int var4 = par2 / 2;
    096
    097            for (int var5 = 0; var5 < var4; ++var5)
    098            {
    099                System.arraycopy(par0ArrayOfInteger, var5 * par1, var3, 0, par1);
    100                System.arraycopy(par0ArrayOfInteger, (par2 - 1 - var5) * par1, par0ArrayOfInteger, var5 * par1, par1);
    101                System.arraycopy(var3, 0, par0ArrayOfInteger, (par2 - 1 - var5) * par1, par1);
    102            }
    103        }
    104    }