    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.awt.Graphics;
    006    import java.awt.image.BufferedImage;
    007    import java.awt.image.DataBufferInt;
    008    import java.awt.image.ImageObserver;
    009
    010    @SideOnly(Side.CLIENT)
    011    public class ImageBufferDownload implements IImageBuffer
    012    {
    013        private int[] imageData;
    014        private int imageWidth;
    015        private int imageHeight;
    016
    017        public BufferedImage parseUserSkin(BufferedImage par1BufferedImage)
    018        {
    019            if (par1BufferedImage == null)
    020            {
    021                return null;
    022            }
    023            else
    024            {
    025                this.imageWidth = 64;
    026                this.imageHeight = 32;
    027                BufferedImage var2 = new BufferedImage(this.imageWidth, this.imageHeight, 2);
    028                Graphics var3 = var2.getGraphics();
    029                var3.drawImage(par1BufferedImage, 0, 0, (ImageObserver)null);
    030                var3.dispose();
    031                this.imageData = ((DataBufferInt)var2.getRaster().getDataBuffer()).getData();
    032                this.setAreaOpaque(0, 0, 32, 16);
    033                this.setAreaTransparent(32, 0, 64, 32);
    034                this.setAreaOpaque(0, 16, 64, 32);
    035                boolean var4 = false;
    036                int var5;
    037                int var6;
    038                int var7;
    039
    040                for (var5 = 32; var5 < 64; ++var5)
    041                {
    042                    for (var6 = 0; var6 < 16; ++var6)
    043                    {
    044                        var7 = this.imageData[var5 + var6 * 64];
    045
    046                        if ((var7 >> 24 & 255) < 128)
    047                        {
    048                            var4 = true;
    049                        }
    050                    }
    051                }
    052
    053                if (!var4)
    054                {
    055                    for (var5 = 32; var5 < 64; ++var5)
    056                    {
    057                        for (var6 = 0; var6 < 16; ++var6)
    058                        {
    059                            var7 = this.imageData[var5 + var6 * 64];
    060
    061                            if ((var7 >> 24 & 255) < 128)
    062                            {
    063                                var4 = true;
    064                            }
    065                        }
    066                    }
    067                }
    068
    069                return var2;
    070            }
    071        }
    072
    073        /**
    074         * Makes the given area of the image transparent if it was previously completely opaque (used to remove the outer
    075         * layer of a skin around the head if it was saved all opaque; this would be redundant so it's assumed that the skin
    076         * maker is just using an image editor without an alpha channel)
    077         */
    078        private void setAreaTransparent(int par1, int par2, int par3, int par4)
    079        {
    080            if (!this.hasTransparency(par1, par2, par3, par4))
    081            {
    082                for (int var5 = par1; var5 < par3; ++var5)
    083                {
    084                    for (int var6 = par2; var6 < par4; ++var6)
    085                    {
    086                        this.imageData[var5 + var6 * this.imageWidth] &= 16777215;
    087                    }
    088                }
    089            }
    090        }
    091
    092        /**
    093         * Makes the given area of the image opaque
    094         */
    095        private void setAreaOpaque(int par1, int par2, int par3, int par4)
    096        {
    097            for (int var5 = par1; var5 < par3; ++var5)
    098            {
    099                for (int var6 = par2; var6 < par4; ++var6)
    100                {
    101                    this.imageData[var5 + var6 * this.imageWidth] |= -16777216;
    102                }
    103            }
    104        }
    105
    106        /**
    107         * Returns true if the given area of the image contains transparent pixels
    108         */
    109        private boolean hasTransparency(int par1, int par2, int par3, int par4)
    110        {
    111            for (int var5 = par1; var5 < par3; ++var5)
    112            {
    113                for (int var6 = par2; var6 < par4; ++var6)
    114                {
    115                    int var7 = this.imageData[var5 + var6 * this.imageWidth];
    116
    117                    if ((var7 >> 24 & 255) < 128)
    118                    {
    119                        return true;
    120                    }
    121                }
    122            }
    123
    124            return false;
    125        }
    126    }