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