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. Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package org.netbeans.jemmy.image;
26 
27 import java.awt.Point;
28 import java.awt.image.BufferedImage;
29 
30 /**
31  * Performs "rough" image search.
32  *
33  * @author Alexandre Iline (alexandre.iline@oracle.com)
34  */
35 public class RoughImageFinder implements ImageFinder {
36 
37     double roughness = .0;
38     int bigWidth, bigHeight;
39     int[][] bigPixels;
40 
41     /**
42      * Creates an instance allowing to find an image inside the one passed as
43      * parameter with some "roughness".
44      *
45      * @param area - Image to search in.
46      * @param roughness - Allowed
47      */
RoughImageFinder(BufferedImage area, double roughness)48     public RoughImageFinder(BufferedImage area, double roughness) {
49         this.roughness = roughness;
50         bigWidth = area.getWidth();
51         bigHeight = area.getHeight();
52         bigPixels = new int[bigWidth][bigHeight];
53         for (int x = 0; x < bigWidth; x++) {
54             for (int y = 0; y < bigHeight; y++) {
55                 bigPixels[x][y] = area.getRGB(x, y);
56             }
57         }
58     }
59 
60     /**
61      * Performs "rough" search.
62      *
63      * @param image an image to search.
64      * @param index an ordinal image location index.
65      * @return Point where number of unmatching pixels less or equal to      <code>image1.getWidth() * image1.getHeight() * roughness<code>
66      */
67     @Override
findImage(BufferedImage image, int index)68     public Point findImage(BufferedImage image, int index) {
69         int smallWidth = image.getWidth();
70         int smallHeight = image.getHeight();
71         int[][] smallPixels = new int[smallWidth][smallHeight];
72         for (int x = 0; x < smallWidth; x++) {
73             for (int y = 0; y < smallHeight; y++) {
74                 smallPixels[x][y] = image.getRGB(x, y);
75             }
76         }
77         double maxRoughPixels = (double) (smallWidth * smallHeight) * roughness;
78         int count = 0;
79         for (int X = 0; X <= bigWidth - smallWidth; X++) {
80             for (int Y = 0; Y <= bigHeight - smallHeight; Y++) {
81                 int roughPixels = 0;
82                 for (int x = 0; x < smallWidth; x++) {
83                     for (int y = 0; y < smallHeight; y++) {
84                         if (smallPixels[x][y] != bigPixels[X + x][Y + y]) {
85                             roughPixels++;
86                             if (roughPixels > maxRoughPixels) {
87                                 break;
88                             }
89                         }
90                     }
91                     if (roughPixels > maxRoughPixels) {
92                         break;
93                     }
94                 }
95                 if (roughPixels <= maxRoughPixels) {
96                     if (count == index) {
97                         return new Point(X, Y);
98                     }
99                     count++;
100                 }
101             }
102         }
103         return null;
104     }
105 }
106