use stbimage instead of imageio
Some checks reported errors
studiorailgun/Renderer/pipeline/head Something is wrong with the build of this commit
Some checks reported errors
studiorailgun/Renderer/pipeline/head Something is wrong with the build of this commit
This commit is contained in:
parent
e6934c83d0
commit
04b48c0c3b
@ -5,13 +5,17 @@ import electrosphere.logger.LoggerInterface;
|
||||
import electrosphere.renderer.OpenGLState;
|
||||
import electrosphere.renderer.RenderingEngine;
|
||||
import electrosphere.util.FileUtils;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL40;
|
||||
import org.lwjgl.stb.STBImage;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
@ -165,54 +169,23 @@ public class Texture {
|
||||
ByteBuffer data;
|
||||
width = 1;
|
||||
height = 1;
|
||||
try {
|
||||
BufferedImage image_data = ImageIO.read(FileUtils.getAssetFile(path));
|
||||
try(MemoryStack stack = MemoryStack.stackPush()) {
|
||||
|
||||
//read
|
||||
LoggerInterface.loggerRenderer.DEBUG("Read image");
|
||||
|
||||
//
|
||||
//transparency check
|
||||
if (
|
||||
image_data.getType() == BufferedImage.TYPE_3BYTE_BGR ||
|
||||
image_data.getType() == BufferedImage.TYPE_INT_RGB
|
||||
){
|
||||
hasTransparency = false;
|
||||
} else if(
|
||||
image_data.getType() == BufferedImage.TYPE_4BYTE_ABGR ||
|
||||
image_data.getType() == BufferedImage.TYPE_INT_ARGB
|
||||
){
|
||||
hasTransparency = true;
|
||||
IntBuffer xBuf = stack.mallocInt(1);
|
||||
IntBuffer yBuf = stack.mallocInt(1);
|
||||
IntBuffer chanBuf = stack.mallocInt(1);
|
||||
data = STBImage.stbi_load(FileUtils.getAssetFile(path).getAbsolutePath(), xBuf, yBuf, chanBuf, 4);
|
||||
if(data == null || data.limit() < 2){
|
||||
throw new IOException("Failed to read " + FileUtils.getAssetFile(path).getAbsolutePath());
|
||||
}
|
||||
|
||||
//
|
||||
//create buffer
|
||||
width = image_data.getWidth();
|
||||
height = image_data.getHeight();
|
||||
if(hasTransparency){
|
||||
data = BufferUtils.createByteBuffer(width * height * 4);
|
||||
} else {
|
||||
data = BufferUtils.createByteBuffer(width * height * 3);
|
||||
}
|
||||
|
||||
//
|
||||
//error check
|
||||
if(data == null){
|
||||
throw new IllegalStateException("Failed to allocate buffer for texture");
|
||||
}
|
||||
|
||||
//
|
||||
//buffer data
|
||||
for(int y = height - 1; y > -1; y--){
|
||||
for(int x = 0; x < width; x++){
|
||||
Color temp = new Color(image_data.getRGB(x, y), hasTransparency);
|
||||
|
||||
data.put((byte)temp.getRed());
|
||||
data.put((byte)temp.getGreen());
|
||||
data.put((byte)temp.getBlue());
|
||||
if(hasTransparency){
|
||||
data.put((byte)temp.getAlpha());
|
||||
}
|
||||
}
|
||||
}
|
||||
//grab values from read data
|
||||
width = xBuf.get();
|
||||
height = yBuf.get();
|
||||
this.hasTransparency = true;
|
||||
} catch (IOException ex) {
|
||||
LoggerInterface.loggerRenderer.DEBUG("Failed to read image");
|
||||
ex.printStackTrace();
|
||||
@ -224,7 +197,6 @@ public class Texture {
|
||||
}
|
||||
|
||||
LoggerInterface.loggerRenderer.DEBUG("Flip buffer");
|
||||
data.flip();
|
||||
|
||||
//call if width != height so opengl figures out how to unpack it properly
|
||||
if(width != height){
|
||||
|
||||
Loading…
Reference in New Issue
Block a user