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 and fill it with white color 67 int height = 50; 68 int width = 50; 69 int numBands = 3; 70 int shortArrayLength = width * height * numBands; 71 short[] pixelData = new short[shortArrayLength]; 72 for (int i = 0; i < shortArrayLength; i++) { 73 pixelData[i] = (short)0xffff; 74 } 75 76 DataBuffer buffer = new DataBufferUShort(pixelData, shortArrayLength); 77 78 int[] bandOffset = {0, 1 ,2}; 79 WritableRaster ras = 80 Raster.createInterleavedRaster(buffer, width, height, 81 width * numBands, numBands, bandOffset, null); 82 83 int nBits[] = {16, 16 ,16}; 84 ColorModel colorModel = new 85 ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 86 nBits, false, false, Transparency.OPAQUE, 87 DataBuffer.TYPE_USHORT); 88 BufferedImage img = new BufferedImage(colorModel, ras, 89 false, null); 90 91 Iterator<ImageWriter> iterWriter = 92 ImageIO.getImageWritersBySuffix("png"); 93 ImageWriter writer = iterWriter.next(); 94 95 ImageWriteParam param = writer.getDefaultWriteParam(); 96 ImageTypeSpecifier specifier = 97 ImageTypeSpecifier. 98 createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR); 99 IIOMetadata metadata = 100 writer.getDefaultImageMetadata(specifier, param); 101 102 // Create tRNS node and merge it with default metadata 103 IIOMetadataNode tRNS_rgb = new IIOMetadataNode("tRNS_RGB"); 104 tRNS_rgb.setAttribute("red", "65535"); 105 tRNS_rgb.setAttribute("green", "65535"); 106 tRNS_rgb.setAttribute("blue", "65535"); 107 108 IIOMetadataNode tRNS = new IIOMetadataNode("tRNS"); 109 tRNS.appendChild(tRNS_rgb); 110 IIOMetadataNode root = 111 new IIOMetadataNode("javax_imageio_png_1.0"); 112 root.appendChild(tRNS); 113 114 metadata.mergeTree("javax_imageio_png_1.0", root); 115 116 writer.setOutput(ios); 117 118 writer.write(metadata, new IIOImage(img, null, metadata), param); 119 120 // Read 16 bit PNG RGB image with tRNS chunk and verify 121 BufferedImage display_img = ImageIO.read(output); 122 Color c = new Color(display_img.getRGB(0, 0), true); 123 if (c.getAlpha() != 0) { 124 throw new RuntimeException("PNGImageReader is not using" + 125 " transparent pixel information present in tRNS chunk"); 126 } 127 } finally { 128 ios.close(); 129 Files.delete(output.toPath()); 130 } 131 132 } 133 } 134