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