Home | History | Annotate | Download | only in photos
      1 /*
      2  * Copyright (C) 2013 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.photos;
     18 
     19 import android.graphics.Bitmap;
     20 import android.graphics.BitmapFactory;
     21 import android.graphics.BitmapRegionDecoder;
     22 import android.graphics.Rect;
     23 import android.util.Log;
     24 import com.android.photos.views.TiledImageRenderer;
     25 
     26 import java.io.IOException;
     27 
     28 public class BitmapRegionTileSource implements TiledImageRenderer.TileSource {
     29 
     30     BitmapRegionDecoder mDecoder;
     31 
     32 
     33     public BitmapRegionTileSource(String path) {
     34         try {
     35             mDecoder = BitmapRegionDecoder.newInstance(path, true);
     36         } catch (IOException e) {
     37             Log.w("BitmapRegionTileSource", "ctor failed", e);
     38         }
     39     }
     40 
     41     @Override
     42     public int getTileSize() {
     43         return 256;
     44     }
     45 
     46     @Override
     47     public int getImageWidth() {
     48         return mDecoder.getWidth();
     49     }
     50 
     51     @Override
     52     public int getImageHeight() {
     53         return mDecoder.getHeight();
     54     }
     55 
     56     @Override
     57     public Bitmap getTile(int level, int x, int y, Bitmap bitmap) {
     58         int tileSize = getTileSize();
     59         int t = tileSize << level;
     60 
     61         Rect wantRegion = new Rect(x, y, x + t, y + t);
     62 
     63         if (bitmap == null) {
     64             bitmap = Bitmap.createBitmap(tileSize, tileSize, Bitmap.Config.ARGB_8888);
     65         }
     66 
     67         BitmapFactory.Options options = new BitmapFactory.Options();
     68         options.inPreferredConfig = Bitmap.Config.ARGB_8888;
     69         options.inPreferQualityOverSpeed = true;
     70         options.inSampleSize =  (1 << level);
     71         options.inBitmap = bitmap;
     72 
     73         try {
     74             // In CropImage, we may call the decodeRegion() concurrently.
     75             bitmap = mDecoder.decodeRegion(wantRegion, options);
     76         } finally {
     77             if (options.inBitmap != bitmap && options.inBitmap != null) {
     78                 options.inBitmap = null;
     79             }
     80         }
     81 
     82         if (bitmap == null) {
     83             Log.w("BitmapRegionTileSource", "fail in decoding region");
     84         }
     85         return bitmap;
     86     }
     87 }
     88