1 /*
2  * Copyright (C) 2016 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.car.apps.common;
17 
18 import static android.graphics.Bitmap.Config.ARGB_8888;
19 
20 import android.annotation.ColorInt;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.content.Context;
24 import android.content.res.Resources;
25 import android.graphics.Bitmap;
26 import android.graphics.Canvas;
27 import android.graphics.Matrix;
28 import android.graphics.Paint;
29 import android.graphics.RectF;
30 import android.graphics.drawable.BitmapDrawable;
31 import android.graphics.drawable.Drawable;
32 import android.util.Log;
33 import android.util.Size;
34 
35 public class BitmapUtils {
36     private static final String TAG = "BitmapUtils";
37 
38     /**
39      * Scales a bitmap while preserving the proportions such that both dimensions are the smallest
40      * values possible that are equal to or larger than the given dimensions.
41      *
42      * This function can be a few times as expensive as Bitmap.createScaledBitmap with
43      * filtering when downscaling, but it produces much nicer results.
44      *
45      * @param bm The bitmap to scale.
46      * @param width The desired width.
47      * @param height The desired height.
48      * @return The scaled bitmap, or the original bitmap if scaling was not necessary.
49      */
scaleBitmap(Bitmap bm, int width, int height)50     public static Bitmap scaleBitmap(Bitmap bm, int width, int height) {
51         if (bm == null || (bm.getHeight() == height && bm.getWidth() == width)) {
52             return bm;
53         }
54 
55         float heightScale = 1f;
56         if (bm.getHeight() > height) {
57             heightScale = (float) height / bm.getHeight();
58         }
59         float widthScale = 1f;
60         if (bm.getWidth() > width) {
61             widthScale = (float) width / bm.getWidth();
62         }
63         float scale = heightScale > widthScale ? heightScale : widthScale;
64         int scaleWidth = (int) Math.ceil(bm.getWidth() * scale);
65         int scaleHeight = (int) Math.ceil(bm.getHeight() * scale);
66 
67         Bitmap scaledBm = bm;
68         // If you try to scale an image down too much in one go, you can end up with discontinuous
69         // interpolation. Therefore, if necessary, we scale the image to twice the desired size
70         // and do a second scaling to the desired size, which smooths jaggedness from the first go.
71         if (scale < .5f) {
72             scaledBm = Bitmap.createScaledBitmap(scaledBm, scaleWidth * 2, scaleHeight * 2, true);
73         }
74 
75         if (scale != 1f) {
76             Bitmap newScaledBitmap = Bitmap
77                     .createScaledBitmap(scaledBm, scaleWidth, scaleHeight, true);
78             if (scaledBm != bm) {
79                 scaledBm.recycle();
80             }
81             scaledBm = newScaledBitmap;
82         }
83         return scaledBm;
84     }
85 
86     /**
87      * Crops the given bitmap to a centered rectangle of the given dimensions.
88      *
89      * @param bm the bitmap to crop.
90      * @param width the width to crop to.
91      * @param height the height to crop to.
92      * @return The cropped bitmap, or the original if no cropping was necessary.
93      */
cropBitmap(Bitmap bm, int width, int height)94     public static Bitmap cropBitmap(Bitmap bm, int width, int height) {
95         if (bm == null) {
96             return bm;
97         }
98         if (bm.getHeight() < height || bm.getWidth() < width) {
99             if (Log.isLoggable(TAG, Log.INFO)) {
100                 Log.i(TAG, String.format(
101                         "Can't crop bitmap to larger dimensions (%d, %d) -> (%d, %d).",
102                         bm.getWidth(), bm.getHeight(), width, height));
103             }
104             return bm;
105         }
106         int x = (bm.getWidth() - width) / 2;
107         int y =(bm.getHeight() - height) / 2;
108         return Bitmap.createBitmap(bm, x, y, width, height);
109     }
110 
111     /** Creates a copy of the given bitmap and applies the given color over the result */
112     @Nullable
createTintedBitmap(@ullable Bitmap image, @ColorInt int colorOverlay)113     public static Bitmap createTintedBitmap(@Nullable Bitmap image, @ColorInt int colorOverlay) {
114         if (image == null) return null;
115         Bitmap clone = Bitmap.createBitmap(image.getWidth(), image.getHeight(), ARGB_8888);
116         Canvas canvas = new Canvas(clone);
117         canvas.drawBitmap(image, 0f, 0f, new Paint());
118         canvas.drawColor(colorOverlay);
119         return clone;
120     }
121 
122     /** Returns a tinted drawable if flagging is enabled and the given drawable is a bitmap. */
123     @NonNull
maybeFlagDrawable(@onNull Context context, @NonNull Drawable drawable)124     public static Drawable maybeFlagDrawable(@NonNull Context context, @NonNull Drawable drawable) {
125         if (drawable instanceof BitmapDrawable) {
126             CommonFlags flags = CommonFlags.getInstance(context);
127             Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
128             if (flags.shouldFlagImproperImageRefs() && bitmap != null) {
129                 Resources res = context.getResources();
130                 int tint = context.getColor(R.color.improper_image_refs_tint_color);
131                 drawable = new BitmapDrawable(res, BitmapUtils.createTintedBitmap(bitmap, tint));
132             }
133         }
134         return drawable;
135     }
136 
137     /** Renders the drawable into a bitmap if needed. */
fromDrawable(Drawable drawable, @Nullable Size bitmapSize)138     public static Bitmap fromDrawable(Drawable drawable, @Nullable Size bitmapSize) {
139         if (drawable instanceof BitmapDrawable) {
140             BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
141             if (bitmapDrawable.getBitmap() != null) {
142                 return bitmapDrawable.getBitmap();
143             }
144         }
145 
146         Matrix matrix = new Matrix();
147         if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
148             bitmapSize = new Size(1, 1);
149             drawable.setBounds(0, 0, bitmapSize.getWidth(), bitmapSize.getHeight());
150         } else {
151             if (bitmapSize == null) {
152                 bitmapSize = new Size(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
153             }
154             drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
155             RectF srcR = new RectF(0f, 0f, drawable.getIntrinsicWidth(),
156                     drawable.getIntrinsicHeight());
157             RectF dstR = new RectF(0f, 0f, bitmapSize.getWidth(), bitmapSize.getHeight());
158             matrix.setRectToRect(srcR, dstR, Matrix.ScaleToFit.CENTER);
159         }
160 
161         Bitmap bitmap = Bitmap.createBitmap(bitmapSize.getWidth(), bitmapSize.getHeight(),
162                 Bitmap.Config.ARGB_8888);
163 
164         Canvas canvas = new Canvas(bitmap);
165         canvas.setMatrix(matrix);
166         drawable.draw(canvas);
167 
168         return bitmap;
169     }
170 }
171