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();
}
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);
}
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;
}
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);
}
}
});
}
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);
}
}
});
}
Aggregations