From ac2e1d761e2f90309bb6588813944f09951176d7 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Wed, 30 Dec 2020 17:03:23 +0000 Subject: [PATCH] Bug 65009: Fix incorrect index for 1-based pictures Adjusted somewhat from Github PR #211 Closes #211 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884948 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hslf/usermodel/HSLFSlideShowImpl.java | 4 ++-- .../org/apache/poi/hslf/usermodel/TestPictures.java | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java index b4cdd8ba80..264dcb0b4d 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java @@ -415,7 +415,7 @@ public final class HSLFSlideShowImpl extends POIDocument implements Closeable { pict.setRawData(imgdata); pict.setOffset(offset); - pict.setIndex(_pictures.size()); + pict.setIndex(_pictures.size() + 1); // index is 1-based _pictures.add(pict); } catch (IllegalArgumentException e) { logger.log(POILogger.ERROR, "Problem reading picture: ", e, "\nYour document will probably become corrupted if you save it!"); @@ -760,7 +760,7 @@ public final class HSLFSlideShowImpl extends POIDocument implements Closeable { offset = prev.getOffset() + prev.getRawData().length + 8; } img.setOffset(offset); - img.setIndex(_pictures.size() + 1); + img.setIndex(_pictures.size() + 1); // index is 1-based _pictures.add(img); return offset; } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java index d53469b02f..5142a5638d 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java @@ -30,6 +30,7 @@ import java.util.Arrays; import java.util.List; import org.apache.poi.POIDataSamples; +import org.apache.poi.hslf.HSLFTestDataSamples; import org.apache.poi.hslf.blip.DIB; import org.apache.poi.hslf.blip.EMF; import org.apache.poi.hslf.blip.JPEG; @@ -361,7 +362,7 @@ public final class TestPictures { HSLFPictureShape pict; HSLFPictureData pdata; - HSLFSlideShow ppt = new HSLFSlideShow(slTests.openResourceAsStream("pictures.ppt")); + HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("pictures.ppt"); List slides = ppt.getSlides(); List pictures = ppt.getPictureData(); assertEquals(5, pictures.size()); @@ -522,7 +523,7 @@ public final class TestPictures { @Test public void testGetPictureName() throws IOException { - HSLFSlideShow ppt = new HSLFSlideShow(slTests.openResourceAsStream("ppt_with_png.ppt")); + HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("ppt_with_png.ppt"); HSLFSlide slide = ppt.getSlides().get(0); HSLFPictureShape p = (HSLFPictureShape)slide.getShapes().get(0); //the first slide contains JPEG @@ -551,4 +552,12 @@ public final class TestPictures { HSLFPictureShape p = (HSLFPictureShape)ppt.getSlides().get(0).getShapes().get(0); assertEquals("tomcat.png", p.getPictureName()); } + + @Test + public void testPictureIndexIsOneBased() throws IOException { + try (HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("ppt_with_png.ppt")) { + HSLFPictureData picture = ppt.getPictureData().get(0); + assertEquals(1, picture.getIndex()); + } + } }