1/*
2 * Copyright (C) 2012 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
17package com.android.contacts.common.util;
18
19import android.graphics.Bitmap;
20import android.graphics.BitmapFactory;
21import android.graphics.Canvas;
22import android.graphics.drawable.Drawable;
23import android.graphics.drawable.BitmapDrawable;
24
25/**
26 * Provides static functions to decode bitmaps at the optimal size
27 */
28public class BitmapUtil {
29    private BitmapUtil() {}
30
31    /**
32     * Returns Width or Height of the picture, depending on which size is smaller. Doesn't actually
33     * decode the picture, so it is pretty efficient to run.
34     */
35    public static int getSmallerExtentFromBytes(byte[] bytes) {
36        final BitmapFactory.Options options = new BitmapFactory.Options();
37
38        // don't actually decode the picture, just return its bounds
39        options.inJustDecodeBounds = true;
40        BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
41
42        // test what the best sample size is
43        return Math.min(options.outWidth, options.outHeight);
44    }
45
46    /**
47     * Finds the optimal sampleSize for loading the picture
48     * @param originalSmallerExtent Width or height of the picture, whichever is smaller
49     * @param targetExtent Width or height of the target view, whichever is bigger.
50     *
51     * If either one of the parameters is 0 or smaller, no sampling is applied
52     */
53    public static int findOptimalSampleSize(int originalSmallerExtent, int targetExtent) {
54        // If we don't know sizes, we can't do sampling.
55        if (targetExtent < 1) return 1;
56        if (originalSmallerExtent < 1) return 1;
57
58        // Test what the best sample size is. To do that, we find the sample size that gives us
59        // the best trade-off between resulting image size and memory requirement. We allow
60        // the down-sampled image to be 20% smaller than the target size. That way we can get around
61        // unfortunate cases where e.g. a 720 picture is requested for 362 and not down-sampled at
62        // all. Why 20%? Why not. Prove me wrong.
63        int extent = originalSmallerExtent;
64        int sampleSize = 1;
65        while ((extent >> 1) >= targetExtent * 0.8f) {
66            sampleSize <<= 1;
67            extent >>= 1;
68        }
69
70        return sampleSize;
71    }
72
73    /**
74     * Decodes the bitmap with the given sample size
75     */
76    public static Bitmap decodeBitmapFromBytes(byte[] bytes, int sampleSize) {
77        final BitmapFactory.Options options;
78        if (sampleSize <= 1) {
79            options = null;
80        } else {
81            options = new BitmapFactory.Options();
82            options.inSampleSize = sampleSize;
83        }
84        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
85    }
86
87    /**
88     * Retrieves a copy of the specified drawable resource, rotated by a specified angle.
89     *
90     * @param resources The current resources.
91     * @param resourceId The resource ID of the drawable to rotate.
92     * @param angle The angle of rotation.
93     * @return Rotated drawable.
94     */
95    public static Drawable getRotatedDrawable(
96            android.content.res.Resources resources, int resourceId, float angle) {
97
98        // Get the original drawable and make a copy which will be rotated.
99        Bitmap original = BitmapFactory.decodeResource(resources, resourceId);
100        Bitmap rotated = Bitmap.createBitmap(
101                original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);
102
103        // Perform the rotation.
104        Canvas tempCanvas = new Canvas(rotated);
105        tempCanvas.rotate(angle, original.getWidth()/2, original.getHeight()/2);
106        tempCanvas.drawBitmap(original, 0, 0, null);
107
108        return new BitmapDrawable(resources,rotated);
109    }
110}
111