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 Gray PNG images. 29 * @run main ReadPngGrayImageWithTRNSChunk 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 class ReadPngGrayImageWithTRNSChunk { 49 50 private static ImageOutputStream ios; 51 private static File output; 52 private static IIOMetadataNode root; 53 private static BufferedImage img; 54 private static ImageWriter writer; 55 private static ImageWriteParam param; 56 private static IIOMetadata metadata; 57 58 private static void initialize(int type) { 59 int width = 2; 60 int height = 1; 61 img = new BufferedImage(width, height, type); 62 Graphics2D g2D = img.createGraphics(); 63 64 // transparent first pixel 65 g2D.setColor(new Color(255, 255, 255)); 66 g2D.fillRect(0, 0, 1, 1); 67 // non-transparent second pixel 68 g2D.setColor(new Color(128, 128,128)); 69 g2D.fillRect(1, 0, 1, 1); 70 g2D.dispose(); 71 72 Iterator<ImageWriter> iterWriter = 73 ImageIO.getImageWritersBySuffix("png"); 74 writer = iterWriter.next(); 75 76 param = writer.getDefaultWriteParam(); 77 ImageTypeSpecifier specifier = 78 ImageTypeSpecifier. 79 createFromBufferedImageType(type); 80 metadata = writer.getDefaultImageMetadata(specifier, param); 81 } 82 83 private static void createOutputStream() throws IOException { 84 String dir = System.getProperty("test.src"); 85 String sep = System.getProperty("file.separator"); 86 String filePath = dir + sep; 87 File directory = new File(filePath); 88 output = File.createTempFile("output", "png", directory); 89 directory.delete(); 90 ios = ImageIO.createImageOutputStream(output); 91 } 92 93 private static void createTRNSNode(String tRNS_value) { 94 IIOMetadataNode tRNS_gray = new IIOMetadataNode("tRNS_Grayscale"); 95 tRNS_gray.setAttribute("gray", tRNS_value); 96 97 IIOMetadataNode tRNS = new IIOMetadataNode("tRNS"); 98 tRNS.appendChild(tRNS_gray); 99 root = new IIOMetadataNode("javax_imageio_png_1.0"); 100 root.appendChild(tRNS); 101 } 102 103 private static boolean verifyAlphaValue(BufferedImage img) { 104 Color firstPixel = new Color(img.getRGB(0, 0), true); 105 Color secondPixel = new Color(img.getRGB(1, 0), true); 106 107 return firstPixel.getAlpha() != 0 || 108 secondPixel.getAlpha() != 255; 109 } 110 111 private static boolean read8BitGrayPNGWithTRNSChunk() throws IOException { 112 initialize(BufferedImage.TYPE_BYTE_GRAY); 113 // Create tRNS node and merge it with default metadata 114 createTRNSNode("255"); 115 116 metadata.mergeTree("javax_imageio_png_1.0", root); 117 118 writer.setOutput(ios); 119 120 writer.write(metadata, new IIOImage(img, null, metadata), param); 121 122 // Read 8 bit PNG Gray image with tRNS chunk 123 BufferedImage display_img = ImageIO.read(output); 124 // Verify alpha values present in first & second pixel 125 return verifyAlphaValue(display_img); 126 } 127 128 private static boolean read16BitGrayPNGWithTRNSChunk() throws IOException { 129 initialize(BufferedImage.TYPE_USHORT_GRAY); 130 // Create tRNS node and merge it with default metadata 131 createTRNSNode("65535"); 132 133 metadata.mergeTree("javax_imageio_png_1.0", root); 134 135 writer.setOutput(ios); 136 137 writer.write(metadata, new IIOImage(img, null, metadata), param); 138 139 // Read 16 bit PNG Gray image with tRNS chunk 140 BufferedImage display_img = ImageIO.read(output); 141 // Verify alpha values present in first & second pixel 142 return verifyAlphaValue(display_img); 143 } 144 145 public static void main(String[] args) throws IOException { 146 // read 8 bit PNG Gray image with tRNS chunk 147 boolean read8BitFail, read16BitFail; 148 try { 149 createOutputStream(); 150 read8BitFail = read8BitGrayPNGWithTRNSChunk(); 151 } finally { 152 ios.close(); 153 Files.delete(output.toPath()); 154 } 155 156 // read 16 bit PNG Gray image with tRNS chunk 157 try { 158 createOutputStream(); 159 read16BitFail = read16BitGrayPNGWithTRNSChunk(); 160 } finally { 161 ios.close(); 162 Files.delete(output.toPath()); 163 } 164 165 if (read8BitFail || read16BitFail) { 166 throw new RuntimeException("PNGImageReader is not using" + 167 " transparent pixel information from tRNS chunk properly"); 168 } 169 } 170 }