Skip to content

Commit

Permalink
8341128: open source some 2d graphics tests
Browse files Browse the repository at this point in the history
Reviewed-by: psadhukhan
  • Loading branch information
prrace committed Oct 3, 2024
1 parent 6f459af commit e89fd1d
Show file tree
Hide file tree
Showing 7 changed files with 563 additions and 0 deletions.
87 changes: 87 additions & 0 deletions test/jdk/java/awt/Graphics2D/BasicStrokeValidate.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
169 changes: 169 additions & 0 deletions test/jdk/java/awt/Graphics2D/DrawImageIAETest/DrawImageIAETest.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions test/jdk/java/awt/Graphics2D/ImageRendering/ImageRendering.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

1 comment on commit e89fd1d

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.