Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 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 | |
| 17 | package android.theme.cts; |
| 18 | |
| 19 | import com.android.ddmlib.Log; |
| 20 | import com.android.ddmlib.Log.LogLevel; |
| 21 | import com.android.tradefed.device.ITestDevice; |
| 22 | |
| 23 | import java.awt.Color; |
| 24 | import java.awt.image.BufferedImage; |
| 25 | import java.io.File; |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 26 | import java.io.IOException; |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 27 | import java.lang.String; |
| 28 | import java.util.concurrent.Callable; |
| 29 | |
| 30 | import javax.imageio.ImageIO; |
| 31 | |
| 32 | /** |
| 33 | * Compares the images generated by the device with the reference images. |
| 34 | */ |
| 35 | public class ComparisonTask implements Callable<Boolean> { |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 36 | private static final String TAG = "ComparisonTask"; |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 37 | |
| 38 | private static final int IMAGE_THRESHOLD = 2; |
| 39 | |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 40 | private final ITestDevice mDevice; |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 41 | private final File mExpected; |
| 42 | private final File mActual; |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 43 | |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 44 | public ComparisonTask(ITestDevice device, File expected, File actual) { |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 45 | mDevice = device; |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 46 | mExpected = expected; |
| 47 | mActual = actual; |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public Boolean call() { |
| 51 | boolean success = false; |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 52 | |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 53 | try { |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 54 | final BufferedImage expected = ImageIO.read(mExpected); |
| 55 | final BufferedImage actual = ImageIO.read(mActual); |
| 56 | if (compare(expected, actual, IMAGE_THRESHOLD)) { |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 57 | success = true; |
| 58 | } else { |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 59 | final File diff = File.createTempFile("diff_" + mExpected.getName(), ".png"); |
| 60 | createDiff(expected, actual, diff); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 61 | Log.logAndDisplay(LogLevel.INFO, TAG, "Diff created: " + diff.getPath()); |
| 62 | } |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 63 | } catch (IOException e) { |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 64 | Log.logAndDisplay(LogLevel.ERROR, TAG, e.toString()); |
| 65 | e.printStackTrace(); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 66 | } |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 67 | |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 68 | return success; |
| 69 | } |
| 70 | |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 71 | /** |
| 72 | * Verifies that the pixels of reference and generated images are similar |
| 73 | * within a specified threshold. |
| 74 | * |
| 75 | * @param expected expected image |
| 76 | * @param actual actual image |
| 77 | * @param threshold maximum difference per channel |
| 78 | * @return {@code true} if the images are similar, false otherwise |
| 79 | */ |
Masanori OGINO | 3c4b6e6 | 2015-09-18 09:12:54 +0900 | [diff] [blame] | 80 | private static int getAlphaScaledBlue(final int color) { |
| 81 | return (color & 0x000000FF) * getAlpha(color) / 255; |
| 82 | } |
| 83 | |
| 84 | private static int getAlphaScaledGreen(final int color) { |
| 85 | return ((color & 0x0000FF00) >> 8) * getAlpha(color) / 255; |
| 86 | } |
| 87 | |
| 88 | private static int getAlphaScaledRed(final int color) { |
| 89 | return ((color & 0x00FF0000) >> 16) * getAlpha(color) / 255; |
| 90 | } |
| 91 | |
| 92 | private static int getAlpha(final int color) { |
| 93 | // use logical shift for keeping an unsigned value |
| 94 | return (color & 0xFF000000) >>> 24; |
| 95 | } |
| 96 | |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 97 | private static boolean compare(BufferedImage reference, BufferedImage generated, int threshold) { |
| 98 | final int w = generated.getWidth(); |
| 99 | final int h = generated.getHeight(); |
| 100 | if (w != reference.getWidth() || h != reference.getHeight()) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | for (int i = 0; i < w; i++) { |
| 105 | for (int j = 0; j < h; j++) { |
| 106 | final int p1 = reference.getRGB(i, j); |
| 107 | final int p2 = generated.getRGB(i, j); |
Masanori OGINO | 3c4b6e6 | 2015-09-18 09:12:54 +0900 | [diff] [blame] | 108 | |
| 109 | final int dr = getAlphaScaledRed(p1) - getAlphaScaledRed(p2); |
| 110 | final int dg = getAlphaScaledGreen(p1) - getAlphaScaledGreen(p2); |
| 111 | final int db = getAlphaScaledBlue(p1) - getAlphaScaledBlue(p2); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 112 | |
| 113 | if (Math.abs(db) > threshold || |
| 114 | Math.abs(dg) > threshold || |
Masanori OGINO | 3c4b6e6 | 2015-09-18 09:12:54 +0900 | [diff] [blame] | 115 | Math.abs(dr) > threshold) { |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 123 | private static void createDiff(BufferedImage expected, BufferedImage actual, File out) |
| 124 | throws IOException { |
| 125 | final int w1 = expected.getWidth(); |
| 126 | final int h1 = expected.getHeight(); |
| 127 | final int w2 = actual.getWidth(); |
| 128 | final int h2 = actual.getHeight(); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 129 | final int width = Math.max(w1, w2); |
| 130 | final int height = Math.max(h1, h2); |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 131 | |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 132 | // The diff will contain image1, image2 and the difference between the two. |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 133 | final BufferedImage diff = new BufferedImage( |
| 134 | width * 3, height, BufferedImage.TYPE_INT_ARGB); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 135 | |
| 136 | for (int i = 0; i < width; i++) { |
| 137 | for (int j = 0; j < height; j++) { |
| 138 | final boolean inBounds1 = i < w1 && j < h1; |
| 139 | final boolean inBounds2 = i < w2 && j < h2; |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 140 | int colorExpected = Color.WHITE.getRGB(); |
| 141 | int colorActual = Color.WHITE.getRGB(); |
| 142 | int colorDiff; |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 143 | if (inBounds1 && inBounds2) { |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 144 | colorExpected = expected.getRGB(i, j); |
| 145 | colorActual = actual.getRGB(i, j); |
| 146 | colorDiff = colorExpected == colorActual ? colorExpected : Color.RED.getRGB(); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 147 | } else if (inBounds1 && !inBounds2) { |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 148 | colorExpected = expected.getRGB(i, j); |
| 149 | colorDiff = Color.BLUE.getRGB(); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 150 | } else if (!inBounds1 && inBounds2) { |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 151 | colorActual = actual.getRGB(i, j); |
| 152 | colorDiff = Color.GREEN.getRGB(); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 153 | } else { |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 154 | colorDiff = Color.MAGENTA.getRGB(); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 155 | } |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 156 | |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 157 | int x = i; |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 158 | diff.setRGB(x, j, colorExpected); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 159 | x += width; |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 160 | diff.setRGB(x, j, colorActual); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 161 | x += width; |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 162 | diff.setRGB(x, j, colorDiff); |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
Alan Viverette | c15ea9f | 2015-08-26 16:33:17 -0400 | [diff] [blame] | 165 | |
Chenjie Luo | 42aaa5c | 2014-08-20 18:33:47 -0700 | [diff] [blame] | 166 | ImageIO.write(diff, "png", out); |
| 167 | } |
| 168 | |
| 169 | } |