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