--- /dev/null 2014-06-09 22:28:44.000000000 +0400 +++ new/test/javax/imageio/plugins/png/PngDitDepthTest.java 2014-06-09 22:28:43.523670700 +0400 @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2014, 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 8041460 +* @summary Checking that IIOMetadata.mergeTree doesn't set incorrect bitDepth +* @run main PngDitDepthTest +*/ + +import org.w3c.dom.Node; + +import javax.imageio.ImageIO; +import javax.imageio.ImageTypeSpecifier; +import javax.imageio.ImageWriteParam; +import javax.imageio.ImageWriter; +import javax.imageio.metadata.IIOInvalidTreeException; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.metadata.IIOMetadataNode; +import java.awt.image.BufferedImage; + +public class PngDitDepthTest { + public static void main(String[] args) throws IIOInvalidTreeException { + BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_3BYTE_BGR); + ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next(); + ImageWriteParam iwp = writer.getDefaultWriteParam(); + ImageTypeSpecifier typeSpecifier = new ImageTypeSpecifier(image); + IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, iwp); + + for (String name : metadata.getMetadataFormatNames()) { + Node root = metadata.getAsTree(name); + if (name.equals("javax_imageio_png_1.0")) { + Node ihdr = getChildNode(root, "IHDR"); + if (ihdr == null) { + ihdr = new IIOMetadataNode("IHDR"); + root.appendChild(ihdr); + } + IIOMetadataNode iioIHDR = (IIOMetadataNode) ihdr; + iioIHDR.setAttribute("interlaceMethod", "adam7"); + metadata.mergeTree(name, root); + } + } + + for (String name : metadata.getMetadataFormatNames()) { + if (name.equals("javax_imageio_png_1.0")) { + Node root = metadata.getAsTree(name); + Node text = getChildNode(root, "tEXt"); + if (text == null) { + text = new IIOMetadataNode("tEXt"); + root.appendChild(text); + } + Node textEntry = getChildNode(text, "tEXtEntry"); + if (textEntry == null) { + textEntry = new IIOMetadataNode("tEXtEntry"); + text.appendChild(textEntry); + } + IIOMetadataNode iioTextEntry = (IIOMetadataNode) textEntry; + iioTextEntry.setAttribute("keyword", "comment"); + iioTextEntry.setAttribute("value", "This is a comment!!!"); + metadata.mergeTree(name, root); + } + } + } + + + static Node getChildNode(Node node, String givenName) { + Node child = node.getFirstChild(); + if (child == null) { + return null; + } + while (child != null) { + String childName = child.getNodeName(); + if (childName.equals(givenName)) { + return child; + } else { + child = child.getNextSibling(); + } + } + return null; + } +}