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         ios = ImageIO.createImageOutputStream(output);
  90     }
  91 
  92     private static void createTRNSNode(String tRNS_value) {
  93         IIOMetadataNode tRNS_gray = new IIOMetadataNode("tRNS_Grayscale");
  94         tRNS_gray.setAttribute("gray", tRNS_value);
  95 
  96         IIOMetadataNode tRNS = new IIOMetadataNode("tRNS");
  97         tRNS.appendChild(tRNS_gray);
  98         root = new IIOMetadataNode("javax_imageio_png_1.0");
  99         root.appendChild(tRNS);
 100     }
 101 
 102     private static boolean verifyAlphaValue(BufferedImage img) {
 103         Color firstPixel = new Color(img.getRGB(0, 0), true);
 104         Color secondPixel = new Color(img.getRGB(1, 0), true);
 105 
 106         return firstPixel.getAlpha() != 0 ||
 107                secondPixel.getAlpha() != 255;
 108     }
 109 
 110     private static boolean read8BitGrayPNGWithTRNSChunk() throws IOException {
 111         initialize(BufferedImage.TYPE_BYTE_GRAY);
 112         // Create tRNS node and merge it with default metadata
 113         createTRNSNode("255");
 114 
 115         metadata.mergeTree("javax_imageio_png_1.0", root);
 116 
 117         writer.setOutput(ios);
 118 
 119         writer.write(metadata, new IIOImage(img, null, metadata), param);
 120         writer.dispose();
 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         writer.dispose();
 139 
 140         // Read 16 bit PNG Gray image with tRNS chunk
 141         BufferedImage display_img = ImageIO.read(output);
 142         // Verify alpha values present in first & second pixel
 143         return verifyAlphaValue(display_img);
 144     }
 145 
 146     public static void main(String[] args) throws IOException {
 147         // read 8 bit PNG Gray image with tRNS chunk
 148         boolean read8BitFail, read16BitFail;
 149         try {
 150             createOutputStream();
 151             read8BitFail = read8BitGrayPNGWithTRNSChunk();
 152         } finally {
 153             if (ios != null) {
 154                 ios.close();
 155             }
 156             if (output != null) {
 157                 Files.delete(output.toPath());
 158             }
 159         }
 160 
 161         // read 16 bit PNG Gray image with tRNS chunk
 162         try {
 163             createOutputStream();
 164             read16BitFail = read16BitGrayPNGWithTRNSChunk();
 165         } finally {
 166             if (ios != null) {
 167                 ios.close();
 168             }
 169             if (output != null) {
 170                 Files.delete(output.toPath());
 171             }
 172         }
 173 
 174         if (read8BitFail || read16BitFail) {
 175             throw new RuntimeException("PNGImageReader is not using" +
 176                 " transparent pixel information from tRNS chunk properly");
 177         }
 178     }
 179 }
 180