1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
/*
* Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4994702
* @key headful
* @summary verifies that no regression were introduced with the fix for this
* bug
*/
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.ImageCapabilities;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.VolatileImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.ImageOutputStream;
public class JPEGsNotAcceleratedTest {
public static final int testRGB = Color.red.getRGB();
public static final int TEST_W = 100;
public static final int TEST_H = 100;
public static final Rectangle roi =
new Rectangle(TEST_W/4, TEST_H/4, TEST_W/2, TEST_H/2);
static Frame f;
static boolean showRes = false;
static int testsFinished = 0;
static int testsStarted = 0;
static boolean lowCompression = false;
static boolean failed = false;
public JPEGsNotAcceleratedTest(String name) {
BufferedImage bi = readTestImage(name, null, null);
runTestOnImage("no dest image, no region of interest", bi, null);
BufferedImage destBI = getDestImage();
bi = readTestImage(name, destBI, null);
runTestOnImage("w/ dest image, no region of interest", bi, null);
// steal the raster, see if the destination image
// gets accelerated
destBI = getDestImage();
DataBuffer db =
((WritableRaster)destBI.getRaster()).getDataBuffer();
bi = readTestImage(name, destBI, null);
runTestOnImage("w/ dest image (with stolen raster),"+
" no region of interest", bi, null);
bi = readTestImage(name, null, roi);
runTestOnImage("no dest image, region of interest", bi, roi);
destBI = getDestImage();
bi = readTestImage(name, destBI, roi);
runTestOnImage("w/ dest image, region of interest", bi, roi);
// accelerate the destination image first, then load
// an image into it. Check that the accelerated copy gets
// updated
destBI = getDestImage();
accelerateImage(destBI);
bi = readTestImage(name, destBI, roi);
runTestOnImage("w/ accelerated dest image,"+
" region of interest", bi, roi);
synchronized (JPEGsNotAcceleratedTest.class) {
testsFinished++;
JPEGsNotAcceleratedTest.class.notify();
}
}
public static BufferedImage readTestImage(String fileName,
BufferedImage dest,
Rectangle srcROI)
{
BufferedImage bi = null;
try {
FileImageInputStream is =
new FileImageInputStream(new File(fileName));
ImageReader reader =
(ImageReader)ImageIO.getImageReaders(is).next();
ImageReadParam param = reader.getDefaultReadParam();
if (dest != null) {
param.setDestination(dest);
}
if (srcROI != null) {
param.setSourceRegion(srcROI);
}
reader.setInput(is);
bi = reader.read(0, param);
} catch (IOException e) {
System.err.println("Error " + e +
" when reading file: " + fileName);
throw new RuntimeException(e);
}
return bi;
}
public static void writeTestImage(String fileName) {
BufferedImage bi =
new BufferedImage(TEST_W, TEST_H, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(new Color(testRGB));
g.fillRect(0, 0, TEST_W, TEST_H);
try {
System.err.printf("Writing %s\n", fileName);
if (lowCompression) {
ImageWriter iw = (ImageWriter)ImageIO.getImageWritersBySuffix("jpeg").next();
if(iw == null) {
throw new RuntimeException("No available image writer for "
+ "jpeg "
+ " Test failed.");
}
File file = new File(fileName);
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
iw.setOutput(ios);
ImageWriteParam param = iw.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(1);
IIOImage iioImg = new IIOImage(bi, null, null);
iw.write(null, iioImg, param);
} else {
ImageIO.write(bi, "jpeg", new File(fileName));
}
} catch (IOException e) {
System.err.println("Error " + e +
" when writing file: " + fileName);
throw new RuntimeException(e);
}
}
public VolatileImage accelerateImage(BufferedImage bi) {
VolatileImage testVI = f.createVolatileImage(TEST_W, TEST_H);
do {
if (testVI.validate(f.getGraphicsConfiguration()) ==
VolatileImage.IMAGE_INCOMPATIBLE)
{
testVI = f.createVolatileImage(TEST_W, TEST_H);
}
Graphics2D g = testVI.createGraphics();
g.setComposite(AlphaComposite.Src);
g.setColor(Color.green);
g.fillRect(0, 0, TEST_W, TEST_H);
g.drawImage(bi, 0, 0, null);
g.drawImage(bi, 0, 0, null);
g.drawImage(bi, 0, 0, null);
g.dispose();
} while (testVI.contentsLost());
return testVI;
}
public BufferedImage getDestImage() {
BufferedImage destBI =
new BufferedImage(TEST_W, TEST_H, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)destBI.getGraphics();
g.setComposite(AlphaComposite.Src);
g.setColor(Color.blue);
g.fillRect(0, 0, TEST_W, TEST_H);
return destBI;
}
public void runTestOnImage(String desc, BufferedImage bi,
Rectangle srcROI)
{
if (srcROI == null) {
srcROI = new Rectangle(0, 0, TEST_W, TEST_H);
}
VolatileImage testVI = accelerateImage(bi);
ImageCapabilities ic =
bi.getCapabilities(f.getGraphicsConfiguration());
boolean accelerated = ic.isAccelerated();
System.err.println("Testing: " + desc +
" -- bi.isAccelerated(): " + accelerated );
BufferedImage snapshot = testVI.getSnapshot();
if (showRes) {
showRes(desc, snapshot);
}
for (int y = 0; y < srcROI.height; y++) {
for (int x = 0; x < srcROI.width; x++) {
int destRGB = snapshot.getRGB(x, y);
if (destRGB != testRGB && destRGB != 0xfffe0000) {
failed = true;
System.err.printf("Test failed at %dx%d pixel=%x\n",
x, y, snapshot.getRGB(x, y));
if (!showRes) {
showRes(desc, snapshot);
}
break;
}
}
}
}
static int startX = 64, startY = 0;
static int frameX = startX, frameY = startY;
private static void showRes(String desc, final BufferedImage src) {
final int w = src.getWidth();
final int h = src.getHeight();
Frame f = new Frame(desc+": dbl-click to exit");
Component c;
f.add(c = new Component() {
public Dimension getPreferredSize() {
return new Dimension(w,h);
}
public void paint(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
g.drawImage(src, 0,0, null);
}
});
c.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1) {
System.exit(0);
}
}
});
f.pack();
synchronized (JPEGsNotAcceleratedTest.class) {
f.setLocation(frameX, frameY);
frameX += f.getWidth();
if ((frameX + f.getWidth()) >
f.getGraphicsConfiguration().getBounds().width)
{
frameY += TEST_H;
if ((frameY + f.getHeight()) >
f.getGraphicsConfiguration().getBounds().height)
{
startY += 30;
startX += 30;
frameY = startY;
}
frameX = startX;
}
};
f.setVisible(true);
}
public static void usage() {
System.err.println("Usage: java Test [file name] [-write][-show][-low]");
System.exit(0);
}
public static void main(String[] args) {
System.setProperty("sun.java2d.pmoffscreen", "true");
System.setProperty("sun.java2d.ddforcevram", "true");
String name = "red.jpg";
f = new Frame();
f.pack();
if (f.getGraphicsConfiguration().getColorModel().getPixelSize() < 16) {
System.err.println("8-bit display mode detected, dithering issues possible, "+
"considering test passed.");
f.dispose();
return;
}
for (String arg : args) {
if (arg.equals("-write")) {
writeTestImage(name);
System.exit(0);
} else if (arg.equals("-show")) {
showRes = true;
} else if (arg.equals("-low")) {
lowCompression = true;
name ="red_low.jpg";
System.err.println("Using low jpeg compression");
} else if (arg.equals("-help")) {
usage();
} else {
final String filename = arg;
testsStarted++;
new Thread(new Runnable() {
public void run() {
new JPEGsNotAcceleratedTest(filename);
}
}).start();
}
}
if (testsStarted == 0) {
writeTestImage(name);
testsStarted++;
new JPEGsNotAcceleratedTest(name);
}
synchronized (JPEGsNotAcceleratedTest.class) {
while (testsFinished < testsStarted) {
try {
JPEGsNotAcceleratedTest.class.wait(100);
} catch (InterruptedException e) {
failed = true; break;
}
}
}
f.dispose();
if (failed) {
throw new RuntimeException("Test failed");
}
System.err.println("Passed.");
}
}
|