diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java index 1afb30f5a6..8d2dd79641 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java @@ -235,6 +235,12 @@ public class HSSFPicture BufferedImage img = r.read(0); int[] dpi = getResolution(r); + + //if DPI is zero then assume standard 96 DPI + //since cannot divide by zero + if (dpi[0] == 0) dpi[0] = 96; + if (dpi[1] == 0) dpi[1] = 96; + size.width = img.getWidth()*96/dpi[0]; size.height = img.getHeight()*96/dpi[1]; diff --git a/src/testcases/org/apache/poi/hssf/data/45829.png b/src/testcases/org/apache/poi/hssf/data/45829.png new file mode 100755 index 0000000000..eccaf30b26 Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/45829.png differ diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java index 87578ae01b..d1a6719e32 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java @@ -47,4 +47,18 @@ public final class TestHSSFPicture extends TestCase{ assertEquals(848, anchor1.getDx2()); assertEquals(240, anchor1.getDy2()); } + + /** + * Bug # 45829 reported ArithmeticException (/ by zero) when resizing png with zero DPI. + */ + public void test45829() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sh1 = wb.createSheet(); + HSSFPatriarch p1 = sh1.createDrawingPatriarch(); + + byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("45829.png"); + int idx1 = wb.addPicture( pictureData, HSSFWorkbook.PICTURE_TYPE_PNG ); + HSSFPicture pic = p1.createPicture(new HSSFClientAnchor(), idx1); + pic.resize(); + } }