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