Skip to content

Commit 6c91a16

Browse files
committedSep 24, 2024
8340367: Opensource few AWT image tests
Reviewed-by: prr
1 parent 865d99f commit 6c91a16

File tree

5 files changed

+513
-0
lines changed

5 files changed

+513
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2000, 2024, 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 4309915
27+
* @library /java/awt/regtesthelpers
28+
* @build PassFailJFrame
29+
* @summary Check that Antialiased text drawn on a BYTE_GRAY image
30+
* resolves the color correctly
31+
* @run main/manual GrayAATextTest
32+
*/
33+
34+
import java.awt.Color;
35+
import java.awt.Dimension;
36+
import java.awt.Font;
37+
import java.awt.Frame;
38+
import java.awt.Graphics;
39+
import java.awt.Graphics2D;
40+
import java.awt.Panel;
41+
import java.awt.RenderingHints;
42+
import java.awt.image.BufferedImage;
43+
44+
public class GrayAATextTest extends Panel {
45+
46+
public static final int WIDTH = 600;
47+
public static final int HEIGHT = 200;
48+
49+
private static final String INSTRUCTIONS = """
50+
All of the strings in a given column should be drawn
51+
in the same color. If the bug is present, then the
52+
Antialiased strings will all be of a fixed color that
53+
is not the same as the other strings in their column.""";
54+
55+
public static void main(String[] args) throws Exception {
56+
PassFailJFrame.builder()
57+
.title("GrayAATextTest Instructions")
58+
.instructions(INSTRUCTIONS)
59+
.rows((int)INSTRUCTIONS.lines().count() + 2)
60+
.columns(35)
61+
.testUI(GrayAATextTest::createTestUI)
62+
.build()
63+
.awaitAndCheck();
64+
}
65+
66+
public void paint(Graphics g) {
67+
BufferedImage bi = new BufferedImage(WIDTH, HEIGHT,
68+
BufferedImage.TYPE_BYTE_GRAY);
69+
Graphics2D g2d = bi.createGraphics();
70+
g2d.setFont(new Font("Helvetica", Font.PLAIN, 24));
71+
g2d.setColor(Color.white);
72+
g2d.fillRect(0, 0, WIDTH / 2, HEIGHT);
73+
drawText(g2d, Color.black, "Black", 25);
74+
drawText(g2d, Color.lightGray, "Light Gray", 175);
75+
g2d.setColor(Color.black);
76+
g2d.fillRect(WIDTH / 2, 0, WIDTH / 2, HEIGHT);
77+
drawText(g2d, Color.white, "White", 325);
78+
drawText(g2d, Color.lightGray, "Light Gray", 475);
79+
g2d.dispose();
80+
g.drawImage(bi, 0, 0, null);
81+
}
82+
83+
public void drawText(Graphics2D g2d, Color c, String colorname, int x) {
84+
g2d.setColor(c);
85+
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
86+
RenderingHints.VALUE_ANTIALIAS_OFF);
87+
g2d.drawString(colorname, x, 50);
88+
g2d.drawString("Aliased", x, 100);
89+
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
90+
RenderingHints.VALUE_ANTIALIAS_ON);
91+
g2d.drawString("Antialiased", x, 150);
92+
}
93+
94+
public Dimension getPreferredSize() {
95+
return new Dimension(WIDTH, HEIGHT);
96+
}
97+
98+
private static Frame createTestUI() {
99+
Frame f = new Frame("GrayAATextTest Frame");
100+
f.add(new GrayAATextTest());
101+
f.setSize(WIDTH, HEIGHT);
102+
return f;
103+
}
104+
}
+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright (c) 2003, 2024, 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 4243044
27+
* @summary This test should show two windows filled with checker
28+
* board pattern. The transparency should runs from left to right from
29+
* total transparent to total opaque.
30+
* @library /java/awt/regtesthelpers
31+
* @build PassFailJFrame
32+
* @run main/manual GrayAlpha
33+
*/
34+
35+
import java.util.List;
36+
import java.awt.Frame;
37+
import java.awt.color.ColorSpace;
38+
import java.awt.Dimension;
39+
import java.awt.Graphics;
40+
import java.awt.Graphics2D;
41+
import java.awt.geom.AffineTransform;
42+
import java.awt.image.BufferedImage;
43+
import java.awt.image.ColorModel;
44+
import java.awt.image.ComponentColorModel;
45+
import java.awt.image.DataBuffer;
46+
import java.awt.image.Raster;
47+
import java.awt.image.WritableRaster;
48+
import java.awt.Point;
49+
import java.awt.Panel;
50+
import java.awt.Transparency;
51+
import java.awt.Window;
52+
53+
public class GrayAlpha extends Panel {
54+
55+
private static final String INSTRUCTIONS = """
56+
This test should show two windows filled with checker board
57+
vpattern. The transparency should runs from left to right from
58+
totally transparent to totally opaque. If either the pattern or
59+
the transparency is not shown correctly, click Fail, otherwise
60+
click Pass.""";
61+
62+
BufferedImage bi;
63+
AffineTransform identityTransform = new AffineTransform();
64+
65+
public static void main(String[] args) throws Exception {
66+
PassFailJFrame.builder()
67+
.title("GrayAlpha Instructions")
68+
.instructions(INSTRUCTIONS)
69+
.rows((int)INSTRUCTIONS.lines().count() + 2)
70+
.columns(35)
71+
.testUI(GrayAlpha::createTestUI)
72+
.build()
73+
.awaitAndCheck();
74+
}
75+
76+
77+
public GrayAlpha(int width, int height,
78+
boolean hasAlpha, boolean isAlphaPremultiplied,
79+
boolean useRGB) {
80+
boolean isAlphaPremuliplied = true;
81+
int bands = useRGB ? 3 : 1;
82+
bands = hasAlpha ? bands + 1 : bands;
83+
84+
ColorSpace cs = useRGB ?
85+
ColorSpace.getInstance(ColorSpace.CS_sRGB) :
86+
ColorSpace.getInstance(ColorSpace.CS_GRAY);
87+
int transparency = hasAlpha ?
88+
Transparency.TRANSLUCENT : Transparency.OPAQUE;
89+
int[] bits = new int[bands];
90+
for (int i = 0; i < bands; i++) {
91+
bits[i] = 8;
92+
}
93+
94+
ColorModel cm = new ComponentColorModel(cs,
95+
bits,
96+
hasAlpha,
97+
isAlphaPremultiplied,
98+
transparency,
99+
DataBuffer.TYPE_BYTE);
100+
WritableRaster wr =
101+
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
102+
width, height, bands,
103+
new Point(0, 0));
104+
105+
for (int b = 0; b < bands; b++) {
106+
for (int y = 0; y < height; y++) {
107+
for (int x = 0; x < width; x++) {
108+
int s;
109+
110+
if (b != bands - 1 || !hasAlpha) {
111+
// Gray band(s), fill with a checkerboard pattern
112+
if (((x / 10) % 2) == ((y / 10) % 2)) {
113+
s = 255;
114+
} else {
115+
s = 0;
116+
}
117+
if (isAlphaPremultiplied) {
118+
int alpha = (x*255)/(width - 1);
119+
s = (s*alpha)/255;
120+
}
121+
} else {
122+
// Alpha band, increase opacity left to right
123+
s = (x*255)/(width - 1);
124+
}
125+
126+
wr.setSample(x, y, b, s);
127+
}
128+
}
129+
}
130+
131+
this.bi = new BufferedImage(cm, wr, isAlphaPremultiplied, null);
132+
}
133+
134+
public Dimension getPreferredSize() {
135+
return new Dimension(bi.getWidth(), bi.getHeight());
136+
}
137+
138+
public void paint(Graphics g) {
139+
if (bi != null) {
140+
((Graphics2D)g).drawImage(bi, 0, 0, null);
141+
}
142+
}
143+
144+
public static Frame makeFrame(String title,
145+
int x, int y, int width, int height,
146+
boolean hasAlpha,
147+
boolean isAlphaPremultiplied,
148+
boolean useRGB) {
149+
Frame f = new Frame(title);
150+
f.add(new GrayAlpha(width, height,
151+
hasAlpha, isAlphaPremultiplied, useRGB));
152+
f.pack();
153+
f.setLocation(x, y);
154+
return f;
155+
}
156+
157+
private static List<Window> createTestUI() {
158+
int width = 200;
159+
int height = 200;
160+
161+
int x = 100;
162+
int y = 100;
163+
164+
Frame f1 = makeFrame("Gray (non-premultiplied)",
165+
x, y, width, height,
166+
true, false, false);
167+
x += width + 20;
168+
169+
Frame f2 = makeFrame("Gray (premultiplied)",
170+
x, y, width, height,
171+
true, true, false);
172+
173+
return List.of(f1, f2);
174+
}
175+
}

0 commit comments

Comments
 (0)
Failed to load comments.