    001/*
    002 * Forge Mod Loader
    003 * Copyright (c) 2012-2013 cpw.
    004 * All rights reserved. This program and the accompanying materials
    005 * are made available under the terms of the GNU Lesser Public License v2.1
    006 * which accompanies this distribution, and is available at
    007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    008 *
    009 * Contributors:
    010 *     cpw - implementation
    011 */
    012
    013package cpw.mods.fml.client;
    014
    015import java.awt.Dimension;
    016import java.awt.image.BufferedImage;
    017import java.io.IOException;
    018import java.io.InputStream;
    019import java.util.Map;
    020
    021import javax.imageio.ImageIO;
    022
    023import com.google.common.collect.Maps;
    024
    025import net.minecraft.client.Minecraft;
    026import net.minecraft.client.renderer.RenderEngine;
    027
    028public class TextureFXManager
    029{
    030    private static final TextureFXManager INSTANCE = new TextureFXManager();
    031
    032    private Minecraft client;
    033
    034    private Map<Integer,TextureHolder> texturesById = Maps.newHashMap();
    035    private Map<String, TextureHolder> texturesByName = Maps.newHashMap();
    036
    037    void setClient(Minecraft client)
    038    {
    039        this.client = client;
    040    }
    041    public BufferedImage loadImageFromTexturePack(RenderEngine renderEngine, String path) throws IOException
    042    {
    043        InputStream image=client.texturePackList.getSelectedTexturePack().getResourceAsStream(path);
    044        if (image==null) {
    045            throw new RuntimeException(String.format("The requested image path %s is not found",path));
    046        }
    047        BufferedImage result=ImageIO.read(image);
    048        if (result==null)
    049        {
    050            throw new RuntimeException(String.format("The requested image path %s appears to be corrupted",path));
    051        }
    052        return result;
    053    }
    054
    055    public static TextureFXManager instance()
    056    {
    057        return INSTANCE;
    058    }
    059
    060    public void fixTransparency(BufferedImage loadedImage, String textureName)
    061    {
    062        if (textureName.matches("^/mob/.*_eyes.*.png$"))
    063        {
    064            for (int x = 0; x < loadedImage.getWidth(); x++) {
    065                for (int y = 0; y < loadedImage.getHeight(); y++) {
    066                    int argb = loadedImage.getRGB(x, y);
    067                    if ((argb & 0xff000000) == 0 && argb != 0) {
    068                        loadedImage.setRGB(x, y, 0);
    069                    }
    070                }
    071            }
    072        }
    073    }
    074    public void bindTextureToName(String name, int index)
    075    {
    076        TextureHolder holder = new TextureHolder();
    077        holder.textureId = index;
    078        holder.textureName = name;
    079        texturesById.put(index,holder);
    080        texturesByName.put(name,holder);
    081    }
    082
    083    public void setTextureDimensions(int index, int j, int k)
    084    {
    085        TextureHolder holder = texturesById.get(index);
    086        if (holder == null)
    087        {
    088            return;
    089        }
    090        holder.x = j;
    091        holder.y = k;
    092    }
    093
    094    private class TextureHolder {
    095        private int textureId;
    096        private String textureName;
    097        private int x;
    098        private int y;
    099    }
    100
    101    public Dimension getTextureDimensions(String texture)
    102    {
    103        return texturesByName.containsKey(texture) ? new Dimension(texturesByName.get(texture).x, texturesByName.get(texture).y) : new Dimension(1,1);
    104    }
    105}