Home | History | Annotate | Download | only in gfx
      1 // Copyright 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 package org.chromium.ui.gfx;
      6 
      7 import android.content.res.Resources;
      8 import android.graphics.Bitmap;
      9 import android.graphics.BitmapFactory;
     10 
     11 import org.chromium.base.CalledByNative;
     12 import org.chromium.base.JNINamespace;
     13 
     14 /**
     15  * Helper class to decode and sample down bitmap resources.
     16  */
     17 @JNINamespace("gfx")
     18 public class BitmapHelper {
     19     @CalledByNative
     20     private static Bitmap createBitmap(int width,
     21                                       int height,
     22                                       int bitmapFormatValue) {
     23         Bitmap.Config bitmapConfig = getBitmapConfigForFormat(bitmapFormatValue);
     24         return Bitmap.createBitmap(width, height, bitmapConfig);
     25     }
     26 
     27     /**
     28      * Decode and sample down a bitmap resource to the requested width and height.
     29      *
     30      * @param name The resource name of the bitmap to decode.
     31      * @param reqWidth The requested width of the resulting bitmap.
     32      * @param reqHeight The requested height of the resulting bitmap.
     33      * @return A bitmap sampled down from the original with the same aspect ratio and dimensions.
     34      *         that are equal to or greater than the requested width and height.
     35      */
     36     @CalledByNative
     37     private static Bitmap decodeDrawableResource(String name,
     38                                                  int reqWidth,
     39                                                  int reqHeight) {
     40         Resources res = Resources.getSystem();
     41         int resId = res.getIdentifier(name, null, null);
     42         if (resId == 0) return null;
     43 
     44         final BitmapFactory.Options options = new BitmapFactory.Options();
     45         options.inJustDecodeBounds = true;
     46         BitmapFactory.decodeResource(res, resId, options);
     47 
     48         options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
     49         options.inJustDecodeBounds = false;
     50         options.inPreferredConfig = Bitmap.Config.ARGB_8888;
     51         return BitmapFactory.decodeResource(res, resId, options);
     52     }
     53 
     54     // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
     55     private static int calculateInSampleSize(BitmapFactory.Options options,
     56                                              int reqWidth,
     57                                              int reqHeight) {
     58         // Raw height and width of image
     59         final int height = options.outHeight;
     60         final int width = options.outWidth;
     61         int inSampleSize = 1;
     62 
     63         if (height > reqHeight || width > reqWidth) {
     64 
     65             // Calculate ratios of height and width to requested height and width
     66             final int heightRatio = Math.round((float) height / (float) reqHeight);
     67             final int widthRatio = Math.round((float) width / (float) reqWidth);
     68 
     69             // Choose the smallest ratio as inSampleSize value, this will guarantee
     70             // a final image with both dimensions larger than or equal to the
     71             // requested height and width.
     72             inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
     73         }
     74 
     75         return inSampleSize;
     76     }
     77 
     78     /**
     79      * Provides a matching integer constant for the Bitmap.Config value passed.
     80      *
     81      * @param bitmapConfig The Bitmap Configuration value.
     82      * @return Matching integer constant for the Bitmap.Config value passed.
     83      */
     84     @CalledByNative
     85     private static int getBitmapFormatForConfig(Bitmap.Config bitmapConfig) {
     86         switch (bitmapConfig) {
     87             case ALPHA_8:
     88                 return BitmapFormat.ALPHA_8;
     89             case ARGB_4444:
     90                 return BitmapFormat.ARGB_4444;
     91             case ARGB_8888:
     92                 return BitmapFormat.ARGB_8888;
     93             case RGB_565:
     94                 return BitmapFormat.RGB_565;
     95             default:
     96                 return BitmapFormat.NO_CONFIG;
     97         }
     98     }
     99 
    100      /**
    101      * Provides a matching Bitmap.Config for the enum config value passed.
    102      *
    103      * @param bitmapFormatValue The Bitmap Configuration enum value.
    104      * @return Matching Bitmap.Config  for the enum value passed.
    105      */
    106     private static Bitmap.Config getBitmapConfigForFormat(int bitmapFormatValue) {
    107         switch (bitmapFormatValue) {
    108             case BitmapFormat.ALPHA_8:
    109                 return Bitmap.Config.ALPHA_8;
    110             case BitmapFormat.ARGB_4444:
    111                 return Bitmap.Config.ARGB_4444;
    112             case BitmapFormat.RGB_565:
    113                 return Bitmap.Config.RGB_565;
    114             case BitmapFormat.ARGB_8888:
    115             default:
    116                 return Bitmap.Config.ARGB_8888;
    117         }
    118     }
    119 
    120 }
    121