Skip to content

Instantly share code, notes, and snippets.

@LexManos
Created April 26, 2015 05:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LexManos/3c700306331080598daf to your computer and use it in GitHub Desktop.
Save LexManos/3c700306331080598daf to your computer and use it in GitHub Desktop.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.common.MinecraftForge;
import com.google.common.base.Function;
import com.google.common.primitives.Ints;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
@Mod(modid = TextureDump.MODID, version = TextureDump.VERSION)
public class TextureDump
{
public static final String MODID = "texture_dump";
public static final String VERSION = "1.0";
@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void postTextureStitch(TextureStitchEvent.Post e) throws Exception
{
saveGlTexture(getName(e.map), e.map.getGlTextureId(), getMip(e.map));
}
private static Field fName;
private static String getName(TextureMap map) throws Exception
{
if (fName == null)
{
fName = TextureMap.class.getDeclaredFields()[7];
fName.setAccessible(true);
}
return ((String)fName.get(map)).replace('/', '_');
}
private static Field fMip;
private static int getMip(TextureMap map) throws Exception
{
if (fMip == null)
{
fMip = TextureMap.class.getDeclaredFields()[8];
fMip.setAccessible(true);
}
return fMip.getInt(map);
}
public static void saveGlTexture(String name, int textureId, int mipmapLevels)
{
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
for (int level = 0; level <= mipmapLevels; level++)
{
int width = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, level, GL11.GL_TEXTURE_WIDTH);
int height = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, level, GL11.GL_TEXTURE_HEIGHT);
int size = width * height;
BufferedImage bufferedimage = new BufferedImage(width, height, 2);
File output = new File(name + "_" + level + ".png");
IntBuffer buffer = BufferUtils.createIntBuffer(size);
int[] data = new int[size];
GL11.glGetTexImage(GL11.GL_TEXTURE_2D, level, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
buffer.get(data);
bufferedimage.setRGB(0, 0, width, height, data, 0, width);
try
{
ImageIO.write(bufferedimage, "png", output);
FMLLog.info("[TextureDump] Exported png to: {}", output.getAbsolutePath());
}
catch (IOException ioexception)
{
FMLLog.info("[TextureDump] Unable to write: ", ioexception);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment