Home | History | Annotate | Download | only in phone
      1 /*
      2  * Copyright (C) 2011 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.phone;
     18 
     19 import android.graphics.Bitmap;
     20 import android.os.SystemClock;
     21 import android.os.SystemProperties;
     22 import android.util.Log;
     23 
     24 
     25 /**
     26  * Image effects used by the in-call UI.
     27  */
     28 public class BitmapUtils {
     29     private static final String TAG = "BitmapUtils";
     30     private static final boolean DBG =
     31             (PhoneApp.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
     32 
     33     /** This class is never instantiated. */
     34     private BitmapUtils() {
     35     }
     36 
     37     //
     38     // Gaussian blur effect
     39     //
     40     // gaussianBlur() and related methods are borrowed from
     41     // BackgroundUtils.java in the Music2 code (which itself was based on
     42     // code from the old Cooliris android Gallery app.)
     43     //
     44     // TODO: possibly consider caching previously-generated blurred bitmaps;
     45     // see getAdaptedBitmap() and mAdaptedBitmapCache in the music app code.
     46     //
     47 
     48     private static final int RED_MASK = 0xff0000;
     49     private static final int RED_MASK_SHIFT = 16;
     50     private static final int GREEN_MASK = 0x00ff00;
     51     private static final int GREEN_MASK_SHIFT = 8;
     52     private static final int BLUE_MASK = 0x0000ff;
     53 
     54     /**
     55      * Creates a blurred version of the given Bitmap.
     56      *
     57      * @param bitmap the input bitmap, presumably a 96x96 pixel contact
     58      *               thumbnail.
     59      */
     60     public static Bitmap createBlurredBitmap(Bitmap bitmap) {
     61         if (DBG) log("createBlurredBitmap()...");
     62         long startTime = SystemClock.uptimeMillis();
     63         if (bitmap == null) {
     64             Log.w(TAG, "createBlurredBitmap: null bitmap");
     65             return null;
     66         }
     67 
     68         if (DBG) log("- input bitmap: " + bitmap.getWidth() + " x " + bitmap.getHeight());
     69 
     70         // The bitmap we pass to gaussianBlur() needs to have a width
     71         // that's a power of 2, so scale up to 128x128.
     72         final int scaledSize = 128;
     73         bitmap = Bitmap.createScaledBitmap(bitmap,
     74                                            scaledSize, scaledSize,
     75                                            true /* filter */);
     76         if (DBG) log("- after resize: " + bitmap.getWidth() + " x " + bitmap.getHeight());
     77 
     78         bitmap = gaussianBlur(bitmap);
     79         if (DBG) log("- after blur: " + bitmap.getWidth() + " x " + bitmap.getHeight());
     80 
     81         long endTime = SystemClock.uptimeMillis();
     82         if (DBG) log("createBlurredBitmap() done (elapsed = " + (endTime - startTime) + " msec)");
     83         return bitmap;
     84     }
     85 
     86     /**
     87      * Apply a gaussian blur filter, and return a new (blurred) bitmap
     88      * that's the same size as the input bitmap.
     89      *
     90      * @param source input bitmap, whose width must be a power of 2
     91      */
     92     public static Bitmap gaussianBlur(Bitmap source) {
     93         int width = source.getWidth();
     94         int height = source.getHeight();
     95         if (DBG) log("gaussianBlur(): input: " + width + " x " + height);
     96 
     97         // Create a source and destination buffer for the image.
     98         int numPixels = width * height;
     99         int[] in = new int[numPixels];
    100         int[] tmp = new int[numPixels];
    101 
    102         // Get the source pixels as 32-bit ARGB.
    103         source.getPixels(in, 0, width, 0, 0, width, height);
    104 
    105         // Gaussian is a separable kernel, so it is decomposed into a horizontal
    106         // and vertical pass.
    107         // The filter function applies the kernel across each row and transposes
    108         // the output.
    109         // Hence we apply it twice to provide efficient horizontal and vertical
    110         // convolution.
    111         // The filter discards the alpha channel.
    112         gaussianBlurFilter(in, tmp, width, height);
    113         gaussianBlurFilter(tmp, in, width, height);
    114 
    115         // Return a bitmap scaled to the desired size.
    116         Bitmap filtered = Bitmap.createBitmap(in, width, height, Bitmap.Config.ARGB_8888);
    117         source.recycle();
    118         return filtered;
    119     }
    120 
    121     private static void gaussianBlurFilter(int[] in, int[] out, int width, int height) {
    122         // This function is currently hardcoded to blur with RADIUS = 4.
    123         // (If you change RADIUS, you'll have to change the weights[] too.)
    124         final int RADIUS = 4;
    125         final int[] weights = { 13, 23, 32, 39, 42, 39, 32, 23, 13}; // Adds up to 256
    126         int inPos = 0;
    127         int widthMask = width - 1; // width must be a power of two.
    128         for (int y = 0; y < height; ++y) {
    129             // Compute the alpha value.
    130             int alpha = 0xff;
    131             // Compute output values for the row.
    132             int outPos = y;
    133             for (int x = 0; x < width; ++x) {
    134                 int red = 0;
    135                 int green = 0;
    136                 int blue = 0;
    137                 for (int i = -RADIUS; i <= RADIUS; ++i) {
    138                     int argb = in[inPos + (widthMask & (x + i))];
    139                     int weight = weights[i+RADIUS];
    140                     red += weight *((argb & RED_MASK) >> RED_MASK_SHIFT);
    141                     green += weight *((argb & GREEN_MASK) >> GREEN_MASK_SHIFT);
    142                     blue += weight *(argb & BLUE_MASK);
    143                 }
    144                 // Output the current pixel.
    145                 out[outPos] = (alpha << 24) | ((red >> 8) << RED_MASK_SHIFT)
    146                     | ((green >> 8) << GREEN_MASK_SHIFT)
    147                         | (blue >> 8);
    148                 outPos += height;
    149             }
    150             inPos += width;
    151         }
    152     }
    153 
    154     //
    155     // Debugging
    156     //
    157 
    158     private static void log(String msg) {
    159         Log.d(TAG, msg);
    160     }
    161 }
    162