public Gif(PApplet parent, String filename) { // this creates a fake image so that the first time this // attempts to draw, something happens that's not an exception super(1, 1, ARGB); this.parent = parent; // create the GifDecoder GifDecoder gifDecoder = createDecoder(parent, filename); // fill up the PImage and the delay arrays frames = extractFrames(gifDecoder); delays = extractDelays(gifDecoder); // get the GIFs repeat count repeatSetting = gifDecoder.getLoopCount(); // re-init our PImage with the new size super.init(frames[0].width, frames[0].height, ARGB); jump(0); parent.registerMethod("dispose", this); // and now, make the magic happen runner = new Thread(this); runner.start(); }
/* * creates an int-array of frame delays in the gifDecoder object */ private static int[] extractDelays(GifDecoder gifDecoder) { int n = gifDecoder.getFrameCount(); int[] delays = new int[n]; for (int i = 0; i < n; i++) { delays[i] = gifDecoder.getDelay(i); // display duration of frame in // milliseconds } return delays; }
/* * creates a PImage-array of gif frames in a GifDecoder object */ private static PImage[] extractFrames(GifDecoder gifDecoder) { int n = gifDecoder.getFrameCount(); PImage[] frames = new PImage[n]; for (int i = 0; i < n; i++) { BufferedImage frame = gifDecoder.getFrame(i); frames[i] = new PImage(frame.getWidth(), frame.getHeight(), ARGB); System.arraycopy( frame.getRGB(0, 0, frame.getWidth(), frame.getHeight(), null, 0, frame.getWidth()), 0, frames[i].pixels, 0, frame.getWidth() * frame.getHeight()); } return frames; }
/* * creates a GifDecoder object and loads a gif file */ private static GifDecoder createDecoder(PApplet parent, String filename) { GifDecoder gifDecoder = new GifDecoder(); gifDecoder.read(createInputStream(parent, filename)); return gifDecoder; }