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 java.util.List;
  26 import org.jemmy.Dimension;
  27 import org.jemmy.image.pixel.Raster.Component;
  28 
  29 /**
  30  *
  31  * @author shura
  32  */
  33 public abstract class ColorMappingComparator implements RasterComparator {
  34 
  35     final private ColorMap left;
  36     final private ColorMap right;
  37     private RasterComparator subComparator;
  38 
  39     /**
  40      *
  41      * @param left
  42      * @param right
  43      * @param subComparator
  44      */
  45     public ColorMappingComparator(ColorMap left, ColorMap right,
  46             RasterComparator subComparator) {
  47         this.subComparator = subComparator;
  48         this.left = left;
  49         this.right = right;
  50     }
  51 
  52     /**
  53      *
  54      * @return
  55      */
  56     public RasterComparator getSubComparator() {
  57         return subComparator;
  58     }
  59 
  60     /**
  61      *
  62      * @param subComparator
  63      */
  64     public void setSubComparator(RasterComparator subComparator) {
  65         this.subComparator = subComparator;
  66     }
  67 
  68     /**
  69      *
  70      * @param both
  71      * @param subComparator
  72      */
  73     public ColorMappingComparator(ColorMap both, RasterComparator subComparator) {
  74         this(both, both, subComparator);
  75     }
  76 
  77     /**
  78      *
  79      * @param image1
  80      * @param image2
  81      * @return
  82      */
  83     public boolean compare(Raster image1, Raster image2) {
  84         return subComparator.compare(map(image1, left), map(image2, right));
  85     }
  86 
  87     /**
  88      *
  89      * @param image
  90      * @param map
  91      * @return
  92      */
  93     public WriteableRaster map(Raster image, ColorMap map) {
  94         WriteableRaster res = createView(image.getSize());
  95         double[] colors = new double[image.getSupported().length];
  96         double[] newColors = new double[image.getSupported().length];
  97         for (int x = 0; x < image.getSize().width; x++) {
  98             for (int y = 0; y < image.getSize().height; y++) {
  99                 image.getColors(x, y, colors);
 100                 map.map(image.getSupported(), colors, newColors);
 101                 res.setColors(x, y, newColors);
 102             }
 103         }
 104         return res;
 105     }
 106 
 107     /**
 108      *
 109      * @param size
 110      * @return
 111      */
 112     protected abstract WriteableRaster createView(Dimension size);
 113 
 114     /**
 115      *
 116      * @return
 117      */
 118     public String getID() {
 119         return ColorMappingComparator.class.getName() + ":" +
 120                 left.getID() + "," + right.getID() + "(" +
 121                 subComparator.getID() + ")";
 122     }
 123 
 124     /**
 125      *
 126      */
 127     public interface ColorMap {
 128 
 129         /**
 130          *
 131          * @param components
 132          * @param values
 133          * @param newValues
 134          */
 135         public void map(Component[] components, double[] values, double[] newValues);
 136         /**
 137          *
 138          * @return
 139          */
 140         public String getID();
 141     }
 142 }