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         directory.delete();
  69         ios = ImageIO.createImageOutputStream(output);
  70     }
  71 
  72     private static void createTRNSNode(String tRNS_value) {
  73         IIOMetadataNode tRNS_rgb = new IIOMetadataNode("tRNS_RGB");
  74         tRNS_rgb.setAttribute("red", tRNS_value);
  75         tRNS_rgb.setAttribute("green", tRNS_value);
  76         tRNS_rgb.setAttribute("blue", tRNS_value);
  77 
  78         IIOMetadataNode tRNS = new IIOMetadataNode("tRNS");
  79         tRNS.appendChild(tRNS_rgb);
  80         root = new IIOMetadataNode("javax_imageio_png_1.0");
  81         root.appendChild(tRNS);
  82     }
  83 
  84     private static boolean verifyAlphaValue(BufferedImage img) {
  85         Color firstPixel = new Color(img.getRGB(0, 0), true);
  86         Color secondPixel = new Color(img.getRGB(1, 0), true);
  87 
  88         return firstPixel.getAlpha() != 0 ||
  89                secondPixel.getAlpha() != 255;
  90     }
  91 
  92     private static boolean read8BitRGBPNGWithTRNSChunk() throws IOException {
  93         int width = 2;
  94         int height = 1;
  95         // Create 8 bit PNG image
  96         BufferedImage img =
  97             new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
  98         Graphics2D g2D = img.createGraphics();
  99 
 100         // transparent first pixel
 101         g2D.setColor(Color.WHITE);
 102         g2D.fillRect(0, 0, 1, 1);
 103         // non-transparent second pixel
 104         g2D.setColor(Color.RED);
 105         g2D.fillRect(1, 0, 1, 1);
 106         g2D.dispose();
 107 
 108         Iterator<ImageWriter> iterWriter =
 109             ImageIO.getImageWritersBySuffix("png");
 110         ImageWriter writer = iterWriter.next();
 111 
 112         ImageWriteParam param = writer.getDefaultWriteParam();
 113         ImageTypeSpecifier specifier =
 114             ImageTypeSpecifier.
 115                 createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR);
 116         IIOMetadata metadata =
 117             writer.getDefaultImageMetadata(specifier, param);
 118 
 119         // Create tRNS node and merge it with default metadata
 120         createTRNSNode("255");
 121 
 122         metadata.mergeTree("javax_imageio_png_1.0", root);
 123 
 124         writer.setOutput(ios);
 125 
 126         writer.write(metadata, new IIOImage(img, null, metadata), param);
 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 
 185         // Read 16 bit PNG RGB image with tRNS chunk
 186         BufferedImage display_img = ImageIO.read(output);
 187         // Verify alpha values present in first & second pixel
 188         return verifyAlphaValue(display_img);
 189     }
 190 
 191     public static void main(String[] args) throws IOException {
 192         // read 8 bit PNG RGB image with tRNS chunk
 193         boolean read8BitFail, read16BitFail;
 194         try {
 195             createOutputStream();
 196             read8BitFail = read8BitRGBPNGWithTRNSChunk();
 197         } finally {
 198             ios.close();
 199             Files.delete(output.toPath());
 200         }
 201 
 202         // read 16 bit PNG RGB image with tRNS chunk
 203         try {
 204             createOutputStream();
 205             read16BitFail = read16BitRGBPNGWithTRNSChunk();
 206         } finally {
 207             ios.close();
 208             Files.delete(output.toPath());
 209         }
 210 
 211         if (read8BitFail || read16BitFail) {
 212             throw new RuntimeException("PNGImageReader is not using" +
 213                 " transparent pixel information from tRNS chunk properly");
 214         }
 215     }
 216 }