< prev index next >

core/JemmyAWTInput/src/org/jemmy/image/ResizeImageComparator.java

Print this page




  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.Graphics2D;
  28 import java.awt.image.BufferedImage;
  29 import org.jemmy.Dimension;
  30 import org.jemmy.env.Environment;
  31 import org.jemmy.env.TestOut;
  32 import org.jemmy.image.pixel.Raster;
  33 import org.jemmy.image.pixel.RasterComparator;
  34 import org.jemmy.image.pixel.ResizeComparator;
  35 
  36 /**
  37  * Comparator that makes image sizes equal to compare them with other comparator
  38  * most of them work only for the images with equal dimensions.
  39  *
  40  * It is controlled by three parameters: <li>Resize Mode - defines way of
  41  * resizing images that are being compared. One of the following constants: <ul> <li>{@linkplain ResizeMode#NO_RESIZE NO_RESIZE},</li> <li>{@linkplain ResizeMode#PROPORTIONAL_RESIZE PROPORTIONAL_RESIZE},</li> <li>{@linkplain ResizeMode#ARBITRARY_RESIZE ARBITRARY_RESIZE}.</li></ul>
  42  * Default value is {@linkplain ResizeMode#PROPORTIONAL_RESIZE}.</li> <li>Resize
  43  * Hint - defines the way of images scaling that is specified when {@linkplain java.awt.Image#getScaledInstance(int, int, int)
  44  * Image.getScaledInstance()} method is invoked. One of the Image.SCALE_XXX is
  45  * expected. Default value is
  46  * {@linkplain java.awt.Image#SCALE_DEFAULT SCALE_DEFAULT}.</li> <li>Proportion
  47  * Distortion Threshold - defines maximum proportion distortion that is used in {@linkplain ResizeMode#PROPORTIONAL_RESIZE
  48  * PROPORTIONAL_RESIZE} mode to deal with cases when rounding and other problems
  49  * could cause sizes to be not ideally proportional.<p/>








  50  * Threshold value is applied in the following way: {@code Math.abs(image.getSize() * scale
  51  * - size) / image.getSize() > PROPORTION_DISTORTION_THRESHOLD}, where {@code image.getSize()}
  52  * is both image dimensions and {@code size} is target width and height (which
  53  * is defined as mininum of sizes of two images) and scale is the scale factor
  54  * that scales the particular image to fit the width and height.
  55  * <p/>
  56  * Default value is 0.02 so as much as 2% of width/height could differ (around 5
  57  * in 0-255 color component values).</li>

  58  *
  59  * @author mrkam
  60  */
  61 public class ResizeImageComparator extends ResizeComparator {
  62 
  63     /**
  64      * Indentifies output where resize details are printed. Output is disabled
  65      * by default.
  66      *
  67      * @see Environment#getOutput(java.lang.String)
  68      */
  69     public static final String OUTPUT = ResizeImageComparator.class.getName()
  70             + ".OUTPUT";
  71 
  72     static {
  73         Environment.getEnvironment().setOutput(OUTPUT, TestOut.getNullOutput());
  74     }
  75     ResizeMode resizeMode;
  76     int hint;
  77     double propDistThreshold;
  78 
  79     /**
  80      * Resize Modes
  81      *
  82      * @see
  83      * #ResizeImageComparator(org.jemmy.image.ResizeImageComparator.ResizeMode,
  84      * org.jemmy.image.ImageComparator)
  85      * @see
  86      * #ResizeImageComparator(org.jemmy.image.ResizeImageComparator.ResizeMode,
  87      * int, double, org.jemmy.image.ImageComparator)
  88      */
  89     public static enum ResizeMode {
  90 
  91         /**
  92          * Images are never resized. Original images are always compared.
  93          */
  94         NO_RESIZE,
  95         /**
  96          * Images are resized only if they have exactly or almost proportional
  97          * sizes which is controlled by {@code proportionDistortion} parameter.
  98          * If images have different proportions no resize is done and original
  99          * images are compared.
 100          *
 101          * @see
 102          * #ResizeImageComparator(org.jemmy.image.ResizeImageComparator.ResizeMode,
 103          * int, double, org.jemmy.image.ImageComparator)
 104          */
 105         PROPORTIONAL_RESIZE,
 106         /**
 107          * Images are always resized to match both width and height and then
 108          * compared.
 109          */
 110         ARBITRARY_RESIZE
 111     }
 112 
 113     /**
 114      * Creates ResizeImageComparator with default resize settings: <li>resize
 115      * mode: {@linkplain ResizeMode#PROPORTIONAL_RESIZE ResizeMode.PROPORTIONAL_RESIZE}.</li>
 116      * <li>proportion distortion threshold: 0.02.</li> <li>resize hint: {@linkplain java.awt.Image#SCALE_DEFAULT Image.SCALE_DEFAULT}.</li>



 117      *
 118      * @param subComparator comparator to compare images after resize.
 119      * @see java.awt.Image#getScaledInstance(int, int, int)
 120      * @see ResizeMode
 121      * @see ResizeImageComparator
 122      */
 123     public ResizeImageComparator(ImageComparator subComparator) {
 124         super(subComparator, ResizeComparator.Mode.LEFT);
 125         this.resizeMode = ResizeMode.PROPORTIONAL_RESIZE;
 126         this.hint = java.awt.Image.SCALE_DEFAULT;
 127         this.propDistThreshold = 0.02;
 128     }
 129 
 130     /**
 131      * Creates ResizeImageComparator with the specified resize mode and default
 132      * settings for other parameters: <li>proportion distortion threshold:
 133      * 0.02.</li> <li>resize hint: {@linkplain java.awt.Image#SCALE_DEFAULT Image.SCALE_DEFAULT}.</li>



 134      *
 135      * @param resizeMode resize mode for this comparator.
 136      * @param subComparator comparator to compare images after resize.
 137      * @see ResizeMode
 138      * @see ResizeImageComparator
 139      */
 140     public ResizeImageComparator(ResizeMode resizeMode,
 141             ImageComparator subComparator) {
 142         this(subComparator);
 143         this.resizeMode = resizeMode;
 144     }
 145 
 146     /**
 147      * Creates ResizeImageComparator with the following settings: <li>resize
 148      * mode: {@linkplain ResizeMode#PROPORTIONAL_RESIZE ResizeMode.PROPORTIONAL_RESIZE}.</li>
 149      * <li>specified proportion distortion threshold.</li> <li>resize hint: {@linkplain java.awt.Image#SCALE_DEFAULT Image.SCALE_DEFAULT}.</li>



 150      *
 151      * @param propDistThreshold proportion distortion threshold.
 152      * @param subComparator comparator to compare images after resize.
 153      * @see ResizeImageComparator
 154      */
 155     public ResizeImageComparator(double propDistThreshold,
 156             ImageComparator subComparator) {
 157         this(subComparator);
 158         this.propDistThreshold = propDistThreshold;
 159     }
 160 
 161     /**
 162      * Creates ResizeImageComparator with specified settings.
 163      *
 164      * @param resizeMode Resize mode.
 165      * @param propDistThreshold Proportion distortion threshold.
 166      * @param hint Resize hint.
 167      * @param subComparator comparator to compare images after resize.
 168      * @see ResizeImageComparator
 169      * @see ResizeMode




  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.Graphics2D;
  28 import java.awt.image.BufferedImage;
  29 import org.jemmy.Dimension;
  30 import org.jemmy.env.Environment;
  31 import org.jemmy.env.TestOut;
  32 import org.jemmy.image.pixel.Raster;
  33 import org.jemmy.image.pixel.RasterComparator;
  34 import org.jemmy.image.pixel.ResizeComparator;
  35 
  36 /**
  37  * Comparator that makes image sizes equal to compare them with other comparator
  38  * most of them work only for the images with equal dimensions.
  39  *
  40  * It is controlled by three parameters:
  41  * <p>
  42  * Resize Mode - defines way of resizing images that are being compared. One of the following constants:
  43  * <ul>
  44 *     <li>{@linkplain ResizeMode#NO_RESIZE NO_RESIZE},</li>
  45 *     <li>{@linkplain ResizeMode#PROPORTIONAL_RESIZE PROPORTIONAL_RESIZE},</li>
  46 *     <li>{@linkplain ResizeMode#ARBITRARY_RESIZE ARBITRARY_RESIZE}.</li>
  47  * </ul>
  48  *
  49  * Default value is {@linkplain ResizeMode#PROPORTIONAL_RESIZE}.
  50  * <ul>
  51  * <li>Resize Hint - defines the way of images scaling that is specified when
  52  * {@linkplain java.awt.Image#getScaledInstance(int, int, int) Image.getScaledInstance()} method is invoked.
  53  * One of the Image.SCALE_XXX is expected. Default value is {@linkplain java.awt.Image#SCALE_DEFAULT SCALE_DEFAULT}.</li>
  54  * <li>Proportion Distortion Threshold - defines maximum proportion distortion that is used in
  55  * {@linkplain ResizeMode#PROPORTIONAL_RESIZE PROPORTIONAL_RESIZE} mode to deal with cases when rounding and other
  56  * problems could cause sizes to be not ideally proportional.
  57  * <p>
  58  * Threshold value is applied in the following way: {@code Math.abs(image.getSize() * scale
  59  * - size) / image.getSize() > PROPORTION_DISTORTION_THRESHOLD}, where {@code image.getSize()}
  60  * is both image dimensions and {@code size} is target width and height (which
  61  * is defined as mininum of sizes of two images) and scale is the scale factor
  62  * that scales the particular image to fit the width and height.
  63  * <p>
  64  * Default value is 0.02 so as much as 2% of width/height could differ (around 5
  65  * in 0-255 color component values).</li>
  66  * </ul>
  67  *
  68  * @author mrkam
  69  */
  70 public class ResizeImageComparator extends ResizeComparator {
  71 
  72     /**
  73      * Identifies output where resize details are printed. Output is disabled
  74      * by default.
  75      *
  76      * @see Environment#getOutput(java.lang.String)
  77      */
  78     public static final String OUTPUT = ResizeImageComparator.class.getName()
  79             + ".OUTPUT";
  80 
  81     static {
  82         Environment.getEnvironment().setOutput(OUTPUT, TestOut.getNullOutput());
  83     }
  84     ResizeMode resizeMode;
  85     int hint;
  86     double propDistThreshold;
  87 
  88     /**
  89      * Resize Modes
  90      *
  91      * @see ResizeImageComparator#ResizeImageComparator(ResizeMode, ImageComparator)
  92      * @see ResizeImageComparator#ResizeImageComparator(ResizeMode, double, int, ImageComparator)




  93      */
  94     public static enum ResizeMode {
  95 
  96         /**
  97          * Images are never resized. Original images are always compared.
  98          */
  99         NO_RESIZE,
 100         /**
 101          * Images are resized only if they have exactly or almost proportional
 102          * sizes which is controlled by {@code proportionDistortion} parameter.
 103          * If images have different proportions no resize is done and original
 104          * images are compared.
 105          *
 106          * @see ResizeImageComparator#ResizeImageComparator(ResizeMode, ImageComparator)


 107          */
 108         PROPORTIONAL_RESIZE,
 109         /**
 110          * Images are always resized to match both width and height and then
 111          * compared.
 112          */
 113         ARBITRARY_RESIZE
 114     }
 115 
 116     /**
 117      * Creates ResizeImageComparator with default resize settings:
 118      * <ul>
 119      * <li>resize mode: {@linkplain ResizeMode#PROPORTIONAL_RESIZE ResizeMode.PROPORTIONAL_RESIZE}.</li>
 120      * <li>proportion distortion threshold: 0.02.</li>
 121      * <li>resize hint: {@linkplain java.awt.Image#SCALE_DEFAULT Image.SCALE_DEFAULT}.</li>
 122      * </ul>
 123      *
 124      * @param subComparator comparator to compare images after resize.
 125      * @see java.awt.Image#getScaledInstance(int, int, int)
 126      * @see ResizeMode
 127      * @see ResizeImageComparator
 128      */
 129     public ResizeImageComparator(ImageComparator subComparator) {
 130         super(subComparator, ResizeComparator.Mode.LEFT);
 131         this.resizeMode = ResizeMode.PROPORTIONAL_RESIZE;
 132         this.hint = java.awt.Image.SCALE_DEFAULT;
 133         this.propDistThreshold = 0.02;
 134     }
 135 
 136     /**
 137      * Creates ResizeImageComparator with the specified resize mode and default
 138      * settings for other parameters:
 139      * <ul>
 140      * <li>proportion distortion threshold: 0.02.</li>
 141      * <li>resize hint: {@linkplain java.awt.Image#SCALE_DEFAULT Image.SCALE_DEFAULT}.</li>
 142      * </ul>
 143      *
 144      * @param resizeMode resize mode for this comparator.
 145      * @param subComparator comparator to compare images after resize.
 146      * @see ResizeMode
 147      * @see ResizeImageComparator
 148      */
 149     public ResizeImageComparator(ResizeMode resizeMode,
 150             ImageComparator subComparator) {
 151         this(subComparator);
 152         this.resizeMode = resizeMode;
 153     }
 154 
 155     /**
 156      * Creates ResizeImageComparator with the following settings:
 157      * <ul>
 158      * <li>resize mode: {@linkplain ResizeMode#PROPORTIONAL_RESIZE ResizeMode.PROPORTIONAL_RESIZE}.</li>
 159      * <li>specified proportion distortion threshold.</li>
 160      * <li>resize hint: {@linkplain java.awt.Image#SCALE_DEFAULT Image.SCALE_DEFAULT}.</li>
 161      * </ul>
 162      *
 163      * @param propDistThreshold proportion distortion threshold.
 164      * @param subComparator comparator to compare images after resize.
 165      * @see ResizeImageComparator
 166      */
 167     public ResizeImageComparator(double propDistThreshold,
 168             ImageComparator subComparator) {
 169         this(subComparator);
 170         this.propDistThreshold = propDistThreshold;
 171     }
 172 
 173     /**
 174      * Creates ResizeImageComparator with specified settings.
 175      *
 176      * @param resizeMode Resize mode.
 177      * @param propDistThreshold Proportion distortion threshold.
 178      * @param hint Resize hint.
 179      * @param subComparator comparator to compare images after resize.
 180      * @see ResizeImageComparator
 181      * @see ResizeMode


< prev index next >