blob: 88753b151a57a36b03c1a824f1b28415b1e174fa [file] [log] [blame]
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.compatibility.common.util;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Color;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Method;
import java.util.Random;
public class BitmapUtils {
private static final String TAG = "BitmapUtils";
private BitmapUtils() {}
private static Boolean compareBasicBitmapsInfo(Bitmap bmp1, Bitmap bmp2) {
if (bmp1 == bmp2) {
return Boolean.TRUE;
}
if (bmp1 == null) {
Log.d(TAG, "compareBitmaps() failed because bmp1 is null");
return Boolean.FALSE;
}
if (bmp2 == null) {
Log.d(TAG, "compareBitmaps() failed because bmp2 is null");
return Boolean.FALSE;
}
if ((bmp1.getWidth() != bmp2.getWidth()) || (bmp1.getHeight() != bmp2.getHeight())) {
Log.d(TAG, "compareBitmaps() failed because sizes don't match "
+ "bmp1=(" + bmp1.getWidth() + "x" + bmp1.getHeight() + "), "
+ "bmp2=(" + bmp2.getWidth() + "x" + bmp2.getHeight() + ")");
return Boolean.FALSE;
}
return null;
}
/**
* Compares two bitmaps by pixels.
*/
public static boolean compareBitmaps(Bitmap bmp1, Bitmap bmp2) {
final Boolean basicComparison = compareBasicBitmapsInfo(bmp1, bmp2);
if (basicComparison != null) return basicComparison.booleanValue();
for (int i = 0; i < bmp1.getWidth(); i++) {
for (int j = 0; j < bmp1.getHeight(); j++) {
if (bmp1.getPixel(i, j) != bmp2.getPixel(i, j)) {
Log.d(TAG, "compareBitmaps(): pixels (" + i + ", " + j + ") don't match");
return false;
}
}
}
return true;
}
/**
* Compares two bitmaps by pixels, with a buffer for mistmatches.
*
* <p>For example, if {@code minimumPrecision} is 0.99, at least 99% of the pixels should
* match.
*/
public static boolean compareBitmaps(Bitmap bmp1, Bitmap bmp2, double minimumPrecision) {
final Boolean basicComparison = compareBasicBitmapsInfo(bmp1, bmp2);
if (basicComparison != null) return basicComparison.booleanValue();
final int width = bmp1.getWidth();
final int height = bmp1.getHeight();
final long numberPixels = width * height;
long numberMismatches = 0;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (bmp1.getPixel(i, j) != bmp2.getPixel(i, j)) {
numberMismatches++;
if (numberMismatches <= 10) {
// Let's not spam logcat...
Log.w(TAG, "compareBitmaps(): pixels (" + i + ", " + j + ") don't match");
}
}
}
}
final double actualPrecision = ((double) numberPixels - numberMismatches) / (numberPixels);
Log.v(TAG, "compareBitmaps(): numberPixels=" + numberPixels
+ ", numberMismatches=" + numberMismatches
+ ", minimumPrecision=" + minimumPrecision
+ ", actualPrecision=" + actualPrecision);
return actualPrecision >= minimumPrecision;
}
public static Bitmap generateRandomBitmap(int width, int height) {
final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Random generator = new Random();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bmp.setPixel(x, y, generator.nextInt(Integer.MAX_VALUE));
}
}
return bmp;
}
public static Bitmap generateWhiteBitmap(int width, int height) {
final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bmp.eraseColor(Color.WHITE);
return bmp;
}
public static Bitmap getWallpaperBitmap(Context context) throws Exception {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
Class<?> noparams[] = {};
Class<?> wmClass = wallpaperManager.getClass();
Method methodGetBitmap = wmClass.getDeclaredMethod("getBitmap", noparams);
return (Bitmap) methodGetBitmap.invoke(wallpaperManager, null);
}
public static ByteArrayInputStream bitmapToInputStream(Bitmap bmp) {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapData = bos.toByteArray();
return new ByteArrayInputStream(bitmapData);
}
private static void logIfBitmapSolidColor(String fileName, Bitmap bitmap) {
int firstColor = bitmap.getPixel(0, 0);
for (int x = 0; x < bitmap.getWidth(); x++) {
for (int y = 0; y < bitmap.getHeight(); y++) {
if (bitmap.getPixel(x, y) != firstColor) {
return;
}
}
}
Log.w(TAG, String.format("%s entire bitmap color is %x", fileName, firstColor));
}
public static void saveBitmap(Bitmap bitmap, String directoryName, String fileName) {
new File(directoryName).mkdirs(); // create dirs if needed
Log.d(TAG, "Saving file: " + fileName + " in directory: " + directoryName);
if (bitmap == null) {
Log.d(TAG, "File not saved, bitmap was null");
return;
}
logIfBitmapSolidColor(fileName, bitmap);
File file = new File(directoryName, fileName);
try (FileOutputStream fileStream = new FileOutputStream(file)) {
bitmap.compress(Bitmap.CompressFormat.PNG, 0 /* ignored for PNG */, fileStream);
fileStream.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}