Path: blob/master/test/jdk/javax/imageio/plugins/png/ReadPngGrayImageWithTRNSChunk.java
41155 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 678845826* @summary Test verifies that PNGImageReader takes tRNS chunk values27* into consideration while reading non-indexed Gray PNG images.28* @run main ReadPngGrayImageWithTRNSChunk29*/3031import java.awt.Graphics2D;32import java.awt.image.BufferedImage;33import java.awt.Color;34import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.IOException;37import java.io.InputStream;38import java.util.Iterator;39import javax.imageio.ImageTypeSpecifier;40import javax.imageio.ImageWriter;41import javax.imageio.ImageIO;42import javax.imageio.ImageWriteParam;43import javax.imageio.metadata.IIOInvalidTreeException;44import javax.imageio.metadata.IIOMetadata;45import javax.imageio.metadata.IIOMetadataNode;46import javax.imageio.stream.ImageOutputStream;47import javax.imageio.IIOImage;4849public class ReadPngGrayImageWithTRNSChunk {5051private static BufferedImage img;52private static ImageWriter writer;53private static ImageWriteParam param;54private static IIOMetadata metadata;55private static byte[] imageByteArray;5657private static void initialize(int type) {58int width = 2;59int height = 1;60img = new BufferedImage(width, height, type);61Graphics2D g2D = img.createGraphics();6263// transparent first pixel64g2D.setColor(new Color(255, 255, 255));65g2D.fillRect(0, 0, 1, 1);66// non-transparent second pixel67g2D.setColor(new Color(128, 128,128));68g2D.fillRect(1, 0, 1, 1);69g2D.dispose();7071Iterator<ImageWriter> iterWriter =72ImageIO.getImageWritersBySuffix("png");73writer = iterWriter.next();7475param = writer.getDefaultWriteParam();76ImageTypeSpecifier specifier =77ImageTypeSpecifier.78createFromBufferedImageType(type);79metadata = writer.getDefaultImageMetadata(specifier, param);80}8182private static void createTRNSNode(String tRNS_value)83throws IIOInvalidTreeException {84IIOMetadataNode tRNS_gray = new IIOMetadataNode("tRNS_Grayscale");85tRNS_gray.setAttribute("gray", tRNS_value);8687IIOMetadataNode tRNS = new IIOMetadataNode("tRNS");88tRNS.appendChild(tRNS_gray);89IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");90root.appendChild(tRNS);91metadata.mergeTree("javax_imageio_png_1.0", root);92}9394private static void writeImage() throws IOException {95ByteArrayOutputStream baos = new ByteArrayOutputStream();96ImageOutputStream ios = ImageIO.createImageOutputStream(baos);97writer.setOutput(ios);98writer.write(metadata, new IIOImage(img, null, metadata), param);99writer.dispose();100101baos.flush();102imageByteArray = baos.toByteArray();103baos.close();104}105106private static boolean verifyAlphaValue(BufferedImage img) {107Color firstPixel = new Color(img.getRGB(0, 0), true);108Color secondPixel = new Color(img.getRGB(1, 0), true);109110return firstPixel.getAlpha() != 0 ||111secondPixel.getAlpha() != 255;112}113114private static boolean read8BitGrayPNGWithTRNSChunk() throws IOException {115initialize(BufferedImage.TYPE_BYTE_GRAY);116// Create tRNS node and merge it with default metadata117createTRNSNode("255");118119writeImage();120121InputStream input= new ByteArrayInputStream(imageByteArray);122// Read 8 bit PNG Gray image with tRNS chunk123BufferedImage verify_img = ImageIO.read(input);124input.close();125// Verify alpha values present in first & second pixel126return verifyAlphaValue(verify_img);127}128129private static boolean read16BitGrayPNGWithTRNSChunk() throws IOException {130initialize(BufferedImage.TYPE_USHORT_GRAY);131// Create tRNS node and merge it with default metadata132createTRNSNode("65535");133134writeImage();135136InputStream input= new ByteArrayInputStream(imageByteArray);137// Read 16 bit PNG Gray image with tRNS chunk138BufferedImage verify_img = ImageIO.read(input);139input.close();140// Verify alpha values present in first & second pixel141return verifyAlphaValue(verify_img);142}143144public static void main(String[] args) throws IOException {145boolean read8BitFail, read16BitFail;146// read 8 bit PNG Gray image with tRNS chunk147read8BitFail = read8BitGrayPNGWithTRNSChunk();148149// read 16 bit PNG Gray image with tRNS chunk150read16BitFail = read16BitGrayPNGWithTRNSChunk();151152if (read8BitFail || read16BitFail) {153throw new RuntimeException("PNGImageReader is not using" +154" transparent pixel information from tRNS chunk properly");155}156}157}158159160161