Path: blob/master/test/jdk/sun/java2d/marlin/ScaleClipTest.java
41149 views
/*1* Copyright (c) 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*/2223import java.awt.BasicStroke;24import java.awt.Color;25import java.awt.Graphics2D;26import java.awt.RenderingHints;27import java.awt.geom.AffineTransform;28import java.awt.geom.Path2D;29import java.awt.image.BufferedImage;30import java.awt.image.Raster;31import java.io.File;32import java.io.IOException;33import javax.imageio.ImageIO;3435/**36* Scaled Line Clipping rendering test37*38* @test39* @summary verify that scaled line is properly rendered40* @bug 821033541*/42public class ScaleClipTest {4344static final boolean SAVE_IMAGE = false;45static final int SIZE = 50;4647enum SCALE_MODE {48ORTHO,49NON_ORTHO,50COMPLEX51};5253public static void main(String[] args) {5455// First display which renderer is tested:56// JDK9 only:57System.setProperty("sun.java2d.renderer.verbose", "true");5859System.out.println("ScaleClipTest: size = " + SIZE);6061final BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);6263boolean fail = false;6465// testNegativeScale:66for (SCALE_MODE mode : SCALE_MODE.values()) {67try {68testNegativeScale(image, mode);69} catch (IllegalStateException ise) {70System.err.println("testNegativeScale[" + mode + "] failed:");71ise.printStackTrace();72fail = true;73}74}7576// testMarginScale:77for (SCALE_MODE mode : SCALE_MODE.values()) {78try {79testMarginScale(image, mode);80} catch (IllegalStateException ise) {81System.err.println("testMarginScale[" + mode + "] failed:");82ise.printStackTrace();83fail = true;84}85}8687// Fail at the end:88if (fail) {89throw new RuntimeException("ScaleClipTest has failures.");90}91}9293private static void testNegativeScale(final BufferedImage image, final SCALE_MODE mode) {9495final Graphics2D g2d = (Graphics2D) image.getGraphics();96try {97g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);98g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);99g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);100101g2d.setBackground(Color.WHITE);102g2d.clearRect(0, 0, SIZE, SIZE);103104g2d.setColor(Color.BLACK);105106// Bug in TransformingPathConsumer2D.adjustClipScale()107// non ortho scale only108final double scale = -1.0;109110final AffineTransform at;111switch (mode) {112default:113case ORTHO:114at = AffineTransform.getScaleInstance(scale, scale);115break;116case NON_ORTHO:117at = AffineTransform.getScaleInstance(scale, scale + 1e-5);118break;119case COMPLEX:120at = AffineTransform.getScaleInstance(scale, scale);121at.concatenate(AffineTransform.getShearInstance(1e-4, 1e-4));122break;123}124g2d.setTransform(at);125126// Set cap/join to reduce clip margin:127g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));128129final Path2D p = new Path2D.Double();130p.moveTo(scale * 10, scale * 10);131p.lineTo(scale * (SIZE - 10), scale * (SIZE - 10));132133g2d.draw(p);134135if (SAVE_IMAGE) {136try {137final File file = new File("ScaleClipTest-testNegativeScale-" + mode + ".png");138139System.out.println("Writing file: " + file.getAbsolutePath());140ImageIO.write(image, "PNG", file);141} catch (IOException ioe) {142ioe.printStackTrace();143}144}145146// Check image:147// 25, 25 = black148checkPixel(image.getData(), 25, 25, Color.BLACK.getRGB());149150} finally {151g2d.dispose();152}153}154155private static void testMarginScale(final BufferedImage image, final SCALE_MODE mode) {156157final Graphics2D g2d = (Graphics2D) image.getGraphics();158try {159g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);160g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);161g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);162163g2d.setBackground(Color.WHITE);164g2d.clearRect(0, 0, SIZE, SIZE);165166g2d.setColor(Color.BLACK);167168// Bug in Stroker.init()169// ortho scale only: scale used twice !170final double scale = 1e-2;171172final AffineTransform at;173switch (mode) {174default:175case ORTHO:176at = AffineTransform.getScaleInstance(scale, scale);177break;178case NON_ORTHO:179at = AffineTransform.getScaleInstance(scale, scale + 1e-5);180break;181case COMPLEX:182at = AffineTransform.getScaleInstance(scale, scale);183at.concatenate(AffineTransform.getShearInstance(1e-4, 1e-4));184break;185}186g2d.setTransform(at);187188final double invScale = 1.0 / scale;189190// Set cap/join to reduce clip margin:191final float w = (float) (3.0 * invScale);192g2d.setStroke(new BasicStroke(w, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));193194final Path2D p = new Path2D.Double();195p.moveTo(invScale * -0.5, invScale * 10);196p.lineTo(invScale * -0.5, invScale * (SIZE - 10));197198g2d.draw(p);199200if (SAVE_IMAGE) {201try {202final File file = new File("ScaleClipTest-testMarginScale-" + mode + ".png");203204System.out.println("Writing file: " + file.getAbsolutePath());205ImageIO.write(image, "PNG", file);206} catch (IOException ioe) {207ioe.printStackTrace();208}209}210211// Check image:212// 0, 25 = black213checkPixel(image.getData(), 0, 25, Color.BLACK.getRGB());214} finally {215g2d.dispose();216}217}218219private static void checkPixel(final Raster raster,220final int x, final int y,221final int expected) {222223final int[] rgb = (int[]) raster.getDataElements(x, y, null);224225if (rgb[0] != expected) {226throw new IllegalStateException("bad pixel at (" + x + ", " + y227+ ") = " + rgb[0] + " expected: " + expected);228}229}230231}232233234