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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/*
* Copyright (c) 2007, 2018, 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.
*
* 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.
*/
/*
* @test
* @key headful
* @bug 6521533 6525997 7102282 8198613
* @summary Verifies that the accelerated codepaths for GradientPaint,
* LinearGradientPaint, and RadialGradientPaint produce results that are
* sufficiently close to those produced by the software codepaths.
* @run main/othervm -Dsun.java2d.uiScale=1 GradientPaints
* @author campbelc
*/
import java.awt.*;
import java.awt.MultipleGradientPaint.ColorSpaceType;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.File;
import java.util.Arrays;
import javax.imageio.ImageIO;
public class GradientPaints extends Canvas {
private static final int TESTW = 600;
private static final int TESTH = 500;
/*
* We expect slight differences in rendering between the OpenGL and
* software pipelines due to algorithmic and rounding differences.
* The purpose of this test is just to make sure that the OGL pipeline
* is producing results that are "reasonably" consistent with those
* produced in software, so we will allow +/-TOLERANCE differences
* in each component. When comparing the test and reference images,
* we add up the number of pixels that fall outside this tolerance
* range and if the sum is larger than some percentage of the total
* number of pixels.
*
* REMIND: Note that we have separate thresholds for linear and radial
* gradients because the visible differences between OGL and software
* are more apparent in the radial cases. In the future we should try
* to reduce the number of mismatches between the two approaches, but
* for now the visible differences are slight enough to not cause worry.
*/
private static final int TOLERANCE = 5;
private static final int ALLOWED_MISMATCHES_LINEAR =
(int)(TESTW * TESTH * 0.18);
private static final int ALLOWED_MISMATCHES_RADIAL =
(int)(TESTW * TESTH * 0.45);
private static boolean done;
private static boolean verbose;
private static final Color[] COLORS = {
new Color(0, 0, 0),
new Color(128, 128, 128),
new Color(255, 0, 0),
new Color(255, 255, 0),
new Color(0, 255, 0),
new Color(0, 255, 255),
new Color(128, 0, 255),
new Color(128, 128, 128),
};
private static enum PaintType {BASIC, LINEAR, RADIAL};
private static enum XformType {IDENTITY, TRANSLATE, SCALE, SHEAR, ROTATE};
private static final int[] numStopsArray = {2, 4, 7};
private static final Object[] hints = {
RenderingHints.VALUE_ANTIALIAS_OFF,
RenderingHints.VALUE_ANTIALIAS_ON,
};
public void paint(Graphics g) {
synchronized (this) {
if (!done) {
done = true;
notifyAll();
}
}
}
private void testOne(BufferedImage refImg, VolatileImage testImg) {
Graphics2D gref = refImg.createGraphics();
Graphics2D gtest = testImg.createGraphics();
Paint paint =
makePaint(PaintType.RADIAL, CycleMethod.REPEAT,
ColorSpaceType.SRGB, XformType.IDENTITY, 7);
Object aahint = hints[0];
renderTest(gref, paint, aahint);
renderTest(gtest, paint, aahint);
Toolkit.getDefaultToolkit().sync();
compareImages(refImg, testImg.getSnapshot(),
TOLERANCE, 0, "");
gref.dispose();
gtest.dispose();
}
private void testAll(Graphics gscreen,
BufferedImage refImg, VolatileImage testImg)
{
Graphics2D gref = refImg.createGraphics();
Graphics2D gtest = testImg.createGraphics();
for (PaintType paintType : PaintType.values()) {
for (CycleMethod cycleMethod : CycleMethod.values()) {
for (ColorSpaceType colorSpace : ColorSpaceType.values()) {
for (XformType xform : XformType.values()) {
for (Object aahint : hints) {
for (int numStops : numStopsArray) {
Paint paint =
makePaint(paintType, cycleMethod,
colorSpace, xform, numStops);
String msg =
"type=" + paintType +
" cycleMethod=" + cycleMethod +
" colorSpace=" + colorSpace +
" xformType=" + xform +
" numStops=" + numStops +
" aa=" + aahint;
renderTest(gref, paint, aahint);
renderTest(gtest, paint, aahint);
gscreen.drawImage(testImg, 0, 0, null);
Toolkit.getDefaultToolkit().sync();
int allowedMismatches =
paintType == PaintType.RADIAL ?
ALLOWED_MISMATCHES_RADIAL :
ALLOWED_MISMATCHES_LINEAR;
compareImages(refImg, testImg.getSnapshot(),
TOLERANCE, allowedMismatches,
msg);
}
}
}
}
}
}
gref.dispose();
gtest.dispose();
}
private Paint makePaint(PaintType paintType,
CycleMethod cycleMethod,
ColorSpaceType colorSpace,
XformType xformType, int numStops)
{
int startX = TESTW/6;
int startY = TESTH/6;
int endX = TESTW/2;
int endY = TESTH/2;
int ctrX = TESTW/2;
int ctrY = TESTH/2;
int focusX = ctrX + 20;
int focusY = ctrY + 20;
float radius = 100.0f;
Paint paint;
AffineTransform transform;
Color[] colors = Arrays.copyOf(COLORS, numStops);
float[] fractions = new float[colors.length];
for (int i = 0; i < fractions.length; i++) {
fractions[i] = ((float)i) / (fractions.length-1);
}
switch (xformType) {
default:
case IDENTITY:
transform = new AffineTransform();
break;
case TRANSLATE:
transform = AffineTransform.getTranslateInstance(2, 2);
break;
case SCALE:
transform = AffineTransform.getScaleInstance(1.2, 1.4);
break;
case SHEAR:
transform = AffineTransform.getShearInstance(0.1, 0.1);
break;
case ROTATE:
transform = AffineTransform.getRotateInstance(Math.PI / 4,
getWidth()/2,
getHeight()/2);
break;
}
switch (paintType) {
case BASIC:
boolean cyclic = (cycleMethod != CycleMethod.NO_CYCLE);
paint =
new GradientPaint(startX, startY, Color.RED,
endX, endY, Color.BLUE, cyclic);
break;
default:
case LINEAR:
paint =
new LinearGradientPaint(new Point2D.Float(startX, startY),
new Point2D.Float(endX, endY),
fractions, colors,
cycleMethod, colorSpace,
transform);
break;
case RADIAL:
paint =
new RadialGradientPaint(new Point2D.Float(ctrX, ctrY),
radius,
new Point2D.Float(focusX, focusY),
fractions, colors,
cycleMethod, colorSpace,
transform);
break;
}
return paint;
}
private void renderTest(Graphics2D g2d, Paint p, Object aahint) {
g2d.setColor(Color.white);
g2d.fillRect(0, 0, TESTW, TESTH);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aahint);
g2d.setPaint(p);
g2d.fillOval(0, 0, TESTW, TESTH);
}
public Dimension getPreferredSize() {
return new Dimension(TESTW, TESTH);
}
private static void compareImages(BufferedImage refImg,
BufferedImage testImg,
int tolerance, int allowedMismatches,
String msg)
{
int numMismatches = 0;
int x1 = 0;
int y1 = 0;
int x2 = refImg.getWidth();
int y2 = refImg.getHeight();
for (int y = y1; y < y2; y++) {
for (int x = x1; x < x2; x++) {
Color expected = new Color(refImg.getRGB(x, y));
Color actual = new Color(testImg.getRGB(x, y));
if (!isSameColor(expected, actual, tolerance)) {
numMismatches++;
}
}
}
if (verbose) {
System.out.println(msg);
}
if (numMismatches > allowedMismatches) {
try {
ImageIO.write(refImg, "png",
new File("GradientPaints.ref.png"));
ImageIO.write(testImg, "png",
new File("GradientPaints.cap.png"));
} catch (Exception e) {
}
if (!verbose) {
System.err.println(msg);
}
throw new RuntimeException("Test failed: Number of mismatches (" +
numMismatches +
") exceeds limit (" +
allowedMismatches +
") with tolerance=" +
tolerance);
}
}
private static boolean isSameColor(Color c1, Color c2, int e) {
int r1 = c1.getRed();
int g1 = c1.getGreen();
int b1 = c1.getBlue();
int r2 = c2.getRed();
int g2 = c2.getGreen();
int b2 = c2.getBlue();
int rmin = Math.max(r2-e, 0);
int gmin = Math.max(g2-e, 0);
int bmin = Math.max(b2-e, 0);
int rmax = Math.min(r2+e, 255);
int gmax = Math.min(g2+e, 255);
int bmax = Math.min(b2+e, 255);
if (r1 >= rmin && r1 <= rmax &&
g1 >= gmin && g1 <= gmax &&
b1 >= bmin && b1 <= bmax)
{
return true;
}
return false;
}
public static void main(String[] args) {
if (args.length == 1 && args[0].equals("-verbose")) {
verbose = true;
}
GradientPaints test = new GradientPaints();
Frame frame = new Frame();
frame.add(test);
frame.pack();
frame.setVisible(true);
// Wait until the component's been painted
synchronized (test) {
while (!done) {
try {
test.wait();
} catch (InterruptedException e) {
throw new RuntimeException("Failed: Interrupted");
}
}
}
GraphicsConfiguration gc = frame.getGraphicsConfiguration();
if (gc.getColorModel() instanceof IndexColorModel) {
System.out.println("IndexColorModel detected: " +
"test considered PASSED");
frame.dispose();
return;
}
BufferedImage refImg =
new BufferedImage(TESTW, TESTH, BufferedImage.TYPE_INT_RGB);
VolatileImage testImg = frame.createVolatileImage(TESTW, TESTH);
testImg.validate(gc);
try {
test.testAll(test.getGraphics(), refImg, testImg);
} finally {
frame.dispose();
}
}
}
|