From 1a833d67f22eefcbc5a76a104d0857a5b1064433 Mon Sep 17 00:00:00 2001 From: George Mount Date: Mon, 20 May 2013 16:54:09 -0700 Subject: Combine code for bitmap decoding and add byte array decoding. Change-Id: Id780f94a5d939e5423b87f2e1d4ddf18b3dfa126 --- src/com/android/photos/data/BitmapDecoder.java | 118 ++++++++++++++++--------- 1 file changed, 78 insertions(+), 40 deletions(-) (limited to 'src/com/android/photos/data/BitmapDecoder.java') diff --git a/src/com/android/photos/data/BitmapDecoder.java b/src/com/android/photos/data/BitmapDecoder.java index f19808d06..3e5a0f778 100644 --- a/src/com/android/photos/data/BitmapDecoder.java +++ b/src/com/android/photos/data/BitmapDecoder.java @@ -18,6 +18,7 @@ package com.android.photos.data; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; +import android.graphics.BitmapFactory.Options; import android.util.Log; import android.util.Pools.Pool; import android.util.Pools.SynchronizedPool; @@ -46,57 +47,100 @@ public class BitmapDecoder { private static final Pool sOptions = new SynchronizedPool(POOL_SIZE); - public static Bitmap decode(InputStream in) { - BitmapFactory.Options opts = getOptions(); + private interface Decoder { + Bitmap decode(T input, BitmapFactory.Options options); + + boolean decodeBounds(T input, BitmapFactory.Options options); + } + + private static abstract class OnlyDecode implements Decoder { + @Override + public boolean decodeBounds(T input, BitmapFactory.Options options) { + decode(input, options); + return true; + } + } + + private static final Decoder sStreamDecoder = new Decoder() { + @Override + public Bitmap decode(InputStream is, Options options) { + return BitmapFactory.decodeStream(is, null, options); + } + + @Override + public boolean decodeBounds(InputStream is, Options options) { + is.mark(HEADER_MAX_SIZE); + BitmapFactory.decodeStream(is, null, options); + try { + is.reset(); + return true; + } catch (IOException e) { + Log.e(TAG, "Could not decode stream to bitmap", e); + return false; + } + } + }; + + private static final Decoder sFileDecoder = new OnlyDecode() { + @Override + public Bitmap decode(String filePath, Options options) { + return BitmapFactory.decodeFile(filePath, options); + } + }; + + private static final Decoder sByteArrayDecoder = new OnlyDecode() { + @Override + public Bitmap decode(byte[] data, Options options) { + return BitmapFactory.decodeByteArray(data, 0, data.length, options); + } + }; + + private static Bitmap delegateDecode(Decoder decoder, T input) { + BitmapFactory.Options options = getOptions(); try { - if (!in.markSupported()) { - in = new BufferedInputStream(in); + options.inJustDecodeBounds = true; + if (!decoder.decodeBounds(input, options)) { + return null; } - opts.inJustDecodeBounds = true; - in.mark(HEADER_MAX_SIZE); - BitmapFactory.decodeStream(in, null, opts); - in.reset(); - opts.inJustDecodeBounds = false; + options.inJustDecodeBounds = false; GalleryBitmapPool pool = GalleryBitmapPool.getInstance(); - Bitmap reuseBitmap = pool.get(opts.outWidth, opts.outHeight); - opts.inBitmap = reuseBitmap; - Bitmap decodedBitmap = BitmapFactory.decodeStream(in, null, opts); + Bitmap reuseBitmap = pool.get(options.outWidth, options.outHeight); + options.inBitmap = reuseBitmap; + Bitmap decodedBitmap = decoder.decode(input, options); if (reuseBitmap != null && decodedBitmap != reuseBitmap) { pool.put(reuseBitmap); } return decodedBitmap; - } catch (IOException e) { - Log.e(TAG, "Could not decode stream to bitmap", e); - return null; } finally { - Utils.closeSilently(in); - release(opts); + options.inBitmap = null; + options.inJustDecodeBounds = false; + sOptions.release(options); } } - public static Bitmap decode(File in) { - return decodeFile(in.toString()); - } - - public static Bitmap decodeFile(String in) { - BitmapFactory.Options opts = getOptions(); + public static Bitmap decode(InputStream in) { try { - opts.inJustDecodeBounds = true; - BitmapFactory.decodeFile(in, opts); - opts.inJustDecodeBounds = false; - GalleryBitmapPool pool = GalleryBitmapPool.getInstance(); - Bitmap reuseBitmap = pool.get(opts.outWidth, opts.outHeight); - opts.inBitmap = reuseBitmap; - Bitmap decodedBitmap = BitmapFactory.decodeFile(in, opts); - if (reuseBitmap != null && decodedBitmap != reuseBitmap) { - pool.put(reuseBitmap); + if (!in.markSupported()) { + in = new BufferedInputStream(in); } - return decodedBitmap; + return delegateDecode(sStreamDecoder, in); } finally { - release(opts); + Utils.closeSilently(in); } } + public static Bitmap decode(File file) { + return decodeFile(file.getPath()); + } + + public static Bitmap decodeFile(String path) { + return delegateDecode(sFileDecoder, path); + } + + public static Bitmap decodeByteArray(byte[] data) { + return delegateDecode(sByteArrayDecoder, data); + } + public static void put(Bitmap bitmap) { GalleryBitmapPool.getInstance().put(bitmap); } @@ -113,10 +157,4 @@ public class BitmapDecoder { return opts; } - - private static void release(BitmapFactory.Options opts) { - opts.inBitmap = null; - opts.inJustDecodeBounds = false; - sOptions.release(opts); - } } -- cgit v1.2.3