001package net.minecraft.client.renderer;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.awt.Graphics;
006import java.awt.image.BufferedImage;
007import java.awt.image.DataBufferInt;
008import java.awt.image.ImageObserver;
009
010@SideOnly(Side.CLIENT)
011public 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 bufferedimage1 = new BufferedImage(this.imageWidth, this.imageHeight, 2);
028            Graphics graphics = bufferedimage1.getGraphics();
029            graphics.drawImage(par1BufferedImage, 0, 0, (ImageObserver)null);
030            graphics.dispose();
031            this.imageData = ((DataBufferInt)bufferedimage1.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 flag = false;
036            int i;
037            int j;
038            int k;
039
040            for (i = 32; i < 64; ++i)
041            {
042                for (j = 0; j < 16; ++j)
043                {
044                    k = this.imageData[i + j * 64];
045
046                    if ((k >> 24 & 255) < 128)
047                    {
048                        flag = true;
049                    }
050                }
051            }
052
053            if (!flag)
054            {
055                for (i = 32; i < 64; ++i)
056                {
057                    for (j = 0; j < 16; ++j)
058                    {
059                        k = this.imageData[i + j * 64];
060
061                        if ((k >> 24 & 255) < 128)
062                        {
063                            flag = true;
064                        }
065                    }
066                }
067            }
068
069            return bufferedimage1;
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 i1 = par1; i1 < par3; ++i1)
083            {
084                for (int j1 = par2; j1 < par4; ++j1)
085                {
086                    this.imageData[i1 + j1 * 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 i1 = par1; i1 < par3; ++i1)
098        {
099            for (int j1 = par2; j1 < par4; ++j1)
100            {
101                this.imageData[i1 + j1 * 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 i1 = par1; i1 < par3; ++i1)
112        {
113            for (int j1 = par2; j1 < par4; ++j1)
114            {
115                int k1 = this.imageData[i1 + j1 * this.imageWidth];
116
117                if ((k1 >> 24 & 255) < 128)
118                {
119                    return true;
120                }
121            }
122        }
123
124        return false;
125    }
126}