/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @bug 6574555 * @summary Test verifies that PNGImageWriter encodes the R, G, B * values of bKGD chunk properly. * @run main VerifyRGBValuesFromBKGDChunk */ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.Iterator; import javax.imageio.ImageTypeSpecifier; import javax.imageio.ImageWriter; import javax.imageio.ImageReader; import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.metadata.IIOMetadata; import javax.imageio.metadata.IIOMetadataNode; import javax.imageio.IIOImage; import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.ImageOutputStream; public class VerifyRGBValuesFromBKGDChunk { public static void main(String[] args) throws IOException { ImageOutputStream ios = null; ImageInputStream iis = null; File output = null; try { int width = 1; int height = 1; BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Iterator iterWriter = ImageIO.getImageWritersBySuffix("png"); ImageWriter writer = iterWriter.next(); ImageWriteParam param = writer.getDefaultWriteParam(); ImageTypeSpecifier specifier = ImageTypeSpecifier. createFromBufferedImageType(BufferedImage.TYPE_INT_BGR); IIOMetadata encodeMetadata = writer.getDefaultImageMetadata(specifier, param); // Write png image with bKGD chunk IIOMetadataNode bKGD_rgb = new IIOMetadataNode("bKGD_RGB"); String red = "100"; String green = "150"; String blue = "200"; bKGD_rgb.setAttribute("red", red); bKGD_rgb.setAttribute("green", green); bKGD_rgb.setAttribute("blue", blue); IIOMetadataNode bKGD = new IIOMetadataNode("bKGD"); bKGD.appendChild(bKGD_rgb); IIOMetadataNode encodeRoot = new IIOMetadataNode("javax_imageio_png_1.0"); encodeRoot.appendChild(bKGD); encodeMetadata.mergeTree("javax_imageio_png_1.0", encodeRoot); String dir = System.getProperty("test.src"); String sep = System.getProperty("file.separator"); String filePath = dir + sep; File directory = new File(filePath); output = File.createTempFile("output", "png", directory); ios = ImageIO.createImageOutputStream(output); writer.setOutput(ios); writer.write(encodeMetadata, new IIOImage(img, null, encodeMetadata), param); writer.dispose(); // Get bKGD chunk from image and verify the values iis = ImageIO.createImageInputStream(output); Iterator iterReader = ImageIO.getImageReadersBySuffix("png"); ImageReader reader = iterReader.next(); reader.setInput(iis, false, false); IIOMetadata decodeMetadata = reader.getImageMetadata(0); IIOMetadataNode decodeRoot = (IIOMetadataNode) decodeMetadata. getAsTree("javax_imageio_png_1.0"); bKGD_rgb = (IIOMetadataNode) decodeRoot.getElementsByTagName("bKGD_RGB").item(0); if (!(red.equals(bKGD_rgb.getAttribute("red")) && green.equals(bKGD_rgb.getAttribute("green")) && blue.equals(bKGD_rgb.getAttribute("blue")))) { throw new RuntimeException("bKGD RGB values are not stored" + " properly"); } } finally { if (ios != null) { ios.close(); } if (iis != null) { iis.close(); } if (output != null) { Files.delete(output.toPath()); } } } }