1

I am following along with a tutorial book on LWJGL3 and I tried to make my own image loading code (The book code does not use the STB library) and it works perfectly fine when the project is run normally (IntelliJ IDE, run directly from the build folder), It loads the image from into the ByteBuffer and stbi_load_from_memory works fine, but as soon as it is compiled and put in a jar file it just stops working, Even tho the image was successfully loaded into the ByteBuffer, as if the stbi_load_from_memory function just returns null if it's in a Jar.

The code:

public static ByteBuffer loadImage(String fileName, IntBuffer w, 
    IntBuffer h, IntBuffer comp) throws Exception
    {
        ByteBuffer image;
        //the class name is Utils
        InputStream imageFile = 
        Utils.class.getResourceAsStream(fileName); 

        //The image data gets put into the byte array no matter if
        //its a jar or not
        byte[] imageData = new byte[imageFile.available()];
        imageFile.read(imageData);
        ByteBuffer imageBuffer = 
        BufferUtils.createByteBuffer(imageData.length);
        imageBuffer.put(imageData);
        imageBuffer.flip();
        //imageBuffer is the right size and has the right contents            

        //This is where everything fails if its in a jar,
        //image is set to null and the Exception is thrown
        image = stbi_load_from_memory(imageBuffer, w, h, comp, 0);

        if(image == null)
        {
            throw new Exception("Failed to load image: " + fileName);
        }

        return image;
    }

It can't be a misspelled file name or recourse path, because it loads the data into the array properly no matter if it's a jar or not.

Additional Information:

  • Java version: 1.8.0_131 64bit

  • Processor architecture: amd64

  • IDE version (if needed for some reason): IntelliJ 2017.1.4 Ultimate

  • OS: Ubuntu 16.04 LTS 64 bit

  • Kernel: Linux 4.8.0-58-generic amd64

  • LWJGL version: 3.1.2

  • The project is made in Gradle 3.3

  • if required i will upload and post the link of the entire source code

MCcomando
  • 13
  • 4

1 Answers1

5

The problem is probably in your imageFile.available(). The available method of InputStream is not a good way to get the size of the stream; in fact you can't get the size of the stream, you have to read it until it ends.

What you want to do is convert your InputStream to a byte array. Look here how to do that: link. The rest of the code should be OK. Note that you need a direct ByteBuffer, so wrapping the byte array will not work, but you are already using the correct method (BufferUtils.createByteBuffer).

In case you still have problems, look at this example from LWJGL: https://github.com/LWJGL/lwjgl3/blob/master/modules/samples/src/test/java/org/lwjgl/demo/stb/Image.java

Jerred Shepherd
  • 530
  • 1
  • 6
  • 18
  • Ok yeah found the problem after longer debugging, Apparently the damn arrays are not the same size after all (had to write it to a file both for the jar and the non jar version and check the sizes and bytes). Sorry for the late reply was on vacation. Thanks mate. – MCcomando Jul 28 '17 at 11:30