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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
/*
* Copyright (c) 2016, 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 6334602
* @summary Test verifies that when we create GIF image from Opaque PNG images
* it should not be transparent.
* @modules java.desktop/com.sun.imageio.plugins.gif
* @run main/manual OpaquePNGToGIFTest
*/
import java.awt.image.BufferedImage;
import com.sun.imageio.plugins.gif.GIFImageMetadata;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public final class OpaquePNGToGIFTest extends Frame {
Image img = null;
private Dimension prefImgSize = new Dimension(100, 100);
private Color canvasColor = new Color(0, 255, 0);
File outputFile = null;
ImageCanvas imageCanvas = new ImageCanvas();
FileOutputStream fos;
private static GridBagLayout layout;
private static JPanel mainControlPanel;
private static JPanel resultButtonPanel;
private static JPanel canvasPanel;
private static JLabel instructionText;
private static JButton startTestButton;
private static JButton passButton;
private static JButton failButton;
private static JDialog dialog;
private static Frame instructionFrame;
private static Frame imageFrame;
Toolkit tk;
ImageWriter writer = null;
boolean testPassed, testGeneratedInterrupt, startButtonClicked;
private static Thread mainThread;
public OpaquePNGToGIFTest() throws Exception {
SwingUtilities.invokeAndWait(() -> {
try {
startTest();
} catch (Exception ex) {
Logger.getLogger(OpaquePNGToGIFTest.class.getName()).
log(Level.SEVERE, null, ex);
}
});
mainThread = Thread.currentThread();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
if (!testPassed && testGeneratedInterrupt) {
throw new RuntimeException("Test failed or didnt run"
+ " properly");
}
}
if (!testGeneratedInterrupt) {
if (img != null) {
img.flush();
}
instructionFrame.dispose();
if (startButtonClicked) {
imageFrame.dispose();
}
fos.close();
Files.delete(outputFile.toPath());
throw new RuntimeException("user has not executed the test");
}
}
public void startTest() throws Exception {
instructionFrame = new Frame();
dialog = new JDialog(instructionFrame);
dialog.setTitle("Instruction Dialog");
layout = new GridBagLayout();
mainControlPanel = new JPanel(layout);
resultButtonPanel = new JPanel(layout);
canvasPanel = new JPanel();
GridBagConstraints gbc = new GridBagConstraints();
String instructions
= "<html> INSTRUCTIONS:<br><br>"
+ "After clicking on Start Test button you will see Red<br> "
+ " circle drawn with light blue background, if the circle<br>"
+ " color changes from Red to Green then press button Fail,<br>"
+ " if it stays Red then press button Pass.<br><br></html>";
instructionText = new JLabel();
instructionText.setText(instructions);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainControlPanel.add(instructionText, gbc);
startTestButton = new JButton("Start Test");
startTestButton.setActionCommand("Start Test");
startTestButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
startButtonClicked = true;
imageFrame = new Frame();
Iterator it = ImageIO.getImageWritersByFormatName("GIF");
while (it.hasNext()) {
writer = (ImageWriter) it.next();
break;
}
// read input opaque PNG image.
String fileName = "opaque_input.png";
String sep = System.getProperty("file.separator");
String dir = System.getProperty("test.src", ".");
System.out.println(dir + " " + sep);
String filePath = dir + sep + fileName;
File inputFile = new File(filePath);
ImageInputStream iis = ImageIO.
createImageInputStream(inputFile);
ImageReader reader = null;
Iterator readerIter = ImageIO.getImageReaders(iis);
while (readerIter.hasNext()) {
reader = (ImageReader) readerIter.next();
break;
}
reader.setInput(iis);
IIOMetadata imgData = reader.getImageMetadata(0);
BufferedImage bi = reader.read(0);
//create temporary GIF imageas output
File directory = new File(dir + sep);
outputFile = File.createTempFile("output",
".gif", directory);
createAnimatedImage(bi, imgData,
writer, outputFile);
writer.dispose();
iis.close();
if (outputFile == null) {
throw new RuntimeException("Unable to create output GIF"
+ " file");
}
// extract GIF image using Toolkit and show it on a Panel.
tk = Toolkit.getDefaultToolkit();
img = tk.getImage(dir + sep + outputFile.getName());
directory.delete();
imageCanvas.setBackground(canvasColor);
imageCanvas.setImage(img);
imageCanvas.setPreferredSize(prefImgSize);
canvasPanel.doLayout();
canvasPanel.add(imageCanvas);
imageFrame.add("Center", canvasPanel);
imageFrame.setSize(200, 200);
imageFrame.setVisible(true);
imageFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
img.flush();
instructionFrame.dispose();
imageFrame.dispose();
fail();
} finally {
try {
fos.close();
Files.delete(outputFile.toPath());
} catch (IOException ex) {
Logger.getLogger(OpaquePNGToGIFTest.class.
getName()).log(Level.SEVERE, null,
ex);
}
}
}
});
} catch (IOException ex) {
Logger.getLogger(OpaquePNGToGIFTest.class.getName()).
log(Level.SEVERE, null, ex);
}
}
});
passButton = new JButton("Pass");
passButton.setActionCommand("Pass");
passButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if (img != null) {
img.flush();
}
instructionFrame.dispose();
if (!startButtonClicked) {
fail();
} else {
imageFrame.dispose();
pass();
}
} finally {
try {
fos.close();
Files.delete(outputFile.toPath());
} catch (IOException ex) {
Logger.getLogger(OpaquePNGToGIFTest.class.
getName()).log(Level.SEVERE, null, ex);
}
}
}
});
failButton = new JButton("Fail");
failButton.setActionCommand("Fail");
failButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if (img != null) {
img.flush();
}
instructionFrame.dispose();
if (!startButtonClicked) {
fail();
} else {
imageFrame.dispose();
fail();
}
} finally {
try {
fos.close();
Files.delete(outputFile.toPath());
} catch (IOException ex) {
Logger.getLogger(OpaquePNGToGIFTest.class.
getName()).log(Level.SEVERE, null, ex);
}
}
}
});
gbc.gridx = 1;
gbc.gridy = 0;
resultButtonPanel.add(startTestButton, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
resultButtonPanel.add(passButton, gbc);
gbc.gridx = 3;
gbc.gridy = 0;
resultButtonPanel.add(failButton, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
mainControlPanel.add(resultButtonPanel, gbc);
dialog.add(mainControlPanel);
dialog.setSize(400, 200);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
if (img != null) {
img.flush();
}
instructionFrame.dispose();
if (!startButtonClicked) {
fail();
} else {
imageFrame.dispose();
fail();
}
} finally {
try {
fos.close();
Files.delete(outputFile.toPath());
} catch (IOException ex) {
Logger.getLogger(OpaquePNGToGIFTest.class.
getName()).log(Level.SEVERE, null, ex);
}
}
}
});
}
public synchronized void pass() {
testPassed = true;
testGeneratedInterrupt = true;
mainThread.interrupt();
}
public synchronized void fail() {
testPassed = false;
testGeneratedInterrupt = true;
mainThread.interrupt();
}
public void createAnimatedImage(BufferedImage bi, IIOMetadata metadata,
ImageWriter writer, File outputFile) {
try {
fos = new FileOutputStream(outputFile);
ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
System.out.println(ios);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
IIOMetadata streamData = writer.getDefaultStreamMetadata(param);
writer.prepareWriteSequence(streamData);
ImageTypeSpecifier specify = new ImageTypeSpecifier(bi);
IIOMetadata imgData = writer.convertImageMetadata(
(IIOMetadata) metadata, specify, param);
GIFImageMetadata gifData = setAnimationProperties(imgData);
IIOImage iim = new IIOImage(bi, null, gifData);
param.setProgressiveMode(param.MODE_DISABLED);
writer.writeToSequence(iim, param);
writer.endWriteSequence();
ios.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public GIFImageMetadata setAnimationProperties(IIOMetadata data) {
ArrayList appIDs = new ArrayList();
appIDs.add(new String("NETSCAPE").getBytes());
ArrayList authCodes = new ArrayList();
authCodes.add(new String("2.0").getBytes());
ArrayList appData = new ArrayList();
byte[] authData = {1, 0, 0};
appData.add(authData);
GIFImageMetadata gifData = (GIFImageMetadata) data;
gifData.delayTime = 200;
// If we set disposalMethod to 2 then only the issue is reproduced.
gifData.disposalMethod = 2;
gifData.userInputFlag = false;
gifData.applicationIDs = appIDs;
gifData.authenticationCodes = authCodes;
gifData.applicationData = appData;
return gifData;
}
public static void main(String args[]) throws Exception {
OpaquePNGToGIFTest test = new OpaquePNGToGIFTest();
}
class ImageCanvas extends Canvas {
Image im = null;
public void setImage(Image img) {
im = img;
}
public void clearImage() {
im = null;
repaint();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
if (im != null) {
g2d.drawImage(im, 1, 1, getWidth(), getHeight(), this);
}
}
}
}
|