]> source.dussan.org Git - poi.git/commitdiff
support caching images in xslf
authorYegor Kozlov <yegor@apache.org>
Wed, 8 Feb 2012 13:44:12 +0000 (13:44 +0000)
committerYegor Kozlov <yegor@apache.org>
Wed, 8 Feb 2012 13:44:12 +0000 (13:44 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1241896 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java

index 9ffcee684bc8bab1bf0bbce5e56131233f8f8e28..22f7ea4a32fadb853f1e3ec9aefe09e9a1fa2d14 100644 (file)
@@ -19,17 +19,10 @@ package org.apache.poi.xslf.usermodel;
 import org.apache.poi.POIXMLDocument;
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.POIXMLException;
+import org.apache.poi.POIXMLRelation;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
-import org.apache.poi.openxml4j.opc.OPCPackage;
-import org.apache.poi.openxml4j.opc.PackagePart;
-import org.apache.poi.openxml4j.opc.PackagePartName;
-import org.apache.poi.openxml4j.opc.TargetMode;
-import org.apache.poi.util.Beta;
-import org.apache.poi.util.Internal;
-import org.apache.poi.util.POILogFactory;
-import org.apache.poi.util.POILogger;
-import org.apache.poi.util.PackageHelper;
-import org.apache.poi.util.Units;
+import org.apache.poi.openxml4j.opc.*;
+import org.apache.poi.util.*;
 import org.apache.poi.xslf.XSLFSlideShow;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlObject;
@@ -338,21 +331,40 @@ public class XMLSlideShow  extends POIXMLDocument {
      * @see XSLFPictureData#PICTURE_TYPE_DIB
      */
     public int addPicture(byte[] pictureData, int format) {
-        getAllPictures();
-        
-        int imageNumber = _pictures.size() + 1;
-        XSLFPictureData img = (XSLFPictureData) createRelationship(
-                XSLFPictureData.RELATIONS[format], XSLFFactory.getInstance(), imageNumber, true);
-        _pictures.add(img);
-        try {
-            OutputStream out = img.getPackagePart().getOutputStream();
-            out.write(pictureData);
-            out.close();
-        } catch (IOException e) {
-            throw new POIXMLException(e);
+        XSLFPictureData img = findPictureData(pictureData);
+        POIXMLRelation relDesc = XSLFPictureData.RELATIONS[format];
+
+        if(img == null) {
+            int imageNumber = _pictures.size();
+            img = (XSLFPictureData) createRelationship(
+                    XSLFPictureData.RELATIONS[format], XSLFFactory.getInstance(), imageNumber + 1, true);
+            _pictures.add(img);
+            try {
+                OutputStream out = img.getPackagePart().getOutputStream();
+                out.write(pictureData);
+                out.close();
+            } catch (IOException e) {
+                throw new POIXMLException(e);
+            }
+            return _pictures.size() - 1;
+        } else {
+            return _pictures.indexOf(img);
         }
-        return imageNumber - 1;
     }
+
+    /**
+     * check if a picture with this picture data already exists in this presentation
+     */
+    XSLFPictureData findPictureData(byte[] pictureData){
+        long checksum = IOUtils.calculateChecksum(pictureData);
+        for(XSLFPictureData pic : getAllPictures()){
+            if(pic.getChecksum() == checksum) {
+                return pic;
+            }
+        }
+        return null;
+    }
+
     public XSLFTableStyles getTableStyles(){
         return _tableStyles;
     }
index 6fd151ff17610bbbf48455ad607d7d3d8ba58264..5bf3703621eca32cf533b7330285db65064112a1 100644 (file)
@@ -168,4 +168,11 @@ public final class XSLFPictureData extends POIXMLDocumentPart {
         return 0;
     }
 
+    long getChecksum(){
+        if(checksum == null){
+            byte[] pictureData = getData();
+            checksum = IOUtils.calculateChecksum(pictureData);
+        }
+        return checksum;
+    }
 }
\ No newline at end of file
index 7d42efa38d6da1302466d3fd96ecc4cc1b7c12ba..bf56481f5984db374824e7455ad2ed1341d0ba28 100644 (file)
@@ -113,4 +113,21 @@ public class TestXSLFPictureShape extends TestCase {
             assertTrue(Arrays.equals(data1, data.getData()));\r
         }\r
     }\r
+\r
+    public void testImageCaching() {\r
+        XMLSlideShow ppt = new XMLSlideShow();\r
+        byte[] img1 = new byte[]{1,2,3};\r
+        byte[] img2 = new byte[]{3,4,5};\r
+        int idx1 = ppt.addPicture(img1, XSLFPictureData.PICTURE_TYPE_PNG);\r
+        assertEquals(0, idx1);\r
+        assertEquals(0, ppt.addPicture(img1, XSLFPictureData.PICTURE_TYPE_PNG));\r
+\r
+        int idx2 = ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG);\r
+        assertEquals(1, idx2);\r
+        assertEquals(1, ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG));\r
+\r
+        XSLFSlide slide1 = ppt.createSlide();\r
+        XSLFSlide slide2 = ppt.createSlide();\r
+\r
+    }\r
 }
\ No newline at end of file