8341128: open source some 2d graphics tests

Reviewed-by: psadhukhan
This commit is contained in:
Phil Race 2024-10-03 19:22:28 +00:00
parent 6f459aff45
commit e89fd1d2ce
7 changed files with 563 additions and 0 deletions

View File

@ -0,0 +1,87 @@
/*
* Copyright (c) 2000, 2024, 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
* @bug 4363534
* @summary This test verifies that setting a non-thin-line BasicStroke
* on a Graphics2D obtained from a BufferedImage will correctly validate
* the pipelines for the line-widening pipeline even if that is the only
* non-default attribute on the graphics.
*
*/
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public class BasicStrokeValidate {
public static final int TESTW = 100;
public static final int TESTH = 100;
public static void main(String[] args) {
BufferedImage bi1 = createImage(false);
BufferedImage bi2 = createImage(true);
compare(bi1, bi2); // images should differ
}
static BufferedImage createImage(boolean dashed) {
BufferedImage bi = new BufferedImage(TESTW, TESTH, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, TESTW, TESTH);
g2d.setColor(Color.black);
if (dashed) {
g2d.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_MITER, 10.0f,
new float[] {2.5f, 3.5f},
0.0f));
}
g2d.drawRect(10, 10, TESTW-20, TESTH-20);
g2d.setStroke(new BasicStroke(10f));
g2d.drawRect(20, 20, TESTW-40, TESTH-40);
return bi;
}
static void compare(BufferedImage i1, BufferedImage i2) {
boolean same = true;
int w = i1.getWidth(), h = i1.getHeight();
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int p1 = i1.getRGB(x, y);
int p2 = i2.getRGB(x, y);
if (p1 != p2) {
same = false;
}
}
if (!same) {
break;
}
}
if (same) {
throw new RuntimeException("No difference");
}
}
}

View File

