jdk/test/sun/java2d/OpenGL/GradientPaints.java
author goetz
Tue, 14 Jun 2016 10:44:59 +0200
changeset 39056 d99e63b6d962
parent 37702 82e32f7e8502
permissions -rw-r--r--
8159690: [TESTBUG] Mark headful tests with @key headful. Reviewed-by: simonis, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37702
82e32f7e8502 7102282: TEST_BUG: sun/java2d/OpenGL/GradientPaints.java should be modified
serb
parents: 5506
diff changeset
     2
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 37702
diff changeset
    26
 * @key headful
37702
82e32f7e8502 7102282: TEST_BUG: sun/java2d/OpenGL/GradientPaints.java should be modified
serb
parents: 5506
diff changeset
    27
 * @bug 6521533 6525997 7102282
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Verifies that the OGL-accelerated codepaths for GradientPaint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * LinearGradientPaint, and RadialGradientPaint produce results that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * sufficiently close to those produced by the software codepaths.
37702
82e32f7e8502 7102282: TEST_BUG: sun/java2d/OpenGL/GradientPaints.java should be modified
serb
parents: 5506
diff changeset
    31
 * @run main/othervm -Dsun.java2d.uiScale=1 -Dsun.java2d.opengl=True GradientPaints
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author campbelc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.MultipleGradientPaint.ColorSpaceType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.MultipleGradientPaint.CycleMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.geom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.imageio.ImageIO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class GradientPaints extends Canvas {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final int TESTW = 600;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final int TESTH = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * We expect slight differences in rendering between the OpenGL and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * software pipelines due to algorithmic and rounding differences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The purpose of this test is just to make sure that the OGL pipeline
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * is producing results that are "reasonably" consistent with those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * produced in software, so we will allow +/-TOLERANCE differences
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * in each component.  When comparing the test and reference images,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * we add up the number of pixels that fall outside this tolerance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * range and if the sum is larger than some percentage of the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * number of pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * REMIND: Note that we have separate thresholds for linear and radial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * gradients because the visible differences between OGL and software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * are more apparent in the radial cases.  In the future we should try
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * to reduce the number of mismatches between the two approaches, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * for now the visible differences are slight enough to not cause worry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final int TOLERANCE = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final int ALLOWED_MISMATCHES_LINEAR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        (int)(TESTW * TESTH * 0.18);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final int ALLOWED_MISMATCHES_RADIAL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        (int)(TESTW * TESTH * 0.45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static boolean done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static boolean verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final Color[] COLORS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        new Color(0, 0, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        new Color(128, 128, 128),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        new Color(255, 0, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        new Color(255, 255, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        new Color(0, 255, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        new Color(0, 255, 255),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        new Color(128, 0, 255),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        new Color(128, 128, 128),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static enum PaintType {BASIC, LINEAR, RADIAL};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static enum XformType {IDENTITY, TRANSLATE, SCALE, SHEAR, ROTATE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final int[] numStopsArray = {2, 4, 7};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final Object[] hints = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        RenderingHints.VALUE_ANTIALIAS_OFF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        RenderingHints.VALUE_ANTIALIAS_ON,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private void testOne(BufferedImage refImg, VolatileImage testImg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Graphics2D gref  = refImg.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        Graphics2D gtest = testImg.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        Paint paint =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            makePaint(PaintType.RADIAL, CycleMethod.REPEAT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                      ColorSpaceType.SRGB, XformType.IDENTITY, 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Object aahint = hints[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        renderTest(gref,  paint, aahint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        renderTest(gtest, paint, aahint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Toolkit.getDefaultToolkit().sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        compareImages(refImg, testImg.getSnapshot(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                      TOLERANCE, 0, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        gref.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        gtest.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private void testAll(Graphics gscreen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                         BufferedImage refImg, VolatileImage testImg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Graphics2D gref  = refImg.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Graphics2D gtest = testImg.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        for (PaintType paintType : PaintType.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            for (CycleMethod cycleMethod : CycleMethod.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                for (ColorSpaceType colorSpace : ColorSpaceType.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    for (XformType xform : XformType.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        for (Object aahint : hints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                            for (int numStops : numStopsArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                Paint paint =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                    makePaint(paintType, cycleMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                              colorSpace, xform, numStops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                    "type=" + paintType +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                    " cycleMethod=" + cycleMethod +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                    " colorSpace=" + colorSpace +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                    " xformType=" + xform +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                    " numStops=" + numStops +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                    " aa=" + aahint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                renderTest(gref,  paint, aahint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                renderTest(gtest, paint, aahint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                gscreen.drawImage(testImg, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                Toolkit.getDefaultToolkit().sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                int allowedMismatches =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                    paintType == PaintType.RADIAL ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                    ALLOWED_MISMATCHES_RADIAL :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                    ALLOWED_MISMATCHES_LINEAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                compareImages(refImg, testImg.getSnapshot(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                              TOLERANCE, allowedMismatches,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                              msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        gref.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        gtest.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private Paint makePaint(PaintType paintType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            CycleMethod cycleMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            ColorSpaceType colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                            XformType xformType, int numStops)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int startX   = TESTW/6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        int startY   = TESTH/6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int endX     = TESTW/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int endY     = TESTH/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int ctrX     = TESTW/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int ctrY     = TESTH/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int focusX   = ctrX + 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        int focusY   = ctrY + 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        float radius = 100.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Paint paint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        AffineTransform transform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        Color[] colors = Arrays.copyOf(COLORS, numStops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        float[] fractions = new float[colors.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        for (int i = 0; i < fractions.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            fractions[i] = ((float)i) / (fractions.length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        switch (xformType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        case IDENTITY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            transform = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        case TRANSLATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            transform = AffineTransform.getTranslateInstance(2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        case SCALE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            transform = AffineTransform.getScaleInstance(1.2, 1.4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        case SHEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            transform = AffineTransform.getShearInstance(0.1, 0.1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        case ROTATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            transform = AffineTransform.getRotateInstance(Math.PI / 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                                          getWidth()/2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                                          getHeight()/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        switch (paintType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        case BASIC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            boolean cyclic = (cycleMethod != CycleMethod.NO_CYCLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            paint =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                new GradientPaint(startX, startY, Color.RED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                  endX, endY, Color.BLUE, cyclic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        case LINEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            paint =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                new LinearGradientPaint(new Point2D.Float(startX, startY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                        new Point2D.Float(endX, endY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                        fractions, colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                        cycleMethod, colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                        transform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        case RADIAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            paint =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                new RadialGradientPaint(new Point2D.Float(ctrX, ctrY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                        radius,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                        new Point2D.Float(focusX, focusY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                        fractions, colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                        cycleMethod, colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                        transform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return paint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private void renderTest(Graphics2D g2d, Paint p, Object aahint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        g2d.setColor(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        g2d.fillRect(0, 0, TESTW, TESTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aahint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        g2d.setPaint(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        g2d.fillOval(0, 0, TESTW, TESTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return new Dimension(TESTW, TESTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private static void compareImages(BufferedImage refImg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                      BufferedImage testImg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                      int tolerance, int allowedMismatches,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                      String msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int numMismatches = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        int x1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        int y1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        int x2 = refImg.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int y2 = refImg.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        for (int y = y1; y < y2; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            for (int x = x1; x < x2; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                Color expected = new Color(refImg.getRGB(x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                Color actual   = new Color(testImg.getRGB(x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                if (!isSameColor(expected, actual, tolerance)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    numMismatches++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (numMismatches > allowedMismatches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                ImageIO.write(refImg,  "png",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                              new File("GradientPaints.ref.png"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                ImageIO.write(testImg, "png",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                              new File("GradientPaints.cap.png"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (!verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                System.err.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throw new RuntimeException("Test failed: Number of mismatches (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                       numMismatches +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                       ") exceeds limit (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                       allowedMismatches +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                       ") with tolerance=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                       tolerance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private static boolean isSameColor(Color c1, Color c2, int e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int r1 = c1.getRed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int g1 = c1.getGreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        int b1 = c1.getBlue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int r2 = c2.getRed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        int g2 = c2.getGreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        int b2 = c2.getBlue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        int rmin = Math.max(r2-e, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int gmin = Math.max(g2-e, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        int bmin = Math.max(b2-e, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int rmax = Math.min(r2+e, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int gmax = Math.min(g2+e, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        int bmax = Math.min(b2+e, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (r1 >= rmin && r1 <= rmax &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            g1 >= gmin && g1 <= gmax &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            b1 >= bmin && b1 <= bmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (args.length == 1 && args[0].equals("-verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        GradientPaints test = new GradientPaints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        Frame frame = new Frame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        frame.add(test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        frame.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        frame.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // Wait until the component's been painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        synchronized (test) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    test.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    throw new RuntimeException("Failed: Interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        GraphicsConfiguration gc = frame.getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (gc.getColorModel() instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            System.out.println("IndexColorModel detected: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                               "test considered PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            frame.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        BufferedImage refImg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            new BufferedImage(TESTW, TESTH, BufferedImage.TYPE_INT_RGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        VolatileImage testImg = frame.createVolatileImage(TESTW, TESTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        testImg.validate(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            test.testAll(test.getGraphics(), refImg, testImg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            frame.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}