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