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; }
/** Returns whether the given icon is an animated GIF. */ public static boolean isAnimated(Icon icon) { if (icon instanceof ImageIcon) { Image image = ((ImageIcon) icon).getImage(); if (image != null) { // Quick check for commonly-occurring animated GIF comment Object comment = image.getProperty("comment", null); if (String.valueOf(comment).startsWith("GifBuilder")) return true; // Check cache of already-decoded images if (decoded.containsKey(image)) { return Boolean.TRUE.equals(decoded.get(image)); } InputStream is = null; try { URL url = new URL(icon.toString()); is = url.openConnection().getInputStream(); } catch (Exception e) { e.printStackTrace(); } if (is == null) { try { // Beware: lots of hackery to obtain the image input stream // Be sure to catch security exceptions ImageProducer p = image.getSource(); // [macavity] - below rem'd lines don't compile on mac // if (p instanceof InputStreamImageSource) { // Method m = // InputStreamImageSource.class.getDeclaredMethod("getDecoder", null); // m.setAccessible(true); // ImageDecoder d = (ImageDecoder)m.invoke(p, null); // if (d instanceof GifImageDecoder) { // GifImageDecoder gd = (GifImageDecoder)d; // Field input = // ImageDecoder.class.getDeclaredField("input"); // input.setAccessible(true); // is = (InputStream)input.get(gd); // } // } } catch (Exception e) { e.printStackTrace(); } } if (is != null) { GifDecoder decoder = new GifDecoder(); decoder.read(is); boolean animated = decoder.getFrameCount() > 1; decoded.put(image, Boolean.valueOf(animated)); return animated; } } return false; } return icon instanceof AnimatedIcon; }
public void run() { mGifDecoder.complete(); int i, n = mGifDecoder.getFrameCount(), t; for (i = 1; i < n; i++) { mTmpBitmap = mGifDecoder.getFrame(i); t = mGifDecoder.getDelay(i); android.util.Log.v("GifAnimationDrawable", "===>Frame " + i + ": " + t + "]"); addFrame(new BitmapDrawable(mTmpBitmap), t); } decoded = true; mGifDecoder = null; }
private void playGif(InputStream stream) { mGifDecoder = new GifDecoder(); mGifDecoder.read(stream); mIsPlayingGif = true; new Thread( new Runnable() { public void run() { final int n = mGifDecoder.getFrameCount(); final int ntimes = mGifDecoder.getLoopCount(); int repetitionCounter = 0; do { for (int i = 0; i < n; i++) { mTmpBitmap = mGifDecoder.getFrame(i); int t = mGifDecoder.getDelay(i); mHandler.post(mUpdateResults); try { Thread.sleep(t); } catch (InterruptedException e) { e.printStackTrace(); } } if (ntimes != 0) { repetitionCounter++; } } while (mIsPlayingGif && (repetitionCounter <= ntimes)); } }) .start(); }
private void incrementFrameIndex() { index++; if (index >= decoder.getFrameCount()) { index = 0; } }
/* * 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; }
public void playGif(InputStream stream) { mGifDecoder = new GifDecoder(); mGifDecoder.read(stream); mIsPlayingGif = true; new Thread(runnable).start(); }
@Override protected void onDraw(Canvas canvas) { // super.onDraw(canvas); final Paint p = new Paint(); Bitmap rebitmap = Bitmap.createScaledBitmap(bitmap, width, height, true); canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), p); // if (decodeStatus == DECODE_STATUS_UNDECODE) { canvas.drawBitmap(bitmap, 0, 0, null); if (playFlag) { decode(); invalidate(); } } else if (decodeStatus == DECODE_STATUS_DECODING) { canvas.drawBitmap(bitmap, 0, 0, null); invalidate(); } else if (decodeStatus == DECODE_STATUS_DECODED) { if (imageType == IMAGE_TYPE_STATIC) { canvas.drawBitmap(bitmap, 0, 0, null); } else if (imageType == IMAGE_TYPE_DYNAMIC) { if (playFlag) { long now = System.currentTimeMillis(); if (time + decoder.getDelay(index) < now) { time += decoder.getDelay(index); incrementFrameIndex(); } Bitmap bitmap = decoder.getFrame(index); if (bitmap != null) { canvas.drawBitmap(bitmap, 0, 0, null); } invalidate(); } else { Bitmap bitmap = decoder.getFrame(index); canvas.drawBitmap(bitmap, 0, 0, null); } } else { canvas.drawBitmap(bitmap, 0, 0, null); } } }
public void run() { final int n = mGifDecoder.getFrameCount(); final int ntimes = mGifDecoder.getLoopCount(); int repetitionCounter = 0; do { for (int i = 0; i < n; i++) { mTmpBitmap = mGifDecoder.getFrame(i); int t = mGifDecoder.getDelay(i); mHandler.post(mUpdateResults); try { Thread.sleep(t); } catch (InterruptedException e) { e.printStackTrace(); } } if (ntimes != 0) { repetitionCounter++; } } while (mIsPlayingGif && (repetitionCounter <= ntimes)); }
@SuppressWarnings("deprecation") public GifAnimationDrawable(InputStream is, boolean inline) throws IOException { super(); InputStream bis = is; if (!BufferedInputStream.class.isInstance(bis)) bis = new BufferedInputStream(is, 32768); decoded = false; mGifDecoder = new GifDecoder(); mGifDecoder.read(bis); mTmpBitmap = mGifDecoder.getFrame(0); android.util.Log.v( "GifAnimationDrawable", "===>Lead frame: [" + width + "x" + height + "; " + mGifDecoder.getDelay(0) + ";" + mGifDecoder.getLoopCount() + "]"); height = mTmpBitmap.getHeight(); width = mTmpBitmap.getWidth(); addFrame(new BitmapDrawable(mTmpBitmap), mGifDecoder.getDelay(0)); setOneShot(mGifDecoder.getLoopCount() != 0); setVisible(true, true); if (inline) { loader.run(); } else { new Thread(loader).start(); } }
/** Load the images */ private void loadImages() { GifDecoder decode = new GifDecoder(); decode.read(file); int numFrames = decode.getFrameCount(); if (numFrames > 0) { images = new GreenfootImage[numFrames]; delay = new int[numFrames]; } else { images = new GreenfootImage[1]; images[0] = new GreenfootImage(1, 1); } for (int i = 0; i < numFrames; i++) { GreenfootImage image = new GreenfootImage(decode.getFrame(i).getWidth(), decode.getFrame(i).getHeight()); BufferedImage frame = image.getAwtImage(); Graphics2D g = (Graphics2D) frame.getGraphics(); g.drawImage(decode.getFrame(i), null, 0, 0); delay[i] = decode.getDelay(i); images[i] = image; } time = System.currentTimeMillis(); }
/* * 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; }
public void nextFrame() { if (decodeStatus == DECODE_STATUS_DECODED && index < decoder.getFrameCount()) { incrementFrameIndex(); invalidate(); } }
private void decrementFrameIndex() { index--; if (index < 0) { index = decoder.getFrameCount() - 1; } }