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