Path: blob/master/test/jdk/javax/imageio/plugins/png/VerifyRGBValuesFromBKGDChunk.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 6574555 510914626* @summary Test verifies that PNGImageWriter encodes the R, G, B27* values of bKGD chunk properly.28* @run main VerifyRGBValuesFromBKGDChunk29*/3031import java.awt.image.BufferedImage;32import java.io.IOException;33import java.util.Iterator;34import javax.imageio.ImageTypeSpecifier;35import javax.imageio.ImageWriter;36import javax.imageio.ImageReader;37import javax.imageio.ImageIO;38import javax.imageio.ImageWriteParam;39import javax.imageio.metadata.IIOInvalidTreeException;40import javax.imageio.metadata.IIOMetadata;41import javax.imageio.metadata.IIOMetadataNode;42import javax.imageio.IIOImage;43import javax.imageio.stream.ImageInputStream;44import javax.imageio.stream.ImageOutputStream;45import java.io.ByteArrayInputStream;46import java.io.ByteArrayOutputStream;47import java.io.InputStream;4849public class VerifyRGBValuesFromBKGDChunk {5051private static IIOMetadata encodeMetadata;52private static ImageWriter writer;53private static ImageReader reader;54private static BufferedImage img;55private static ImageWriteParam param;56private static boolean nativeBKGDFail, standardBKGDFail;57private static IIOMetadataNode bKGD_RGBNode;58private static final String BKGDRED = "100";59private static final String BKGDGREEN = "150";60private static final String BKGDBLUE = "200";6162private static void mergeStandardMetadata() throws IIOInvalidTreeException {63IIOMetadataNode background_rgb = new IIOMetadataNode("BackgroundColor");6465background_rgb.setAttribute("red", BKGDRED);66background_rgb.setAttribute("green", BKGDGREEN);67background_rgb.setAttribute("blue", BKGDBLUE);6869IIOMetadataNode chroma = new IIOMetadataNode("Chroma");70chroma.appendChild(background_rgb);71IIOMetadataNode encodeRoot = new IIOMetadataNode("javax_imageio_1.0");72encodeRoot.appendChild(chroma);7374ImageTypeSpecifier specifier =75ImageTypeSpecifier.76createFromBufferedImageType(BufferedImage.TYPE_INT_BGR);77encodeMetadata = writer.getDefaultImageMetadata(specifier, param);78encodeMetadata.mergeTree("javax_imageio_1.0", encodeRoot);79}8081private static void mergeNativeMetadata() throws IIOInvalidTreeException {82IIOMetadataNode bKGD_rgb = new IIOMetadataNode("bKGD_RGB");8384bKGD_rgb.setAttribute("red", BKGDRED);85bKGD_rgb.setAttribute("green", BKGDGREEN);86bKGD_rgb.setAttribute("blue", BKGDBLUE);8788IIOMetadataNode bKGD = new IIOMetadataNode("bKGD");89bKGD.appendChild(bKGD_rgb);90IIOMetadataNode encodeRoot =91new IIOMetadataNode("javax_imageio_png_1.0");92encodeRoot.appendChild(bKGD);9394ImageTypeSpecifier specifier =95ImageTypeSpecifier.96createFromBufferedImageType(BufferedImage.TYPE_INT_BGR);97encodeMetadata = writer.getDefaultImageMetadata(specifier, param);98encodeMetadata.mergeTree("javax_imageio_png_1.0", encodeRoot);99}100101private static void writeAndReadMetadata() throws IOException {102ByteArrayOutputStream baos = new ByteArrayOutputStream();103ImageOutputStream ios = ImageIO.createImageOutputStream(baos);104writer.setOutput(ios);105106writer.write(encodeMetadata,107new IIOImage(img, null, encodeMetadata), param);108109baos.flush();110byte[] imageByteArray = baos.toByteArray();111baos.close();112113// Get bKGD chunk from image and verify the values114InputStream input= new ByteArrayInputStream(imageByteArray);115ImageInputStream iis = ImageIO.createImageInputStream(input);116reader.setInput(iis, false, false);117118IIOMetadata decodeMetadata = reader.getImageMetadata(0);119IIOMetadataNode decodeRoot =120(IIOMetadataNode) decodeMetadata.121getAsTree("javax_imageio_png_1.0");122bKGD_RGBNode = (IIOMetadataNode)123decodeRoot.getElementsByTagName("bKGD_RGB").item(0);124}125126private static boolean verifyRGBValues() {127return (!(BKGDRED.equals(bKGD_RGBNode.getAttribute("red")) &&128BKGDGREEN.equals(bKGD_RGBNode.getAttribute("green")) &&129BKGDBLUE.equals(bKGD_RGBNode.getAttribute("blue"))));130}131132private static void VerifyNativeRGBValuesFromBKGDChunk()133throws IOException {134135mergeNativeMetadata();136writeAndReadMetadata();137nativeBKGDFail = verifyRGBValues();138}139140private static void VerifyStandardRGBValuesFromBKGDChunk()141throws IOException {142143mergeStandardMetadata();144writeAndReadMetadata();145standardBKGDFail = verifyRGBValues();146}147148public static void main(String[] args) throws IOException {149int width = 1;150int height = 1;151img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);152Iterator<ImageWriter> iterWriter =153ImageIO.getImageWritersBySuffix("png");154writer = iterWriter.next();155156param = writer.getDefaultWriteParam();157158Iterator<ImageReader> iterReader =159ImageIO.getImageReadersBySuffix("png");160reader = iterReader.next();161// Verify bKGD RGB values after merging metadata using native tree162VerifyNativeRGBValuesFromBKGDChunk();163164// Verify bKGD RGB values after merging metadata using standard tree165VerifyStandardRGBValuesFromBKGDChunk();166167writer.dispose();168reader.dispose();169170if (nativeBKGDFail || standardBKGDFail) {171throw new RuntimeException("bKGD RGB values are not stored" +172" properly");173}174}175}176177178179