Search in sources :

Example 1 with GifDecoder

use of com.koushikdutta.ion.gif.GifDecoder in project ion by koush.

the class GifTests method testGif.

public void testGif() throws Exception {
    byte[] bytes = Ion.with(getContext()).load("https://raw2.github.com/koush/ion/master/ion-sample/mark.gif").asByteArray().get();
    GifDecoder decoder = new GifDecoder(bytes);
    GifFrame frame = decoder.nextFrame();
    frame = decoder.nextFrame();
}
Also used : GifDecoder(com.koushikdutta.ion.gif.GifDecoder) GifFrame(com.koushikdutta.ion.gif.GifFrame)

Example 2 with GifDecoder

use of com.koushikdutta.ion.gif.GifDecoder in project ion by koush.

the class Issues method testIssue760.

public void testIssue760() throws Exception {
    // https://github.com/koush/ion/issues/760
    byte[] bytes = Ion.with(getContext()).load("https://media.giphy.com/media/ykzXbY24BFqY8/giphy.gif").asByteArray().get();
    GifDecoder decoder = new GifDecoder(bytes);
    for (int i = 0; i <= 3; i++) {
        // Skip to the interesting frames.
        decoder.nextFrame();
    }
    // Get frame 4's pixel value.
    int frame4Pixel = decoder.nextFrame().image.getPixel(35, 200);
    // Get frame 5's pixel value which reproduces the incorrect value.
    int frame5Pixel = decoder.nextFrame().image.getPixel(35, 200);
    // The pixel value should not have changed between frame 4 and frame 5.
    String assertionMessage = "{Pixel(35,200,4): " + Integer.toHexString(frame4Pixel) + "} should be equal to {Pixel(35,200,5): " + Integer.toHexString(frame5Pixel) + "}";
    Assert.assertEquals(assertionMessage, frame4Pixel, frame5Pixel);
}
Also used : GifDecoder(com.koushikdutta.ion.gif.GifDecoder)

Example 3 with GifDecoder

use of com.koushikdutta.ion.gif.GifDecoder in project ion by koush.

the class StreamLoader method loadGif.

protected BitmapInfo loadGif(String key, Point size, InputStream in, BitmapFactory.Options options) throws Exception {
    GifDecoder gifDecoder = new GifDecoder(ByteBuffer.wrap(StreamUtility.readToEndAsArray(in)));
    GifFrame frame = gifDecoder.nextFrame();
    BitmapInfo info = new BitmapInfo(key, options.outMimeType, frame.image, size);
    info.gifDecoder = gifDecoder;
    return info;
}
Also used : GifDecoder(com.koushikdutta.ion.gif.GifDecoder) GifFrame(com.koushikdutta.ion.gif.GifFrame) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Example 4 with GifDecoder

use of com.koushikdutta.ion.gif.GifDecoder in project ion by koush.

the class LoadBitmap method onCompleted.

@Override
public void onCompleted(Exception e, final Response<ByteBufferList> response) {
    if (e == null)
        e = response.getException();
    if (e != null) {
        report(e, null);
        return;
    }
    final ByteBufferList result = response.getResult();
    if (ion.bitmapsPending.tag(key) != this) {
        result.recycle();
        return;
    }
    Ion.getBitmapLoadExecutorService().execute(new Runnable() {

        @Override
        public void run() {
            if (ion.bitmapsPending.tag(key) != LoadBitmap.this) {
                result.recycle();
                return;
            }
            ByteBuffer bb = null;
            try {
                bb = result.getAll();
                Bitmap bitmap;
                GifDecoder gifDecoder;
                BitmapFactory.Options options = ion.bitmapCache.prepareBitmapOptions(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), resizeWidth, resizeHeight);
                final Point size = new Point(options.outWidth, options.outHeight);
                if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
                    // new GifDecoder(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
                    gifDecoder = new GifDecoder(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
                    GifFrame frame = gifDecoder.nextFrame();
                    bitmap = frame.image;
                    // the byte buffer is needed by the decoder
                    bb = null;
                } else {
                    bitmap = IonBitmapCache.loadBitmap(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), options);
                    gifDecoder = null;
                    if (bitmap == null)
                        throw new Exception("failed to load bitmap");
                }
                BitmapInfo info = new BitmapInfo(key, options.outMimeType, bitmap, size);
                info.gifDecoder = gifDecoder;
                info.servedFrom = response.getServedFrom();
                report(null, info);
            } catch (OutOfMemoryError e) {
                report(new Exception(e), null);
            } catch (Exception e) {
                report(e, null);
            } finally {
                ByteBufferList.reclaim(bb);
            }
        }
    });
}
Also used : GifDecoder(com.koushikdutta.ion.gif.GifDecoder) Bitmap(android.graphics.Bitmap) ByteBufferList(com.koushikdutta.async.ByteBufferList) GifFrame(com.koushikdutta.ion.gif.GifFrame) Point(android.graphics.Point) ByteBuffer(java.nio.ByteBuffer) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Example 5 with GifDecoder

