summaryrefslogtreecommitdiffstats
path: root/src/com/android/photos
diff options
context:
space:
mode:
authorGeorge Mount <mount@google.com>2013-05-21 22:30:59 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2013-05-21 22:30:59 +0000
commitbc2c0d6cda0955490db62ab3a4f60678ca34b88c (patch)
tree9a9b6f6d1d852a70081975a40e236eef149009ee /src/com/android/photos
parent407fe728f95645ca43d7c2d3254856c6314ad534 (diff)
parent1a833d67f22eefcbc5a76a104d0857a5b1064433 (diff)
downloadandroid_packages_apps_Snap-bc2c0d6cda0955490db62ab3a4f60678ca34b88c.tar.gz
android_packages_apps_Snap-bc2c0d6cda0955490db62ab3a4f60678ca34b88c.tar.bz2
android_packages_apps_Snap-bc2c0d6cda0955490db62ab3a4f60678ca34b88c.zip
Merge "Combine code for bitmap decoding and add byte array decoding." into gb-ub-photos-carlsbad
Diffstat (limited to 'src/com/android/photos')
-rw-r--r--src/com/android/photos/data/BitmapDecoder.java118
1 files changed, 78 insertions, 40 deletions
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<BitmapFactory.Options> sOptions =
new SynchronizedPool<BitmapFactory.Options>(POOL_SIZE);
- public static Bitmap decode(InputStream in) {
- BitmapFactory.Options opts = getOptions();
+ private interface Decoder<T> {
+ Bitmap decode(T input, BitmapFactory.Options options);
+
+ boolean decodeBounds(T input, BitmapFactory.Options options);
+ }
+
+ private static abstract class OnlyDecode<T> implements Decoder<T> {
+ @Override
+ public boolean decodeBounds(T input, BitmapFactory.Options options) {
+ decode(input, options);
+ return true;
+ }
+ }
+
+ private static final Decoder<InputStream> sStreamDecoder = new Decoder<InputStream>() {
+ @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<String> sFileDecoder = new OnlyDecode<String>() {
+ @Override
+ public Bitmap decode(String filePath, Options options) {
+ return BitmapFactory.decodeFile(filePath, options);
+ }
+ };
+
+ private static final Decoder<byte[]> sByteArrayDecoder = new OnlyDecode<byte[]>() {
+ @Override
+ public Bitmap decode(byte[] data, Options options) {
+ return BitmapFactory.decodeByteArray(data, 0, data.length, options);
+ }
+ };
+
+ private static <T> Bitmap delegateDecode(Decoder<T> 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);
- }
}