aboutsummaryrefslogtreecommitdiff
path: root/src/solaris/classes/sun/java2d/xr/XRUtils.java
blob: 0b0cecc37cb5b2a3629babfb3aca6b000eed57f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package sun.java2d.xr;

import java.awt.*;
import java.awt.MultipleGradientPaint.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import sun.java2d.loops.*;
import static java.awt.AlphaComposite.*;

/**
 * XRender constants and utility methods.
 *
 * @author Clemens Eisserer
 */

public class XRUtils {
    public static final int None = 0;

    /* Composition Operators */
    public static final byte PictOpClear = 0;
    public static final byte PictOpSrc = 1;
    public static final byte PictOpDst = 2;
    public static final byte PictOpOver = 3;
    public static final byte PictOpOverReverse = 4;
    public static final byte PictOpIn = 5;
    public static final byte PictOpInReverse = 6;
    public static final byte PictOpOut = 7;
    public static final byte PictOpOutReverse = 8;
    public static final byte PictOpAtop = 9;
    public static final byte PictOpAtopReverse = 10;
    public static final byte PictOpXor = 11;
    public static final byte PictOpAdd = 12;
    public static final byte PictOpSaturate = 13;

    /* Repeats */
    public static final int RepeatNone = 0;
    public static final int RepeatNormal = 1;
    public static final int RepeatPad = 2;
    public static final int RepeatReflect = 3;

    /* Interpolation qualities */
    public static final int FAST = 0;
    public static final int GOOD = 1;
    public static final int BEST = 2;
    public static final byte[] FAST_NAME = "fast".getBytes();
    public static final byte[] GOOD_NAME = "good".getBytes();
    public static final byte[] BEST_NAME = "best".getBytes();

    /* PictFormats */
    public static final int PictStandardARGB32 = 0;
    public static final int PictStandardRGB24 = 1;
    public static final int PictStandardA8 = 2;
    public static final int PictStandardA4 = 3;
    public static final int PictStandardA1 = 4;

    /**
     * Maps the specified affineTransformOp to the corresponding XRender image
     * filter.
     */
    public static int ATransOpToXRQuality(int affineTranformOp) {

        switch (affineTranformOp) {
        case AffineTransformOp.TYPE_NEAREST_NEIGHBOR:
            return FAST;

        case AffineTransformOp.TYPE_BILINEAR:
            return GOOD;

        case AffineTransformOp.TYPE_BICUBIC:
            return BEST;
        }

        return -1;
    }

    /**
     * Maps the specified affineTransformOp to the corresponding XRender image
     * filter.
     */
    public static byte[] ATransOpToXRQualityName(int affineTranformOp) {

        switch (affineTranformOp) {
        case AffineTransformOp.TYPE_NEAREST_NEIGHBOR:
            return FAST_NAME;

        case AffineTransformOp.TYPE_BILINEAR:
            return GOOD_NAME;

        case AffineTransformOp.TYPE_BICUBIC:
            return BEST_NAME;
        }

        return null;
    }


    public static byte[] getFilterName(int filterType) {
        switch (filterType) {
        case FAST:
            return FAST_NAME;
        case GOOD:
            return GOOD_NAME;
        case BEST:
            return BEST_NAME;
        }

        return null;
    }


    /**
     * Returns the XRender picture Format which is required to fullfill the
     * Java2D transparency requirement.
     */
    public static int getPictureFormatForTransparency(int transparency) {
        switch (transparency) {
        case Transparency.OPAQUE:
            return PictStandardRGB24;

        case Transparency.BITMASK:
        case Transparency.TRANSLUCENT:
            return PictStandardARGB32;
        }

        return -1;
    }


    public static SurfaceType getXRSurfaceTypeForTransparency(int transparency) {
        if (transparency == Transparency.OPAQUE) {
            return SurfaceType.IntRgb;
        }else {
            return SurfaceType.IntArgbPre;
        }
    }

    /**
     * Maps Java2D CycleMethod to XRender's Repeat property.
     */
    public static int getRepeatForCycleMethod(CycleMethod cycleMethod) {
        if (cycleMethod.equals(CycleMethod.NO_CYCLE)) {
            return RepeatPad;
        } else if (cycleMethod.equals(CycleMethod.REFLECT)) {
            return RepeatReflect;
        } else if (cycleMethod.equals(CycleMethod.REPEAT)) {
            return RepeatNormal;
        }

        return RepeatNone;
    }

    /**
     * Converts a double into an XFixed.
     */
    public static int XDoubleToFixed(double dbl) {
        return (int) (dbl * 65536);
    }

    public static double XFixedToDouble(int fixed) {
        return ((double) fixed) / 65536;
    }

    public static int[] convertFloatsToFixed(float[] values) {
        int[] fixed = new int[values.length];

        for (int i = 0; i < values.length; i++) {
            fixed[i] = XDoubleToFixed(values[i]);
        }

        return fixed;
    }

    public static long intToULong(int signed) {
        if (signed < 0) {
            return ((long) signed) + (((long) Integer.MAX_VALUE) -
                    ((long) Integer.MIN_VALUE) + 1);
        }

        return signed;
    }

    /**
     * Maps the specified Java2D composition rule, to the corresponding XRender
     * composition rule.
     */
    public static byte j2dAlphaCompToXR(int j2dRule) {
        switch (j2dRule) {
        case CLEAR:
            return PictOpClear;

        case SRC:
            return PictOpSrc;

        case DST:
            return PictOpDst;

        case SRC_OVER:
            return PictOpOver;

        case DST_OVER:
            return PictOpOverReverse;

        case SRC_IN:
            return PictOpIn;

        case DST_IN:
            return PictOpInReverse;

        case SRC_OUT:
            return PictOpOut;

        case DST_OUT:
            return PictOpOutReverse;

        case SRC_ATOP:
            return PictOpAtop;

        case DST_ATOP:
            return PictOpAtopReverse;

        case XOR:
            return PictOpXor;
        }

        throw new InternalError("No XRender equivalent available for requested java2d composition rule: "+j2dRule);
    }

    public static short clampToShort(int x) {
        return (short) (x > Short.MAX_VALUE
                           ? Short.MAX_VALUE
                           : (x < Short.MIN_VALUE ? Short.MIN_VALUE : x));
    }

    public static int clampToUShort(int x) {
        return (x > 65535 ? 65535 : (x < 0) ? 0 : x);
    }

    public static boolean isTransformQuadrantRotated(AffineTransform tr) {
        return ((tr.getType() & (AffineTransform.TYPE_GENERAL_ROTATION |
                 AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0);
    }

    public static boolean isMaskEvaluated(byte xrCompRule) {
        switch (xrCompRule) {
        case PictOpOver:
        case PictOpOverReverse:
        case PictOpAtop:
        case PictOpXor:
            return true;
        }

        return false;
    }
}