Path: blob/master/test/jdk/javax/imageio/plugins/png/VerifyBitDepthScalingWithTRNSChunk.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 821179526* @summary Test verifies that PNGImageReader maintains proper27* number of bands for scale array when PNG image28* has tRNS chunk.29* @run main VerifyBitDepthScalingWithTRNSChunk30*/3132import java.awt.Graphics2D;33import java.awt.image.BufferedImage;34import java.awt.Color;35import java.awt.image.IndexColorModel;36import java.io.ByteArrayInputStream;37import java.io.ByteArrayOutputStream;38import java.io.IOException;39import java.io.InputStream;40import java.util.Iterator;41import javax.imageio.ImageTypeSpecifier;42import javax.imageio.ImageWriter;43import javax.imageio.ImageIO;44import javax.imageio.ImageWriteParam;45import javax.imageio.metadata.IIOInvalidTreeException;46import javax.imageio.metadata.IIOMetadata;47import javax.imageio.metadata.IIOMetadataNode;48import javax.imageio.stream.ImageOutputStream;49import javax.imageio.IIOImage;5051public class VerifyBitDepthScalingWithTRNSChunk {5253private static BufferedImage img;54private static ImageWriter writer;55private static ImageWriteParam param;56private static IIOMetadata metadata;57private static byte[] imageByteArray;5859private static void initialize(int type) {60int width = 1;61int height = 1;62// create Palette & IndexColorModel for bitdepth 163int size = 2;64int bitDepth = 1;65byte[] r = new byte[size];66byte[] g = new byte[size];67byte[] b = new byte[size];6869r[0] = g[0] = b[0] = 0;70r[1] = g[1] = b[1] = (byte)255;7172IndexColorModel cm = new IndexColorModel(bitDepth, size, r, g, b);73img = new BufferedImage(width, height, type, cm);74Graphics2D g2D = img.createGraphics();75g2D.setColor(new Color(255, 255, 255));76g2D.fillRect(0, 0, width, height);7778Iterator<ImageWriter> iterWriter =79ImageIO.getImageWritersBySuffix("png");80writer = iterWriter.next();8182param = writer.getDefaultWriteParam();83ImageTypeSpecifier specifier =84ImageTypeSpecifier.85createFromBufferedImageType(type);86metadata = writer.getDefaultImageMetadata(specifier, param);87}8889private static void createTRNSNode(String tRNS_value)90throws IIOInvalidTreeException {91IIOMetadataNode tRNS_gray = new IIOMetadataNode("tRNS_Grayscale");92tRNS_gray.setAttribute("gray", tRNS_value);9394IIOMetadataNode tRNS = new IIOMetadataNode("tRNS");95tRNS.appendChild(tRNS_gray);96IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");97root.appendChild(tRNS);98metadata.mergeTree("javax_imageio_png_1.0", root);99}100101private static void writeImage() throws IOException {102ByteArrayOutputStream baos = new ByteArrayOutputStream();103ImageOutputStream ios = ImageIO.createImageOutputStream(baos);104writer.setOutput(ios);105writer.write(metadata, new IIOImage(img, null, metadata), param);106writer.dispose();107108baos.flush();109imageByteArray = baos.toByteArray();110baos.close();111}112113private static void verifyBitDepthScalingWithTRNSChunk()114throws IOException {115initialize(BufferedImage.TYPE_BYTE_BINARY);116// Create tRNS node with some value and merge it with default metadata117createTRNSNode("255");118119writeImage();120121InputStream input= new ByteArrayInputStream(imageByteArray);122/*123* Read 1 bit PNG Gray image with tRNS chunk.124* Since bitDepth is 1 there will be scaling of each channel,125* and it has tRNS chunk for which we will add extra alpha channel.126* This will result in creation of scale array in PNGImageReader.127*/128ImageIO.read(input);129input.close();130}131132public static void main(String[] args) throws IOException {133verifyBitDepthScalingWithTRNSChunk();134}135}136137138139