    001    /*
    002     * The FML Forge Mod Loader suite.
    003     * Copyright (C) 2012 cpw
    004     *
    005     * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
    006     * Software Foundation; either version 2.1 of the License, or any later version.
    007     *
    008     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
    009     * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    010     *
    011     * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51
    012     * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    013     */
    014
    015    package net.minecraft.src;
    016
    017    import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
    018
    019    import java.awt.Graphics2D;
    020    import java.awt.image.BufferedImage;
    021    import java.awt.image.ImageObserver;
    022
    023    import org.lwjgl.opengl.GL11;
    024
    025    import cpw.mods.fml.client.FMLClientHandler;
    026    import cpw.mods.fml.client.FMLTextureFX;
    027
    028    public class ModTextureStatic extends FMLTextureFX
    029    {
    030        private boolean oldanaglyph = false;
    031        private int[] pixels = null;
    032        private String targetTex = null;
    033        private int storedSize;
    034        private BufferedImage overrideData = null;
    035        private int needApply = 10;
    036
    037
    038        public ModTextureStatic(int icon, int target, BufferedImage image)
    039        {
    040            this(icon, 1, target, image);
    041        }
    042
    043        public ModTextureStatic(int icon, int size, int target, BufferedImage image)
    044        {
    045            this(icon, size, (target == 0 ? "/terrain.png" : "/gui/items.png"), image);
    046        }
    047
    048        public ModTextureStatic(int icon, int size, String target, BufferedImage image)
    049        {
    050            super(icon);
    051            RenderEngine re = FMLClientHandler.instance().getClient().renderEngine;
    052
    053            targetTex = target;
    054            storedSize = size;
    055            tileSize = size;
    056            tileImage = re.getTexture(target);
    057            overrideData = image;
    058        }
    059
    060        @Override
    061        public void setup()
    062        {
    063            super.setup();
    064            int sWidth  = overrideData.getWidth();
    065            int sHeight = overrideData.getHeight();
    066
    067            pixels = new int[tileSizeSquare];
    068            if (tileSizeBase == sWidth && tileSizeBase == sHeight)
    069            {
    070                overrideData.getRGB(0, 0, sWidth, sHeight, pixels, 0, sWidth);
    071            }
    072            else
    073            {
    074                BufferedImage tmp = new BufferedImage(tileSizeBase, tileSizeBase, 6);
    075                Graphics2D gfx = tmp.createGraphics();
    076                gfx.drawImage(overrideData, 0, 0, tileSizeBase, tileSizeBase, 0, 0, sWidth, sHeight, (ImageObserver)null);
    077                tmp.getRGB(0, 0, tileSizeBase, tileSizeBase, pixels, 0, tileSizeBase);
    078                gfx.dispose();
    079            }
    080
    081            update();
    082        }
    083
    084        @Override
    085        public void onTick()
    086        {
    087            if (oldanaglyph != anaglyphEnabled)
    088            {
    089                update();
    090            }
    091            // This makes it so we only apply the texture to the target texture when we need to,
    092            //due to the fact that update is called when the Effect is first registered, we actually
    093            //need to wait for the next one.
    094            tileSize = (needApply == 0 ? 0 : storedSize);
    095            if (needApply > 0)
    096            {
    097                needApply--;
    098            }
    099        }
    100
    101        @Override
    102        public void bindImage(RenderEngine par1RenderEngine)
    103        {
    104            GL11.glBindTexture(GL_TEXTURE_2D, par1RenderEngine.getTexture(targetTex));
    105        }
    106
    107        public void update()
    108        {
    109            needApply = 10;
    110            for (int idx = 0; idx < pixels.length; idx++)
    111            {
    112                int i = idx * 4;
    113                int a = pixels[idx] >> 24 & 255;
    114                int r = pixels[idx] >> 16 & 255;
    115                int g = pixels[idx] >> 8 & 255;
    116                int b = pixels[idx] >> 0 & 255;
    117
    118                if (anaglyphEnabled)
    119                {
    120                    r = g = b = (r + g + b) / 3;
    121                }
    122
    123                imageData[i + 0] = (byte)r;
    124                imageData[i + 1] = (byte)g;
    125                imageData[i + 2] = (byte)b;
    126                imageData[i + 3] = (byte)a;
    127            }
    128
    129            oldanaglyph = anaglyphEnabled;
    130        }
    131
    132        //Implementation of http://scale2x.sourceforge.net/algorithm.html
    133        public static BufferedImage scale2x(BufferedImage image)
    134        {
    135            int w = image.getWidth();
    136            int h = image.getHeight();
    137            BufferedImage tmp = new BufferedImage(w * 2, h * 2, 2);
    138
    139            for (int x = 0; x < h; ++x)
    140            {
    141                int x2 = x * 2;
    142                for (int y = 0; y < w; ++y)
    143                {
    144                    int y2 = y * 2;
    145                    int E = image.getRGB(y, x);
    146                    int D = (x == 0     ? E : image.getRGB(y,     x - 1));
    147                    int B = (y == 0     ? E : image.getRGB(y - 1, x    ));
    148                    int H = (y >= w - 1 ? E : image.getRGB(y + 1, x    ));
    149                    int F = (x >= h - 1 ? E : image.getRGB(y,     x + 1));
    150
    151                    int e0, e1, e2, e3;
    152
    153                    if (B != H && D != F)
    154                    {
    155                        e0 = D == B ? D : E;
    156                        e1 = B == F ? F : E;
    157                        e2 = D == H ? D : E;
    158                        e3 = H == F ? F : E;
    159                    }
    160                    else
    161                    {
    162                        e0 = e1 = e2 = e3 = E;
    163                    }
    164
    165                    tmp.setRGB(y2,     x2,     e0);
    166                    tmp.setRGB(y2 + 1, x2,     e1);
    167                    tmp.setRGB(y2,     x2 + 1, e2);
    168                    tmp.setRGB(y2 + 1, x2 + 1, e3);
    169                }
    170            }
    171
    172            return tmp;
    173        }
    174
    175
    176        @Override
    177        public String toString()
    178        {
    179            return String.format("ModTextureStatic %s @ %d", targetTex, iconIndex);
    180        }
    181    }