@ -0,0 +1,169 @@
/*
* Copyright (c) 1999, 2024, 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
* @bug 4191004
* @summary Tests that no IllegalArgumentException is thrown when calling
* drawImage with certain conditions
* @key headful
*/
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.geom.GeneralPath;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawImageIAETest extends Frame {
static String filename = "/duke.gif";
private volatile Image dimg;
private volatile BufferedImage bimg;
static volatile DrawImageIAETest app;
static volatile JFrame jframe;
static volatile boolean passed = true;
static volatile Exception exception = null;
static volatile CountDownLatch imageLatch = new CountDownLatch(1);
DrawImageIAETest(String title) {
super(title);
}
public static void main(final String[] args) throws Exception {
EventQueue.invokeAndWait(DrawImageIAETest:: createUI);
imageLatch.await(3, TimeUnit.MILLISECONDS);
try {
if (!passed) {
throw new RuntimeException("Test FAILED: exception caught:" + exception);
}
} finally {
if (jframe != null) {
EventQueue.invokeAndWait(jframe::dispose);
}
if (app != null) {
EventQueue.invokeAndWait(app::dispose);
}
}
}
static void createUI() {
app = new DrawImageIAETest("DrawImageIAETest");
app.setLayout (new BorderLayout());
app.setSize(200,200);
app.setLocationRelativeTo(null);
app.setVisible(true);
String file;
try {
String dir = System.getProperty("test.src",
System.getProperty("user.dir"));
file = dir + filename;
} catch (Exception e) {
file = "." + filename;
}
Image textureAlphaSource = null;
MediaTracker tracker = new MediaTracker(app);
app.dimg = Toolkit.getDefaultToolkit().getImage(file);
tracker.addImage(app.dimg, 1);
try {
tracker.waitForAll();
imageLatch.countDown();
} catch (Exception e) {
System.err.println("Can't load images");
}
if (app.dimg == null) {
passed = false;
return;
}
jframe = new JFrame("Test DrawImageIAETest");
jframe.setSize(300, 300);
JPanel jpanel;
jframe.getContentPane().add("Center", jpanel = new JPanel() {
public void paint(Graphics _g) {
Graphics2D g = (Graphics2D)_g;
Dimension d = getSize();
Graphics2D g2 = app.createGraphics2D(d.width, d.height);
app.drawDemo(d.width, d.height, g2);
g2.dispose();
g.drawImage(app.bimg, 0, 0, app);
}
});
jpanel.setSize(140, 140);
jframe.setVisible(true);
}
public void drawDemo(int w, int h, Graphics2D g2) {
GeneralPath p1 = new GeneralPath();
GeneralPath p2 = new GeneralPath();
int dukeX = 73;
int dukeY = 26;
double x = 118;
double y = 17;
double ew = 50;
double eh = 48;
p1.append(new Ellipse2D.Double(x, y, ew, eh), false);
p2.append(new Rectangle2D.Double(x+5, y+5, ew-10, eh-10),false);
g2.setClip(p1);
g2.clip(p2);
try {
g2.drawImage(dimg, dukeX, dukeY, null);
} catch (IllegalArgumentException e) {
passed = false;
exception = e;
}
}
public Graphics2D createGraphics2D(int w, int h) {
Graphics2D g2 = null;
if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) {
bimg = (BufferedImage) createImage(w, h);
}
g2 = bimg.createGraphics();
g2.setBackground(getBackground());
g2.clearRect(0, 0, w, h);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
return g2;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 1999, 2024, 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
* @bug 4203598
* @summary This test verifies that an image with transparent background can be displayed
* correctly with the red background color given.
* The correct display should be the sleeping Duke on a red background.
*
*/
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ImageRendering {
public static void main(String[] args) throws Exception {
String imgName = "snooze.gif";
File file = new File(System.getProperty("test.src", "."), imgName);
BufferedImage image = ImageIO.read(file);
int w = image.getWidth();
int h = image.getHeight();
BufferedImage dest = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = dest.createGraphics();
g2d.drawImage(image, 0, 0, Color.red, null);
int redPixel = Color.red.getRGB();
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int srcPixel = image.getRGB(x, y);
if ((srcPixel & 0x0ff000000) == 0) {
int destPix = dest.getRGB(x, y);
if (destPix != redPixel) {
throw new RuntimeException("Not red at x=" + x +
" y=" + y +
"pix = " + Integer.toHexString(destPix));
}
}
}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -0,0 +1,82 @@
/*
* Copyright (c) 2001, 2024, 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
* @bug 4210466 4417756
* @summary thin lines are not draw correctly under large scales
*/
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.image.BufferedImage;
import java.awt.geom.Ellipse2D;
public class ScaledThinLineTest {
public static void main(String[] args) {
ScaledThinLineTest c1 = new ScaledThinLineTest(200, 200);
ScaledThinLineTest c2 = new ScaledThinLineTest(1, 10000);
ScaledThinLineTest c3 = new ScaledThinLineTest(10000, 1);
ScaledThinLineTest c4 = new ScaledThinLineTest(0.01, 10000);
ScaledThinLineTest c5 = new ScaledThinLineTest(10000, 0.01);
compare(c1.bi, c2.bi);
compare(c2.bi, c3.bi);
compare(c3.bi, c4.bi);
compare(c4.bi, c5.bi);
}
private final Shape shape;
private final double scaleX,scaleY;
private BufferedImage bi = null;
public ScaledThinLineTest(double width, double height) {
shape = new Ellipse2D.Double(0.25*width, 0.25*height, width, height);
scaleX = 200/width;
scaleY = 200/height;
int iw = 300, ih = 300;
bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, iw, ih);
g2.setColor(Color.black);
g2.scale(scaleX,scaleY);
g2.setStroke(new BasicStroke(0));
g2.draw(shape);
}
static void compare(BufferedImage i1, BufferedImage i2) {
int w = i1.getWidth(), h = i1.getHeight();
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int p1 = i1.getRGB(x, y);
int p2 = i2.getRGB(x, y);
if (p1 != p2) {
System.out.println("images differ at " + x + " " + y);
}
}
}
}
}

