javaopenglbufferedimageanimated-gifapng

Loading an animated image to a BufferedImage array


I'm trying to implement animated textures into an OpenGL game seamlessly. I made a generic ImageDecoder class to translate any BufferedImage into a ByteBuffer. It works perfectly for now, though it doesn't load animated images.

I'm not trying to load an animated image as an ImageIcon. I need the BufferedImage to get an OpenGL-compliant ByteBuffer.

How can I load every frames as a BufferedImage array in an animated image ? On a similar note, how can I get the animation rate / period ?

Does Java handle APNG ?


Solution

  • I don't think Java supports APNG by default, but you can use an 3rd party library to parse it:

    http://code.google.com/p/javapng/source/browse/trunk/javapng2/src/apng/com/sixlegs/png/AnimatedPngImage.java?r=300

    That might be your easiest method. As for getting the frames from an animated gif you have to register an ImageObserver:

    new ImageIcon( url ).setImageObserver( new ImageObserver() {
        public void imageUpdate( Image img, int infoFlags, int x, int y, int width, int height ) {
            if( infoFlags & ImageObserver.FRAMEBITS == ImageObserver.FRAMEBITS ) {
                // another frame was loaded do something with it.
            }
        }
    });
    

    This loads asynchronously on another thread so imageUpdate() won't be called immediately. But it will be called for each frame as it parses it.

    http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/ImageObserver.html