Login  Register

Does TextureIO.newTextureData work with InputStream textures?

Posted by elect on May 04, 2015; 9:11am
URL: https://forum.jogamp.org/Does-TextureIO-newTextureData-work-with-InputStream-textures-tp4034413.html

Hi,

I am trying to replace the way to load textures resources from "url-file" to "inputstream" since I want it working even outside the IDE, with the dist/ jar..

But I get a null point exception and I am wondering if I am doing it properly or not

Here I get the inputStream

InputStream inputStream = NvImage.class.getResourceAsStream(filePath);

and then I try to retrieve the textureData

TextureData textureData = TextureIO.newTextureData(gl4.getGLProfile(), inputStream, false, TextureIO.DDS);

But I get this exception

Mai 04, 2015 10:56:46 AM nvGlSamples.bindlessApp.BindlessApp initBindlessTextures
SCHWERWIEGEND: null
java.io.EOFException
        at java.io.DataInputStream.readShort(DataInputStream.java:315)
        at com.jogamp.opengl.util.texture.spi.SGIImage.isSGIImage(SGIImage.java:206)
        at com.jogamp.opengl.util.texture.TextureIO$SGITextureProvider.newTextureData(TextureIO.java:1055)
        at com.jogamp.opengl.util.texture.TextureIO.newTextureDataImpl(TextureIO.java:834)
        at com.jogamp.opengl.util.texture.TextureIO.newTextureData(TextureIO.java:247)
        at nvGlSamples.util.NvImage.uploadTextureFromDDSFile(NvImage.java:40)
        at nvGlSamples.bindlessApp.BindlessApp.initBindlessTextures(BindlessApp.java:379)
        at nvGlSamples.bindlessApp.BindlessApp.initRendering(BindlessApp.java:195)
        at nvGlSamples.util.NvSampleApp.init(NvSampleApp.java:75)
        at jogamp.opengl.GLDrawableHelper.init(GLDrawableHelper.java:641)
        at jogamp.opengl.GLDrawableHelper.init(GLDrawableHelper.java:663)
        at jogamp.opengl.GLAutoDrawableBase$1.run(GLAutoDrawableBase.java:430)
        at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1275)
        at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1131)
        at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:680)
        at jogamp.opengl.GLAutoDrawableBase.defaultWindowResizedOp(GLAutoDrawableBase.java:259)
        at com.jogamp.newt.opengl.GLWindow.access$200(GLWindow.java:119)
        at com.jogamp.newt.opengl.GLWindow$2.windowResized(GLWindow.java:141)
        at jogamp.newt.WindowImpl.consumeWindowEvent(WindowImpl.java:3682)
        at jogamp.newt.WindowImpl.sendWindowEvent(WindowImpl.java:3616)
        at jogamp.newt.WindowImpl.setVisibleActionImpl(WindowImpl.java:1003)
        at jogamp.newt.WindowImpl$VisibleAction.run(WindowImpl.java:1015)
        at com.jogamp.common.util.RunnableTask.run(RunnableTask.java:150)
        at jogamp.newt.DefaultEDTUtil$NEDT.run(DefaultEDTUtil.java:372)

Looking at the place where it fails, it is in the TextureIO.SGITextureProvide:

    //----------------------------------------------------------------------
    // SGI RGB image provider
    static class SGITextureProvider extends StreamBasedTextureProvider {
        @Override
        public TextureData newTextureData(final GLProfile glp, final InputStream stream,
                                          int internalFormat,
                                          int pixelFormat,
                                          final boolean mipmap,
                                          final String fileSuffix) throws IOException {
            if (SGI.equals(fileSuffix) ||
                SGI_RGB.equals(fileSuffix) ||
                SGIImage.isSGIImage(stream)) {
                final SGIImage image = SGIImage.read(stream);
                if (pixelFormat == 0) {
                    pixelFormat = image.getFormat();
                }
                if (internalFormat == 0) {
                    internalFormat = image.getFormat();
                }
                return new TextureData(glp, internalFormat,
                                       image.getWidth(),
                                       image.getHeight(),
                                       0,
                                       pixelFormat,
                                       GL.GL_UNSIGNED_BYTE,
                                       mipmap,
                                       false,
                                       false,
                                       ByteBuffer.wrap(image.getData()),
                                       null);
            }

            return null;
        }
    }

at the .isSGIImage:

    /** Determines from the magic number whether the given InputStream
        points to an SGI RGB image. The given InputStream must return
        true from markSupported() and support a minimum of two bytes
        of read-ahead. */
    public static boolean isSGIImage(InputStream in) throws IOException {
        if (!(in instanceof BufferedInputStream)) {
            in = new BufferedInputStream(in);
        }
        if (!in.markSupported()) {
            throw new IOException("Can not test non-destructively whether given InputStream is an SGI RGB image");
        }
        final DataInputStream dIn = new DataInputStream(in);
        dIn.mark(4);
        final short magic = dIn.readShort();
        dIn.reset();
        return (magic == MAGIC);
    }

at the .dIn.readShort()

Do you think it is a bug or I did some stupid mistake?