Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8041460

PNGImageWriter creates metadata with incorrect bitDepth

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P3
    • None
    • 7u15
    • client-libs

    Description

      FULL PRODUCT VERSION :
      java version " 1.7.0_17 "
      Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
      Java HotSpot(TM) Client VM (build 23.7-b01, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      I try to update the interlaceMethod attribute in PNG file (see the wpng05 example). When I insert IHDR in the javax_imageio_png_1.0 metadata, ImageWriter creates PNG containing illegal bitDepth=3.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the wpng05 example.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The file should contain
      bitDepth= " 1 " | " 2 " | " 4 " | " 8 " | " 16 " .

      ACTUAL -
      The file contains bitDepth=3 and program crashes because of this.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      javax.imageio.metadata.IIOInvalidTreeException: Illegal value for attribute bitDepth!

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package tests.images2;

      import java.awt.image.*;
      import javax.imageio.*;
      import javax.imageio.metadata.*;
      import java.util.Iterator;
      import org.w3c.dom.*;

      public class wpng05 {
      public static void main(String args[]) {
      BufferedImage image= new BufferedImage(500,500,BufferedImage.TYPE_3BYTE_BGR);
      Iterator iter= ImageIO.getImageWritersByFormatName( " png " );
      ImageWriter writer= (ImageWriter)iter.next();
      ImageWriteParam iwp= writer.getDefaultWriteParam();
      ImageTypeSpecifier typeSpecifier= new ImageTypeSpecifier(image);
      IIOMetadata metadata= writer.getDefaultImageMetadata(typeSpecifier,iwp);
      String[] names= metadata.getMetadataFormatNames();
      int length= names.length;
      for (int i = 0; i < length; i++) {
      System.out.println( " BEFORE: Format name: " +names[i]);
      Node root= metadata.getAsTree(names[i]);
      displayMetadata(root);
      };
      for (int i = 0; i < length; i++) {
      Node root= metadata.getAsTree(names[i]);
      if (names[i].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 " );
      // iioIHDR.setAttribute( " interlaceMethod " , " none " );
      try {
      metadata.mergeTree(names[i],root);
      } catch (IIOInvalidTreeException e) {
      System.out.printf( " (1) IIOInvalidTreeException: %s
       " ,e);
      }
      }
      };
      for (int i = 0; i < length; i++) {
      System.out.println( " AFTER: Format name: " +names[i]);
      Node root= metadata.getAsTree(names[i]);
      displayMetadata(root);
      };
      for (int i= 0; i < length; i++) {
      if (names[i].equals( " javax_imageio_png_1.0 " )) {
      Node root= metadata.getAsTree(names[i]);
      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!!! " );
      try {
      metadata.mergeTree(names[i],root);
      } catch (IIOInvalidTreeException e) {
      System.out.printf( " (2) IIOInvalidTreeException: %s
       " ,e);
      }
      }
      };
      }
      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;
      }
      static void displayMetadata(Node root) {
      displayMetadata(root,0);
      }
      static void displayMetadata(Node node, int level) {
      indent(level);
      System.out.print(node.getNodeName());
      NamedNodeMap map= node.getAttributes();
      if (map != null) {
      int length= map.getLength();
      for (int i= 0; i < length; i++) {
      Node attr= map.item(i);
      System.out.print(
       " " + attr.getNodeName() +
        " =\ " " + attr.getNodeValue() + " \ " " );
      if (node.getNodeName().equals( " NumProgressiveScans " ) &&
      attr.getNodeName().equals( " value " )) {
      }
      }
      } else {
      System.out.print( " NO MAP! " );
      };
      Node child= node.getFirstChild();
      if (child == null) {
      System.out.println( " " );
      return;
      };
      System.out.println( " " );
      while (child != null) {
      displayMetadata(child, level + 1);
      child= child.getNextSibling();
      };
      }
      static void indent(int level) {
      for (int i= 0; i < level; i++) {
      System.out.print( " " );
      }
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I don?t know!

      Attachments

        Issue Links

          Activity

            People

              mcherkas Mikhail Cherkasov (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: