1 /*
2  * Copyright (C) 2017 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.asset;
17 
18 import android.graphics.Bitmap;
19 import android.graphics.Point;
20 import android.graphics.Rect;
21 
22 /**
23  * Collection of static utility methods for decoding and processing Bitmaps.
24  */
25 public class BitmapUtils {
26     private static final float DEFAULT_CENTER_ALIGNMENT = 0.5f;
27 
28     // Suppress default constructor for noninstantiability.
BitmapUtils()29     private BitmapUtils() {
30         throw new AssertionError();
31     }
32 
33     /**
34      * Calculates the highest subsampling factor to scale the source image to the target view without
35      * losing visible quality. Final result is based on powers of 2 because it should be set as
36      * BitmapOptions#inSampleSize.
37      *
38      * @param srcWidth     Width of source image.
39      * @param srcHeight    Height of source image.
40      * @param targetWidth  Width of target view.
41      * @param targetHeight Height of target view.
42      * @return Highest subsampling factor as a power of 2.
43      */
calculateInSampleSize( int srcWidth, int srcHeight, int targetWidth, int targetHeight)44     public static int calculateInSampleSize(
45             int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
46         int shift = 0;
47         int halfHeight = srcHeight / 2;
48         int halfWidth = srcWidth / 2;
49 
50         // Calculate the largest inSampleSize value that is a power of 2 and keeps both the result
51         // bitmap's height and width at least as large as the target height and width.
52         while (((halfHeight >> shift) >= targetHeight) && ((halfWidth >> shift) >= targetWidth)) {
53             shift++;
54         }
55 
56         return 1 << shift;
57     }
58 
59     /**
60      * Generates a hash code for the given bitmap. Computation starts with a nonzero prime number,
61      * then for the integer values of height, width, and a selection of pixel colors, multiplies the
62      * result by 31 and adds said integer value. Multiply by 31 because it is prime and conveniently 1
63      * less than 32 which is 2 ^ 5, allowing the VM to replace multiplication by a bit shift and
64      * subtraction for performance.
65      * <p>
66      * This method should be called off the UI thread.
67      */
generateHashCode(Bitmap bitmap)68     public static long generateHashCode(Bitmap bitmap) {
69         long result = 17;
70 
71         int width = bitmap.getWidth();
72         int height = bitmap.getHeight();
73 
74         result = 31 * result + width;
75         result = 31 * result + height;
76 
77         // Traverse pixels exponentially so that hash code generation scales well with large images.
78         for (int x = 0; x < width; x = x * 2 + 1) {
79             for (int y = 0; y < height; y = y * 2 + 1) {
80                 result = 31 * result + bitmap.getPixel(x, y);
81             }
82         }
83 
84         return result;
85     }
86 
87     /**
88      * Calculates horizontal alignment of the rect within the supplied dimensions.
89      *
90      * @return A float value between 0 and 1 specifying horizontal alignment; 0 for left-aligned, 0.5
91      * for horizontal center-aligned, and 1 for right-aligned.
92      */
calculateHorizontalAlignment(Point dimensions, Rect rect)93     public static float calculateHorizontalAlignment(Point dimensions, Rect rect) {
94         int paddingLeft = rect.left;
95         int paddingRight = dimensions.x - rect.right;
96         int totalHorizontalPadding = paddingLeft + paddingRight;
97         // Zero horizontal padding means that there is no room to crop horizontally so we just fall
98         // back to a default center-alignment value.
99         return (totalHorizontalPadding == 0)
100                 ? DEFAULT_CENTER_ALIGNMENT
101                 : paddingLeft / ((float) paddingLeft + paddingRight);
102     }
103 
104     /**
105      * Calculates vertical alignment of the rect within the supplied dimensions.
106      *
107      * @return A float value between 0 and 1 specifying vertical alignment; 0 for top-aligned, 0.5 for
108      * vertical center-aligned, and 1 for bottom-aligned.
109      */
calculateVerticalAlignment(Point dimensions, Rect rect)110     public static float calculateVerticalAlignment(Point dimensions, Rect rect) {
111         int paddingTop = rect.top;
112         int paddingBottom = dimensions.y - rect.bottom;
113         int totalVerticalPadding = paddingTop + paddingBottom;
114         // Zero vertical padding means that there is no room to crop vertically so we just fall back to
115         // a default center-alignment value.
116         return (totalVerticalPadding == 0)
117                 ? DEFAULT_CENTER_ALIGNMENT
118                 : paddingTop / ((float) paddingTop + paddingBottom);
119     }
120 }
121