Path: blob/master/test/jdk/java/awt/Graphics2D/DrawString/AntialiasedTextArtifact.java
41153 views
/*1* Copyright (c) 2016, 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* @bug 801507026* @summary Tests for artifacts around the edges of anti-aliased text27* drawn over translucent background color.28*/29import java.awt.Color;30import java.awt.Font;31import java.awt.Graphics2D;32import java.awt.RenderingHints;33import java.awt.image.BufferedImage;34import java.io.IOException;3536public class AntialiasedTextArtifact {37/* Image dimensions */38private static final int TEST_IMAGE_WIDTH = 2800;39private static final int TEST_IMAGE_HEIGHT = 100;40private static final String TEST_STRING =41"The quick brown fox jumps over the lazy dog. 0123456789.";4243/*44* The artifacts appear when text is drawn ontop of translucent45* background. In other words, a background with alpha channel.46* Hence we test the algorithm for image types that contain either47* straight alpha channel or pre-multiplied alpha channel. In48* addition we test the images with other common pixel formats.49*/50private static final int[] TYPES = {BufferedImage.TYPE_INT_ARGB,51BufferedImage.TYPE_INT_ARGB_PRE,52BufferedImage.TYPE_4BYTE_ABGR,53BufferedImage.TYPE_4BYTE_ABGR_PRE,54BufferedImage.TYPE_INT_RGB,55BufferedImage.TYPE_INT_BGR,56BufferedImage.TYPE_3BYTE_BGR};5758public static void main(String[] args) throws IOException {59/* Iterate over different image types */60for (int type : TYPES) {61BufferedImage testImg = getBufferedImage(type);6263/* Draw anti-aliased string and check for artifacts */64drawAntialiasedString(testImg);65checkArtifact(testImg);66}67}6869private static BufferedImage getBufferedImage(int imageType) {70/* Create a Graphics2D object from the given image type */71BufferedImage image = new BufferedImage(TEST_IMAGE_WIDTH,72TEST_IMAGE_HEIGHT,73imageType);74return image;75}7677private static void drawAntialiasedString(BufferedImage image) {78/* Create Graphics2D object */79Graphics2D graphics = (Graphics2D) image.getGraphics();8081/* Fill the image with translucent color */82graphics.setColor(new Color(127, 127, 127, 127));83graphics.fillRect(0, 0, TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT);8485/* Drawstring with Antialiasing hint */86graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,87RenderingHints.VALUE_TEXT_ANTIALIAS_ON);88Font font = new Font("Verdana" , Font.PLAIN, 60);89graphics.setFont(font);90graphics.setColor(new Color(255, 0, 0));91graphics.drawString(TEST_STRING, 10, 75);92graphics.dispose();93}9495private static void checkArtifact(BufferedImage image) throws IOException {96int componentMask = 0xff;97int colorThreshold = 200;98int rowIndex = 0;99int colIndex = 0;100101/* Loop through every pixel to check for possible artifact */102for (rowIndex = 0; rowIndex < image.getHeight(); rowIndex++) {103for (colIndex = 0; colIndex < image.getWidth(); colIndex++) {104/*105* API: getRGB(x,y) returns color in INT_ARGB color space.106* Extract individual color components with a simple mask.107*/108int colorValue = image.getRGB(colIndex, rowIndex);109int colorComponent1 = colorValue & componentMask;110int colorComponent2 = (colorValue>>8) & componentMask;111int colorComponent3 = (colorValue>>16) & componentMask;112113/*114* Artifacts are predominantly a subjective decision based on115* the quality of the rendered image content. However, in the116* current use-case, the artifacts around the edges of the anti117* aliased text appear like spots of white pixels without any118* relation to the color of foreground text or the background119* translucent shape.120*121* To identify the artifact pixels, each color component from122* the testImage is compared with a constant threshold. The123* component threshold has been set based on observation from124* different experiments on mulitple Java versions.125*/126if (colorComponent1 >= colorThreshold127&& colorComponent2 >= colorThreshold128&& colorComponent3 >= colorThreshold) {129/* Artifact has been noticed. Report error. */130throw new RuntimeException("Test Failed.");131}132}133}134}135}136137138