887
|
1 |
/*
|
|
2 |
* Copyright 2007-2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
/*
|
|
24 |
* @test
|
|
25 |
* @bug 5009033 6603000 6666362
|
|
26 |
* @summary Verifies that images transformed with bilinear filtering do not
|
|
27 |
* leave artifacts at the edges.
|
|
28 |
* @run main/othervm DrawImageBilinear
|
|
29 |
* @run main/othervm -Dsun.java2d.opengl=True DrawImageBilinear
|
|
30 |
* @author campbelc
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.awt.Canvas;
|
|
34 |
import java.awt.Color;
|
|
35 |
import java.awt.Component;
|
|
36 |
import java.awt.Dimension;
|
|
37 |
import java.awt.Frame;
|
|
38 |
import java.awt.Graphics;
|
|
39 |
import java.awt.Graphics2D;
|
|
40 |
import java.awt.GraphicsConfiguration;
|
|
41 |
import java.awt.Point;
|
|
42 |
import java.awt.Rectangle;
|
|
43 |
import java.awt.RenderingHints;
|
|
44 |
import java.awt.Robot;
|
|
45 |
import java.awt.Toolkit;
|
|
46 |
import java.awt.image.BufferedImage;
|
|
47 |
import java.awt.image.IndexColorModel;
|
|
48 |
import java.awt.image.VolatileImage;
|
|
49 |
|
|
50 |
public class DrawImageBilinear extends Canvas {
|
|
51 |
|
|
52 |
private static final int SIZE = 5;
|
|
53 |
|
|
54 |
private static boolean done;
|
|
55 |
private BufferedImage bimg1, bimg2;
|
|
56 |
private VolatileImage vimg;
|
|
57 |
private static volatile BufferedImage capture;
|
|
58 |
private static void doCapture(Component test) {
|
|
59 |
// Grab the screen region
|
|
60 |
try {
|
|
61 |
Robot robot = new Robot();
|
|
62 |
Point pt1 = test.getLocationOnScreen();
|
|
63 |
Rectangle rect =
|
|
64 |
new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());
|
|
65 |
capture = robot.createScreenCapture(rect);
|
|
66 |
} catch (Exception e) {
|
|
67 |
e.printStackTrace();
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
private void renderPattern(Graphics g) {
|
|
72 |
g.setColor(Color.red);
|
|
73 |
g.fillRect(0, 0, SIZE, SIZE);
|
|
74 |
//g.setColor(Color.green);
|
|
75 |
//g.drawRect(0, 0, SIZE-1, SIZE-1);
|
|
76 |
g.dispose();
|
|
77 |
}
|
|
78 |
|
|
79 |
public void paint(Graphics g) {
|
|
80 |
Graphics2D g2d = (Graphics2D)g;
|
|
81 |
|
|
82 |
if (bimg1 == null) {
|
|
83 |
bimg1 = (BufferedImage)createImage(SIZE, SIZE);
|
|
84 |
bimg1.setAccelerationPriority(0.0f);
|
|
85 |
renderPattern(bimg1.createGraphics());
|
|
86 |
|
|
87 |
bimg2 = (BufferedImage)createImage(SIZE, SIZE);
|
|
88 |
renderPattern(bimg2.createGraphics());
|
|
89 |
|
|
90 |
vimg = createVolatileImage(SIZE, SIZE);
|
|
91 |
vimg.validate(getGraphicsConfiguration());
|
|
92 |
renderPattern(vimg.createGraphics());
|
|
93 |
}
|
|
94 |
|
|
95 |
do {
|
|
96 |
g2d.setColor(Color.white);
|
|
97 |
g2d.fillRect(0, 0, getWidth(), getHeight());
|
|
98 |
|
|
99 |
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
|
|
100 |
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
101 |
|
|
102 |
// first time will be a sw->surface blit
|
|
103 |
g2d.drawImage(bimg1, 10, 10, 40, 40, null);
|
|
104 |
|
|
105 |
// second time will be a texture->surface blit
|
|
106 |
g2d.drawImage(bimg2, 80, 10, 40, 40, null);
|
|
107 |
g2d.drawImage(bimg2, 80, 10, 40, 40, null);
|
|
108 |
|
|
109 |
// third time will be a pbuffer->surface blit
|
|
110 |
if (vimg.validate(getGraphicsConfiguration()) != VolatileImage.IMAGE_OK) {
|
|
111 |
renderPattern(vimg.createGraphics());
|
|
112 |
}
|
|
113 |
g2d.drawImage(vimg, 150, 10, 40, 40, null);
|
|
114 |
|
|
115 |
Toolkit.getDefaultToolkit().sync();
|
|
116 |
} while (vimg.contentsLost());
|
|
117 |
|
|
118 |
synchronized (this) {
|
|
119 |
if (!done) {
|
|
120 |
doCapture(this);
|
|
121 |
done = true;
|
|
122 |
}
|
|
123 |
notifyAll();
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
public Dimension getPreferredSize() {
|
|
128 |
return new Dimension(200, 100);
|
|
129 |
}
|
|
130 |
|
|
131 |
private static void testRegion(BufferedImage bi,
|
|
132 |
Rectangle affectedRegion)
|
|
133 |
{
|
|
134 |
int x1 = affectedRegion.x;
|
|
135 |
int y1 = affectedRegion.y;
|
|
136 |
int x2 = x1 + affectedRegion.width;
|
|
137 |
int y2 = y1 + affectedRegion.height;
|
|
138 |
|
|
139 |
for (int y = y1; y < y2; y++) {
|
|
140 |
for (int x = x1; x < x2; x++) {
|
|
141 |
int actual = bi.getRGB(x, y);
|
|
142 |
if ((actual != 0xfffe0000) && (actual != 0xffff0000)) {
|
|
143 |
throw new RuntimeException("Test failed at x="+x+" y="+y+
|
|
144 |
" (expected=0xffff0000"+
|
|
145 |
" actual=0x"+
|
|
146 |
Integer.toHexString(actual) +
|
|
147 |
")");
|
|
148 |
}
|
|
149 |
}
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
public static void main(String[] args) {
|
|
154 |
boolean show = false;
|
|
155 |
for (String arg : args) {
|
|
156 |
if ("-show".equals(arg)) {
|
|
157 |
show = true;
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
DrawImageBilinear test = new DrawImageBilinear();
|
|
162 |
Frame frame = new Frame();
|
|
163 |
frame.add(test);
|
|
164 |
frame.pack();
|
|
165 |
frame.setVisible(true);
|
|
166 |
|
|
167 |
// Wait until the component's been painted
|
|
168 |
synchronized (test) {
|
|
169 |
while (!done) {
|
|
170 |
try {
|
|
171 |
test.wait();
|
|
172 |
} catch (InterruptedException e) {
|
|
173 |
throw new RuntimeException("Failed: Interrupted");
|
|
174 |
}
|
|
175 |
}
|
|
176 |
}
|
|
177 |
|
|
178 |
GraphicsConfiguration gc = frame.getGraphicsConfiguration();
|
|
179 |
if (gc.getColorModel() instanceof IndexColorModel) {
|
|
180 |
System.out.println("IndexColorModel detected: " +
|
|
181 |
"test considered PASSED");
|
|
182 |
frame.dispose();
|
|
183 |
return;
|
|
184 |
}
|
|
185 |
|
|
186 |
if (!show) {
|
|
187 |
frame.dispose();
|
|
188 |
}
|
|
189 |
if (capture == null) {
|
|
190 |
throw new RuntimeException("Failed: capture is null");
|
|
191 |
}
|
|
192 |
|
|
193 |
// Test background color
|
|
194 |
int pixel = capture.getRGB(5, 5);
|
|
195 |
if (pixel != 0xffffffff) {
|
|
196 |
throw new RuntimeException("Failed: Incorrect color for " +
|
|
197 |
"background");
|
|
198 |
}
|
|
199 |
|
|
200 |
// Test pixels
|
|
201 |
testRegion(capture, new Rectangle(10, 10, 40, 40));
|
|
202 |
testRegion(capture, new Rectangle(80, 10, 40, 40));
|
|
203 |
testRegion(capture, new Rectangle(150, 10, 40, 40));
|
|
204 |
}
|
|
205 |
}
|