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
This commit is contained in:
Dominik Stadler 2020-12-30 17:03:23 +00:00
parent 1303f44abf
commit ac2e1d761e
2 changed files with 13 additions and 4 deletions

View File

@ -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;
}

View File

@ -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<HSLFSlide> slides = ppt.getSlides();
List<HSLFPictureData> 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());
}
}
}