Path: blob/master/test/jdk/javax/imageio/plugins/png/ReadPngRGBImageWithTRNSChunk.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 RGB PNG images.28* @run main ReadPngRGBImageWithTRNSChunk29*/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;48import java.awt.image.DataBuffer;49import java.awt.image.DataBufferUShort;50import java.awt.image.WritableRaster;51import java.awt.image.Raster;52import java.awt.color.ColorSpace;53import java.awt.image.ColorModel;54import java.awt.image.ComponentColorModel;55import java.awt.Transparency;5657public class ReadPngRGBImageWithTRNSChunk {5859private static BufferedImage img;60private static IIOMetadata metadata;61private static ImageWriteParam param;62private static ImageWriter writer;63private static byte[] imageByteArray;6465private static void createTRNSNode(String tRNS_value)66throws IIOInvalidTreeException {67IIOMetadataNode tRNS_rgb = new IIOMetadataNode("tRNS_RGB");68tRNS_rgb.setAttribute("red", tRNS_value);69tRNS_rgb.setAttribute("green", tRNS_value);70tRNS_rgb.setAttribute("blue", tRNS_value);7172IIOMetadataNode tRNS = new IIOMetadataNode("tRNS");73tRNS.appendChild(tRNS_rgb);74IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");75root.appendChild(tRNS);76metadata.mergeTree("javax_imageio_png_1.0", root);77}7879private static void writeImage() throws IOException {80ByteArrayOutputStream baos = new ByteArrayOutputStream();81ImageOutputStream ios = ImageIO.createImageOutputStream(baos);82writer.setOutput(ios);83writer.write(metadata, new IIOImage(img, null, metadata), param);84writer.dispose();8586baos.flush();87imageByteArray = baos.toByteArray();88baos.close();89}9091private static boolean verifyAlphaValue(BufferedImage img) {92Color firstPixel = new Color(img.getRGB(0, 0), true);93Color secondPixel = new Color(img.getRGB(1, 0), true);9495return firstPixel.getAlpha() != 0 ||96secondPixel.getAlpha() != 255;97}9899private static boolean read8BitRGBPNGWithTRNSChunk() throws IOException {100int width = 2;101int height = 1;102// Create 8 bit PNG image103img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);104Graphics2D g2D = img.createGraphics();105106// transparent first pixel107g2D.setColor(Color.WHITE);108g2D.fillRect(0, 0, 1, 1);109// non-transparent second pixel110g2D.setColor(Color.RED);111g2D.fillRect(1, 0, 1, 1);112g2D.dispose();113114Iterator<ImageWriter> iterWriter =115ImageIO.getImageWritersBySuffix("png");116writer = iterWriter.next();117118param = writer.getDefaultWriteParam();119ImageTypeSpecifier specifier =120ImageTypeSpecifier.121createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR);122metadata = writer.getDefaultImageMetadata(specifier, param);123124// Create tRNS node and merge it with default metadata125createTRNSNode("255");126127writeImage();128129InputStream input= new ByteArrayInputStream(imageByteArray);130// Read 8 bit PNG RGB image with tRNS chunk131BufferedImage verify_img = ImageIO.read(input);132input.close();133// Verify alpha values present in first & second pixel134return verifyAlphaValue(verify_img);135}136137private static boolean read16BitRGBPNGWithTRNSChunk() throws IOException {138// Create 16 bit PNG image139int height = 1;140int width = 2;141int numBands = 3;142int shortArrayLength = width * height * numBands;143short[] pixelData = new short[shortArrayLength];144// transparent first pixel145pixelData[0] = (short)0xffff;146pixelData[1] = (short)0xffff;147pixelData[2] = (short)0xffff;148// non-transparent second pixel149pixelData[3] = (short)0xffff;150pixelData[4] = (short)0xffff;151pixelData[5] = (short)0xfffe;152153DataBuffer buffer = new DataBufferUShort(pixelData, shortArrayLength);154155int[] bandOffset = {0, 1 ,2};156WritableRaster ras =157Raster.createInterleavedRaster(buffer, width, height,158width * numBands, numBands, bandOffset, null);159160int nBits[] = {16, 16 ,16};161ColorModel colorModel = new162ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),163nBits, false, false, Transparency.OPAQUE,164DataBuffer.TYPE_USHORT);165img = new BufferedImage(colorModel, ras, false, null);166167Iterator<ImageWriter> iterWriter =168ImageIO.getImageWritersBySuffix("png");169writer = iterWriter.next();170171param = writer.getDefaultWriteParam();172ImageTypeSpecifier specifier = new ImageTypeSpecifier(img);173metadata = writer.getDefaultImageMetadata(specifier, param);174175// Create tRNS node and merge it with default metadata176createTRNSNode("65535");177178writeImage();179180InputStream input= new ByteArrayInputStream(imageByteArray);181// Read 16 bit PNG RGB image with tRNS chunk182BufferedImage verify_img = ImageIO.read(input);183input.close();184// Verify alpha values present in first & second pixel185return verifyAlphaValue(verify_img);186}187188public static void main(String[] args) throws IOException {189boolean read8BitFail, read16BitFail;190// read 8 bit PNG RGB image with tRNS chunk191read8BitFail = read8BitRGBPNGWithTRNSChunk();192193// read 16 bit PNG RGB image with tRNS chunk194read16BitFail = read16BitRGBPNGWithTRNSChunk();195196if (read8BitFail || read16BitFail) {197throw new RuntimeException("PNGImageReader is not using" +198" transparent pixel information from tRNS chunk properly");199}200}201}202203204205