use of com.koushikdutta.ion.gif.GifDecoder in project ion by koush.

the class LoadDeepZoom method onCompleted.

@Override
public void onCompleted(Exception e, final Response<File> response) {
    if (e == null)
        e = response.getException();
    if (e != null) {
        report(e, null);
        return;
    }
    final File tempFile = response.getResult();
    if (ion.bitmapsPending.tag(key) != this) {
        // Log.d("IonBitmapLoader", "Bitmap load cancelled (no longer needed)");
        return;
    }
    Ion.getBitmapLoadExecutorService().execute(new Runnable() {

        @Override
        public void run() {
            FileInputStream fin = null;
            try {
                File file;
                // file cache will be null if the file is on the local file system already
                if (fileCache != null) {
                    fileCache.commitTempFiles(key, tempFile);
                    file = fileCache.getFile(key);
                } else {
                    // local file system, use the "temp" file as the source.
                    file = tempFile;
                }
                BitmapFactory.Options options = ion.getBitmapCache().prepareBitmapOptions(file, 0, 0);
                final Point size = new Point(options.outWidth, options.outHeight);
                if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
                    fin = fileCache.get(key);
                    GifDecoder gifDecoder = new GifDecoder(ByteBuffer.wrap(StreamUtility.readToEndAsArray(fin)));
                    GifFrame frame = gifDecoder.nextFrame();
                    BitmapInfo info = new BitmapInfo(key, options.outMimeType, frame.image, size);
                    info.gifDecoder = gifDecoder;
                    report(null, info);
                    return;
                }
                BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(file.toString(), false);
                Bitmap bitmap = decoder.decodeRegion(new Rect(0, 0, size.x, size.y), options);
                if (bitmap == null)
                    throw new Exception("unable to load decoder");
                BitmapInfo info = new BitmapInfo(key, options.outMimeType, bitmap, size);
                info.decoder = decoder;
                info.decoderFile = file;
                info.servedFrom = response.getServedFrom();
                report(null, info);
            } catch (Exception e) {
                report(e, null);
            } finally {
                StreamUtility.closeQuietly(fin);
            }
        }
    });
}
Also used : Rect(android.graphics.Rect) BitmapRegionDecoder(android.graphics.BitmapRegionDecoder) Point(android.graphics.Point) FileInputStream(java.io.FileInputStream) GifDecoder(com.koushikdutta.ion.gif.GifDecoder) Bitmap(android.graphics.Bitmap) GifFrame(com.koushikdutta.ion.gif.GifFrame) File(java.io.File) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Aggregations

GifDecoder (com.koushikdutta.ion.gif.GifDecoder)5 GifFrame (com.koushikdutta.ion.gif.GifFrame)4 BitmapInfo (com.koushikdutta.ion.bitmap.BitmapInfo)3 Bitmap (android.graphics.Bitmap)2 Point (android.graphics.Point)2 BitmapRegionDecoder (android.graphics.BitmapRegionDecoder)1 Rect (android.graphics.Rect)1 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ByteBuffer (java.nio.ByteBuffer)1