View File

@ -0,0 +1,159 @@
/*
* Copyright (c) 1999, 2024, 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
* @bug 4190429
* @key headful
* @summary In this bug, text drawing performance should be reasonable.
* And should (per string) be consistent with the size of the
* rectangle in which the string is drawn, not the rectangle
* bounding the whole window.
*/
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class TextPerf extends Canvas {
static volatile CountDownLatch paintLatch = new CountDownLatch(1);
static volatile long paintTime = 5000; // causes test fail if it is not updated.
static volatile Frame frame;
public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait(TextPerf::createUI);
paintLatch.await(5, TimeUnit.SECONDS);
if (paintTime > 2000) {
throw new RuntimeException("Paint time is " + paintTime + "ms");
}
if (frame != null) {
EventQueue.invokeAndWait(frame::dispose);
}
}
static void createUI() {
frame = new Frame("TextPerf");
frame.setLayout(new BorderLayout());
TextPerf tp = new TextPerf();
frame.add(tp, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public Dimension getPreferredSize() {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
return new Dimension(d.width - 50, d.height - 50);
}
static Font[] fonts = {
new Font(Font.SERIF, Font.PLAIN, 10),
new Font(Font.SANS_SERIF, Font.PLAIN, 10),
new Font(Font.MONOSPACED, Font.ITALIC, 10),
new Font(Font.SERIF, Font.PLAIN, 14),
new Font(Font.SERIF, Font.BOLD, 12),
};
public void paint(Graphics g1) {
Graphics2D g = (Graphics2D)g1;
String text = "Hello,_Wgjpqy!";
Toolkit.getDefaultToolkit().sync();
long startTime = System.currentTimeMillis();
FontMetrics[] cachedMetrics = new FontMetrics[fonts.length];
Dimension size = getSize();
int prim = 0;
int spaceWidth = 5;
Color cols[] = { Color.red, Color.blue, Color.yellow,
Color.green, Color.pink, Color.orange} ;
for (int y = 20; y < size.height; y += 20) {
int i = 0;
for (int x = 0; x < size.width; i++) {
Font font = fonts[i % fonts.length];
FontMetrics metrics = cachedMetrics[i % fonts.length];
if (metrics == null) {
metrics = g.getFontMetrics(font);
cachedMetrics[i % fonts.length] = metrics;
}
g.setFont(font);
g.setColor(cols[i % cols.length]);
switch (prim++) {
case 0: g.drawString(text, x, y);
break;
case 1: g.drawBytes(text.getBytes(), 0, text.length(), x, y);
break;
case 2: char[] chs= new char[text.length()];
text.getChars(0,text.length(), chs, 0);
g.drawChars(chs, 0, text.length(), x, y);
break;
case 3: GlyphVector gv = font.createGlyphVector(
g.getFontRenderContext(), text);
g.drawGlyphVector(gv, (float)x, (float)y);
default: prim = 0;
}
x += metrics.stringWidth(text) + spaceWidth;
}
}
// Draw some transformed text to verify correct bounds calculated
AffineTransform at = AffineTransform.getTranslateInstance(50, 50);
at.scale(7.0,7.0);
at.rotate(1.0);
g.transform(at);
g.setColor(Color.black);
Font font = new Font(Font.SERIF, Font.PLAIN, 20);
RenderingHints hints = new RenderingHints(null);
hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHints(hints);
g.setFont(font);
FontMetrics metrics = g.getFontMetrics(font);
g.drawString("Java", 5,5);
Toolkit.getDefaultToolkit().sync();
long endTime = System.currentTimeMillis();
paintTime = endTime - startTime;
String msg = "repainted in " + paintTime + " milliseconds";
System.out.println(msg);
System.out.flush();
paintLatch.countDown();
}
}