STBImage.stbi_load returns null when loading an image with alpha

Started by Andrew_3ds, October 18, 2015, 22:07:17

Previous topic - Next topic

Andrew_3ds

Title pretty much explains what is happening. It loads my images fine except for ones with transparency in them.
Here's what I am trying to do:
public class TextureLoader {
    public static Texture loadPNG(File dir, String texName) {
        File f = new File(dir+"/"+texName+".png");
        try {
            if(!f.exists()) throw new IOException();

            IntBuffer w = BufferUtils.createIntBuffer(1);
            IntBuffer h = BufferUtils.createIntBuffer(1);
            IntBuffer comp = BufferUtils.createIntBuffer(1);
            ByteBuffer buffer = STBImage.stbi_load(f.toString(),w,h,comp,4);

            if(buffer == null) throw new IOException();

            int imgWidth = w.get();
            int imgHeight = h.get();

            int texId = glGenTextures();
            glBindTexture(GL_TEXTURE_2D, texId);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, imgWidth, imgHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
            glGenerateMipmap(GL_TEXTURE_2D);

            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

            glBindTexture(GL_TEXTURE_2D, 0);

            buffer.clear();

            Logging.sysMsg("Loaded texture " + texName + ".png | " + imgWidth + "x" + imgWidth);

            return new Texture(texId, imgWidth, imgWidth);
        } catch (IOException e) {
            Logging.errMsg("Texture " + texName + " doesn't exit");
            if(!texName.equals("default"))
                return TextureHandler.defaultTexture;
            else return null;
        }
    }
}

quew8

You can get the reason an stbi load fails using the stbi_failure_reason() function.

So change the error test to this:
if(buffer == null) {
    throw new IOException(STBImage.stbi_failure_reason());
}

and see what STBI has to say about it.

Andrew_3ds

Giving me this error:
<22:42.46> [ERROR #3] PNG not supported: 1/2/4/8-bit only

spasi


Andrew_3ds

Okay. I will just have to resave all my images with different bit depth. I tried it on one and it loaded just fine. Thanks.