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.OpenGLState;
|
||||||
import electrosphere.renderer.RenderingEngine;
|
import electrosphere.renderer.RenderingEngine;
|
||||||
import electrosphere.util.FileUtils;
|
import electrosphere.util.FileUtils;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import javax.imageio.ImageIO;
|
import java.nio.IntBuffer;
|
||||||
|
|
||||||
import org.lwjgl.BufferUtils;
|
import org.lwjgl.BufferUtils;
|
||||||
import org.lwjgl.opengl.GL40;
|
import org.lwjgl.opengl.GL40;
|
||||||
|
import org.lwjgl.stb.STBImage;
|
||||||
|
import org.lwjgl.system.MemoryStack;
|
||||||
import org.lwjgl.system.MemoryUtil;
|
import org.lwjgl.system.MemoryUtil;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL11.*;
|
import static org.lwjgl.opengl.GL11.*;
|
||||||
@ -165,54 +169,23 @@ public class Texture {
|
|||||||
ByteBuffer data;
|
ByteBuffer data;
|
||||||
width = 1;
|
width = 1;
|
||||||
height = 1;
|
height = 1;
|
||||||
try {
|
try(MemoryStack stack = MemoryStack.stackPush()) {
|
||||||
BufferedImage image_data = ImageIO.read(FileUtils.getAssetFile(path));
|
|
||||||
|
//read
|
||||||
LoggerInterface.loggerRenderer.DEBUG("Read image");
|
LoggerInterface.loggerRenderer.DEBUG("Read image");
|
||||||
|
IntBuffer xBuf = stack.mallocInt(1);
|
||||||
//
|
IntBuffer yBuf = stack.mallocInt(1);
|
||||||
//transparency check
|
IntBuffer chanBuf = stack.mallocInt(1);
|
||||||
if (
|
data = STBImage.stbi_load(FileUtils.getAssetFile(path).getAbsolutePath(), xBuf, yBuf, chanBuf, 4);
|
||||||
image_data.getType() == BufferedImage.TYPE_3BYTE_BGR ||
|
if(data == null || data.limit() < 2){
|
||||||
image_data.getType() == BufferedImage.TYPE_INT_RGB
|
throw new IOException("Failed to read " + FileUtils.getAssetFile(path).getAbsolutePath());
|
||||||
){
|
|
||||||
hasTransparency = false;
|
|
||||||
} else if(
|
|
||||||
image_data.getType() == BufferedImage.TYPE_4BYTE_ABGR ||
|
|
||||||
image_data.getType() == BufferedImage.TYPE_INT_ARGB
|
|
||||||
){
|
|
||||||
hasTransparency = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
//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);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
//grab values from read data
|
||||||
//error check
|
width = xBuf.get();
|
||||||
if(data == null){
|
height = yBuf.get();
|
||||||
throw new IllegalStateException("Failed to allocate buffer for texture");
|
this.hasTransparency = true;
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
//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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
LoggerInterface.loggerRenderer.DEBUG("Failed to read image");
|
LoggerInterface.loggerRenderer.DEBUG("Failed to read image");
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
@ -224,7 +197,6 @@ public class Texture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
LoggerInterface.loggerRenderer.DEBUG("Flip buffer");
|
LoggerInterface.loggerRenderer.DEBUG("Flip buffer");
|
||||||
data.flip();
|
|
||||||
|
|
||||||
//call if width != height so opengl figures out how to unpack it properly
|
//call if width != height so opengl figures out how to unpack it properly
|
||||||
if(width != height){
|
if(width != height){
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user