package net.minecraft.src;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.imageio.ImageIO;
public class TexturePackCustom extends TexturePackImplementation {
/** ZipFile object used to access the texture pack file's contents. */
private ZipFile texturePackZipFile;
public TexturePackCustom(String par1Str, File par2File) {
super(par1Str, par2File, par2File.getName());
}
/**
* Delete the OpenGL texture id of the pack's thumbnail image, and close the
* zip file in case of TexturePackCustom.
*/
public void deleteTexturePack(RenderEngine par1RenderEngine) {
super.deleteTexturePack(par1RenderEngine);
try {
if (texturePackZipFile != null) {
texturePackZipFile.close();
}
} catch (IOException var3) {
}
texturePackZipFile = null;
}
/**
* Gives a texture resource as InputStream.
*/
public InputStream getResourceAsStream(String par1Str) {
this.openTexturePackFile();
try {
ZipEntry zipentry = texturePackZipFile.getEntry(par1Str
.substring(1));
if (zipentry != null) {
InputStream is = texturePackZipFile.getInputStream(zipentry);
return prepareImage(is, par1Str);
}
} catch (Exception var3) {
}
return super.getResourceAsStream(par1Str);
}
/**
* Open the texture pack's file and initialize texturePackZipFile
*/
private void openTexturePackFile() {
if (texturePackZipFile == null) {
try {
texturePackZipFile = new ZipFile(texturePackFile);
} catch (IOException var2) {
}
}
}
public static InputStream prepareImage(InputStream is, String path)
throws IOException {
if (!path.contains("terrain.png"))
return is;
BufferedImage img = ImageIO.read(is);
is.close();
String transparent = "-----------+++-+" +
"------------++++" +
"------++-------+" +
"-+--+--++------+" +
"-+-+-+++-+--+--+" +
"++++++--++++++++" +
"++++-----------+" +
"+--+-----++++--+" +
"+---+-----+-+--+" +
"----+++++-++-+-+" +
"---+++--+++-+++-" +
"---++++---+-----" +
"---++---++++++++" +
"--------+-----++" +
"--+++--++----+++" +
"++++++++++++++++";
int w = img.getWidth(), h = img.getHeight();
BufferedImage bg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bg.createGraphics();
int tilew = w / 16, tileh = h / 16;
g2d.setColor(Color.black);
for (int y = 0; y < 16; y++)
for (int x = 0; x < 16; x++)
if (transparent.charAt(16 * y + x) != '+')
g2d.fillRect(x * tilew, y * tileh, tilew, tileh);
g2d.drawImage(img, 0, 0, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bg, "png", baos);
return new ByteArrayInputStream(baos.toByteArray());
}
}