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.pixel;
  26 
  27 import org.jemmy.Dimension;
  28 import org.jemmy.image.Image;
  29 import org.jemmy.image.ImageComparator;
  30 
  31 /**
  32  *
  33  * @author shura
  34  */
  35 public abstract class ResizeComparator implements ImageComparator {
  36 
  37     private ImageComparator subComparator;
  38     private Mode mode;
  39 
  40     /**
  41      *
  42      */
  43     public enum Mode {
  44 
  45         /**
  46          *
  47          */
  48         LEFT,
  49         /**
  50          *
  51          */
  52         RIGTH,
  53         /**
  54          *
  55          */
  56         MAX
  57     };
  58 
  59     /**
  60      *
  61      * @param subComparator
  62      * @param mode
  63      */
  64     public ResizeComparator(ImageComparator subComparator, Mode mode) {
  65         this.subComparator = subComparator;
  66         this.mode = mode;
  67     }
  68 
  69     /**
  70      *
  71      * @param subComparator
  72      */
  73     public ResizeComparator(ImageComparator subComparator) {
  74         this(subComparator, Mode.MAX);
  75     }
  76 
  77     /**
  78      *
  79      * @return
  80      */
  81     public ImageComparator getSubComparator() {
  82         return subComparator;
  83     }
  84 
  85     /**
  86      *
  87      * @param subComparator
  88      */
  89     public void setSubComparator(ImageComparator subComparator) {
  90         this.subComparator = subComparator;
  91     }
  92 
  93     /**
  94      *
  95      * @param image1
  96      * @param image2
  97      * @return
  98      */
  99     public Image compare(Image image1, Image image2) {
 100         if(image1 == null || image2 == null) {
 101             return (image1 == null) ? image2 : image1;
 102         }
 103         Dimension size;
 104         switch (mode) {
 105             case LEFT:
 106                 size = getSize(image1);
 107                 break;
 108             case RIGTH:
 109                 size = getSize(image2);
 110                 break;
 111             case MAX:
 112                 Dimension size1 = getSize(image1);
 113                 Dimension size2 = getSize(image2);
 114                 size = new Dimension(Math.max(size1.width, size2.width),
 115                         Math.max(size1.height, size2.height));
 116                 break;
 117             default:
 118                 throw new IllegalStateException("mode is not recognized");
 119         }
 120         Image r1 = resize(image1, size);
 121         Image r2 = resize(image2, size);
 122         if(r1 == null) {
 123             return image1;
 124         }
 125         if(r2 == null) {
 126             return image2;
 127         }
 128         return subComparator.compare(r1, r2);
 129     }
 130 
 131     /**
 132      *
 133      * @param image
 134      * @param size
 135      * @return
 136      */
 137     abstract public Image resize(Image image, Dimension size);
 138 
 139     /**
 140      *
 141      * @param image
 142      * @return
 143      */
 144     abstract public Dimension getSize(Image image);
 145 
 146     /**
 147      *
 148      * @return
 149      */
 150     public String getID() {
 151         return ResizeComparator.class.getName() + "(" + subComparator.getID() + ")";
 152     }
 153 }