Path: blob/master/test/jdk/sun/java2d/SunGraphics2D/PolyVertTest.java
41149 views
/*1* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 4678208 4771101 6328481 6588884 819861327* @summary verify the pixelization of degenerate polylines and polygons28* @run main PolyVertTest29* @run main/othervm -Dsun.java2d.d3d=True PolyVertTest -hwonly30*/3132import java.awt.*;33import java.awt.event.*;34import java.awt.geom.*;35import java.awt.image.*;3637public class PolyVertTest {38static int TESTWIDTH;39static int TESTHEIGHT;40static final int REG_TEST_WIDTH = 10;41static final int REG_TEST_HEIGHT = 10;42static final int FULL_TEST_WIDTH = 50;43static final int FULL_TEST_HEIGHT = 200;4445static final int FRINGE = 2;46static final int GREEN = Color.green.getRGB();47static final int RED = Color.red.getRGB();4849static BufferedImage refImg;50static BufferedImage errorImg;51static Graphics errorG;52static Component testCanvas;5354static int totalbadpixels;55static int totalfuzzypixels;56static int numbadtests;57static int numfuzzytests;58static int numframes;59static int fuzzystarty;6061static boolean counting;62static boolean showerrors;63static boolean showresults;64static boolean fringe;65static boolean forceerror;66static boolean fulltest = true;67static boolean hwonly;6869static WindowListener windowCloser = new WindowAdapter() {70public void windowClosing(WindowEvent e) {71e.getWindow().hide();72if (--numframes <= 0) {73System.exit(0);74}75}76};7778public PolyVertTest() {79/*80setBackground(Color.white);81setForeground(Color.black);82*/83}8485static int polypts[][][] = {86{87// void polygon (no points)88{}, {},89},90{91// one point92{ 0 }, { 0 },93},94{95// two points96{ 0, 5 }, { 0, 0 },97{ 0, 0, 6, 1,9810, 0, 6, 1,9920, 0, 6, 1 },100{ 0, 0, 6, 1,10110, 0, 1, 1, 15, 0, 1, 1,10220, 0, 1, 1, 25, 0, 1, 1 },103{ 10, 0, 1, 1,10420, 0, 1, 1 },105},106{107// open triangle108{ 0, 5, 5 }, { 0, 0, 5 },109110{ 0, 0, 6, 1, 5, 1, 1, 5,11111210, 0, 6, 1, 15, 1, 1, 5, 11, 1, 1, 1,11312, 2, 1, 1, 13, 3, 1, 1, 14, 4, 1, 1,11411520, 0, 6, 1, 25, 1, 1, 5, 21, 1, 1, 1,11622, 2, 1, 1, 23, 3, 1, 1, 24, 4, 1, 1 },117118{ 0, 0, 6, 1, 5, 1, 1, 5,11912010, 0, 6, 1, 15, 1, 1, 5, 11, 1, 1, 1,12112, 2, 1, 1, 13, 3, 1, 1, 14, 4, 1, 1,12212320, 0, 6, 1, 25, 1, 1, 5, 21, 1, 1, 1,12422, 2, 1, 1, 23, 3, 1, 1, 24, 4, 1, 1 },125126{ 10, 0, 1, 1,12720, 0, 1, 1 },128},129{130// closed triangle131{ 0, 5, 5, 0 }, { 0, 0, 5, 0 },132133{ 0, 0, 6, 1, 5, 1, 1, 5, 1, 1, 1, 1,1342, 2, 1, 1, 3, 3, 1, 1, 4, 4, 1, 1,13513610, 0, 6, 1, 15, 1, 1, 5, 11, 1, 1, 1,13712, 2, 1, 1, 13, 3, 1, 1, 14, 4, 1, 1,13813920, 0, 6, 1, 25, 1, 1, 5, 21, 1, 1, 1,14022, 2, 1, 1, 23, 3, 1, 1, 24, 4, 1, 1 },141142{ 1, 0, 5, 1, 5, 1, 1, 5, 1, 1, 1, 1,1432, 2, 1, 1, 3, 3, 1, 1, 4, 4, 1, 1,14414510, 0, 6, 1, 15, 1, 1, 5, 11, 1, 1, 1,14612, 2, 1, 1, 13, 3, 1, 1, 14, 4, 1, 1,14714820, 0, 6, 1, 25, 1, 1, 5, 21, 1, 1, 1,14922, 2, 1, 1, 23, 3, 1, 1, 24, 4, 1, 1 },150151{ 0, 0, 1, 1,15210, 0, 1, 1,15320, 0, 1, 1 },154},155{156// empty line157{ 0, 0 }, { 0, 0 },158{ 0, 0, 1, 1,15910, 0, 1, 1,16020, 0, 1, 1 },161},162{163// empty triangle164{ 0, 0, 0 }, { 0, 0, 0 },165{ 0, 0, 1, 1,16610, 0, 1, 1,16720, 0, 1, 1 },168},169};170171public static void render(Graphics2D g2d) {172g2d.setColor(Color.white);173g2d.fillRect(0, 0, TESTWIDTH, TESTHEIGHT);174g2d.setColor(Color.black);175176if (forceerror) {177g2d.fillRect(2, 2, 2, 2);178g2d.fillRect(15, 5, 1, 1);179}180181if (!fulltest) {182g2d.draw(new Rectangle2D.Double(5, 5, 0, 0));183return;184}185186g2d.drawLine(10, 10, 10, 10);187g2d.draw(new Line2D.Double(20, 10, 20, 10));188189g2d.drawRect(10, 20, 0, 0);190g2d.draw(new Rectangle2D.Double(20, 20, 0, 0));191192g2d.setXORMode(Color.white);193194g2d.drawLine(10, 30, 10, 30);195g2d.draw(new Line2D.Double(20, 30, 20, 30));196197g2d.drawRect(10, 40, 0, 0);198g2d.draw(new Rectangle2D.Double(20, 40, 0, 0));199200g2d.setPaintMode();201202int y = 50;203for (int i = 0; i < polypts.length; i++) {204int data[][] = polypts[i];205int xpoints[] = data[0];206int ypoints[] = data[1];207int npoints = xpoints.length;208g2d.translate(10, y);209g2d.drawPolyline(xpoints, ypoints, npoints);210g2d.translate(10, 0);211g2d.drawPolygon(xpoints, ypoints, npoints);212g2d.translate(10, 0);213g2d.draw(new Polygon(xpoints, ypoints, npoints));214g2d.translate(-30, -y);215y += 10;216}217g2d.setXORMode(Color.white);218for (int i = 0; i < polypts.length; i++) {219int data[][] = polypts[i];220int xpoints[] = data[0];221int ypoints[] = data[1];222int npoints = xpoints.length;223g2d.translate(10, y);224g2d.drawPolyline(xpoints, ypoints, npoints);225g2d.translate(10, 0);226g2d.drawPolygon(xpoints, ypoints, npoints);227g2d.translate(10, 0);228g2d.draw(new Polygon(xpoints, ypoints, npoints));229g2d.translate(-30, -y);230y += 10;231}232g2d.setPaintMode();233}234235public Dimension getPreferredSize() {236return new Dimension(500, 500);237}238239public static void usage(int exitcode) {240System.err.println("usage: java PolyVertTest [<option>]*");241System.err.println(" -usage "+242"print this usage summary");243System.err.println(" -count "+244"run all tests and accumulate error counts");245System.err.println(" -forceerror "+246"force at least one error in each test");247System.err.println(" -fringe "+248"draw a yellow fringe around problems");249System.err.println(" -showerrors "+250"display results window for tests with problems");251System.err.println(" -showresults "+252"display results window for all tests");253System.err.println(" -quicktest "+254"only run test cases reported in bug reports");255System.err.println(" -fulltest "+256"run full suite of test cases for a 'unit test'");257System.err.println(" -hwonly "+258"only run tests for screen and VolatileImage");259System.exit(exitcode);260}261262public static void main(String argv[]) {263for (int i = 0; i < argv.length; i++) {264String arg = argv[i];265if (arg.equalsIgnoreCase("-count")) {266counting = true;267} else if (arg.equalsIgnoreCase("-forceerror")) {268forceerror = true;269} else if (arg.equalsIgnoreCase("-fringe")) {270fringe = true;271} else if (arg.equalsIgnoreCase("-showerrors")) {272showerrors = true;273} else if (arg.equalsIgnoreCase("-showresults")) {274showresults = true;275} else if (arg.equalsIgnoreCase("-quicktest")) {276fulltest = false;277} else if (arg.equalsIgnoreCase("-fulltest")) {278fulltest = true;279} else if (arg.equalsIgnoreCase("-hwonly")) {280hwonly = true;281} else if (arg.equalsIgnoreCase("-usage")) {282usage(0);283} else {284System.err.println("unknown option: "+arg);285usage(1);286}287}288289if (fulltest) {290TESTWIDTH = FULL_TEST_WIDTH;291TESTHEIGHT = FULL_TEST_HEIGHT;292} else {293TESTWIDTH = REG_TEST_WIDTH;294TESTHEIGHT = REG_TEST_HEIGHT;295}296297// Prevents premature exit by the WindowAdapter if the user298// closes the last visible results window before we've299// finished our tests.300numframes++;301302makeReferenceImage();303testScreen();304testVolatileImage();305if (!hwonly) {306testBufferedImage();307testOffscreen();308testCompatibleImages();309}310if (totalfuzzypixels > 0) {311System.err.println(totalfuzzypixels+" fuzzy pixels found in "+312numfuzzytests+" tests");313}314if (totalbadpixels > 0) {315throw new RuntimeException(totalbadpixels+" bad pixels found in "+316numbadtests+" tests");317}318System.out.println("Test done - no bad pixels found");319320--numframes;321322if (counting || ((showresults || showerrors) && numframes == 0)) {323System.exit(0);324}325}326327public static void makeReferenceImage() {328refImg = new BufferedImage(TESTWIDTH, TESTHEIGHT,329BufferedImage.TYPE_INT_RGB);330Graphics g = refImg.getGraphics();331332g.setColor(Color.white);333g.fillRect(0, 0, TESTWIDTH, TESTHEIGHT);334335g.setColor(Color.black);336337if (!fulltest) {338g.fillRect(5, 5, 1, 1);339g.dispose();340return;341}342343for (int y = 10; y < 50; y += 10) {344g.fillRect(10, y, 1, 1);345g.fillRect(20, y, 1, 1);346}347int y = 50;348for (int i = 0; i < polypts.length; i++) {349int data[][] = polypts[i];350g.translate(10, y);351if (data.length > 2) {352int rectvals[] = data[2];353for (int j = 0; j < rectvals.length; j += 4) {354g.fillRect(rectvals[j+0], rectvals[j+1],355rectvals[j+2], rectvals[j+3]);356}357}358g.translate(-10, -y);359y += 10;360}361fuzzystarty = y;362for (int i = 0; i < polypts.length; i++) {363int data[][] = polypts[i];364g.translate(10, y);365if (data.length > 2) {366int rectvals[] = data.length > 3 ? data[3] : data[2];367for (int j = 0; j < rectvals.length; j += 4) {368g.fillRect(rectvals[j+0], rectvals[j+1],369rectvals[j+2], rectvals[j+3]);370}371}372g.translate(-10, -y);373y += 10;374}375g.dispose();376}377378public static void initerrorbuf() {379if (errorImg == null) {380droperrorbuf();381errorImg = new BufferedImage(TESTWIDTH, TESTHEIGHT,382BufferedImage.TYPE_INT_RGB);383}384if (errorG == null) {385errorG = errorImg.getGraphics();386}387errorG.setColor(Color.green);388errorG.fillRect(0, 0, TESTWIDTH, TESTHEIGHT);389errorG.setColor(Color.red);390}391392public static void droperrorbuf() {393errorImg = null;394if (errorG != null) {395errorG.dispose();396}397errorG = null;398}399400public static void test(Image img, String name) {401Graphics2D g2d = (Graphics2D) img.getGraphics();402render(g2d);403g2d.dispose();404verify(img, name);405}406407public static void test(BufferedImage bimg, String name) {408Graphics2D g2d = bimg.createGraphics();409render(g2d);410g2d.dispose();411verify(bimg, name);412}413414public static void verify(Image img, String name) {415BufferedImage bimg;416if (img instanceof BufferedImage) {417bimg = (BufferedImage) img;418} else {419bimg = new BufferedImage(TESTWIDTH, TESTHEIGHT,420BufferedImage.TYPE_INT_RGB);421Graphics g = bimg.getGraphics();422g.drawImage(img, 0, 0, null);423g.dispose();424}425verify(bimg, name);426}427428public static boolean isFuzzyPixel(int X, int Y) {429int ytrans = fuzzystarty;430if (!fulltest || Y < ytrans) {431return false;432}433for (int i = 0; i < polypts.length; i++) {434int data[][] = polypts[i];435if (data.length > 4) {436int rectvals[] = data[4];437for (int j = 0; j < rectvals.length; j += 4) {438int rectx = rectvals[j+0] + 10;439int recty = rectvals[j+1] + ytrans;440int rectw = rectvals[j+2];441int recth = rectvals[j+3];442if (X >= rectx && Y >= recty &&443X < rectx + rectw && Y < recty + recth)444{445return true;446}447}448}449ytrans += 10;450}451return false;452}453454public static void verify(BufferedImage bimg, String name) {455int numbadpixels = 0;456int numfuzzypixels = 0;457for (int y = 0; y < TESTHEIGHT; y++) {458for (int x = 0; x < TESTWIDTH; x++) {459if (refImg.getRGB(x, y) != bimg.getRGB(x, y)) {460boolean isfuzzy = isFuzzyPixel(x, y);461if (showerrors || showresults) {462if (errorG == null) {463initerrorbuf();464}465errorG.setColor(isfuzzy ? Color.blue : Color.red);466errorG.fillRect(x, y, 1, 1);467} else if (!counting && !isfuzzy) {468throw new RuntimeException("Error at "+x+", "+y+469" while testing: "+name);470}471if (isfuzzy) {472numfuzzypixels++;473} else {474numbadpixels++;475}476}477}478}479if (numbadpixels > 0 || numfuzzypixels > 0) {480if (numbadpixels > 0) {481totalbadpixels += numbadpixels;482numbadtests++;483}484if (numfuzzypixels > 0) {485totalfuzzypixels += numfuzzypixels;486numfuzzytests++;487}488System.out.println(numbadpixels+" bad pixels and "+489numfuzzypixels+" questionable pixels "+490"found while testing "+name);491if (showerrors || showresults) {492displaydiffs(bimg, name);493}494} else if (showresults) {495if (errorG == null) {496initerrorbuf();497}498displaydiffs(bimg, name);499}500}501502public static void displaydiffs(BufferedImage bimg, String name) {503if (fringe) {504errorG.setColor(Color.yellow);505for (int y = 0; y < TESTHEIGHT; y++) {506for (int x = 0; x < TESTWIDTH; x++) {507if (errorImg.getRGB(x, y) == RED) {508for (int iy = y-FRINGE; iy <= y+FRINGE; iy++) {509for (int ix = x-FRINGE; ix <= x+FRINGE; ix++) {510if (ix >= 0 && ix < TESTWIDTH &&511iy >= 0 && iy < TESTHEIGHT &&512errorImg.getRGB(ix, iy) == GREEN)513{514errorG.fillRect(ix, iy, 1, 1);515}516}517}518}519}520}521}522Frame f = new Frame("Results for "+name);523f.setLayout(new BorderLayout());524f.addWindowListener(windowCloser);525++numframes;526Panel p = new Panel();527p.add(new ImageCanvas(bimg));528p.add(new ImageCanvas(errorImg));529p.add(new ImageCanvas(refImg));530f.add(p, "Center");531droperrorbuf();532f.pack();533f.show();534}535536public static void testBufferedImage() {537testBufferedImage(BufferedImage.TYPE_INT_RGB, "IntXrgb");538testBufferedImage(BufferedImage.TYPE_INT_ARGB, "IntArgb");539testBufferedImage(BufferedImage.TYPE_3BYTE_BGR, "ThreeByte");540testBufferedImage(BufferedImage.TYPE_4BYTE_ABGR, "FourByte");541testBufferedImage(BufferedImage.TYPE_USHORT_555_RGB, "UShort555");542testBufferedImage(BufferedImage.TYPE_BYTE_GRAY, "ByteGray");543testBufferedImage(BufferedImage.TYPE_BYTE_INDEXED, "Indexed");544}545546public static void testBufferedImage(int type, String name) {547BufferedImage bimg = new BufferedImage(TESTWIDTH, TESTHEIGHT, type);548test(bimg, name);549}550551public static void testScreen() {552Frame f = new Frame("PolyVertTest");553TestCanvas child = new TestCanvas();554testCanvas = child;555f.add(child);556f.pack();557f.show();558BufferedImage bimg = child.getImage();559f.hide();560verify(bimg, "Screen");561}562563public static void testOffscreen() {564Image img = testCanvas.createImage(TESTWIDTH, TESTHEIGHT);565test(img, "Offscreen");566}567568public static void testCompatibleImages() {569GraphicsEnvironment genv =570GraphicsEnvironment.getLocalGraphicsEnvironment();571GraphicsDevice gdevs[] = genv.getScreenDevices();572for (int i = 0; i < gdevs.length; i++) {573testCompatibleImages(gdevs[i]);574}575}576577public static void testCompatibleImages(GraphicsDevice gdev) {578GraphicsConfiguration gconfigs[] = gdev.getConfigurations();579for (int i = 0; i < gconfigs.length; i++) {580testCompatibleImages(gconfigs[i]);581}582}583584public static void testCompatibleImages(GraphicsConfiguration gconfig) {585test(gconfig.createCompatibleImage(TESTWIDTH, TESTHEIGHT),586gconfig+".createCompat()");587test(gconfig.createCompatibleImage(TESTWIDTH, TESTHEIGHT,588Transparency.OPAQUE),589gconfig+".createCompat(OPAQUE)");590test(gconfig.createCompatibleImage(TESTWIDTH, TESTHEIGHT,591Transparency.BITMASK),592gconfig+".createCompat(BITMASK)");593test(gconfig.createCompatibleImage(TESTWIDTH, TESTHEIGHT,594Transparency.TRANSLUCENT),595gconfig+".createCompat(TRANSLUCENT)");596test(gconfig.createCompatibleVolatileImage(TESTWIDTH, TESTHEIGHT),597gconfig+".createCompatVolatile()");598}599600public static void testVolatileImage() {601Image img = testCanvas.createVolatileImage(TESTWIDTH, TESTHEIGHT);602test(img, "Volatile");603}604605public static class ImageCanvas extends Canvas {606BufferedImage bimg;607608public ImageCanvas(BufferedImage bimg) {609this.bimg = bimg;610}611612public Dimension getPreferredSize() {613return new Dimension(bimg.getWidth(), bimg.getHeight());614}615616public void paint(Graphics g) {617g.drawImage(bimg, 0, 0, null);618}619}620621public static class TestCanvas extends Canvas {622BufferedImage bimg;623624public Dimension getPreferredSize() {625return new Dimension(TESTWIDTH, TESTHEIGHT);626}627628public void paint(Graphics g) {629if (bimg != null ||630getWidth() < TESTWIDTH ||631getHeight() < TESTHEIGHT)632{633return;634}635render((Graphics2D) g);636Toolkit.getDefaultToolkit().sync();637Point p = getLocationOnScreen();638Rectangle r = new Rectangle(p.x, p.y, TESTWIDTH, TESTHEIGHT);639try {640bimg = new Robot().createScreenCapture(r);641} catch (AWTException e) {642e.printStackTrace();643}644synchronized (this) {645notifyAll();646}647}648649public synchronized BufferedImage getImage() {650while (bimg == null) {651try {652wait();653} catch (InterruptedException e) {654return null;655}656}657return bimg;658}659}660}661662663