Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
| 17 | package com.android.gallery3d.ui; |
| 18 | |
Owen Lin | ed97d58 | 2012-07-26 17:16:47 +0800 | [diff] [blame] | 19 | import android.annotation.TargetApi; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.Bitmap.Config; |
| 22 | import android.graphics.BitmapFactory; |
| 23 | import android.graphics.BitmapRegionDecoder; |
Owen Lin | 30aa920 | 2012-06-27 19:01:21 +0800 | [diff] [blame] | 24 | import android.graphics.Canvas; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 25 | import android.graphics.Rect; |
| 26 | |
Owen Lin | 30aa920 | 2012-06-27 19:01:21 +0800 | [diff] [blame] | 27 | import com.android.gallery3d.common.ApiHelper; |
Owen Lin | 1b2cfc1 | 2011-10-18 16:57:15 +0800 | [diff] [blame] | 28 | import com.android.gallery3d.common.Utils; |
Owen Lin | c2c0b01 | 2012-05-17 15:59:25 -0700 | [diff] [blame] | 29 | import com.android.gallery3d.data.BitmapPool; |
Owen Lin | 1b2cfc1 | 2011-10-18 16:57:15 +0800 | [diff] [blame] | 30 | |
Your Name | 6dc3866 | 2012-12-04 10:01:41 -0800 | [diff] [blame^] | 31 | public class TileImageViewAdapter implements TileImageView.TileSource { |
Owen Lin | 1b2cfc1 | 2011-10-18 16:57:15 +0800 | [diff] [blame] | 32 | private static final String TAG = "TileImageViewAdapter"; |
Chih-Chung Chang | 15b351a | 2012-03-15 16:38:45 +0800 | [diff] [blame] | 33 | protected ScreenNail mScreenNail; |
Chih-Chung Chang | b8be1e0 | 2012-04-17 20:35:14 +0800 | [diff] [blame] | 34 | protected boolean mOwnScreenNail; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 35 | protected BitmapRegionDecoder mRegionDecoder; |
| 36 | protected int mImageWidth; |
| 37 | protected int mImageHeight; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 38 | protected int mLevelCount; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 39 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 40 | public TileImageViewAdapter() { |
| 41 | } |
| 42 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 43 | public synchronized void clear() { |
Owen Lin | 030f8da | 2012-10-11 15:50:38 +0800 | [diff] [blame] | 44 | mScreenNail = null; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 45 | mImageWidth = 0; |
| 46 | mImageHeight = 0; |
| 47 | mLevelCount = 0; |
| 48 | mRegionDecoder = null; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 49 | } |
| 50 | |
Owen Lin | 030f8da | 2012-10-11 15:50:38 +0800 | [diff] [blame] | 51 | // Caller is responsible to recycle the ScreenNail |
Chih-Chung Chang | 15b351a | 2012-03-15 16:38:45 +0800 | [diff] [blame] | 52 | public synchronized void setScreenNail( |
| 53 | ScreenNail screenNail, int width, int height) { |
Chih-Chung Chang | b8be1e0 | 2012-04-17 20:35:14 +0800 | [diff] [blame] | 54 | Utils.checkNotNull(screenNail); |
Owen Lin | 030f8da | 2012-10-11 15:50:38 +0800 | [diff] [blame] | 55 | mScreenNail = screenNail; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 56 | mImageWidth = width; |
| 57 | mImageHeight = height; |
| 58 | mRegionDecoder = null; |
| 59 | mLevelCount = 0; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public synchronized void setRegionDecoder(BitmapRegionDecoder decoder) { |
| 63 | mRegionDecoder = Utils.checkNotNull(decoder); |
| 64 | mImageWidth = decoder.getWidth(); |
| 65 | mImageHeight = decoder.getHeight(); |
| 66 | mLevelCount = calculateLevelCount(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private int calculateLevelCount() { |
| 70 | return Math.max(0, Utils.ceilLog2( |
Chih-Chung Chang | 15b351a | 2012-03-15 16:38:45 +0800 | [diff] [blame] | 71 | (float) mImageWidth / mScreenNail.getWidth())); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 72 | } |
| 73 | |
Owen Lin | c2c0b01 | 2012-05-17 15:59:25 -0700 | [diff] [blame] | 74 | // Gets a sub image on a rectangle of the current photo. For example, |
| 75 | // getTile(1, 50, 50, 100, 3, pool) means to get the region located |
| 76 | // at (50, 50) with sample level 1 (ie, down sampled by 2^1) and the |
| 77 | // target tile size (after sampling) 100 with border 3. |
| 78 | // |
| 79 | // From this spec, we can infer the actual tile size to be |
| 80 | // 100 + 3x2 = 106, and the size of the region to be extracted from the |
| 81 | // photo to be 200 with border 6. |
| 82 | // |
| 83 | // As a result, we should decode region (50-6, 50-6, 250+6, 250+6) or |
| 84 | // (44, 44, 256, 256) from the original photo and down sample it to 106. |
Owen Lin | ed97d58 | 2012-07-26 17:16:47 +0800 | [diff] [blame] | 85 | @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB) |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 86 | @Override |
Your Name | 6dc3866 | 2012-12-04 10:01:41 -0800 | [diff] [blame^] | 87 | public Bitmap getTile(int level, int x, int y, int tileSize, BitmapPool pool) { |
Owen Lin | 30aa920 | 2012-06-27 19:01:21 +0800 | [diff] [blame] | 88 | if (!ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_REGION_DECODER) { |
Your Name | 6dc3866 | 2012-12-04 10:01:41 -0800 | [diff] [blame^] | 89 | return getTileWithoutReusingBitmap(level, x, y, tileSize); |
Owen Lin | 30aa920 | 2012-06-27 19:01:21 +0800 | [diff] [blame] | 90 | } |
| 91 | |
Owen Lin | c2c0b01 | 2012-05-17 15:59:25 -0700 | [diff] [blame] | 92 | int t = tileSize << level; |
Chih-Chung Chang | cb4fb7c | 2012-03-21 16:19:21 +0800 | [diff] [blame] | 93 | |
Your Name | 6dc3866 | 2012-12-04 10:01:41 -0800 | [diff] [blame^] | 94 | Rect wantRegion = new Rect(x, y, x + t, y + t); |
Owen Lin | c2c0b01 | 2012-05-17 15:59:25 -0700 | [diff] [blame] | 95 | |
| 96 | boolean needClear; |
Ray Chen | 6fc8d72 | 2012-05-02 15:31:38 +0800 | [diff] [blame] | 97 | BitmapRegionDecoder regionDecoder = null; |
Owen Lin | c2c0b01 | 2012-05-17 15:59:25 -0700 | [diff] [blame] | 98 | |
Ray Chen | 6fc8d72 | 2012-05-02 15:31:38 +0800 | [diff] [blame] | 99 | synchronized (this) { |
| 100 | regionDecoder = mRegionDecoder; |
| 101 | if (regionDecoder == null) return null; |
Owen Lin | c2c0b01 | 2012-05-17 15:59:25 -0700 | [diff] [blame] | 102 | |
| 103 | // We need to clear a reused bitmap, if wantRegion is not fully |
| 104 | // within the image. |
| 105 | needClear = !new Rect(0, 0, mImageWidth, mImageHeight) |
| 106 | .contains(wantRegion); |
Ray Chen | 6fc8d72 | 2012-05-02 15:31:38 +0800 | [diff] [blame] | 107 | } |
| 108 | |
Owen Lin | c2c0b01 | 2012-05-17 15:59:25 -0700 | [diff] [blame] | 109 | Bitmap bitmap = pool == null ? null : pool.getBitmap(); |
| 110 | if (bitmap != null) { |
| 111 | if (needClear) bitmap.eraseColor(0); |
| 112 | } else { |
Your Name | 6dc3866 | 2012-12-04 10:01:41 -0800 | [diff] [blame^] | 113 | bitmap = Bitmap.createBitmap(tileSize, tileSize, Config.ARGB_8888); |
Owen Lin | c2c0b01 | 2012-05-17 15:59:25 -0700 | [diff] [blame] | 114 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 115 | |
| 116 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 117 | options.inPreferredConfig = Config.ARGB_8888; |
| 118 | options.inPreferQualityOverSpeed = true; |
| 119 | options.inSampleSize = (1 << level); |
Owen Lin | c2c0b01 | 2012-05-17 15:59:25 -0700 | [diff] [blame] | 120 | options.inBitmap = bitmap; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 121 | |
Owen Lin | c2c0b01 | 2012-05-17 15:59:25 -0700 | [diff] [blame] | 122 | try { |
| 123 | // In CropImage, we may call the decodeRegion() concurrently. |
| 124 | synchronized (regionDecoder) { |
| 125 | bitmap = regionDecoder.decodeRegion(wantRegion, options); |
| 126 | } |
| 127 | } finally { |
| 128 | if (options.inBitmap != bitmap && options.inBitmap != null) { |
| 129 | if (pool != null) pool.recycle(options.inBitmap); |
| 130 | options.inBitmap = null; |
| 131 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Owen Lin | 1b2cfc1 | 2011-10-18 16:57:15 +0800 | [diff] [blame] | 134 | if (bitmap == null) { |
| 135 | Log.w(TAG, "fail in decoding region"); |
Owen Lin | 1b2cfc1 | 2011-10-18 16:57:15 +0800 | [diff] [blame] | 136 | } |
Owen Lin | c2c0b01 | 2012-05-17 15:59:25 -0700 | [diff] [blame] | 137 | return bitmap; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 138 | } |
| 139 | |
Owen Lin | 30aa920 | 2012-06-27 19:01:21 +0800 | [diff] [blame] | 140 | private Bitmap getTileWithoutReusingBitmap( |
Your Name | 6dc3866 | 2012-12-04 10:01:41 -0800 | [diff] [blame^] | 141 | int level, int x, int y, int tileSize) { |
Owen Lin | 30aa920 | 2012-06-27 19:01:21 +0800 | [diff] [blame] | 142 | int t = tileSize << level; |
Your Name | 6dc3866 | 2012-12-04 10:01:41 -0800 | [diff] [blame^] | 143 | Rect wantRegion = new Rect(x, y, x + t, y + t); |
Owen Lin | 30aa920 | 2012-06-27 19:01:21 +0800 | [diff] [blame] | 144 | |
| 145 | BitmapRegionDecoder regionDecoder; |
| 146 | Rect overlapRegion; |
| 147 | |
| 148 | synchronized (this) { |
| 149 | regionDecoder = mRegionDecoder; |
| 150 | if (regionDecoder == null) return null; |
| 151 | overlapRegion = new Rect(0, 0, mImageWidth, mImageHeight); |
| 152 | Utils.assertTrue(overlapRegion.intersect(wantRegion)); |
| 153 | } |
| 154 | |
| 155 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 156 | options.inPreferredConfig = Config.ARGB_8888; |
| 157 | options.inPreferQualityOverSpeed = true; |
| 158 | options.inSampleSize = (1 << level); |
| 159 | Bitmap bitmap = null; |
| 160 | |
| 161 | // In CropImage, we may call the decodeRegion() concurrently. |
| 162 | synchronized (regionDecoder) { |
| 163 | bitmap = regionDecoder.decodeRegion(overlapRegion, options); |
| 164 | } |
| 165 | |
| 166 | if (bitmap == null) { |
| 167 | Log.w(TAG, "fail in decoding region"); |
| 168 | } |
| 169 | |
| 170 | if (wantRegion.equals(overlapRegion)) return bitmap; |
| 171 | |
Your Name | 6dc3866 | 2012-12-04 10:01:41 -0800 | [diff] [blame^] | 172 | Bitmap result = Bitmap.createBitmap(tileSize, tileSize, Config.ARGB_8888); |
Owen Lin | 30aa920 | 2012-06-27 19:01:21 +0800 | [diff] [blame] | 173 | Canvas canvas = new Canvas(result); |
| 174 | canvas.drawBitmap(bitmap, |
| 175 | (overlapRegion.left - wantRegion.left) >> level, |
| 176 | (overlapRegion.top - wantRegion.top) >> level, null); |
| 177 | return result; |
| 178 | } |
| 179 | |
| 180 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 181 | @Override |
Chih-Chung Chang | 15b351a | 2012-03-15 16:38:45 +0800 | [diff] [blame] | 182 | public ScreenNail getScreenNail() { |
| 183 | return mScreenNail; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public int getImageHeight() { |
| 188 | return mImageHeight; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public int getImageWidth() { |
| 193 | return mImageWidth; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public int getLevelCount() { |
| 198 | return mLevelCount; |
| 199 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 200 | } |