Path: blob/master/src/java.desktop/unix/classes/sun/java2d/xr/XRUtils.java
41159 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.xr;2627import java.awt.*;28import java.awt.geom.Point2D;29import java.awt.MultipleGradientPaint.*;30import java.awt.geom.AffineTransform;31import java.awt.image.*;32import sun.java2d.loops.*;33import static java.awt.AlphaComposite.*;3435/**36* XRender constants and utility methods.37*38* @author Clemens Eisserer39*/4041public class XRUtils {42public static final int None = 0;4344/* Composition Operators */45public static final byte PictOpClear = 0;46public static final byte PictOpSrc = 1;47public static final byte PictOpDst = 2;48public static final byte PictOpOver = 3;49public static final byte PictOpOverReverse = 4;50public static final byte PictOpIn = 5;51public static final byte PictOpInReverse = 6;52public static final byte PictOpOut = 7;53public static final byte PictOpOutReverse = 8;54public static final byte PictOpAtop = 9;55public static final byte PictOpAtopReverse = 10;56public static final byte PictOpXor = 11;57public static final byte PictOpAdd = 12;58public static final byte PictOpSaturate = 13;5960/* Repeats */61public static final int RepeatNone = 0;62public static final int RepeatNormal = 1;63public static final int RepeatPad = 2;64public static final int RepeatReflect = 3;6566/* Interpolation qualities */67public static final int FAST = 0;68public static final int GOOD = 1;69public static final int BEST = 2;70public static final byte[] FAST_NAME = "fast".getBytes();71public static final byte[] GOOD_NAME = "good".getBytes();72public static final byte[] BEST_NAME = "best".getBytes();7374/* PictFormats */75public static final int PictStandardARGB32 = 0;76public static final int PictStandardRGB24 = 1;77public static final int PictStandardA8 = 2;78public static final int PictStandardA4 = 3;79public static final int PictStandardA1 = 4;8081/**82* Maps the specified affineTransformOp to the corresponding XRender image83* filter.84*/85public static int ATransOpToXRQuality(int affineTranformOp) {8687switch (affineTranformOp) {88case AffineTransformOp.TYPE_NEAREST_NEIGHBOR:89return FAST;9091case AffineTransformOp.TYPE_BILINEAR:92return GOOD;9394case AffineTransformOp.TYPE_BICUBIC:95return BEST;96}9798return -1;99}100101/**102* Maps the specified affineTransformOp to the corresponding XRender image103* filter.104*/105public static byte[] ATransOpToXRQualityName(int affineTranformOp) {106107switch (affineTranformOp) {108case AffineTransformOp.TYPE_NEAREST_NEIGHBOR:109return FAST_NAME;110111case AffineTransformOp.TYPE_BILINEAR:112return GOOD_NAME;113114case AffineTransformOp.TYPE_BICUBIC:115return BEST_NAME;116}117118return null;119}120121122public static byte[] getFilterName(int filterType) {123switch (filterType) {124case FAST:125return FAST_NAME;126case GOOD:127return GOOD_NAME;128case BEST:129return BEST_NAME;130}131132return null;133}134135136/**137* Returns the XRender picture Format which is required to fullfill the138* Java2D transparency requirement.139*/140public static int getPictureFormatForTransparency(int transparency) {141switch (transparency) {142case Transparency.OPAQUE:143return PictStandardRGB24;144145case Transparency.BITMASK:146case Transparency.TRANSLUCENT:147return PictStandardARGB32;148}149150return -1;151}152153154public static SurfaceType getXRSurfaceTypeForTransparency(int transparency) {155if (transparency == Transparency.OPAQUE) {156return SurfaceType.IntRgb;157}else {158return SurfaceType.IntArgbPre;159}160}161162/**163* Maps Java2D CycleMethod to XRender's Repeat property.164*/165public static int getRepeatForCycleMethod(CycleMethod cycleMethod) {166if (cycleMethod.equals(CycleMethod.NO_CYCLE)) {167return RepeatPad;168} else if (cycleMethod.equals(CycleMethod.REFLECT)) {169return RepeatReflect;170} else if (cycleMethod.equals(CycleMethod.REPEAT)) {171return RepeatNormal;172}173174return RepeatNone;175}176177/**178* Converts a double into an XFixed.179*/180public static int XDoubleToFixed(double dbl) {181return (int) (dbl * 65536);182}183184public static double XFixedToDouble(int fixed) {185return ((double) fixed) / 65536;186}187188public static int[] convertFloatsToFixed(float[] values) {189int[] fixed = new int[values.length];190191for (int i = 0; i < values.length; i++) {192fixed[i] = XDoubleToFixed(values[i]);193}194195return fixed;196}197198public static long intToULong(int signed) {199if (signed < 0) {200return ((long) signed) + (((long) Integer.MAX_VALUE) -201((long) Integer.MIN_VALUE) + 1);202}203204return signed;205}206207/**208* Maps the specified Java2D composition rule, to the corresponding XRender209* composition rule.210*/211public static byte j2dAlphaCompToXR(int j2dRule) {212switch (j2dRule) {213case CLEAR:214return PictOpClear;215216case SRC:217return PictOpSrc;218219case DST:220return PictOpDst;221222case SRC_OVER:223return PictOpOver;224225case DST_OVER:226return PictOpOverReverse;227228case SRC_IN:229return PictOpIn;230231case DST_IN:232return PictOpInReverse;233234case SRC_OUT:235return PictOpOut;236237case DST_OUT:238return PictOpOutReverse;239240case SRC_ATOP:241return PictOpAtop;242243case DST_ATOP:244return PictOpAtopReverse;245246case XOR:247return PictOpXor;248}249250throw new InternalError("No XRender equivalent available for requested java2d composition rule: "+j2dRule);251}252253public static short clampToShort(int x) {254return (short) (x > Short.MAX_VALUE255? Short.MAX_VALUE256: (x < Short.MIN_VALUE ? Short.MIN_VALUE : x));257}258259public static int clampToUShort(int x) {260return (x > 65535 ? 65535 : (x < 0) ? 0 : x);261}262263public static boolean isDoubleInShortRange(double dbl) {264return dbl <= Short.MAX_VALUE && dbl >= Short.MIN_VALUE;265}266267public static boolean isPointCoordInShortRange(Point2D p) {268return isDoubleInShortRange(p.getX()) && isDoubleInShortRange(p.getY());269}270271272public static boolean isTransformQuadrantRotated(AffineTransform tr) {273return ((tr.getType() & (AffineTransform.TYPE_GENERAL_ROTATION |274AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0);275}276277public static boolean isMaskEvaluated(byte xrCompRule) {278switch (xrCompRule) {279case PictOpOver:280case PictOpOverReverse:281case PictOpAtop:282case PictOpXor:283return true;284}285286return false;287}288}289290291