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 import org.jemmy.Point;
  26 import java.awt.image.BufferedImage;
  27 
  28 /**
  29  * Performs "rough" image search.
  30  *
  31  * @author Alexandre Iline (alexandre.iline@sun.com)
  32  */
  33 public class RoughImageFinder implements ImageFinder {
  34     private final double roughness;
  35     private int bigWidth, bigHeight;
  36     private int[][] bigPixels;
  37 
  38     /**
  39      * Creates an instance allowing to find an image inside the one
  40      * passed as parameter with some "roughness".
  41      * @param area - Image to search in.
  42      * @param roughness - Allowed
  43      */
  44     public RoughImageFinder(BufferedImage area, double roughness) {
  45         this.roughness = roughness;
  46         bigWidth  = area.getWidth();
  47         bigHeight = area.getHeight();
  48         bigPixels = new int[bigWidth][bigHeight];
  49         for(int x = 0; x < bigWidth; x++) {
  50             for(int y = 0; y < bigHeight; y++) {
  51                 bigPixels[x][y] = area.getRGB(x, y);
  52             }
  53         }
  54     }
  55 
  56     /**
  57      * Performs "rough" search.
  58      * @param image an image to search.
  59      * @param index an ordinal image location index.
  60      * @return Point where number of unmatching pixels less or equal to
  61      * <code>image1.getWidth() * image1.getHeight() * roughness<code>
  62      */
  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         double maxRoughPixels = (double)(smallWidth * smallHeight) * roughness;
  73         int count = 0;
  74         for(int X = 0; X <= bigWidth - smallWidth; X++) {
  75             for(int Y = 0; Y <= bigHeight - smallHeight; Y++) {
  76                 int roughPixels = 0;
  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                             roughPixels++;
  81                             if(roughPixels > maxRoughPixels) {
  82                                 break;
  83                             }
  84                         }
  85                     }
  86                     if(roughPixels > maxRoughPixels) {
  87                         break;
  88                     }
  89                 }
  90                 if(roughPixels <= maxRoughPixels) {
  91                     if(count == index) {
  92                         return(new Point(X, Y));
  93                     }
  94                     count++;
  95                 }
  96             }
  97         }
  98         return(null);
  99     }
 100 }