    001package net.minecraft.client.renderer.texture;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.awt.image.BufferedImage;
    006import java.io.File;
    007import java.io.IOException;
    008import java.nio.ByteBuffer;
    009import javax.imageio.ImageIO;
    010import net.minecraft.client.Minecraft;
    011import org.lwjgl.opengl.GL11;
    012import org.lwjgl.opengl.GL12;
    013import org.lwjgl.opengl.GL13;
    014
    015@SideOnly(Side.CLIENT)
    016public class Texture
    017{
    018    private int field_94293_a;
    019    private int field_94291_b;
    020    private int field_94292_c;
    021    private final int field_94289_d;
    022    private final int field_94290_e;
    023    private final int field_94287_f;
    024    private final int field_94288_g;
    025    private final int field_94300_h;
    026    private final int field_94301_i;
    027    private final int field_94298_j;
    028    private final int field_94299_k;
    029    private final boolean field_94296_l;
    030    private final String field_94297_m;
    031    private Rect2i field_94294_n;
    032    private boolean field_94295_o;
    033    private boolean field_94304_p;
    034    private boolean field_94303_q;
    035    private ByteBuffer field_94302_r;
    036
    037    private Texture(String par1Str, int par2, int par3, int par4, int par5, int par6, int par7, int par8, int par9)
    038    {
    039        this.field_94297_m = par1Str;
    040        this.field_94292_c = par2;
    041        this.field_94289_d = par3;
    042        this.field_94290_e = par4;
    043        this.field_94287_f = par5;
    044        this.field_94288_g = par7;
    045        this.field_94301_i = par8;
    046        this.field_94298_j = par9;
    047        this.field_94299_k = par6;
    048        this.field_94294_n = new Rect2i(0, 0, par3, par4);
    049
    050        if (par4 == 1 && par5 == 1)
    051        {
    052            this.field_94300_h = 3552;
    053        }
    054        else if (par5 == 1)
    055        {
    056            this.field_94300_h = 3553;
    057        }
    058        else
    059        {
    060            this.field_94300_h = 32879;
    061        }
    062
    063        this.field_94296_l = par8 != 9728 && par8 != 9729 || par9 != 9728 && par9 != 9729;
    064
    065        if (par2 != 2)
    066        {
    067            this.field_94293_a = GL11.glGenTextures();
    068            GL11.glBindTexture(this.field_94300_h, this.field_94293_a);
    069            GL11.glTexParameteri(this.field_94300_h, GL11.GL_TEXTURE_MIN_FILTER, par8);
    070            GL11.glTexParameteri(this.field_94300_h, GL11.GL_TEXTURE_MAG_FILTER, par9);
    071            GL11.glTexParameteri(this.field_94300_h, GL11.GL_TEXTURE_WRAP_S, par6);
    072            GL11.glTexParameteri(this.field_94300_h, GL11.GL_TEXTURE_WRAP_T, par6);
    073        }
    074        else
    075        {
    076            this.field_94293_a = -1;
    077        }
    078
    079        this.field_94291_b = TextureManager.func_94267_b().func_94265_c();
    080    }
    081
    082    public Texture(String par1Str, int par2, int par3, int par4, int par5, int par6, int par7, int par8, BufferedImage par9BufferedImage)
    083    {
    084        this(par1Str, par2, par3, par4, 1, par5, par6, par7, par8, par9BufferedImage);
    085    }
    086
    087    public Texture(String par1Str, int par2, int par3, int par4, int par5, int par6, int par7, int par8, int par9, BufferedImage par10BufferedImage)
    088    {
    089        this(par1Str, par2, par3, par4, par5, par6, par7, par8, par9);
    090
    091        if (par10BufferedImage == null)
    092        {
    093            if (par3 != -1 && par4 != -1)
    094            {
    095                byte[] abyte = new byte[par3 * par4 * par5 * 4];
    096
    097                for (int i2 = 0; i2 < abyte.length; ++i2)
    098                {
    099                    abyte[i2] = 0;
    100                }
    101
    102                this.field_94302_r = ByteBuffer.allocateDirect(abyte.length);
    103                this.field_94302_r.clear();
    104                this.field_94302_r.put(abyte);
    105                this.field_94302_r.position(0).limit(abyte.length);
    106
    107                if (this.field_94304_p)
    108                {
    109                    this.func_94285_g();
    110                }
    111                else
    112                {
    113                    this.field_94303_q = false;
    114                }
    115            }
    116            else
    117            {
    118                this.field_94295_o = false;
    119            }
    120        }
    121        else
    122        {
    123            this.field_94295_o = true;
    124            this.func_94278_a(par10BufferedImage);
    125
    126            if (par2 != 2)
    127            {
    128                this.func_94285_g();
    129                this.field_94304_p = false;
    130            }
    131        }
    132    }
    133
    134    public final Rect2i func_94274_a()
    135    {
    136        return this.field_94294_n;
    137    }
    138
    139    public void func_94272_a(Rect2i par1Rect2i, int par2)
    140    {
    141        if (this.field_94300_h != 32879)
    142        {
    143            Rect2i rect2i1 = new Rect2i(0, 0, this.field_94289_d, this.field_94290_e);
    144            rect2i1.func_94156_a(par1Rect2i);
    145            this.field_94302_r.position(0);
    146
    147            for (int j = rect2i1.func_94160_b(); j < rect2i1.func_94160_b() + rect2i1.func_94157_d(); ++j)
    148            {
    149                int k = j * this.field_94289_d * 4;
    150
    151                for (int l = rect2i1.func_94158_a(); l < rect2i1.func_94158_a() + rect2i1.func_94159_c(); ++l)
    152                {
    153                    this.field_94302_r.put(k + l * 4 + 0, (byte)(par2 >> 24 & 255));
    154                    this.field_94302_r.put(k + l * 4 + 1, (byte)(par2 >> 16 & 255));
    155                    this.field_94302_r.put(k + l * 4 + 2, (byte)(par2 >> 8 & 255));
    156                    this.field_94302_r.put(k + l * 4 + 3, (byte)(par2 >> 0 & 255));
    157                }
    158            }
    159
    160            if (this.field_94304_p)
    161            {
    162                this.func_94285_g();
    163            }
    164            else
    165            {
    166                this.field_94303_q = false;
    167            }
    168        }
    169    }
    170
    171    public void func_94279_c(String par1Str)
    172    {
    173        BufferedImage bufferedimage = new BufferedImage(this.field_94289_d, this.field_94290_e, 2);
    174        ByteBuffer bytebuffer = this.func_94273_h();
    175        byte[] abyte = new byte[this.field_94289_d * this.field_94290_e * 4];
    176        bytebuffer.position(0);
    177        bytebuffer.get(abyte);
    178
    179        for (int i = 0; i < this.field_94289_d; ++i)
    180        {
    181            for (int j = 0; j < this.field_94290_e; ++j)
    182            {
    183                int k = j * this.field_94289_d * 4 + i * 4;
    184                byte b0 = 0;
    185                int l = b0 | (abyte[k + 2] & 255) << 0;
    186                l |= (abyte[k + 1] & 255) << 8;
    187                l |= (abyte[k + 0] & 255) << 16;
    188                l |= (abyte[k + 3] & 255) << 24;
    189                bufferedimage.setRGB(i, j, l);
    190            }
    191        }
    192
    193        this.field_94302_r.position(this.field_94289_d * this.field_94290_e * 4);
    194
    195        try
    196        {
    197            ImageIO.write(bufferedimage, "png", new File(Minecraft.getMinecraftDir(), par1Str));
    198        }
    199        catch (IOException ioexception)
    200        {
    201            ioexception.printStackTrace();
    202        }
    203    }
    204
    205    public void func_94281_a(int par1, int par2, Texture par3Texture, boolean par4)
    206    {
    207        if (this.field_94300_h != 32879)
    208        {
    209            ByteBuffer bytebuffer = par3Texture.func_94273_h();
    210            this.field_94302_r.position(0);
    211            bytebuffer.position(0);
    212
    213            for (int k = 0; k < par3Texture.func_94276_e(); ++k)
    214            {
    215                int l = par2 + k;
    216                int i1 = k * par3Texture.func_94275_d() * 4;
    217                int j1 = l * this.field_94289_d * 4;
    218
    219                if (par4)
    220                {
    221                    l = par2 + (par3Texture.func_94276_e() - k);
    222                }
    223
    224                for (int k1 = 0; k1 < par3Texture.func_94275_d(); ++k1)
    225                {
    226                    int l1 = j1 + (k1 + par1) * 4;
    227                    int i2 = i1 + k1 * 4;
    228
    229                    if (par4)
    230                    {
    231                        l1 = par1 + k1 * this.field_94289_d * 4 + l * 4;
    232                    }
    233
    234                    this.field_94302_r.put(l1 + 0, bytebuffer.get(i2 + 0));
    235                    this.field_94302_r.put(l1 + 1, bytebuffer.get(i2 + 1));
    236                    this.field_94302_r.put(l1 + 2, bytebuffer.get(i2 + 2));
    237                    this.field_94302_r.put(l1 + 3, bytebuffer.get(i2 + 3));
    238                }
    239            }
    240
    241            this.field_94302_r.position(this.field_94289_d * this.field_94290_e * 4);
    242
    243            if (this.field_94304_p)
    244            {
    245                this.func_94285_g();
    246            }
    247            else
    248            {
    249                this.field_94303_q = false;
    250            }
    251        }
    252    }
    253
    254    public void func_94278_a(BufferedImage par1BufferedImage)
    255    {
    256        if (this.field_94300_h != 32879)
    257        {
    258            int i = par1BufferedImage.getWidth();
    259            int j = par1BufferedImage.getHeight();
    260
    261            if (i <= this.field_94289_d && j <= this.field_94290_e)
    262            {
    263                int[] aint = new int[] {3, 0, 1, 2};
    264                int[] aint1 = new int[] {3, 2, 1, 0};
    265                int[] aint2 = this.field_94288_g == 32993 ? aint1 : aint;
    266                int[] aint3 = new int[this.field_94289_d * this.field_94290_e];
    267                int k = par1BufferedImage.getTransparency();
    268                par1BufferedImage.getRGB(0, 0, this.field_94289_d, this.field_94290_e, aint3, 0, i);
    269                byte[] abyte = new byte[this.field_94289_d * this.field_94290_e * 4];
    270
    271                for (int l = 0; l < this.field_94290_e; ++l)
    272                {
    273                    for (int i1 = 0; i1 < this.field_94289_d; ++i1)
    274                    {
    275                        int j1 = l * this.field_94289_d + i1;
    276                        int k1 = j1 * 4;
    277                        abyte[k1 + aint2[0]] = (byte)(aint3[j1] >> 24 & 255);
    278                        abyte[k1 + aint2[1]] = (byte)(aint3[j1] >> 16 & 255);
    279                        abyte[k1 + aint2[2]] = (byte)(aint3[j1] >> 8 & 255);
    280                        abyte[k1 + aint2[3]] = (byte)(aint3[j1] >> 0 & 255);
    281                    }
    282                }
    283
    284                this.field_94302_r = ByteBuffer.allocateDirect(abyte.length);
    285                this.field_94302_r.clear();
    286                this.field_94302_r.put(abyte);
    287                this.field_94302_r.limit(abyte.length);
    288
    289                if (this.field_94304_p)
    290                {
    291                    this.func_94285_g();
    292                }
    293                else
    294                {
    295                    this.field_94303_q = false;
    296                }
    297            }
    298            else
    299            {
    300                Minecraft.getMinecraft().func_98033_al().func_98236_b("transferFromImage called with a BufferedImage with dimensions (" + i + ", " + j + ") larger than the Texture dimensions (" + this.field_94289_d + ", " + this.field_94290_e + "). Ignoring.");
    301            }
    302        }
    303    }
    304
    305    public int func_94284_b()
    306    {
    307        return this.field_94291_b;
    308    }
    309
    310    public int func_94282_c()
    311    {
    312        return this.field_94293_a;
    313    }
    314
    315    public int func_94275_d()
    316    {
    317        return this.field_94289_d;
    318    }
    319
    320    public int func_94276_e()
    321    {
    322        return this.field_94290_e;
    323    }
    324
    325    public String func_94280_f()
    326    {
    327        return this.field_94297_m;
    328    }
    329
    330    public void func_94277_a(int par1)
    331    {
    332        if (this.field_94287_f == 1)
    333        {
    334            GL11.glEnable(GL11.GL_TEXTURE_2D);
    335        }
    336        else
    337        {
    338            GL11.glEnable(GL12.GL_TEXTURE_3D);
    339        }
    340
    341        GL13.glActiveTexture(GL13.GL_TEXTURE0 + par1);
    342        GL11.glBindTexture(this.field_94300_h, this.field_94293_a);
    343
    344        if (!this.field_94303_q)
    345        {
    346            this.func_94285_g();
    347        }
    348    }
    349
    350    public void func_94285_g()
    351    {
    352        this.field_94302_r.flip();
    353
    354        if (this.field_94290_e != 1 && this.field_94287_f != 1)
    355        {
    356            GL12.glTexImage3D(this.field_94300_h, 0, this.field_94288_g, this.field_94289_d, this.field_94290_e, this.field_94287_f, 0, this.field_94288_g, GL11.GL_UNSIGNED_BYTE, this.field_94302_r);
    357        }
    358        else if (this.field_94290_e != 1)
    359        {
    360            GL11.glTexImage2D(this.field_94300_h, 0, this.field_94288_g, this.field_94289_d, this.field_94290_e, 0, this.field_94288_g, GL11.GL_UNSIGNED_BYTE, this.field_94302_r);
    361        }
    362        else
    363        {
    364            GL11.glTexImage1D(this.field_94300_h, 0, this.field_94288_g, this.field_94289_d, 0, this.field_94288_g, GL11.GL_UNSIGNED_BYTE, this.field_94302_r);
    365        }
    366
    367        this.field_94303_q = true;
    368    }
    369
    370    public ByteBuffer func_94273_h()
    371    {
    372        return this.field_94302_r;
    373    }
    374}