1 /* 2 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.jemmy.image; 24 25 26 import java.awt.Color; 27 import java.awt.image.BufferedImage; 28 import java.io.File; 29 import java.io.FileInputStream; 30 import java.io.FileNotFoundException; 31 import java.io.FileOutputStream; 32 import java.io.IOException; 33 import javax.imageio.ImageIO; 34 35 36 /** 37 * Contains util methods to work with images. 38 * 39 * @author Alexandre Iline (alexandre.iline@sun.com) 40 */ 41 class ImageTool { 42 43 /** 44 * Increases image. 45 * @param image an image to enlarge. 46 * @param zoom A scale. 47 * @return a result image. 48 */ 49 public static BufferedImage enlargeImage(BufferedImage image, int zoom) { 50 int wight = image.getWidth(); 51 int height = image.getHeight(); 52 BufferedImage result = new BufferedImage(wight * zoom, 53 height * zoom, 54 image.getType()); 55 int rgb; 56 for (int x = 0; x < wight; x++) { 57 for (int y = 0; y < height; y++) { 58 rgb = image.getRGB(x, y); 59 for (int i = 0; i < zoom; i++) { 60 for (int j = 0; j < zoom; j++) { 61 result.setRGB(x * zoom + i, 62 y * zoom + j, 63 rgb); 64 } 65 } 66 } 67 } 68 return (result); 69 } 70 71 /** 72 * Subtracts second image from first one. 73 * Could be used to save file difference for future analysis. 74 * @param minuend an image to subtract from. 75 * @param deduction an image to subtract. 76 * @return a result image. 77 */ 78 public static BufferedImage subtractImage(BufferedImage minuend, BufferedImage deduction) { 79 return (subtractImage(minuend, deduction, 0, 0, null)); 80 } 81 82 /** 83 * Subtracts second image from first one. 84 * Could be used to save file difference for future analysis. 85 * @param minuend an image to subtract from. 86 * @param deduction an image to subtract. 87 * @param highlight - a color to highlight the difference. If null, 88 * color difference is shown. 89 * @return a result image. 90 */ 91 public static BufferedImage subtractImage(BufferedImage minuend, BufferedImage deduction, Color highlight) { 92 return (subtractImage(minuend, deduction, 0, 0, highlight)); 93 } 94 95 /** 96 * Subtracts subimage from image. 97 * Could be used to save file difference for future analysis. 98 * @param minuend an image to subtract from. 99 * @param deduction an image to subtract. 100 * @param relativeX - deduction-in-minuend X coordinate 101 * @param relativeY - deduction-in-minuend Y coordinate 102 * @return a result image. 103 */ 104 public static BufferedImage subtractImage(BufferedImage minuend, BufferedImage deduction, int relativeX, int relativeY) { 105 return subtractImage(minuend, deduction, relativeX, relativeY, null); 106 } 107 108 /** 109 * Subtracts subimage from image. 110 * Could be used to save file difference for future analysis. 111 * @param minuend an image to subtract from. 112 * @param deduction an image to subtract. 113 * @param relativeX - deduction-in-minuend X coordinate 114 * @param relativeY - deduction-in-minuend Y coordinate 115 * @param highlight - a color to highlight the difference. If null, 116 * color difference is shown. 117 * @return a result image. 118 */ 119 public static BufferedImage subtractImage(BufferedImage minuend, BufferedImage deduction, int relativeX, int relativeY, Color highlight) { 120 int mWidth = minuend.getWidth(); 121 int mHeight = minuend.getHeight(); 122 int dWidth = deduction.getWidth(); 123 int dHeight = deduction.getHeight(); 124 125 int maxWidth = (mWidth > relativeX + dWidth) ? mWidth : (relativeX + dWidth); 126 int maxHeight = (mHeight > relativeY + dHeight) ? mHeight : (relativeY + dHeight); 127 128 BufferedImage result = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_RGB); 129 int mColor, dColor; 130 for (int x = 0; x < maxWidth; x++) { 131 for (int y = 0; y < maxHeight; y++) { 132 if (x >= mWidth || 133 y >= mHeight) { 134 mColor = 0; 135 } else { 136 mColor = minuend.getRGB(x, y); 137 } 138 if (x >= dWidth + relativeX || 139 y >= dHeight + relativeY || 140 x < relativeX || 141 y < relativeY) { 142 dColor = 0; 143 } else { 144 dColor = deduction.getRGB(x - relativeX, y - relativeY); 145 } 146 result.setRGB(x, y, (mColor != dColor) ? subtractColor((highlight != null) ? highlight.getRGB() : 0, mColor, dColor) : 0); 147 } 148 } 149 return (result); 150 } 151 152 public static double distance(int rgb1, int rgb2) { 153 float [] buffer1 = new float[3]; 154 float [] buffer2 = new float[3]; 155 Color c1 = new Color(rgb1); 156 Color c2 = new Color(rgb2); 157 c1.getRGBColorComponents(buffer1); 158 c2.getRGBColorComponents(buffer2); 159 double distSquare = 0; 160 for (int i = 0; i < 3; i++) { 161 distSquare += (buffer1[i] - buffer2[i]) * (buffer1[i] - buffer2[i]); 162 } 163 return Math.sqrt(distSquare); 164 } 165 166 private static int subtractColor(int highlight, int m, int d) { 167 if (highlight == 0) { 168 float scale = (float) (distance(m, d) / Math.sqrt(3)); 169 return new Color(scale, scale, scale).getRGB(); 170 } else { 171 return highlight; 172 } 173 } 174 /** 175 * @param args the command line arguments 176 */ 177 public static void main(String[] args) throws IOException { 178 if (args.length != 2) { 179 usage(); 180 System.exit(1); 181 } 182 compare(new File(args[0]).getAbsoluteFile(), new File(args[1]).getAbsoluteFile()); 183 } 184 185 private static void compare(File golden, File results) throws IOException { 186 if (golden.isDirectory()) { 187 File rfl; 188 for (File fl : golden.listFiles()) { 189 compare(fl, new File(results.getAbsolutePath() + File.separator + fl.getName())); 190 } 191 for (File fl : results.listFiles()) { 192 rfl = new File(golden.getAbsolutePath() + File.separator + fl.getName()); 193 if (!rfl.exists()) { 194 compare(rfl, fl); 195 } 196 } 197 } else { 198 DiffDialog dialog = new DiffDialog(); 199 dialog.setImages(golden.exists() ? ImageIO.read(golden) : null, results.exists() ? ImageIO.read(results) : null); 200 dialog.setVisible(true); 201 switch (dialog.status) { 202 case -2: 203 System.exit(0); 204 case -1: 205 copy(results, golden); 206 break; 207 case 1: 208 golden.delete(); 209 break; 210 } 211 } 212 } 213 214 private static void usage() { 215 System.out.println("java -jar JemmyAWTInput.jar <golden image set> <test result image set>"); 216 } 217 218 private static void copy(File results, File golden) throws FileNotFoundException, IOException { 219 if (golden.exists()) { 220 golden.delete(); 221 } 222 FileInputStream from = new FileInputStream(results); 223 FileOutputStream to = new FileOutputStream(golden); 224 byte[] buffer = new byte[4096]; 225 int bytesRead; 226 227 while ((bytesRead = from.read(buffer)) != -1) { 228 to.write(buffer, 0, bytesRead); // write 229 } 230 } 231 }