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