1 /* 2 * Copyright (C) 2019 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 package com.android.wallpaper.util; 17 18 import android.content.Context; 19 import android.graphics.Bitmap; 20 import android.graphics.Rect; 21 import android.renderscript.Allocation; 22 import android.renderscript.Element; 23 import android.renderscript.RenderScript; 24 import android.renderscript.ScriptIntrinsicBlur; 25 import android.util.Log; 26 27 /** 28 * Class with different bitmap processors to apply to bitmaps 29 */ 30 public final class BitmapProcessor { 31 32 private static final String TAG = "BitmapProcessor"; 33 private static final float BLUR_RADIUS = 20f; 34 private static final int DOWNSAMPLE = 5; 35 BitmapProcessor()36 private BitmapProcessor() { 37 } 38 39 /** 40 * Function that blurs the content of a bitmap. 41 * 42 * @param context the Application Context 43 * @param bitmap the bitmap we want to blur. 44 * @param outWidth the end width of the blurred bitmap. 45 * @param outHeight the end height of the blurred bitmap. 46 * @return the blurred bitmap. 47 */ blur(Context context, Bitmap bitmap, int outWidth, int outHeight)48 public static Bitmap blur(Context context, Bitmap bitmap, int outWidth, 49 int outHeight) { 50 RenderScript renderScript = RenderScript.create(context); 51 ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript, 52 Element.U8_4(renderScript)); 53 Allocation input = null; 54 Allocation output = null; 55 Bitmap inBitmap = null; 56 57 try { 58 Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 59 WallpaperCropUtils.fitToSize(rect, outWidth / DOWNSAMPLE, outHeight / DOWNSAMPLE); 60 61 inBitmap = Bitmap.createScaledBitmap(bitmap, rect.width(), rect.height(), 62 true /* filter */); 63 64 // Render script blurs only support ARGB_8888, we need a conversion if we got a 65 // different bitmap config. 66 if (inBitmap.getConfig() != Bitmap.Config.ARGB_8888) { 67 Bitmap oldIn = inBitmap; 68 inBitmap = oldIn.copy(Bitmap.Config.ARGB_8888, false /* isMutable */); 69 oldIn.recycle(); 70 } 71 Bitmap outBitmap = Bitmap.createBitmap(inBitmap.getWidth(), inBitmap.getHeight(), 72 Bitmap.Config.ARGB_8888); 73 74 input = Allocation.createFromBitmap(renderScript, inBitmap, 75 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE); 76 output = Allocation.createFromBitmap(renderScript, outBitmap); 77 78 blur.setRadius(BLUR_RADIUS); 79 blur.setInput(input); 80 blur.forEach(output); 81 output.copyTo(outBitmap); 82 83 return outBitmap; 84 85 } catch (IllegalArgumentException ex) { 86 Log.e(TAG, "error while blurring bitmap", ex); 87 } finally { 88 if (input != null) { 89 input.destroy(); 90 } 91 92 if (output != null) { 93 output.destroy(); 94 } 95 96 blur.destroy(); 97 if (inBitmap != null) { 98 inBitmap.recycle(); 99 } 100 } 101 102 return null; 103 } 104 } 105