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 RGB PNG images. 29 * @run main ReadPngRGBImageWithTRNSChunk 30 */ 31 32 import java.awt.Graphics2D; 33 import java.awt.image.BufferedImage; 34 import java.awt.Color; 35 import java.io.ByteArrayInputStream; 36 import java.io.ByteArrayOutputStream; 37 import java.io.IOException; 38 import java.io.InputStream; 39 import java.util.Iterator; 40 import javax.imageio.ImageTypeSpecifier; 41 import javax.imageio.ImageWriter; 42 import javax.imageio.ImageIO; 43 import javax.imageio.ImageWriteParam; 44 import javax.imageio.metadata.IIOInvalidTreeException; 45 import javax.imageio.metadata.IIOMetadata; 46 import javax.imageio.metadata.IIOMetadataNode; 47 import javax.imageio.stream.ImageOutputStream; 48 import javax.imageio.IIOImage; 49 import java.awt.image.DataBuffer; 50 import java.awt.image.DataBufferUShort; 51 import java.awt.image.WritableRaster; 52 import java.awt.image.Raster; 53 import java.awt.color.ColorSpace; 54 import java.awt.image.ColorModel; 55 import java.awt.image.ComponentColorModel; 56 import java.awt.Transparency; 57 58 public class ReadPngRGBImageWithTRNSChunk { 59 60 private static BufferedImage img; 61 private static IIOMetadata metadata; 62 private static ImageWriteParam param; 63 private static ImageWriter writer; 64 private static byte[] imageByteArray; 65 66 private static void createTRNSNode(String tRNS_value) 67 throws IIOInvalidTreeException { 68 IIOMetadataNode tRNS_rgb = new IIOMetadataNode("tRNS_RGB"); 69 tRNS_rgb.setAttribute("red", tRNS_value); 70 tRNS_rgb.setAttribute("green", tRNS_value); 71 tRNS_rgb.setAttribute("blue", tRNS_value); 72 73 IIOMetadataNode tRNS = new IIOMetadataNode("tRNS"); 74 tRNS.appendChild(tRNS_rgb); 75 IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0"); 76 root.appendChild(tRNS); 77 metadata.mergeTree("javax_imageio_png_1.0", root); 78 } 79 80 private static void writeImage() throws IOException { 81 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 82 ImageOutputStream ios = ImageIO.createImageOutputStream(baos); 83 writer.setOutput(ios); 84 writer.write(metadata, new IIOImage(img, null, metadata), param); 85 writer.dispose(); 86 87 baos.flush(); 88 imageByteArray = baos.toByteArray(); 89 baos.close(); 90 } 91 92 private static boolean verifyAlphaValue(BufferedImage img) { 93 Color firstPixel = new Color(img.getRGB(0, 0), true); 94 Color secondPixel = new Color(img.getRGB(1, 0), true); 95 96 return firstPixel.getAlpha() != 0 || 97 secondPixel.getAlpha() != 255; 98 } 99 100 private static boolean read8BitRGBPNGWithTRNSChunk() throws IOException { 101 int width = 2; 102 int height = 1; 103 // Create 8 bit PNG image 104 img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); 105 Graphics2D g2D = img.createGraphics(); 106 107 // transparent first pixel 108 g2D.setColor(Color.WHITE); 109 g2D.fillRect(0, 0, 1, 1); 110 // non-transparent second pixel 111 g2D.setColor(Color.RED); 112 g2D.fillRect(1, 0, 1, 1); 113 g2D.dispose(); 114 115 Iterator<ImageWriter> iterWriter = 116 ImageIO.getImageWritersBySuffix("png"); 117 writer = iterWriter.next(); 118 119 param = writer.getDefaultWriteParam(); 120 ImageTypeSpecifier specifier = 121 ImageTypeSpecifier. 122 createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR); 123 metadata = writer.getDefaultImageMetadata(specifier, param); 124 125 // Create tRNS node and merge it with default metadata 126 createTRNSNode("255"); 127 128 writeImage(); 129 130 InputStream input= new ByteArrayInputStream(imageByteArray); 131 // Read 8 bit PNG RGB image with tRNS chunk 132 BufferedImage verify_img = ImageIO.read(input); 133 input.close(); 134 // Verify alpha values present in first & second pixel 135 return verifyAlphaValue(verify_img); 136 } 137 138 private static boolean read16BitRGBPNGWithTRNSChunk() throws IOException { 139 // Create 16 bit PNG image 140 int height = 1; 141 int width = 2; 142 int numBands = 3; 143 int shortArrayLength = width * height * numBands; 144 short[] pixelData = new short[shortArrayLength]; 145 // transparent first pixel 146 pixelData[0] = (short)0xffff; 147 pixelData[1] = (short)0xffff; 148 pixelData[2] = (short)0xffff; 149 // non-transparent second pixel 150 pixelData[3] = (short)0xffff; 151 pixelData[4] = (short)0xffff; 152 pixelData[5] = (short)0xfffe; 153 154 DataBuffer buffer = new DataBufferUShort(pixelData, shortArrayLength); 155 156 int[] bandOffset = {0, 1 ,2}; 157 WritableRaster ras = 158 Raster.createInterleavedRaster(buffer, width, height, 159 width * numBands, numBands, bandOffset, null); 160 161 int nBits[] = {16, 16 ,16}; 162 ColorModel colorModel = new 163 ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 164 nBits, false, false, Transparency.OPAQUE, 165 DataBuffer.TYPE_USHORT); 166 img = new BufferedImage(colorModel, ras, false, null); 167 168 Iterator<ImageWriter> iterWriter = 169 ImageIO.getImageWritersBySuffix("png"); 170 writer = iterWriter.next(); 171 172 param = writer.getDefaultWriteParam(); 173 ImageTypeSpecifier specifier = new ImageTypeSpecifier(img); 174 metadata = writer.getDefaultImageMetadata(specifier, param); 175 176 // Create tRNS node and merge it with default metadata 177 createTRNSNode("65535"); 178 179 writeImage(); 180 181 InputStream input= new ByteArrayInputStream(imageByteArray); 182 // Read 16 bit PNG RGB image with tRNS chunk 183 BufferedImage verify_img = ImageIO.read(input); 184 input.close(); 185 // Verify alpha values present in first & second pixel 186 return verifyAlphaValue(verify_img); 187 } 188 189 public static void main(String[] args) throws IOException { 190 boolean read8BitFail, read16BitFail; 191 // read 8 bit PNG RGB image with tRNS chunk 192 read8BitFail = read8BitRGBPNGWithTRNSChunk(); 193 194 // read 16 bit PNG RGB image with tRNS chunk 195 read16BitFail = read16BitRGBPNGWithTRNSChunk(); 196 197 if (read8BitFail || read16BitFail) { 198 throw new RuntimeException("PNGImageReader is not using" + 199 " transparent pixel information from tRNS chunk properly"); 200 } 201 } 202 } 203