ImageUtils.java revision 5c2d84675b239bc04ae98c75526e5b81897ee183
1/*
2 * Copyright (C) 2014 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.internal.util;
18
19import android.graphics.Bitmap;
20
21/**
22 * Utility class for image analysis and processing.
23 *
24 * @hide
25 */
26public class ImageUtils {
27
28    // Amount (max is 255) that two channels can differ before the color is no longer "gray".
29    private static final int TOLERANCE = 20;
30
31    // Alpha amount for which values below are considered transparent.
32    private static final int ALPHA_TOLERANCE = 50;
33
34    private int[] mTempBuffer;
35
36    /**
37     * Checks whether a bitmap is grayscale. Grayscale here means "very close to a perfect
38     * gray".
39     */
40    public boolean isGrayscale(Bitmap bitmap) {
41        final int height = bitmap.getHeight();
42        final int width = bitmap.getWidth();
43        int size = height*width;
44
45        ensureBufferSize(size);
46        bitmap.getPixels(mTempBuffer, 0, width, 0, 0, width, height);
47        for (int i = 0; i < size; i++) {
48            if (!isGrayscale(mTempBuffer[i])) {
49                return false;
50            }
51        }
52        return true;
53    }
54
55    /**
56     * Makes sure that {@code mTempBuffer} has at least length {@code size}.
57     */
58    private void ensureBufferSize(int size) {
59        if (mTempBuffer == null || mTempBuffer.length < size) {
60            mTempBuffer = new int[size];
61        }
62    }
63
64    /**
65     * Classifies a color as grayscale or not. Grayscale here means "very close to a perfect
66     * gray"; if all three channels are approximately equal, this will return true.
67     *
68     * Note that really transparent colors are always grayscale.
69     */
70    public static boolean isGrayscale(int color) {
71        int alpha = 0xFF & (color >> 24);
72        if (alpha < ALPHA_TOLERANCE) {
73            return true;
74        }
75
76        int r = 0xFF & (color >> 16);
77        int g = 0xFF & (color >> 8);
78        int b = 0xFF & color;
79
80        return Math.abs(r - g) < TOLERANCE
81                && Math.abs(r - b) < TOLERANCE
82                && Math.abs(g - b) < TOLERANCE;
83    }
84}
85