JDK-4991647 : PNGMetadata.getAsTree() sets bitDepth to invalid value
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.imageio
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-02-10
  • Updated: 2015-01-21
  • Resolved: 2014-07-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8 JDK 9
7u76Fixed 8u40Fixed 9 b26Fixed
Related Reports
Duplicate :  
Duplicate :  
Description

Name: cc94850			Date: 02/09/2004


FULL PRODUCT VERSION :
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
When trying to get the metadata as tree, the first call of the getAsTree() method returns a valid PNGMetadata tree.  After setting the metadata from this tree with the setFromTree() method, the metadata object is still intact(node information is correctly stored in the PNGMetadata object). When trying to retreive the metadata as tree again ( getAsTree() ) the returned tree is invalid (bitDepht value is corrupted).

The error is caused by the PNGMetadata.getNativeTree() method:
 IHDR_node.setAttribute("bitDepth", Integer.toString(IHDR_bitDepth));
should be:
 IHDR_node.setAttribute("bitDepth", IHDR_bitDepths[IHDR_bitDepth]);


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
-create an empty, new PNGMetadata object
-call getAsTree() method
-call setFromTree() method
-call getAsTree() method
-call setFromTree() method

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
bitDepht should have been set to 8
ACTUAL -
bitDepht was set to 3 instead of 8

ERROR MESSAGES/STACK TRACES THAT OCCUR :
javax.imageio.metadata.IIOInvalidTreeException: Illegal value for attribute bitDepth!
	at com.sun.imageio.plugins.png.PNGMetadata.fatal(PNGMetadata.java:1076)
	at com.sun.imageio.plugins.png.PNGMetadata.getEnumeratedAttribute(PNGMetadata.java:1179)
	at com.sun.imageio.plugins.png.PNGMetadata.getEnumeratedAttribute(PNGMetadata.java:1187)
	at com.sun.imageio.plugins.png.PNGMetadata.mergeNativeTree(PNGMetadata.java:1243)
	at com.sun.imageio.plugins.png.PNGMetadata.mergeTree(PNGMetadata.java:1217)
	at javax.imageio.metadata.IIOMetadata.setFromTree(IIOMetadata.java:705)
	at TestCase.main(TestCase.java:54)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.image.ColorModel;
import java.awt.image.SampleModel;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;

import org.w3c.dom.Node;

import com.sun.imageio.plugins.png.PNGMetadata;

/*
 * Created on 30.01.2004
 */

/**
 * @author Beskid Lucian Cristian
 */
public class TestCase {

	public static void main(String[] args) {
		
		// getting the writer for the png format
		Iterator iter = ImageIO.getImageWritersByFormatName("png");
		ImageWriter writer = (ImageWriter)iter.next();
		
		// creating a color model
		ColorModel colorModel= ColorModel.getRGBdefault();
		
		// creating a sample model
		SampleModel sampleModel = colorModel.createCompatibleSampleModel(640, 480);
		
		// creating a default metadata object
		IIOMetadata metaData = writer.getDefaultImageMetadata( new ImageTypeSpecifier(colorModel, sampleModel), null);
		
		if (metaData instanceof PNGMetadata){
			System.out.println("Assumption that this is a PNGMetadata object is correct.");
			
			String formatName = metaData.getNativeMetadataFormatName();
			
			// first call
			Node metaDataNode = metaData.getAsTree(formatName);
			try {
				metaData.setFromTree(formatName, metaDataNode);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			
			// second call (bitdepht is already set to an invalid value)
			metaDataNode = metaData.getAsTree(formatName);
			
			// here an exception will occur because of the invalid bit depht
			try {
				metaData.setFromTree(formatName, metaDataNode);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
}

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

CUSTOMER SUBMITTED WORKAROUND :
No workaround because the getNativeTree() method is private.
(Incident Review ID: 236471) 
======================================================================

Comments
review: http://mail.openjdk.java.net/pipermail/2d-dev/2014-June/004634.html http://mail.openjdk.java.net/pipermail/2d-dev/2014-July/004664.html
10-07-2014