LexManos

ModTextureStatic

May 5th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.02 KB | None | 0 0
  1. /*
  2.  * The FML Forge Mod Loader suite.
  3.  * Copyright (C) 2012 cpw
  4.  *
  5.  * 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
  6.  * Software Foundation; either version 2.1 of the License, or any later version.
  7.  *
  8.  * 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
  9.  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  10.  *
  11.  * 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
  12.  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13.  */
  14.  
  15. package net.minecraft.src;
  16.  
  17. import java.awt.Graphics2D;
  18. import java.awt.image.BufferedImage;
  19. import java.awt.image.ImageObserver;
  20.  
  21. import org.lwjgl.opengl.GL11;
  22. import static org.lwjgl.opengl.GL11.*;
  23.  
  24. public class ModTextureStatic extends TextureFX
  25. {
  26.     private boolean oldanaglyph = false;
  27.     private int[] pixels = null;
  28.     private String targetTex = null;
  29.    
  30.    
  31.     public ModTextureStatic(int icon, int target, BufferedImage image)
  32.     {
  33.         this(icon, 1, target, image);
  34.     }
  35.  
  36.     public ModTextureStatic(int icon, int size, int target, BufferedImage image)    
  37.     {
  38.         this(icon, size, (target == 0 ? "/terrain.png" : "/gui/items.png"), image);
  39.     }
  40.    
  41.     public ModTextureStatic(int icon, int size, String target, BufferedImage image)
  42.     {
  43.         super(icon);
  44.         RenderEngine re = ModLoader.getMinecraftInstance().field_6315_n;
  45.        
  46.         targetTex = target;
  47.         field_1129_e = size;
  48.         field_1128_f = re.func_1070_a(target);
  49.        
  50.         func_782_a(re);
  51.  
  52.         int sWidth  = image.getWidth();
  53.         int sHeight = image.getHeight();
  54.         int tWidth  = GL11.glGetTexLevelParameteri(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH ) / 16;
  55.         int tHeight = GL11.glGetTexLevelParameteri(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT) / 16;
  56.        
  57.         pixels = new int[tWidth * tHeight];
  58.         field_1127_a = new byte[tWidth * tHeight * 4];
  59.        
  60.  
  61.         if (tWidth == sWidth && tHeight == sHeight)
  62.         {
  63.             image.getRGB(0, 0, sWidth, sHeight, pixels, 0, sWidth);
  64.         }
  65.         else
  66.         {
  67.             BufferedImage tmp = new BufferedImage(tWidth, tHeight, 6);
  68.             Graphics2D gfx = tmp.createGraphics();
  69.             gfx.drawImage(image, 0, 0, tWidth, tHeight, 0, 0, sWidth, sHeight, (ImageObserver)null);
  70.             tmp.getRGB(0, 0, tWidth, tHeight, pixels, 0, tWidth);
  71.             gfx.dispose();
  72.         }
  73.  
  74.         update();
  75.     }
  76.    
  77.     public void func_783_a()
  78.     {
  79.         if (oldanaglyph != field_1131_c)
  80.         {
  81.             update();
  82.         }
  83.     }
  84.  
  85.     public void func_782_a(RenderEngine p_782_1_)
  86.     {
  87.         GL11.glBindTexture(GL_TEXTURE_2D, p_782_1_.func_1070_a(targetTex));
  88.     }
  89.    
  90.     public void update()
  91.     {
  92.         for (int idx = 0; idx < pixels.length; idx++)
  93.         {
  94.             int i = idx * 4;
  95.             int a = pixels[idx] >> 24 & 255;
  96.             int r = pixels[idx] >> 16 & 255;
  97.             int g = pixels[idx] >> 8 & 255;
  98.             int b = pixels[idx] >> 0 & 255;
  99.  
  100.             if (field_1131_c)
  101.             {
  102.                 r = g = b = (r + g + b) / 3;
  103.             }
  104.  
  105.             field_1127_a[i + 0] = (byte)r;
  106.             field_1127_a[i + 1] = (byte)g;
  107.             field_1127_a[i + 2] = (byte)b;
  108.             field_1127_a[i + 3] = (byte)a;
  109.         }
  110.  
  111.         oldanaglyph = field_1131_c;
  112.     }
  113.    
  114.     //Implementation of http://scale2x.sourceforge.net/algorithm.html
  115.     public static BufferedImage scale2x(BufferedImage image)
  116.     {
  117.         int w = image.getWidth();
  118.         int h = image.getHeight();
  119.         BufferedImage tmp = new BufferedImage(w * 2, h * 2, 2);
  120.  
  121.         for (int x = 0; x < h; ++x)
  122.         {
  123.             int x2 = x * 2;
  124.             for (int y = 0; y < w; ++y)
  125.             {
  126.                 int y2 = y * 2;
  127.                 int E = image.getRGB(y, x);
  128.                 int D = (x == 0     ? E : image.getRGB(y,     x - 1));
  129.                 int B = (y == 0     ? E : image.getRGB(y - 1, x    ));
  130.                 int H = (y >= w - 1 ? E : image.getRGB(y + 1, x    ));
  131.                 int F = (x >= h - 1 ? E : image.getRGB(y,     x + 1));
  132.  
  133.                 int e0, e1, e2, e3;
  134.  
  135.                 if (B != H && D != F)
  136.                 {
  137.                     e0 = D == B ? D : E;
  138.                     e1 = B == F ? F : E;
  139.                     e2 = D == H ? D : E;
  140.                     e3 = H == F ? F : E;
  141.                 }
  142.                 else
  143.                 {
  144.                     e0 = e1 = e2 = e3 = E;
  145.                 }
  146.  
  147.                 tmp.setRGB(y2,     x2,     e0);
  148.                 tmp.setRGB(y2 + 1, x2,     e1);
  149.                 tmp.setRGB(y2,     x2 + 1, e2);
  150.                 tmp.setRGB(y2 + 1, x2 + 1, e3);
  151.             }
  152.         }
  153.  
  154.         return tmp;
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment