| 1 |
/* ImageSeeker.java |
| 2 |
Copyright (C) 2012 Red Hat, Inc. |
| 3 |
|
| 4 |
This file is part of IcedTea. |
| 5 |
|
| 6 |
IcedTea is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, version 2. |
| 9 |
|
| 10 |
IcedTea is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with IcedTea; see the file COPYING. If not, write to |
| 17 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 |
02110-1301 USA. |
| 19 |
|
| 20 |
Linking this library statically or dynamically with other modules is |
| 21 |
making a combined work based on this library. Thus, the terms and |
| 22 |
conditions of the GNU General Public License cover the whole |
| 23 |
combination. |
| 24 |
|
| 25 |
As a special exception, the copyright holders of this library give you |
| 26 |
permission to link this library with independent modules to produce an |
| 27 |
executable, regardless of the license terms of these independent |
| 28 |
modules, and to copy and distribute the resulting executable under |
| 29 |
terms of your choice, provided that you also meet, for each linked |
| 30 |
independent module, the terms and conditions of the license of that |
| 31 |
module. An independent module is a module which is not derived from |
| 32 |
or based on this library. If you modify this library, you may extend |
| 33 |
this exception to your version of the library, but you are not |
| 34 |
obligated to do so. If you do not wish to do so, delete this |
| 35 |
exception statement from your version. |
| 36 |
*/ |
| 37 |
|
| 38 |
|
| 39 |
package net.sourceforge.jnlp.awt.imagesearch; |
| 40 |
|
| 41 |
import java.awt.Color; |
| 42 |
import java.awt.Rectangle; |
| 43 |
import java.awt.image.BufferedImage; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class ImageSeeker |
| 48 |
{ |
| 49 |
public static Rectangle findExactImage(BufferedImage marker, BufferedImage screen){ |
| 50 |
return findExactImage(marker, screen, new Rectangle(0,0,screen.getWidth(), screen.getHeight())); |
| 51 |
} |
| 52 |
|
| 53 |
public static Rectangle findExactImage(BufferedImage marker /*usually small*/, BufferedImage screen, Rectangle actionArea) { |
| 54 |
Rectangle result = new Rectangle(0, 0, 0, 0); |
| 55 |
boolean found = false; |
| 56 |
boolean ok = true; |
| 57 |
//to filter out values with alpha |
| 58 |
boolean[][] mask = getMask(marker); |
| 59 |
//accessing those too often, copying |
| 60 |
int[][] markerPixels = getPixels(marker); |
| 61 |
int mw = marker.getWidth(); |
| 62 |
int mh = marker.getHeight(); |
| 63 |
for (int y = actionArea.y; (y < (actionArea.y + actionArea.height - marker.getHeight())) && !found; y++) { |
| 64 |
for (int x = actionArea.x; (x < (actionArea.x + actionArea.width - marker.getWidth())) && !found; x++) { |
| 65 |
|
| 66 |
|
| 67 |
for (int my = 0; (my < mh) && ok; my++) { |
| 68 |
for (int mx = 0; (mx < mw) && ok; mx++) { |
| 69 |
|
| 70 |
//ignore masked (having alpha) values |
| 71 |
if (!mask[mx][my]) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
if (markerPixels[mx][my] != screen.getRGB(x + mx, y + my)) { |
| 75 |
ok = false; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
if( ok ){ |
| 80 |
found = true; |
| 81 |
result.x = x; |
| 82 |
result.y = y; |
| 83 |
result.height = marker.getHeight(); |
| 84 |
result.width = marker.getWidth(); |
| 85 |
}else{ |
| 86 |
ok = true; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if(found){ |
| 92 |
return result; |
| 93 |
}else{ |
| 94 |
return null; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public static Rectangle findBlurredImage(BufferedImage marker, BufferedImage testImage, double minCorrelation){ |
| 99 |
return findBlurredImage(marker, testImage, minCorrelation, new Rectangle(0,0,testImage.getWidth(), testImage.getHeight())); |
| 100 |
} |
| 101 |
|
| 102 |
public static Rectangle findBlurredImage(BufferedImage marker, BufferedImage testImage, double minCorrelation, Rectangle actionArea) |
| 103 |
{ |
| 104 |
int maxX = actionArea.width - marker.getWidth() - 1; |
| 105 |
int maxY = actionArea.height - marker.getHeight() - 1; |
| 106 |
int markerMaxX = marker.getWidth(); |
| 107 |
int markerMaxY = marker.getHeight(); |
| 108 |
|
| 109 |
// it is much faster to work directly with color components stored as float values |
| 110 |
float[][][] testImageArray = createArrayForOneColorComponent(actionArea); |
| 111 |
float[][][] markerImageArray = createArrayForOneColorComponent(marker); |
| 112 |
|
| 113 |
convertImageToFloatArray(testImage, testImageArray, actionArea); |
| 114 |
convertImageToFloatArray(marker, markerImageArray); |
| 115 |
|
| 116 |
int bestX = -1; |
| 117 |
int bestY = -1; |
| 118 |
double bestCorrelation = -1; |
| 119 |
|
| 120 |
for (int yoffset = 0; yoffset < maxY; yoffset++ ) |
| 121 |
{ |
| 122 |
for (int xoffset = 0; xoffset < maxX; xoffset++) |
| 123 |
{ |
| 124 |
double correlation = computeCorrelation(markerMaxX, markerMaxY, testImageArray, markerImageArray, yoffset, xoffset); |
| 125 |
if (correlation > bestCorrelation) |
| 126 |
{ |
| 127 |
bestCorrelation = correlation; |
| 128 |
bestX = xoffset + actionArea.x; |
| 129 |
bestY = yoffset + actionArea.y; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
if(bestCorrelation > minCorrelation){ |
| 134 |
return new Rectangle(bestX, bestY, marker.getWidth(), marker.getHeight()); |
| 135 |
}else{ |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Create three-dimensional array with the same size as tested image |
| 143 |
* dimensions (last dimension is used for storing RGB components). |
| 144 |
* |
| 145 |
* @param testImage tested image |
| 146 |
* @return newly created three-dimensional array |
| 147 |
*/ |
| 148 |
private static float[][][] createArrayForOneColorComponent(BufferedImage testImage) |
| 149 |
{ |
| 150 |
return new float[testImage.getHeight()][testImage.getWidth()][3]; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Create three-dimensional array with the same size as the given area |
| 155 |
* dimensions (last dimension is used for storing RGB components). |
| 156 |
* |
| 157 |
* @param actionArea |
| 158 |
* @return newly created three-dimensional array |
| 159 |
*/ |
| 160 |
private static float[][][] createArrayForOneColorComponent(Rectangle actionArea) |
| 161 |
{ |
| 162 |
return new float[actionArea.height][actionArea.width][3]; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Conversion from BufferedImage into three dimensional float arrays. |
| 167 |
* It's much faster to work with float arrays even if it's memory ineficient. |
| 168 |
* |
| 169 |
* @param testImage tested image |
| 170 |
* @param array array to fill |
| 171 |
*/ |
| 172 |
private static void convertImageToFloatArray(BufferedImage testImage, float[][][] array) |
| 173 |
{ |
| 174 |
for (int y = 0; y < testImage.getHeight(); y++) |
| 175 |
{ |
| 176 |
for (int x = 0; x < testImage.getWidth(); x++) |
| 177 |
{ |
| 178 |
int c = testImage.getRGB(x, y); |
| 179 |
// filter out alpha channel |
| 180 |
c = c & 0xffffff; |
| 181 |
array[y][x][0] = ((c >> 16) & 0xff) - 128f; |
| 182 |
array[y][x][1] = ((c >> 8) & 0xff) - 128f; |
| 183 |
array[y][x][2] = (c & 0xff) - 128f; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Conversion from BufferedImage into three dimensional float arrays. |
| 190 |
* It's much faster to work with float arrays even if it's memory ineficient. |
| 191 |
* This method converts only a given part of the image (actionArea) |
| 192 |
* |
| 193 |
* @param testImage tested image |
| 194 |
* @param array array to fill |
| 195 |
* @param actionArea rectangle part of the image to convert |
| 196 |
*/ |
| 197 |
private static void convertImageToFloatArray(BufferedImage testImage, float[][][] array, Rectangle actionArea) |
| 198 |
{ |
| 199 |
for (int y = actionArea.y; y < (actionArea.height + actionArea.y); y++) |
| 200 |
{ |
| 201 |
for (int x = actionArea.x; x < (actionArea.width + actionArea.x); x++) |
| 202 |
{ |
| 203 |
int c = testImage.getRGB(x, y); |
| 204 |
// filter out alpha channel |
| 205 |
c = c & 0xffffff; |
| 206 |
array[y - actionArea.y][x - actionArea.x][0] = ((c >> 16) & 0xff) - 128f; |
| 207 |
array[y - actionArea.y][x - actionArea.x][1] = ((c >> 8) & 0xff) - 128f; |
| 208 |
array[y - actionArea.y][x - actionArea.x][2] = (c & 0xff) - 128f; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Compute correlation for given two images and 2D offset. |
| 215 |
* |
| 216 |
* @param maxX |
| 217 |
* @param maxY |
| 218 |
* @param testImageArray |
| 219 |
* @param markerImageArray |
| 220 |
* @param yoffset |
| 221 |
* @param xoffset |
| 222 |
* @return |
| 223 |
*/ |
| 224 |
private static double computeCorrelation(int maxX, int maxY, float[][][] testImageArray, |
| 225 |
float[][][] markerImageArray, int yoffset, int xoffset) |
| 226 |
{ |
| 227 |
double correlation = 0; |
| 228 |
for (int y = 0; y < maxY; y++) |
| 229 |
{ |
| 230 |
for (int x = 0; x < maxX; x++) |
| 231 |
{ |
| 232 |
for (int rgbIndex = 0; rgbIndex < 3; rgbIndex++) |
| 233 |
{ |
| 234 |
float colorComponent1 = markerImageArray[y][x][rgbIndex]; |
| 235 |
float colorComponent2 = testImageArray[yoffset + y][xoffset + x][rgbIndex]; |
| 236 |
correlation += colorComponent1 * colorComponent2; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
return correlation; |
| 241 |
} |
| 242 |
|
| 243 |
public static int findHorizontalRule(BufferedImage screen, |
| 244 |
Color ruleColor, Color bgColor, boolean fromTop) { |
| 245 |
final int height = screen.getHeight(); |
| 246 |
int gap = 0; |
| 247 |
|
| 248 |
if (!fromTop) { |
| 249 |
return findHorizontalEdgeGap(screen, ruleColor, |
| 250 |
bgColor, 1, height - 1, gap); |
| 251 |
} else { |
| 252 |
return findHorizontalEdgeGap(screen, bgColor, |
| 253 |
ruleColor, 1, height - 1, gap); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
public static int findHorizontalEdgeGap(BufferedImage screen, |
| 258 |
Color area1Color, Color area2Color, int y1, int y2, int gap) { |
| 259 |
final int width = screen.getWidth(); |
| 260 |
final int area1RGB = area1Color.getRGB(); |
| 261 |
final int area2RGB = area2Color.getRGB(); |
| 262 |
int edgePosition = Integer.MIN_VALUE; |
| 263 |
int lastFound = Integer.MIN_VALUE; |
| 264 |
|
| 265 |
for (int y = y1+1; y < y2 - gap; y++) { |
| 266 |
int found = 0; |
| 267 |
for (int x = 0; x < width; x++) { |
| 268 |
int c1 = screen.getRGB(x, y - 1); |
| 269 |
int c2 = screen.getRGB(x, y + gap); |
| 270 |
if (c1 == area1RGB && c2 == area2RGB) { |
| 271 |
found++; |
| 272 |
} |
| 273 |
} |
| 274 |
if (found > lastFound) { |
| 275 |
lastFound = found; |
| 276 |
edgePosition = y; |
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
return edgePosition; |
| 282 |
} |
| 283 |
|
| 284 |
public static int findVerticalEdgeGap(BufferedImage screen, |
| 285 |
Color area1Color, Color area2Color, int y1, int y2, int gap) { |
| 286 |
final int width = screen.getWidth(); |
| 287 |
final int area1RGB = area1Color.getRGB(); |
| 288 |
final int area2RGB = area2Color.getRGB(); |
| 289 |
int edgePosition = Integer.MIN_VALUE; |
| 290 |
int lastFound = Integer.MIN_VALUE; |
| 291 |
|
| 292 |
for (int x = 1; x < width - 1 - gap; x++) { |
| 293 |
int found = 0; |
| 294 |
for (int y = y1; y < y2; y++) { |
| 295 |
int c1 = screen.getRGB(x - 1, y); |
| 296 |
int c2 = screen.getRGB(x + gap, y); |
| 297 |
if (c1 == area1RGB && c2 == area2RGB) { |
| 298 |
found++; |
| 299 |
} |
| 300 |
} |
| 301 |
if (found > lastFound) { |
| 302 |
lastFound = found; |
| 303 |
edgePosition = x; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
return edgePosition; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* method findColoredAreaGap finds a rectangle of given color surrounded by |
| 312 |
* area of the second color with a possible gap at the border |
| 313 |
* |
| 314 |
* @param screen |
| 315 |
* @param searchForColor |
| 316 |
* @param surroundWithColor |
| 317 |
* @param y1 |
| 318 |
* @param y2 |
| 319 |
* @param gap |
| 320 |
* @return |
| 321 |
*/ |
| 322 |
public static Rectangle findColoredAreaGap(BufferedImage screen, Color searchForColor, Color surroundWithColor, int y1, int y2, int gap) { |
| 323 |
|
| 324 |
int ymin = findHorizontalEdgeGap(screen, surroundWithColor, searchForColor, y1, y2, gap); |
| 325 |
int ymax = findHorizontalEdgeGap(screen, searchForColor, surroundWithColor, y1, y2, gap); |
| 326 |
int xmin = findVerticalEdgeGap(screen, surroundWithColor, searchForColor, ymin, ymax, gap); |
| 327 |
int xmax = findVerticalEdgeGap(screen, searchForColor, surroundWithColor, ymin, ymax, gap); |
| 328 |
|
| 329 |
return new Rectangle(xmin, ymin, xmax - xmin, ymax - ymin); |
| 330 |
} |
| 331 |
|
| 332 |
public static boolean isRectangleValid(Rectangle r){ |
| 333 |
|
| 334 |
if (r == null) return false; |
| 335 |
|
| 336 |
return (r.width != 0)&&(r.height != 0)&&(r.x != Integer.MIN_VALUE)&&(r.y != Integer.MIN_VALUE); |
| 337 |
} |
| 338 |
|
| 339 |
public static BufferedImage getMaskImage(BufferedImage icon) { |
| 340 |
int w = icon.getWidth(); |
| 341 |
int h = icon.getHeight(); |
| 342 |
boolean[][] b = getMask(icon); |
| 343 |
BufferedImage mask = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY); |
| 344 |
for (int y = 0; y < h; y++) { |
| 345 |
for (int x = 0; x < w; x++) { |
| 346 |
if (b[x][y]) { |
| 347 |
mask.setRGB(x, y, Color.white.getRGB()); |
| 348 |
} else { |
| 349 |
mask.setRGB(x, y, Color.black.getRGB()); |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
return mask; |
| 354 |
} |
| 355 |
|
| 356 |
public static boolean[][] getMask(BufferedImage icon) { |
| 357 |
int w = icon.getWidth(); |
| 358 |
int h = icon.getHeight(); |
| 359 |
boolean[][] r = new boolean[w][h]; |
| 360 |
for (int y = 0; y < h; y++) { |
| 361 |
for (int x = 0; x < w; x++) { |
| 362 |
int i = icon.getRGB(x, y); |
| 363 |
int alpha = (i >> 24) & 0xff; |
| 364 |
if (alpha == 255) { |
| 365 |
r[x][y] = true; |
| 366 |
} else { |
| 367 |
r[x][y] = false; |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
return r; |
| 372 |
} |
| 373 |
|
| 374 |
public static int[][] getPixels(BufferedImage icon) { |
| 375 |
int w = icon.getWidth(); |
| 376 |
int h = icon.getHeight(); |
| 377 |
int[][] r = new int[w][h]; |
| 378 |
for (int x = 0; x < w; x++) { |
| 379 |
for (int y = 0; y < h; y++) { |
| 380 |
int i = icon.getRGB(x, y); |
| 381 |
//remove mask? not yet... |
| 382 |
r[x][y] = i; |
| 383 |
} |
| 384 |
} |
| 385 |
return r; |
| 386 |
} |
| 387 |
} |