List p = new ArrayList();
int pos = 0;
- while (pos < pictstream.length) {
+ // An empty picture record (length 0) will take up 8 bytes
+ while (pos <= (pictstream.length-8)) {
int offset = pos;
- //image signature
+ // Image signature
int signature = LittleEndian.getUShort(pictstream, pos);
pos += LittleEndian.SHORT_SIZE;
- //image type + 0xF018
+ // Image type + 0xF018
int type = LittleEndian.getUShort(pictstream, pos);
pos += LittleEndian.SHORT_SIZE;
- //image size
+ // Image size (excluding the 8 byte header)
int imgsize = LittleEndian.getInt(pictstream, pos);
pos += LittleEndian.INT_SIZE;
-
byte[] imgdata = new byte[imgsize];
System.arraycopy(pictstream, pos, imgdata, 0, imgdata.length);
- try {
- PictureData pict = PictureData.create(type - 0xF018);
- pict.setRawData(imgdata);
- pict.setOffset(offset);
- p.add(pict);
- } catch(IllegalArgumentException e) {
- System.err.println("Problem reading picture: " + e + "\nYou document will probably become corrupted if you save it!");
- }
+ // If they type (including the bonus 0xF018) is 0, skip it
+ if(type == 0) {
+ System.err.println("Problem reading picture: Invalid image type 0, on picture with length" + imgsize + ".\nYou document will probably become corrupted if you save it!");
+ } else {
+ // Build the PictureData object from the data
+ try {
+ PictureData pict = PictureData.create(type - 0xF018);
+ pict.setRawData(imgdata);
+ pict.setOffset(offset);
+ p.add(pict);
+ } catch(IllegalArgumentException e) {
+ System.err.println("Problem reading picture: " + e + "\nYou document will probably become corrupted if you save it!");
+ }
+ }
pos += imgsize;
}
\r
}\r
\r
-\r
+ /**\r
+ * Test that on a party corrupt powerpoint document, which has \r
+ * crazy pictures of type 0, we do our best.\r
+ */\r
+ public void testZeroPictureType() throws Exception {\r
+ HSLFSlideShow hslf = new HSLFSlideShow(new File(cwd, "PictureTypeZero.ppt").getPath());\r
+\r
+ // Should still have 2 real pictures\r
+ assertEquals(2, hslf.getPictures().length);\r
+ // Both are real pictures, both WMF\r
+ assertEquals(Picture.WMF, hslf.getPictures()[0].getType());\r
+ assertEquals(Picture.WMF, hslf.getPictures()[1].getType());\r
+\r
+ // TODO: DISABLED: Pending bug #41176\r
+\r
+ // Now test what happens when we use the SlideShow interface\r
+ //SlideShow ppt = new SlideShow(hslf);\r
+ }\r
}\r