2370N/A/*
2685N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2370N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2370N/A *
2370N/A * This code is free software; you can redistribute it and/or modify it
2370N/A * under the terms of the GNU General Public License version 2 only, as
2685N/A * published by the Free Software Foundation. Oracle designates this
2370N/A * particular file as subject to the "Classpath" exception as provided
2685N/A * by Oracle in the LICENSE file that accompanied this code.
2370N/A *
2370N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2370N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2370N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2370N/A * version 2 for more details (a copy is included in the LICENSE file that
2370N/A * accompanied this code).
2370N/A *
2370N/A * You should have received a copy of the GNU General Public License version
2370N/A * 2 along with this work; if not, write to the Free Software Foundation,
2370N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2370N/A *
2685N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2685N/A * or visit www.oracle.com if you need additional information or have any
2685N/A * questions.
2370N/A */
2370N/A
2370N/Apackage sun.java2d.xr;
2370N/A
2370N/Aimport java.awt.*;
2370N/Aimport java.awt.MultipleGradientPaint.*;
2370N/Aimport java.awt.image.*;
2370N/Aimport sun.java2d.loops.*;
2370N/Aimport static java.awt.AlphaComposite.*;
2370N/A
2370N/A/**
2370N/A * XRender constants and utility methods.
2370N/A *
2370N/A * @author Clemens Eisserer
2370N/A */
2370N/A
2370N/Apublic class XRUtils {
2370N/A public static final int None = 0;
2370N/A
2370N/A /* Composition Operators */
2370N/A public static final byte PictOpClear = 0;
2370N/A public static final byte PictOpSrc = 1;
2370N/A public static final byte PictOpDst = 2;
2370N/A public static final byte PictOpOver = 3;
2370N/A public static final byte PictOpOverReverse = 4;
2370N/A public static final byte PictOpIn = 5;
2370N/A public static final byte PictOpInReverse = 6;
2370N/A public static final byte PictOpOut = 7;
2370N/A public static final byte PictOpOutReverse = 8;
2370N/A public static final byte PictOpAtop = 9;
2370N/A public static final byte PictOpAtopReverse = 10;
2370N/A public static final byte PictOpXor = 11;
2370N/A public static final byte PictOpAdd = 12;
2370N/A public static final byte PictOpSaturate = 13;
2370N/A
2370N/A /* Repeats */
2370N/A public static final int RepeatNone = 0;
2370N/A public static final int RepeatNormal = 1;
2370N/A public static final int RepeatPad = 2;
2370N/A public static final int RepeatReflect = 3;
2370N/A
2370N/A /* Interpolation qualities */
2370N/A public static final int FAST = 0;
2370N/A public static final int GOOD = 1;
2370N/A public static final int BEST = 2;
2370N/A public static final byte[] FAST_NAME = "fast".getBytes();
2370N/A public static final byte[] GOOD_NAME = "good".getBytes();
2370N/A public static final byte[] BEST_NAME = "best".getBytes();
2370N/A
2370N/A /* PictFormats */
2370N/A public static final int PictStandardARGB32 = 0;
2370N/A public static final int PictStandardRGB24 = 1;
2370N/A public static final int PictStandardA8 = 2;
2370N/A public static final int PictStandardA4 = 3;
2370N/A public static final int PictStandardA1 = 4;
2370N/A
2370N/A /**
2370N/A * Maps the specified affineTransformOp to the corresponding XRender image
2370N/A * filter.
2370N/A */
2370N/A public static int ATransOpToXRQuality(int affineTranformOp) {
2370N/A
2370N/A switch (affineTranformOp) {
2370N/A case AffineTransformOp.TYPE_NEAREST_NEIGHBOR:
2370N/A return FAST;
2370N/A
2370N/A case AffineTransformOp.TYPE_BILINEAR:
2370N/A return GOOD;
2370N/A
2370N/A case AffineTransformOp.TYPE_BICUBIC:
2370N/A return BEST;
2370N/A }
2370N/A
2370N/A return -1;
2370N/A }
2370N/A
2370N/A /**
2370N/A * Maps the specified affineTransformOp to the corresponding XRender image
2370N/A * filter.
2370N/A */
2370N/A public static byte[] ATransOpToXRQualityName(int affineTranformOp) {
2370N/A
2370N/A switch (affineTranformOp) {
2370N/A case AffineTransformOp.TYPE_NEAREST_NEIGHBOR:
2370N/A return FAST_NAME;
2370N/A
2370N/A case AffineTransformOp.TYPE_BILINEAR:
2370N/A return GOOD_NAME;
2370N/A
2370N/A case AffineTransformOp.TYPE_BICUBIC:
2370N/A return BEST_NAME;
2370N/A }
2370N/A
2370N/A return null;
2370N/A }
2370N/A
2370N/A
2370N/A public static byte[] getFilterName(int filterType) {
2370N/A switch (filterType) {
2370N/A case FAST:
2370N/A return FAST_NAME;
2370N/A case GOOD:
2370N/A return GOOD_NAME;
2370N/A case BEST:
2370N/A return BEST_NAME;
2370N/A }
2370N/A
2370N/A return null;
2370N/A }
2370N/A
2370N/A
2370N/A /**
2370N/A * Returns the XRender picture Format which is required to fullfill the
2370N/A * Java2D transparency requirement.
2370N/A */
2370N/A public static int getPictureFormatForTransparency(int transparency) {
2370N/A switch (transparency) {
2370N/A case Transparency.OPAQUE:
2370N/A return PictStandardRGB24;
2370N/A
2370N/A case Transparency.BITMASK:
2370N/A case Transparency.TRANSLUCENT:
2370N/A return PictStandardARGB32;
2370N/A }
2370N/A
2370N/A return -1;
2370N/A }
2370N/A
2370N/A
2370N/A public static SurfaceType getXRSurfaceTypeForTransparency(int transparency) {
2370N/A if (transparency == Transparency.OPAQUE) {
2370N/A return SurfaceType.IntRgb;
2370N/A }else {
2370N/A return SurfaceType.IntArgbPre;
2370N/A }
2370N/A }
2370N/A
2370N/A /**
2370N/A * Maps Java2D CycleMethod to XRender's Repeat property.
2370N/A */
2370N/A public static int getRepeatForCycleMethod(CycleMethod cycleMethod) {
2370N/A if (cycleMethod.equals(CycleMethod.NO_CYCLE)) {
2370N/A return RepeatPad;
2370N/A } else if (cycleMethod.equals(CycleMethod.REFLECT)) {
2370N/A return RepeatReflect;
2370N/A } else if (cycleMethod.equals(CycleMethod.REPEAT)) {
2370N/A return RepeatNormal;
2370N/A }
2370N/A
2370N/A return RepeatNone;
2370N/A }
2370N/A
2370N/A /**
2370N/A * Converts a double into an XFixed.
2370N/A */
2370N/A public static int XDoubleToFixed(double dbl) {
2370N/A return (int) (dbl * 65536);
2370N/A }
2370N/A
2370N/A public static double XFixedToDouble(int fixed) {
2370N/A return ((double) fixed) / 65536;
2370N/A }
2370N/A
2370N/A public static int[] convertFloatsToFixed(float[] values) {
2370N/A int[] fixed = new int[values.length];
2370N/A
2370N/A for (int i = 0; i < values.length; i++) {
2370N/A fixed[i] = XDoubleToFixed(values[i]);
2370N/A }
2370N/A
2370N/A return fixed;
2370N/A }
2370N/A
2370N/A public static long intToULong(int signed) {
2370N/A if (signed < 0) {
2370N/A return ((long) signed) + (((long) Integer.MAX_VALUE) -
2370N/A ((long) Integer.MIN_VALUE) + 1);
2370N/A }
2370N/A
2370N/A return signed;
2370N/A }
2370N/A
2370N/A /**
2370N/A * Maps the specified Java2D composition rule, to the corresponding XRender
2370N/A * composition rule.
2370N/A */
2370N/A public static byte j2dAlphaCompToXR(int j2dRule) {
2370N/A switch (j2dRule) {
2370N/A case CLEAR:
2370N/A return PictOpClear;
2370N/A
2370N/A case SRC:
2370N/A return PictOpSrc;
2370N/A
2370N/A case DST:
2370N/A return PictOpDst;
2370N/A
2370N/A case SRC_OVER:
2370N/A return PictOpOver;
2370N/A
2370N/A case DST_OVER:
2370N/A return PictOpOverReverse;
2370N/A
2370N/A case SRC_IN:
2370N/A return PictOpIn;
2370N/A
2370N/A case DST_IN:
2370N/A return PictOpInReverse;
2370N/A
2370N/A case SRC_OUT:
2370N/A return PictOpOut;
2370N/A
2370N/A case DST_OUT:
2370N/A return PictOpOutReverse;
2370N/A
2370N/A case SRC_ATOP:
2370N/A return PictOpAtop;
2370N/A
2370N/A case DST_ATOP:
2370N/A return PictOpAtopReverse;
2370N/A
2370N/A case XOR:
2370N/A return PictOpXor;
2370N/A }
2370N/A
2370N/A throw new InternalError("No XRender equivalent available for requested java2d composition rule: "+j2dRule);
2370N/A }
2370N/A
2370N/A public static short clampToShort(int x) {
2370N/A return (short) (x > Short.MAX_VALUE
2370N/A ? Short.MAX_VALUE
2370N/A : (x < Short.MIN_VALUE ? Short.MIN_VALUE : x));
2370N/A }
2370N/A
2370N/A public static short clampToUShort(int x) {
2370N/A return (short) (x > 65535 ? 65535 : (x < 0) ? 0 : x);
2370N/A }
2370N/A}