1/* 2 * Copyright (c) 1997, 2016, 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 */ 23package org.netbeans.jemmy.image; 24 25import java.awt.Point; 26import java.awt.image.BufferedImage; 27 28/** 29 * Performs "strict" (i.e. based on all pixels matching) image search. 30 * 31 * @author Alexandre Iline (alexandre.iline@oracle.com) 32 */ 33public class StrictImageFinder implements ImageFinder { 34 35 int bigWidth, bigHeight; 36 int[][] bigPixels; 37 38 /** 39 * Creates an instance searching subimages insige a parameter image. 40 * 41 * @param area - Image to search in. 42 */ 43 public StrictImageFinder(BufferedImage area) { 44 bigWidth = area.getWidth(); 45 bigHeight = area.getHeight(); 46 bigPixels = new int[bigWidth][bigHeight]; 47 for (int x = 0; x < bigWidth; x++) { 48 for (int y = 0; y < bigHeight; y++) { 49 bigPixels[x][y] = area.getRGB(x, y); 50 } 51 } 52 } 53 54 /** 55 * Searchs for an image inside image passed into constructor. 56 * 57 * @param image an image to search. 58 * @param index an ordinal image location index. If equal to 1, for example, 59 * second appropriate location will be found. 60 * @return Left-up corner coordinates of image location. 61 */ 62 @Override 63 public Point findImage(BufferedImage image, int index) { 64 int smallWidth = image.getWidth(); 65 int smallHeight = image.getHeight(); 66 int[][] smallPixels = new int[smallWidth][smallHeight]; 67 for (int x = 0; x < smallWidth; x++) { 68 for (int y = 0; y < smallHeight; y++) { 69 smallPixels[x][y] = image.getRGB(x, y); 70 } 71 } 72 boolean good; 73 int count = 0; 74 for (int X = 0; X <= bigWidth - smallWidth; X++) { 75 for (int Y = 0; Y <= bigHeight - smallHeight; Y++) { 76 good = true; 77 for (int x = 0; x < smallWidth; x++) { 78 for (int y = 0; y < smallHeight; y++) { 79 if (smallPixels[x][y] != bigPixels[X + x][Y + y]) { 80 good = false; 81 break; 82 } 83 } 84 if (!good) { 85 break; 86 } 87 } 88 if (good) { 89 if (count == index) { 90 return new Point(X, Y); 91 } 92 count++; 93 } 94 } 95 } 96 return null; 97 } 98} 99