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