1 /* 2 * Copyright (c) 2018, 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 6788458 27 * @summary Test verifies that PNGImageReader takes tRNS chunk values 28 * into consideration while reading non-indexed 8 bit PNG image. 29 * @run main Read8BitPNGWithTRNSChunk 30 */ 31 32 import java.awt.Graphics2D; 33 import java.awt.image.BufferedImage; 34 import java.awt.Color; 35 import java.io.File; 36 import java.io.IOException; 37 import java.nio.file.Files; 38 import java.util.Iterator; 39 import javax.imageio.ImageTypeSpecifier; 40 import javax.imageio.ImageWriter; 41 import javax.imageio.ImageIO; 42 import javax.imageio.ImageWriteParam; 43 import javax.imageio.metadata.IIOMetadata; 44 import javax.imageio.metadata.IIOMetadataNode; 45 import javax.imageio.stream.ImageOutputStream; 46 import javax.imageio.IIOImage; 47 48 public final class Read8BitPNGWithTRNSChunk { 49 50 public static void main(String[] args) throws IOException { 51 String dir = System.getProperty("test.src"); 52 String sep = System.getProperty("file.separator"); 53 String filePath = dir + sep; 54 File directory = new File(filePath); 55 File output = File.createTempFile("output", "png", directory); 56 directory.delete(); 57 ImageOutputStream ios = ImageIO.createImageOutputStream(output); 58 try { 59 // Create 8 bit PNG image and fill it with white color 60 BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR); 61 Graphics2D g2D = img.createGraphics(); 62 63 g2D.setColor(Color.WHITE); 64 g2D.fillRect(0, 0, 1, 1); 65 g2D.dispose(); 66 67 Iterator<ImageWriter> iterWriter = ImageIO.getImageWritersBySuffix("png"); 68 ImageWriter writer = iterWriter.next(); 69 70 ImageWriteParam param = writer.getDefaultWriteParam(); 71 ImageTypeSpecifier specifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR); 72 IIOMetadata metadata = writer.getDefaultImageMetadata(specifier, param); 73 74 // Create tRNS node and merge it with default metadata 75 IIOMetadataNode tRNS_rgb = new IIOMetadataNode("tRNS_RGB"); 76 tRNS_rgb.setAttribute("red", "255"); 77 tRNS_rgb.setAttribute("green", "255"); 78 tRNS_rgb.setAttribute("blue", "255"); 79 80 IIOMetadataNode tRNS = new IIOMetadataNode("tRNS"); 81 tRNS.appendChild(tRNS_rgb); 82 IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0"); 83 root.appendChild(tRNS); 84 85 metadata.mergeTree("javax_imageio_png_1.0", root); 86 87 writer.setOutput(ios); 88 89 writer.write(metadata, new IIOImage(img, null, metadata), param); 90 91 // Read 8 bit PNG RGB image with tRNS chunk and verify 92 BufferedImage display_img = ImageIO.read(output); 93 Color c = new Color(display_img.getRGB(0, 0), true); 94 if (c.getAlpha() != 0) { 95 throw new RuntimeException("PNGImageReader is not using" + 96 " transparent pixel information present in tRNS chunk"); 97 } 98 } finally { 99 ios.close(); 100 Files.delete(output.toPath()); 101 } 102 } 103 } 104