Bug 45829 - HSSFPicture.getImageDimension() fails when DPI of image is zero
Summary: HSSFPicture.getImageDimension() fails when DPI of image is zero
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: unspecified
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2008-09-17 17:15 UTC by Peter Runge
Modified: 2008-09-18 03:28 UTC (History)
1 user (show)

Test PNG image with zero DPI (259 bytes, image/png)
2008-09-17 17:15 UTC, Peter Runge
Patch for HSSFPicture.java (645 bytes, patch)
2008-09-17 17:37 UTC, Peter Runge
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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

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