]> source.dussan.org Git - poi.git/commitdiff
correctly append picture data if Pictures stream contains entries with zero length...
authorYegor Kozlov <yegor@apache.org>
Tue, 10 Feb 2009 19:53:52 +0000 (19:53 +0000)
committerYegor Kozlov <yegor@apache.org>
Tue, 10 Feb 2009 19:53:52 +0000 (19:53 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@743080 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java

index f2aaf8db1232f6f16de55bb2709e10757b9fcda1..120656ca95b433ba6ed02344051087e7821ecf5a 100644 (file)
@@ -37,7 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.5-beta6" date="2008-??-??">
-           <action dev="POI-DEVELOPERS" type="fix">remove me</action>
+           <action dev="POI-DEVELOPERS" type="fix">46627 - Fixed offset of added images if Pictures stream contains pictures with zero length</action>
         </release>
         <release version="3.5-beta5" date="2008-02-19">
            <action dev="POI-DEVELOPERS" type="fix">46536 - When shifting rows, update formulas on that sheet to point to the new location of those rows</action>
index bee0982eccd4966eb668483a7d1ae26c949ae4ac..f8b6fb674d52e3624e648b9cd2e1a77d2b73e483 100644 (file)
@@ -34,7 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.5-beta6" date="2008-??-??">
-           <action dev="POI-DEVELOPERS" type="fix">remove me</action>
+           <action dev="POI-DEVELOPERS" type="fix">46627 - Fixed offset of added images if Pictures stream contains pictures with zero length</action>
         </release>
         <release version="3.5-beta5" date="2008-02-19">
            <action dev="POI-DEVELOPERS" type="fix">46536 - When shifting rows, update formulas on that sheet to point to the new location of those rows</action>
index 8273d87cc523e2fbf9c18f0ddd279cdfd0d51f3b..c0a92a3188a2e7414955ade0808e5dbbcbf3cbbe 100644 (file)
@@ -66,7 +66,7 @@ public final class HSLFSlideShow extends POIDocument {
        private Record[] _records;
 
        // Raw Pictures contained in the pictures stream
-       private PictureData[] _pictures;
+       private List<PictureData> _pictures;
 
     // Embedded objects stored in storage records in the document stream, lazily populated.
     private ObjectData[] _objects;
@@ -291,6 +291,8 @@ public final class HSLFSlideShow extends POIDocument {
         * Find and read in pictures contained in this presentation
         */
        private void readPictures() throws IOException {
+        _pictures = new ArrayList<PictureData>();
+
                byte[] pictstream;
 
                try {
@@ -304,9 +306,7 @@ public final class HSLFSlideShow extends POIDocument {
                        return;
                }
 
-        List p = new ArrayList();
         int pos = 0;
-
                // An empty picture record (length 0) will take up 8 bytes
         while (pos <= (pictstream.length-8)) {
             int offset = pos;
@@ -325,7 +325,7 @@ public final class HSLFSlideShow extends POIDocument {
                        // (0 is allowed, but odd, since we do wind on by the header each
                        //  time, so we won't get stuck)
                        if(imgsize < 0) {
-                               throw new CorruptPowerPointFileException("The file contains a picture, at position " + p.size() + ", which has a negatively sized data length, so we can't trust any of the picture data");
+                               throw new CorruptPowerPointFileException("The file contains a picture, at position " + _pictures.size() + ", which has a negatively sized data length, so we can't trust any of the picture data");
                        }
 
                        // If they type (including the bonus 0xF018) is 0, skip it
@@ -343,7 +343,7 @@ public final class HSLFSlideShow extends POIDocument {
                     pict.setRawData(imgdata);
 
                     pict.setOffset(offset);
-                                       p.add(pict);
+                                       _pictures.add(pict);
                                } catch(IllegalArgumentException e) {
                                        logger.log(POILogger.ERROR, "Problem reading picture: " + e + "\nYou document will probably become corrupted if you save it!");
                                }
@@ -351,8 +351,6 @@ public final class HSLFSlideShow extends POIDocument {
             
             pos += imgsize;
         }
-
-               _pictures = (PictureData[])p.toArray(new PictureData[p.size()]);
        }
 
 
@@ -453,10 +451,10 @@ public final class HSLFSlideShow extends POIDocument {
 
        
         // Write any pictures, into another stream
-        if (_pictures != null) {
+        if (_pictures.size() > 0) {
             ByteArrayOutputStream pict = new ByteArrayOutputStream();
-            for (int i = 0; i < _pictures.length; i++ ) {
-                _pictures[i].write(pict);
+            for (PictureData p : _pictures) {
+                p.write(pict);
             }
             outFS.createDocument(
                 new ByteArrayInputStream(pict.toByteArray()), "Pictures"
@@ -502,21 +500,21 @@ public final class HSLFSlideShow extends POIDocument {
        }
        
        /**
-        *  Add a new picture to this presentation.
+        * Add a new picture to this presentation.
+     *
+     * @return offset of this picture in the Pictures stream
         */
-       public void addPicture(PictureData img) {
-               // Copy over the existing pictures, into an array one bigger
-               PictureData[] lst;
-               if(_pictures == null) {
-                       lst = new PictureData[1];
-               } else {
-                       lst = new PictureData[(_pictures.length+1)];
-                       System.arraycopy(_pictures,0,lst,0,_pictures.length);
-               }
-               // Add in the new image
-               lst[lst.length - 1] = img;
-               _pictures = lst;
-       }
+       public int addPicture(PictureData img) {
+               int offset = 0;
+
+        if(_pictures.size() > 0){
+            PictureData prev = _pictures.get(_pictures.size() - 1);
+            offset = prev.getOffset() + prev.getRawData().length + 8;
+        }
+        img.setOffset(offset);
+        _pictures.add(img);
+        return offset;
+   }
 
        /* ******************* fetching methods follow ********************* */
 
@@ -544,7 +542,7 @@ public final class HSLFSlideShow extends POIDocument {
         *  presentation doesn't contain pictures.
         */
        public PictureData[] getPictures() {
-               return _pictures;
+               return _pictures.toArray(new PictureData[_pictures.size()]);
        }
 
     /**
index 6414d177ce53aad2b04ede739d31d6ca1c29e2aa..1c210365129b0d00d6610def48612c6ca4712a76 100644 (file)
@@ -739,7 +739,6 @@ public final class SlideShow {
         byte[] uid = PictureData.getChecksum(data);
 
         EscherContainerRecord bstore;
-        int offset = 0;
 
         EscherContainerRecord dggContainer = _documentRecord.getPPDrawingGroup().getDggContainer();
         bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
@@ -763,13 +762,13 @@ public final class SlideShow {
                 if (Arrays.equals(bse.getUid(), uid)){
                     return i + 1;
                 }
-                offset += bse.getSize();
              }
         }
 
         PictureData pict = PictureData.create(format);
         pict.setData(data);
-        pict.setOffset(offset);
+
+        int offset = _hslfSlideShow.addPicture(pict);
 
         EscherBSERecord bse = new EscherBSERecord();
         bse.setRecordId(EscherBSERecord.RECORD_ID);
@@ -786,13 +785,12 @@ public final class SlideShow {
 
         bse.setRef(0);
         bse.setOffset(offset);
+        bse.setRemainingData( new byte[0] );
 
         bstore.addChildRecord(bse);
         int count = bstore.getChildRecords().size();
         bstore.setOptions((short)( (count << 4) | 0xF ));
 
-        _hslfSlideShow.addPicture(pict);
-
         return count;
     }
 
index 78458f0630d291cacef63767dab49d13d90c804b..2944a74664f4d3ab9ef5000eab30811dfc882e9e 100644 (file)
@@ -436,7 +436,21 @@ public class TestPictures extends TestCase{
         pdata = pict.getPictureData();
         assertTrue(pdata instanceof WMF);
         assertEquals(Picture.WMF, pdata.getType());
-       }
+
+        //add a new picture, it should be correctly appended to the Pictures stream
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        for(PictureData p : pictures) p.write(out);
+        out.close();
+
+        int streamSize = out.size();
+
+        PictureData data = PictureData.create(Picture.JPEG);
+        data.setData(new byte[100]);
+        int offset = hslf.addPicture(data);
+        assertEquals(streamSize, offset);
+        assertEquals(3, ppt.getPictureData().length);
+
+    }
 
     public void testGetPictureName() throws Exception {
         SlideShow ppt = new SlideShow(new HSLFSlideShow(new File(cwd, "ppt_with_png.ppt").getPath()));