DecodeUtils.java revision 6c8d0fd879242f78a0c1a7daf4c037043663a49f
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.gallery3d.data; 18 19import com.android.gallery3d.common.BitmapUtils; 20import com.android.gallery3d.common.Utils; 21import com.android.gallery3d.util.ThreadPool.CancelListener; 22import com.android.gallery3d.util.ThreadPool.JobContext; 23 24import android.content.ContentResolver; 25import android.graphics.Bitmap; 26import android.graphics.Bitmap.Config; 27import android.graphics.BitmapFactory; 28import android.graphics.BitmapFactory.Options; 29import android.graphics.BitmapRegionDecoder; 30import android.graphics.Rect; 31import android.net.Uri; 32import android.os.ParcelFileDescriptor; 33 34import java.io.FileDescriptor; 35import java.io.FileInputStream; 36 37public class DecodeUtils { 38 private static final String TAG = "DecodeService"; 39 40 private static class DecodeCanceller implements CancelListener { 41 Options mOptions; 42 public DecodeCanceller(Options options) { 43 mOptions = options; 44 } 45 public void onCancel() { 46 mOptions.requestCancelDecode(); 47 } 48 } 49 50 public static Bitmap requestDecode(JobContext jc, final String filePath, 51 Options options) { 52 if (options == null) options = new Options(); 53 jc.setCancelListener(new DecodeCanceller(options)); 54 return ensureGLCompatibleBitmap( 55 BitmapFactory.decodeFile(filePath, options)); 56 } 57 58 public static Bitmap requestDecode(JobContext jc, FileDescriptor fd, Options options) { 59 if (options == null) options = new Options(); 60 jc.setCancelListener(new DecodeCanceller(options)); 61 return ensureGLCompatibleBitmap( 62 BitmapFactory.decodeFileDescriptor(fd, null, options)); 63 } 64 65 public static Bitmap requestDecode(JobContext jc, byte[] bytes, 66 Options options) { 67 return requestDecode(jc, bytes, 0, bytes.length, options); 68 } 69 70 public static Bitmap requestDecode(JobContext jc, byte[] bytes, int offset, 71 int length, Options options) { 72 if (options == null) options = new Options(); 73 jc.setCancelListener(new DecodeCanceller(options)); 74 return ensureGLCompatibleBitmap( 75 BitmapFactory.decodeByteArray(bytes, offset, length, options)); 76 } 77 78 public static Bitmap requestDecode(JobContext jc, final String filePath, 79 Options options, int targetSize) { 80 FileInputStream fis = null; 81 try { 82 fis = new FileInputStream(filePath); 83 FileDescriptor fd = fis.getFD(); 84 return requestDecode(jc, fd, options, targetSize); 85 } catch (Exception ex) { 86 Log.w(TAG, ex); 87 return null; 88 } finally { 89 Utils.closeSilently(fis); 90 } 91 } 92 93 public static Bitmap requestDecode(JobContext jc, FileDescriptor fd, 94 Options options, int targetSize) { 95 if (options == null) options = new Options(); 96 jc.setCancelListener(new DecodeCanceller(options)); 97 98 options.inJustDecodeBounds = true; 99 BitmapFactory.decodeFileDescriptor(fd, null, options); 100 if (jc.isCancelled()) return null; 101 102 options.inSampleSize = BitmapUtils.computeSampleSizeLarger( 103 options.outWidth, options.outHeight, targetSize); 104 options.inJustDecodeBounds = false; 105 106 Bitmap result = BitmapFactory.decodeFileDescriptor(fd, null, options); 107 // We need to resize down if the decoder does not support inSampleSize. 108 // (For example, GIF images.) 109 result = BitmapUtils.resizeDownIfTooBig(result, targetSize, true); 110 return ensureGLCompatibleBitmap(result); 111 } 112 113 /** 114 * Decodes the bitmap from the given byte array if the image size is larger than the given 115 * requirement. 116 * 117 * Note: The returned image may be resized down. However, both width and height must be 118 * larger than the <code>targetSize</code>. 119 */ 120 public static Bitmap requestDecodeIfBigEnough(JobContext jc, byte[] data, 121 Options options, int targetSize) { 122 if (options == null) options = new Options(); 123 jc.setCancelListener(new DecodeCanceller(options)); 124 125 options.inJustDecodeBounds = true; 126 BitmapFactory.decodeByteArray(data, 0, data.length, options); 127 if (jc.isCancelled()) return null; 128 if (options.outWidth < targetSize || options.outHeight < targetSize) { 129 return null; 130 } 131 options.inSampleSize = BitmapUtils.computeSampleSizeLarger( 132 options.outWidth, options.outHeight, targetSize); 133 options.inJustDecodeBounds = false; 134 return ensureGLCompatibleBitmap( 135 BitmapFactory.decodeByteArray(data, 0, data.length, options)); 136 } 137 138 public static Bitmap requestDecode(JobContext jc, 139 FileDescriptor fileDescriptor, Rect paddings, Options options) { 140 if (options == null) options = new Options(); 141 jc.setCancelListener(new DecodeCanceller(options)); 142 return ensureGLCompatibleBitmap(BitmapFactory.decodeFileDescriptor 143 (fileDescriptor, paddings, options)); 144 } 145 146 // TODO: This function should not be called directly from 147 // DecodeUtils.requestDecode(...), since we don't have the knowledge 148 // if the bitmap will be uploaded to GL. 149 public static Bitmap ensureGLCompatibleBitmap(Bitmap bitmap) { 150 if (bitmap == null || bitmap.getConfig() != null) return bitmap; 151 Bitmap newBitmap = bitmap.copy(Config.ARGB_8888, false); 152 bitmap.recycle(); 153 return newBitmap; 154 } 155 156 public static BitmapRegionDecoder requestCreateBitmapRegionDecoder( 157 JobContext jc, byte[] bytes, int offset, int length, 158 boolean shareable) { 159 if (offset < 0 || length <= 0 || offset + length > bytes.length) { 160 throw new IllegalArgumentException(String.format( 161 "offset = %s, length = %s, bytes = %s", 162 offset, length, bytes.length)); 163 } 164 165 try { 166 return BitmapRegionDecoder.newInstance( 167 bytes, offset, length, shareable); 168 } catch (Throwable t) { 169 Log.w(TAG, t); 170 return null; 171 } 172 } 173 174 public static BitmapRegionDecoder requestCreateBitmapRegionDecoder( 175 JobContext jc, String filePath, boolean shareable) { 176 try { 177 return BitmapRegionDecoder.newInstance(filePath, shareable); 178 } catch (Throwable t) { 179 Log.w(TAG, t); 180 return null; 181 } 182 } 183 184 public static BitmapRegionDecoder requestCreateBitmapRegionDecoder( 185 JobContext jc, FileDescriptor fd, boolean shareable) { 186 try { 187 return BitmapRegionDecoder.newInstance(fd, shareable); 188 } catch (Throwable t) { 189 Log.w(TAG, t); 190 return null; 191 } 192 } 193 194 public static BitmapRegionDecoder requestCreateBitmapRegionDecoder( 195 JobContext jc, Uri uri, ContentResolver resolver, 196 boolean shareable) { 197 ParcelFileDescriptor pfd = null; 198 try { 199 pfd = resolver.openFileDescriptor(uri, "r"); 200 return BitmapRegionDecoder.newInstance( 201 pfd.getFileDescriptor(), shareable); 202 } catch (Throwable t) { 203 Log.w(TAG, t); 204 return null; 205 } finally { 206 Utils.closeSilently(pfd); 207 } 208 } 209} 210