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