1 /*
   2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4895512
  27  * @summary Test that default image metadata for BMP image writer is not null
  28  */
  29 
  30 import java.awt.Color;
  31 import java.awt.Graphics;
  32 import java.awt.image.BufferedImage;
  33 import java.util.Iterator;
  34 
  35 import javax.imageio.ImageIO;
  36 import javax.imageio.ImageTypeSpecifier;
  37 import javax.imageio.ImageWriteParam;
  38 import javax.imageio.ImageWriter;
  39 import javax.imageio.metadata.IIOMetadata;
  40 
  41 import org.w3c.dom.NamedNodeMap;
  42 import org.w3c.dom.Node;
  43 
  44 public class BmpDefaultImageMetadataTest {
  45     ImageWriter writer = null;
  46     IIOMetadata imageData = null;
  47     ImageWriteParam writeParam = null;
  48     BufferedImage bimg = null;
  49 
  50     public BmpDefaultImageMetadataTest(String format) {
  51         try {
  52             bimg = new BufferedImage(200, 200, bimg.TYPE_INT_RGB);
  53             Graphics gg = bimg.getGraphics();
  54             gg.setColor(Color.red);
  55             gg.fillRect(50, 50, 100, 100);
  56 
  57             Iterator it = ImageIO.getImageWritersByFormatName(format);
  58             if (it.hasNext()) {
  59                 writer = (ImageWriter) it.next();
  60             }
  61             if (writer == null) {
  62                 throw new RuntimeException("No writer available for the given format."
  63                                            + " Test failed.");
  64             }
  65             writeParam = writer.getDefaultWriteParam();
  66 
  67             System.out.println("Testing Image Metadata for "+format+"\n");
  68             imageData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(bimg), writeParam);
  69             if (imageData == null) {
  70                 System.out.println("return value is null. No default image metadata is associated with "+format+" writer");
  71                 throw new RuntimeException("Default image metadata is null."
  72                                            + " Test failed.");
  73             }
  74             int j = 0;
  75             String imageDataNames[] = null;
  76             if(imageData != null) {
  77                 System.out.println("Is standard metadata format supported (Image) ? "+
  78                                    imageData.isStandardMetadataFormatSupported() );
  79                 imageDataNames = imageData.getMetadataFormatNames();
  80                 System.out.println("\nAll supported Metadata Format Names\n");
  81                 if(imageDataNames!=null){
  82                     for(j=0; j<imageDataNames.length; j++)  {
  83                         System.out.println("FORMAT NAME: "+imageDataNames[j]);
  84                         if (imageDataNames[j].equals(imageData.getNativeMetadataFormatName())) {
  85                             System.out.println("This is a Native Metadata format\n");
  86                         } else {
  87                             System.out.println("\n");
  88                         }
  89                         System.out.println("");
  90                         System.out.println("IIOImageMetadata DOM tree for "+imageDataNames[j]);
  91                         System.out.println("");
  92                         Node imageNode = imageData.getAsTree(imageDataNames[j]);
  93                         displayMetadata(imageNode);
  94                         System.out.println("\n\n");
  95                     }
  96                 }
  97             }
  98         }catch(Exception e){
  99             e.printStackTrace();
 100             throw new RuntimeException("Exception was thrown."
 101                                        + " Test failed.");
 102         }
 103     }
 104 
 105     public void displayMetadata(Node root) {
 106         displayMetadata(root, 0);
 107     }
 108 
 109     void indent(int level) {
 110         for (int i = 0; i < level; i++) {
 111             System.out.print(" ");
 112         }
 113     }
 114 
 115     void displayMetadata(Node node, int level) {
 116         indent(level); // emit open tag
 117         System.out.print("<" + node.getNodeName());
 118         NamedNodeMap map = node.getAttributes();
 119         if (map != null) { // print attribute values
 120             int length = map.getLength();
 121             for (int i = 0; i < length; i++) {
 122                 Node attr = map.item(i);
 123                 System.out.print(" " + attr.getNodeName() +
 124                                  "=\"" + attr.getNodeValue() + "\"");
 125             }
 126         }
 127         Node child = node.getFirstChild();
 128 
 129         if (node.getNodeValue() != null && !node.getNodeValue().equals("") ) {
 130             System.out.println(">");
 131             indent(level);
 132             System.out.println(node.getNodeValue());
 133             indent(level); // emit close tag
 134             System.out.println("</" + node.getNodeName() + ">");
 135         } else  if (child != null) {
 136             System.out.println(">"); // close current tag
 137             while (child != null) { // emit child tags recursively
 138                 displayMetadata(child, level + 1);
 139                 child = child.getNextSibling();
 140             }
 141             indent(level); // emit close tag
 142             System.out.println("</" + node.getNodeName() + ">");
 143         } else {
 144             System.out.println("/>");
 145         }
 146     }
 147 
 148     public static void main(String args[]) {
 149         BmpDefaultImageMetadataTest test = new BmpDefaultImageMetadataTest("bmp");
 150     }
 151 }