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 java.awt.image.BufferedImage; 26 import org.jemmy.image.pixel.PixelEqualityRasterComparator; 27 28 /** 29 * Compares two images with color mapping defined by 30 * <code>ColorModel</code> implementation. 31 * 32 * @author Alexandre Iline (alexandre.iline@sun.com) 33 * @deprecated Use classes form org.jemmy.image.pixel instead. 34 */ 35 @Deprecated 36 public class ColorImageComparator implements ImageComparator { 37 38 ColorMap leftMap, rightMap; 39 ImageComparator comparator = null; 40 41 /** 42 * Creates a comparator with a color maps. Object created by this 43 * constructor behaves like 44 * <code>StrictImageComparator</code>. Object created works faster because 45 * it does not create intermediate images for another comparator. 46 * 47 * @param map Map applied to both left and right images during comparision. 48 */ 49 public ColorImageComparator(ColorMap map) { 50 this(map, new StrictImageComparator()); 51 } 52 53 /** 54 * Creates a comparator with 55 * <code>map</code> color mapping. Actual comparision perfomed by 56 * <code>comparator</code> parameter. 57 * 58 * @param map Map applied to both left and right images during comparision. 59 * @param subComparator comporator to perform a comparision of to images 60 * with mapped colors. 61 */ 62 public ColorImageComparator(ColorMap map, ImageComparator subComparator) { 63 this(map, map, subComparator); 64 } 65 66 /** 67 * Creates a comparator with two color maps. Object created by this 68 * constructor behaves like 69 * <code>StrictImageComparator</code>. Object created works faster because 70 * it does not create intermediate images for another comparator. 71 * 72 * @param leftMap Map applied to the left image during comparision. 73 * @param rightMap Map applied to the right image during comparision. 74 */ 75 public ColorImageComparator(ColorMap leftMap, ColorMap rightMap) { 76 this(leftMap, rightMap, new BufferedImageComparator(new PixelEqualityRasterComparator(0))); 77 } 78 79 /** 80 * Creates a comparator with two color maps. Actual comparision perfomed by 81 * <code>comparator</code> parameter. 82 * 83 * @param leftMap Map applied to the left image during comparision. 84 * @param rightMap Map applied to the right image during comparision. 85 * @param subComparator comporator to perform a comparision of to images 86 * with mapped colors. 87 */ 88 public ColorImageComparator(ColorMap leftMap, ColorMap rightMap, ImageComparator subComparator) { 89 this.leftMap = leftMap; 90 this.rightMap = rightMap; 91 this.comparator = subComparator; 92 } 93 94 /** 95 * Compares images by 96 * <code>ImageComparator</code> passed into constructor, or itself if no 97 * <code>ImageComparator</code> was passed, processing both images by 98 * <code>ColorMap</code> instance before comparision. 99 */ 100 @Override 101 public Image compare(Image image1, Image image2) { 102 return (comparator.compare( 103 recolor((AWTImage)image1, leftMap), 104 recolor((AWTImage)image2, rightMap))); 105 } 106 107 private AWTImage recolor(AWTImage isrc, ColorMap map) { 108 BufferedImage src = isrc.getTheImage(); 109 BufferedImage result = new BufferedImage(src.getWidth(), src.getHeight(), src.getType()); 110 for (int x = 0; x < src.getWidth(); x++) { 111 for (int y = 0; y < src.getWidth(); y++) { 112 result.setRGB(x, y, map.mapColor(src.getRGB(x, y))); 113 } 114 } 115 return new AWTImage(result); 116 } 117 118 protected final boolean compareColors(int rgb1, int rgb2) { 119 return (leftMap.mapColor(rgb1) == rightMap.mapColor(rgb2)); 120 } 121 122 public String getID() { 123 return ColorImageComparator.class.getName(); 124 } 125 126 /** 127 * Interface to map colors during the comparision. 128 */ 129 public static interface ColorMap { 130 131 /** 132 * Maps one color into another. 133 * 134 * @param rgb an original color. 135 * @return a converted color. 136 */ 137 public int mapColor(int rgb); 138 } 139 140 /** 141 * Turns 142 * <code>foreground</code> color to white, other - to black. 143 */ 144 public static class ForegroundColorMap implements ColorMap { 145 146 int foreground; 147 148 /** 149 * Constructs a ColorImageComparator$ForegroundColorMap object. 150 * 151 * @param foreground Foreground color. 152 */ 153 public ForegroundColorMap(int foreground) { 154 this.foreground = foreground; 155 } 156 157 public int mapColor(int rgb) { 158 return ((rgb == foreground) ? 0xffffff : 0); 159 } 160 } 161 162 /** 163 * Turns 164 * <code>background</code> color to black, left others unchanged. 165 */ 166 public static class BackgroundColorMap implements ColorMap { 167 168 int background; 169 170 /** 171 * Constructs a ColorImageComparator$BackgroundColorMap object. 172 * 173 * @param background Background color. 174 */ 175 public BackgroundColorMap(int background) { 176 this.background = background; 177 } 178 179 public int mapColor(int rgb) { 180 return ((rgb == background) ? 0 : rgb); 181 } 182 } 183 }