1 /*
   2  * Copyright (c) 2010, 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 
  26 package sun.java2d.xr;
  27 
  28 import java.awt.*;
  29 import java.awt.geom.AffineTransform;
  30 import java.awt.MultipleGradientPaint.*;
  31 import java.awt.image.*;
  32 import sun.java2d.loops.*;
  33 import static java.awt.AlphaComposite.*;
  34 
  35 /**
  36  * XRender constants and utility methods.
  37  *
  38  * @author Clemens Eisserer
  39  */
  40 
  41 public class XRUtils {
  42     public static final int None = 0;
  43 
  44     /* Composition Operators */
  45     public static final byte PictOpClear = 0;
  46     public static final byte PictOpSrc = 1;
  47     public static final byte PictOpDst = 2;
  48     public static final byte PictOpOver = 3;
  49     public static final byte PictOpOverReverse = 4;
  50     public static final byte PictOpIn = 5;
  51     public static final byte PictOpInReverse = 6;
  52     public static final byte PictOpOut = 7;
  53     public static final byte PictOpOutReverse = 8;
  54     public static final byte PictOpAtop = 9;
  55     public static final byte PictOpAtopReverse = 10;
  56     public static final byte PictOpXor = 11;
  57     public static final byte PictOpAdd = 12;
  58     public static final byte PictOpSaturate = 13;
  59 
  60     /* Repeats */
  61     public static final int RepeatNone = 0;
  62     public static final int RepeatNormal = 1;
  63     public static final int RepeatPad = 2;
  64     public static final int RepeatReflect = 3;
  65 
  66     /* Interpolation qualities */
  67     public static final int FAST = 0;
  68     public static final int GOOD = 1;
  69     public static final int BEST = 2;
  70     public static final byte[] FAST_NAME = "fast".getBytes();
  71     public static final byte[] GOOD_NAME = "good".getBytes();
  72     public static final byte[] BEST_NAME = "best".getBytes();
  73 
  74     /* PictFormats */
  75     public static final int PictStandardARGB32 = 0;
  76     public static final int PictStandardRGB24 = 1;
  77     public static final int PictStandardA8 = 2;
  78     public static final int PictStandardA4 = 3;
  79     public static final int PictStandardA1 = 4;
  80 
  81     /**
  82      * Maps the specified affineTransformOp to the corresponding XRender image
  83      * filter.
  84      */
  85     public static int ATransOpToXRQuality(int affineTranformOp) {
  86 
  87         switch (affineTranformOp) {
  88         case AffineTransformOp.TYPE_NEAREST_NEIGHBOR:
  89             return FAST;
  90 
  91         case AffineTransformOp.TYPE_BILINEAR:
  92             return GOOD;
  93 
  94         case AffineTransformOp.TYPE_BICUBIC:
  95             return BEST;
  96         }
  97 
  98         return -1;
  99     }
 100 
 101     /**
 102      * Maps the specified affineTransformOp to the corresponding XRender image
 103      * filter.
 104      */
 105     public static byte[] ATransOpToXRQualityName(int affineTranformOp) {
 106 
 107         switch (affineTranformOp) {
 108         case AffineTransformOp.TYPE_NEAREST_NEIGHBOR:
 109             return FAST_NAME;
 110 
 111         case AffineTransformOp.TYPE_BILINEAR:
 112             return GOOD_NAME;
 113 
 114         case AffineTransformOp.TYPE_BICUBIC:
 115             return BEST_NAME;
 116         }
 117 
 118         return null;
 119     }
 120 
 121 
 122     public static byte[] getFilterName(int filterType) {
 123         switch (filterType) {
 124         case FAST:
 125             return FAST_NAME;
 126         case GOOD:
 127             return GOOD_NAME;
 128         case BEST:
 129             return BEST_NAME;
 130         }
 131 
 132         return null;
 133     }
 134 
 135 
 136     /**
 137      * Returns the XRender picture Format which is required to fullfill the
 138      * Java2D transparency requirement.
 139      */
 140     public static int getPictureFormatForTransparency(int transparency) {
 141         switch (transparency) {
 142         case Transparency.OPAQUE:
 143             return PictStandardRGB24;
 144 
 145         case Transparency.BITMASK:
 146         case Transparency.TRANSLUCENT:
 147             return PictStandardARGB32;
 148         }
 149 
 150         return -1;
 151     }
 152 
 153 
 154     public static SurfaceType getXRSurfaceTypeForTransparency(int transparency) {
 155         if (transparency == Transparency.OPAQUE) {
 156             return SurfaceType.IntRgb;
 157         }else {
 158             return SurfaceType.IntArgbPre;
 159         }
 160     }
 161 
 162     /**
 163      * Maps Java2D CycleMethod to XRender's Repeat property.
 164      */
 165     public static int getRepeatForCycleMethod(CycleMethod cycleMethod) {
 166         if (cycleMethod.equals(CycleMethod.NO_CYCLE)) {
 167             return RepeatPad;
 168         } else if (cycleMethod.equals(CycleMethod.REFLECT)) {
 169             return RepeatReflect;
 170         } else if (cycleMethod.equals(CycleMethod.REPEAT)) {
 171             return RepeatNormal;
 172         }
 173 
 174         return RepeatNone;
 175     }
 176 
 177     /**
 178      * Converts a double into an XFixed.
 179      */
 180     public static int XDoubleToFixed(double dbl) {
 181         return (int) (dbl * 65536);
 182     }
 183 
 184     public static double XFixedToDouble(int fixed) {
 185         return ((double) fixed) / 65536;
 186     }
 187 
 188     public static int[] convertFloatsToFixed(float[] values) {
 189         int[] fixed = new int[values.length];
 190 
 191         for (int i = 0; i < values.length; i++) {
 192             fixed[i] = XDoubleToFixed(values[i]);
 193         }
 194 
 195         return fixed;
 196     }
 197 
 198     public static long intToULong(int signed) {
 199         if (signed < 0) {
 200             return ((long) signed) + (((long) Integer.MAX_VALUE) -
 201                     ((long) Integer.MIN_VALUE) + 1);
 202         }
 203 
 204         return signed;
 205     }
 206 
 207     /**
 208      * Maps the specified Java2D composition rule, to the corresponding XRender
 209      * composition rule.
 210      */
 211     public static byte j2dAlphaCompToXR(int j2dRule) {
 212         switch (j2dRule) {
 213         case CLEAR:
 214             return PictOpClear;
 215 
 216         case SRC:
 217             return PictOpSrc;
 218 
 219         case DST:
 220             return PictOpDst;
 221 
 222         case SRC_OVER:
 223             return PictOpOver;
 224 
 225         case DST_OVER:
 226             return PictOpOverReverse;
 227 
 228         case SRC_IN:
 229             return PictOpIn;
 230 
 231         case DST_IN:
 232             return PictOpInReverse;
 233 
 234         case SRC_OUT:
 235             return PictOpOut;
 236 
 237         case DST_OUT:
 238             return PictOpOutReverse;
 239 
 240         case SRC_ATOP:
 241             return PictOpAtop;
 242 
 243         case DST_ATOP:
 244             return PictOpAtopReverse;
 245 
 246         case XOR:
 247             return PictOpXor;
 248         }
 249 
 250         throw new InternalError("No XRender equivalent available for requested java2d composition rule: "+j2dRule);
 251     }
 252 
 253     public static short clampToShort(int x) {
 254         return (short) (x > Short.MAX_VALUE
 255                            ? Short.MAX_VALUE
 256                            : (x < Short.MIN_VALUE ? Short.MIN_VALUE : x));
 257     }
 258 
 259     public static int clampToUShort(int x) {
 260         return (x > 65535 ? 65535 : (x < 0) ? 0 : x);
 261     }
 262     
 263     public static boolean isTransformQuadrantRotated(AffineTransform tr) {
 264          return ((tr.getType() & (AffineTransform.TYPE_GENERAL_ROTATION |
 265                  AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0);
 266     }
 267     
 268     public static boolean isMaskEvaluated(byte xrCompRule) {
 269         switch (xrCompRule) {
 270         case PictOpOver:
 271         case PictOpOverReverse:
 272         case PictOpAtop:
 273         case PictOpXor:
 274             return true;
 275         }
 276 
 277         return false;
 278     }
 279 }
--- EOF ---