author | prr |
Wed, 29 Nov 2017 09:47:02 -0800 | |
changeset 47982 | 44371ff1ee9a |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
37800
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
1 |
/* |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
4 |
* |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
8 |
* |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
13 |
* accompanied this code). |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
14 |
* |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
18 |
* |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
21 |
* questions. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
22 |
*/ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
23 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
24 |
/* |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
25 |
* @test |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
26 |
* @bug 8015070 |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
27 |
* @summary Tests for artifacts around the edges of anti-aliased text |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
28 |
* drawn over translucent background color. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
29 |
*/ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
30 |
import java.awt.Color; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
31 |
import java.awt.Font; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
32 |
import java.awt.Graphics2D; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
33 |
import java.awt.RenderingHints; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
34 |
import java.awt.image.BufferedImage; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
35 |
import java.io.IOException; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
36 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
37 |
public class AntialiasedTextArtifact { |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
38 |
/* Image dimensions */ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
39 |
private static final int TEST_IMAGE_WIDTH = 2800; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
40 |
private static final int TEST_IMAGE_HEIGHT = 100; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
41 |
private static final String TEST_STRING = |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
42 |
"The quick brown fox jumps over the lazy dog. 0123456789."; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
43 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
44 |
/* |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
45 |
* The artifacts appear when text is drawn ontop of translucent |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
46 |
* background. In other words, a background with alpha channel. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
47 |
* Hence we test the algorithm for image types that contain either |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
48 |
* straight alpha channel or pre-multiplied alpha channel. In |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
49 |
* addition we test the images with other common pixel formats. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
50 |
*/ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
51 |
private static final int[] TYPES = {BufferedImage.TYPE_INT_ARGB, |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
52 |
BufferedImage.TYPE_INT_ARGB_PRE, |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
53 |
BufferedImage.TYPE_4BYTE_ABGR, |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
54 |
BufferedImage.TYPE_4BYTE_ABGR_PRE, |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
55 |
BufferedImage.TYPE_INT_RGB, |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
56 |
BufferedImage.TYPE_INT_BGR, |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
57 |
BufferedImage.TYPE_3BYTE_BGR}; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
58 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
59 |
public static void main(String[] args) throws IOException { |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
60 |
/* Iterate over different image types */ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
61 |
for (int type : TYPES) { |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
62 |
BufferedImage testImg = getBufferedImage(type); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
63 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
64 |
/* Draw anti-aliased string and check for artifacts */ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
65 |
drawAntialiasedString(testImg); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
66 |
checkArtifact(testImg); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
67 |
} |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
68 |
} |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
69 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
70 |
private static BufferedImage getBufferedImage(int imageType) { |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
71 |
/* Create a Graphics2D object from the given image type */ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
72 |
BufferedImage image = new BufferedImage(TEST_IMAGE_WIDTH, |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
73 |
TEST_IMAGE_HEIGHT, |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
74 |
imageType); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
75 |
return image; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
76 |
} |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
77 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
78 |
private static void drawAntialiasedString(BufferedImage image) { |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
79 |
/* Create Graphics2D object */ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
80 |
Graphics2D graphics = (Graphics2D) image.getGraphics(); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
81 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
82 |
/* Fill the image with translucent color */ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
83 |
graphics.setColor(new Color(127, 127, 127, 127)); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
84 |
graphics.fillRect(0, 0, TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
85 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
86 |
/* Drawstring with Antialiasing hint */ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
87 |
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
88 |
RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
89 |
Font font = new Font("Verdana" , Font.PLAIN, 60); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
90 |
graphics.setFont(font); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
91 |
graphics.setColor(new Color(255, 0, 0)); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
92 |
graphics.drawString(TEST_STRING, 10, 75); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
93 |
graphics.dispose(); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
94 |
} |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
95 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
96 |
private static void checkArtifact(BufferedImage image) throws IOException { |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
97 |
int componentMask = 0xff; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
98 |
int colorThreshold = 200; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
99 |
int rowIndex = 0; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
100 |
int colIndex = 0; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
101 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
102 |
/* Loop through every pixel to check for possible artifact */ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
103 |
for (rowIndex = 0; rowIndex < image.getHeight(); rowIndex++) { |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
104 |
for (colIndex = 0; colIndex < image.getWidth(); colIndex++) { |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
105 |
/* |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
106 |
* API: getRGB(x,y) returns color in INT_ARGB color space. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
107 |
* Extract individual color components with a simple mask. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
108 |
*/ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
109 |
int colorValue = image.getRGB(colIndex, rowIndex); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
110 |
int colorComponent1 = colorValue & componentMask; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
111 |
int colorComponent2 = (colorValue>>8) & componentMask; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
112 |
int colorComponent3 = (colorValue>>16) & componentMask; |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
113 |
|
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
114 |
/* |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
115 |
* Artifacts are predominantly a subjective decision based on |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
116 |
* the quality of the rendered image content. However, in the |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
117 |
* current use-case, the artifacts around the edges of the anti |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
118 |
* aliased text appear like spots of white pixels without any |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
119 |
* relation to the color of foreground text or the background |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
120 |
* translucent shape. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
121 |
* |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
122 |
* To identify the artifact pixels, each color component from |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
123 |
* the testImage is compared with a constant threshold. The |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
124 |
* component threshold has been set based on observation from |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
125 |
* different experiments on mulitple Java versions. |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
126 |
*/ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
127 |
if (colorComponent1 >= colorThreshold |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
128 |
&& colorComponent2 >= colorThreshold |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
129 |
&& colorComponent3 >= colorThreshold) { |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
130 |
/* Artifact has been noticed. Report error. */ |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
131 |
throw new RuntimeException("Test Failed."); |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
132 |
} |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
133 |
} |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
134 |
} |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
135 |
} |
518e44c93fbb
8015070: Antialiased text on translucent backgrounds gets bright artifacts
ntv
parents:
diff
changeset
|
136 |
} |