1/*
2 * Copyright (C) 2013 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.gallery3d.filtershow.tools;
18
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.graphics.Paint;
22import android.graphics.Rect;
23import android.graphics.RectF;
24
25/**
26 * A factory class for producing bitmaps to use as UI icons.
27 */
28public class IconFactory {
29
30    /**
31     * Builds an icon with the dimensions iconWidth:iconHeight. If scale is set
32     * the source image is stretched to fit within the given dimensions;
33     * otherwise, the source image is cropped to the proper aspect ratio.
34     *
35     * @param sourceImage image to create an icon from.
36     * @param iconWidth width of the icon bitmap.
37     * @param iconHeight height of the icon bitmap.
38     * @param scale if true, stretch sourceImage to fit the icon dimensions.
39     * @return an icon bitmap with the dimensions iconWidth:iconHeight.
40     */
41    public static Bitmap createIcon(Bitmap sourceImage, int iconWidth, int iconHeight,
42            boolean scale) {
43        if (sourceImage == null) {
44            throw new IllegalArgumentException("Null argument to buildIcon");
45        }
46
47        int sourceWidth = sourceImage.getWidth();
48        int sourceHeight = sourceImage.getHeight();
49
50        if (sourceWidth == 0 || sourceHeight == 0 || iconWidth == 0 || iconHeight == 0) {
51            throw new IllegalArgumentException("Bitmap with dimension 0 used as input");
52        }
53
54        Bitmap icon = Bitmap.createBitmap(iconWidth, iconHeight,
55                Bitmap.Config.ARGB_8888);
56        drawIcon(icon, sourceImage, scale);
57        return icon;
58    }
59
60    /**
61     * Draws an icon in the destination bitmap. If scale is set the source image
62     * is stretched to fit within the destination dimensions; otherwise, the
63     * source image is cropped to the proper aspect ratio.
64     *
65     * @param dest bitmap into which to draw the icon.
66     * @param sourceImage image to create an icon from.
67     * @param scale if true, stretch sourceImage to fit the destination.
68     */
69    public static void drawIcon(Bitmap dest, Bitmap sourceImage, boolean scale) {
70        if (dest == null || sourceImage == null) {
71            throw new IllegalArgumentException("Null argument to buildIcon");
72        }
73
74        int sourceWidth = sourceImage.getWidth();
75        int sourceHeight = sourceImage.getHeight();
76        int iconWidth = dest.getWidth();
77        int iconHeight = dest.getHeight();
78
79        if (sourceWidth == 0 || sourceHeight == 0 || iconWidth == 0 || iconHeight == 0) {
80            throw new IllegalArgumentException("Bitmap with dimension 0 used as input");
81        }
82
83        Rect destRect = new Rect(0, 0, iconWidth, iconHeight);
84        Canvas canvas = new Canvas(dest);
85
86        Rect srcRect = null;
87        if (scale) {
88            // scale image to fit in icon (stretches if aspect isn't the same)
89            srcRect = new Rect(0, 0, sourceWidth, sourceHeight);
90        } else {
91            // crop image to aspect ratio iconWidth:iconHeight
92            float wScale = sourceWidth / (float) iconWidth;
93            float hScale = sourceHeight / (float) iconHeight;
94            float s = Math.min(hScale, wScale);
95
96            float iw = iconWidth * s;
97            float ih = iconHeight * s;
98
99            float borderW = (sourceWidth - iw) / 2.0f;
100            float borderH = (sourceHeight - ih) / 2.0f;
101            RectF rec = new RectF(borderW, borderH, borderW + iw, borderH + ih);
102            srcRect = new Rect();
103            rec.roundOut(srcRect);
104        }
105
106        canvas.drawBitmap(sourceImage, srcRect, destRect, new Paint(Paint.FILTER_BITMAP_FLAG));
107    }
108}
109