Bug 45829

Summary: HSSFPicture.getImageDimension() fails when DPI of image is zero
Product: POI Reporter: Peter Runge <prunge>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal CC: prunge
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Attachments: Test PNG image with zero DPI
Patch for HSSFPicture.java

Description Peter Runge 2008-09-17 17:15:11 UTC
Created attachment 22597 [details]
Test PNG image with zero DPI

POI version 3.1-FINAL-20080629.

When calling HSSFPicture.resize() for a PNG image that does not specify a DPI (DPI width and height is zero), HSSFPicture.getImageDimension() method fails with ArithmeticException (/ by zero).

It seems that in getResolution() the HorizontalPixelSize and VerticalPixelSize image metadata elements have values of zero if DPI is set to zero in PNG files.  While this might indicate a problem with the PNG file, failing with / by zero error is not appropriate as other software can happily load and display these files.  It would be more appropriate to either fall back to DPI defaults (96 DPI) rather than returning zeroes from this method or have special handling in getImageDimension() so that DPI is not used if it is zero.

Stack trace:
java.lang.ArithmeticException: / by zero
	at org.apache.poi.hssf.usermodel.HSSFPicture.getImageDimension(HSSFPicture.java:238)
	at org.apache.poi.hssf.usermodel.HSSFPicture.getPreferredSize(HSSFPicture.java:120)
	at org.apache.poi.hssf.usermodel.HSSFPicture.resize(HSSFPicture.java:97)
	...
Comment 1 Peter Runge 2008-09-17 17:37:42 UTC
Created attachment 22598 [details]
Patch for HSSFPicture.java
Comment 2 Peter Runge 2008-09-17 17:38:48 UTC
Created patch for HSSFPicture.java against
trunk/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java.

Added zero checking in getImageDimension() before division and using defaults
if zero is found.
Comment 3 Yegor Kozlov 2008-09-18 03:28:44 UTC
Fixed in r696622

Yegor