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 16 bit PNG image. 29 * @run main Read16BitPNGWithTRNSChunk 30 */ 31 32 import java.awt.color.ColorSpace; 33 import java.awt.image.BufferedImage; 34 import java.awt.image.ColorModel; 35 import java.awt.image.ComponentColorModel; 36 import java.awt.image.DataBuffer; 37 import java.awt.image.DataBufferUShort; 38 import java.awt.image.WritableRaster; 39 import java.awt.image.Raster; 40 import java.awt.Color; 41 import java.awt.Transparency; 42 import java.io.File; 43 import java.io.IOException; 44 import java.nio.file.Files; 45 import java.util.Iterator; 46 import javax.imageio.ImageTypeSpecifier; 47 import javax.imageio.ImageWriter; 48 import javax.imageio.ImageIO; 49 import javax.imageio.ImageWriteParam; 50 import javax.imageio.metadata.IIOMetadata; 51 import javax.imageio.metadata.IIOMetadataNode; 52 import javax.imageio.stream.ImageOutputStream; 53 import javax.imageio.IIOImage; 54 55 public final class Read16BitPNGWithTRNSChunk { 56 57 public static void main(String[] args) throws IOException { 58 String dir = System.getProperty("test.src"); 59 String sep = System.getProperty("file.separator"); 60 String filePath = dir + sep; 61 File directory = new File(filePath); 62 File output = File.createTempFile("output", "png", directory); 63 directory.delete(); 64 ImageOutputStream ios = ImageIO.createImageOutputStream(output); 65 try { 66 // Create 16 bit PNG image 67 int height = 1; 68 int width = 2; 69 int numBands = 3; 70 int shortArrayLength = width * height * numBands; 71 short[] pixelData = new short[shortArrayLength]; 72 // transparent first pixel 73 pixelData[0] = (short)0xffff; 74 pixelData[1] = (short)0xffff; 75 pixelData[2] = (short)0xffff; 76 // non-transparent second pixel 77 pixelData[3] = (short)0xffff; 78 pixelData[4] = (short)0xffff; 79 pixelData[5] = (short)0xfffe; 80 81 DataBuffer buffer = new DataBufferUShort(pixelData, shortArrayLength); 82 83 int[] bandOffset = {0, 1 ,2}; 84 WritableRaster ras = 85 Raster.createInterleavedRaster(buffer, width, height, 86 width * numBands, numBands, bandOffset, null); 87 88 int nBits[] = {16, 16 ,16}; 89 ColorModel colorModel = new 90 ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 91 nBits, false, false, Transparency.OPAQUE, 92 DataBuffer.TYPE_USHORT); 93 BufferedImage img = new BufferedImage(colorModel, ras, 94 false, null); 95 96 Iterator<ImageWriter> iterWriter = 97 ImageIO.getImageWritersBySuffix("png"); 98 ImageWriter writer = iterWriter.next(); 99 100 ImageWriteParam param = writer.getDefaultWriteParam(); 101 ImageTypeSpecifier specifier = 102 ImageTypeSpecifier. 103 createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR); 104 IIOMetadata metadata = 105 writer.getDefaultImageMetadata(specifier, param); 106 107 // Create tRNS node and merge it with default metadata 108 IIOMetadataNode tRNS_rgb = new IIOMetadataNode("tRNS_RGB"); 109 tRNS_rgb.setAttribute("red", "65535"); 110 tRNS_rgb.setAttribute("green", "65535"); 111 tRNS_rgb.setAttribute("blue", "65535"); 112 113 IIOMetadataNode tRNS = new IIOMetadataNode("tRNS"); 114 tRNS.appendChild(tRNS_rgb); 115 IIOMetadataNode root = 116 new IIOMetadataNode("javax_imageio_png_1.0"); 117 root.appendChild(tRNS); 118 119 metadata.mergeTree("javax_imageio_png_1.0", root); 120 121 writer.setOutput(ios); 122 123 writer.write(metadata, new IIOImage(img, null, metadata), param); 124 125 // Read 16 bit PNG RGB image with tRNS chunk 126 BufferedImage display_img = ImageIO.read(output); 127 // Verify alpha values present in first & second pixel 128 Color firstPixel = new Color(display_img.getRGB(0, 0), true); 129 Color secondPixel = new Color(display_img.getRGB(1, 0), true); 130 if (firstPixel.getAlpha() != 0 || 131 secondPixel.getAlpha() != 255) 132 { 133 throw new RuntimeException("PNGImageReader is not using" + 134 " transparent pixel information from tRNS chunk properly"); 135 } 136 } finally { 137 ios.close(); 138 Files.delete(output.toPath()); 139 } 140 141 } 142 } 143