1 /*
   2  * Copyright (c) 2014, 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 4991647
  27 * @summary PNGMetadata.getAsTree() sets bitDepth to invalid value
  28 * @run main PngDitDepthTest
  29 */
  30 
  31 import org.w3c.dom.Node;
  32 
  33 import javax.imageio.ImageIO;
  34 import javax.imageio.ImageTypeSpecifier;
  35 import javax.imageio.ImageWriter;
  36 import javax.imageio.metadata.IIOInvalidTreeException;
  37 import javax.imageio.metadata.IIOMetadata;
  38 import java.awt.image.ColorModel;
  39 import java.awt.image.SampleModel;
  40 import java.util.Iterator;
  41 
  42 public class PngDitDepthTest {
  43     
  44     public static void main(String[] args) throws IIOInvalidTreeException {
  45 
  46         // getting the writer for the png format
  47         Iterator iter = ImageIO.getImageWritersByFormatName("png");
  48         ImageWriter writer = (ImageWriter) iter.next();
  49 
  50         // creating a color model
  51         ColorModel colorModel = ColorModel.getRGBdefault();
  52 
  53         // creating a sample model
  54         SampleModel sampleModel = colorModel.createCompatibleSampleModel(640, 480);
  55 
  56         // creating a default metadata object
  57         IIOMetadata metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(colorModel, sampleModel), null);
  58         String formatName = metaData.getNativeMetadataFormatName();
  59 
  60         // first call
  61         Node metaDataNode = metaData.getAsTree(formatName);
  62         try {
  63             metaData.setFromTree(formatName, metaDataNode);
  64         } catch (Exception ex) {
  65             ex.printStackTrace();
  66         }
  67 
  68         // second call (bitdepht is already set to an invalid value)
  69         metaDataNode = metaData.getAsTree(formatName);
  70 
  71         metaData.setFromTree(formatName, metaDataNode);
  72 
  73     }
  74 }