]> source.dussan.org Git - poi.git/commitdiff
Correctly increment the reference count of a blip when a picture is inserted
authorYegor Kozlov <yegor@apache.org>
Sun, 8 Jun 2008 12:30:25 +0000 (12:30 +0000)
committerYegor Kozlov <yegor@apache.org>
Sun, 8 Jun 2008 12:30:25 +0000 (12:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@664490 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ddf/EscherDggRecord.java
src/scratchpad/src/org/apache/poi/hslf/model/Picture.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java
src/scratchpad/testcases/org/apache/poi/hslf/model/TestPicture.java [new file with mode: 0755]

index 0ff00016d506a00d5b03284f84b00fc6ce517ed0..cdc9281a8e4eef3c915ed8b02d4193bf28667e2d 100644 (file)
@@ -238,6 +238,10 @@ public class EscherDggRecord
         return maxDgId;
     }
 
+    public void setMaxDrawingGroupId(int id){
+        maxDgId = id;
+    }
+
      public FileIdCluster[] getFileIdClusters()
     {
         return field_5_fileIdClusters;
index 375249b519c79b15f82068a5eba0901fc1193d8d..34b1fd89bcf2e1875143d49047f2308b07c6ded8 100644 (file)
@@ -176,16 +176,11 @@ public class Picture extends SimpleShape {
     public PictureData getPictureData(){
         SlideShow ppt = getSheet().getSlideShow();
         PictureData[] pict = ppt.getPictureData();
-        Document doc = ppt.getDocumentRecord();
-        EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
-        EscherContainerRecord bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
 
-        List lst = bstore.getChildRecords();
-        int idx = getPictureIndex();
-        if (idx == 0){
+        EscherBSERecord bse = getEscherBSERecord();
+        if (bse == null){
             logger.log(POILogger.ERROR, "no reference to picture data found ");
         } else {
-            EscherBSERecord bse = (EscherBSERecord)lst.get(idx-1);
             for ( int i = 0; i < pict.length; i++ ) {
                 if (pict[i].getOffset() ==  bse.getOffset()){
                     return pict[i];
@@ -196,6 +191,21 @@ public class Picture extends SimpleShape {
         return null;
     }
 
+    protected EscherBSERecord getEscherBSERecord(){
+        SlideShow ppt = getSheet().getSlideShow();
+        Document doc = ppt.getDocumentRecord();
+        EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
+        EscherContainerRecord bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
+
+        List lst = bstore.getChildRecords();
+        int idx = getPictureIndex();
+        if (idx == 0){
+            return null;
+        } else {
+            return (EscherBSERecord)lst.get(idx-1);
+        }
+    }
+
     /**
      * Name of this picture.
      *
@@ -238,6 +248,10 @@ public class Picture extends SimpleShape {
      */
     protected void afterInsert(Sheet sh){
         super.afterInsert(sh);
+
+        EscherBSERecord bse = getEscherBSERecord();
+        bse.setRef(bse.getRef() + 1);
+
         java.awt.Rectangle anchor = getAnchor();
         if (anchor.equals(new java.awt.Rectangle())){
             setDefaultSize();
@@ -249,21 +263,8 @@ public class Picture extends SimpleShape {
         ShapePainter.paint(this, graphics);
 
         PictureData data = getPictureData();
-        if (data  instanceof Bitmap){
-            BufferedImage img = null;
-            try {
-                       img = ImageIO.read(new ByteArrayInputStream(data.getData()));
-            }
-            catch (Exception e){
-                logger.log(POILogger.WARN, "ImageIO failed to create image. image.type: " + data.getType());
-                return;
-            }
-            Rectangle anchor = getAnchor();
-            Image scaledImg = img.getScaledInstance(anchor.width, anchor.height, Image.SCALE_SMOOTH);
-            graphics.drawImage(scaledImg, anchor.x, anchor.y, null);
-        } else {
-            logger.log(POILogger.WARN, "Rendering of metafiles is not yet supported. image.type: " + (data == null ? "NA" : data.getClass().getName()));
-        }
+        data.draw(graphics, this);
+
         graphics.setTransform(at);
     }
 }
index d969c5b8810a41104f58ee86fd039b1319b7c247..56e94431ec1cbbb30aca50d0c23d62134e463333 100644 (file)
@@ -734,7 +734,7 @@ public class SlideShow
         else if (format == Picture.WMF) bse.setBlipTypeMacOS((byte)Picture.PICT);
         else if (format == Picture.PICT) bse.setBlipTypeWin32((byte)Picture.WMF);
 
-        bse.setRef(1);
+        bse.setRef(0);
         bse.setOffset(offset);
 
         bstore.addChildRecord(bse);
diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestPicture.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestPicture.java
new file mode 100755 (executable)
index 0000000..cec4f19
--- /dev/null
@@ -0,0 +1,73 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.hslf.model;\r
+\r
+import junit.framework.*;\r
+\r
+import java.io.FileOutputStream;\r
+import java.io.File;\r
+import java.awt.*;\r
+\r
+import org.apache.poi.hslf.usermodel.SlideShow;\r
+import org.apache.poi.hslf.HSLFSlideShow;\r
+import org.apache.poi.ddf.EscherBSERecord;\r
+\r
+/**\r
+ * Test Picture shape.\r
+ * \r
+ * @author Yegor Kozlov\r
+ */\r
+public class TestPicture extends TestCase {\r
+\r
+    /**\r
+     * Test that the reference count of a blip is incremented every time the picture is inserted.\r
+     * This is important when the same image appears multiple times in a slide show.\r
+     *\r
+     */\r
+    public void testMultiplePictures() throws Exception {\r
+        String cwd = System.getProperty("HSLF.testdata.path");\r
+        SlideShow ppt = new SlideShow();\r
+\r
+        Slide s = ppt.createSlide();\r
+        Slide s2 = ppt.createSlide();\r
+        Slide s3 = ppt.createSlide();\r
+\r
+        int idx = ppt.addPicture(new File(cwd, "clock.jpg"), Picture.JPEG);\r
+        Picture pict = new Picture(idx);\r
+        Picture pict2 = new Picture(idx);\r
+        Picture pict3 = new Picture(idx);\r
+\r
+        pict.setAnchor(new Rectangle(10,10,100,100));\r
+        s.addShape(pict);\r
+        EscherBSERecord bse1 = pict.getEscherBSERecord();\r
+        assertEquals(1, bse1.getRef());\r
+\r
+        pict2.setAnchor(new Rectangle(10,10,100,100));\r
+        s2.addShape(pict2);\r
+        EscherBSERecord bse2 = pict.getEscherBSERecord();\r
+        assertSame(bse1, bse2);\r
+        assertEquals(2, bse1.getRef());\r
+\r
+        pict3.setAnchor(new Rectangle(10,10,100,100));\r
+        s3.addShape(pict3);\r
+        EscherBSERecord bse3 = pict.getEscherBSERecord();\r
+        assertSame(bse2, bse3);\r
+        assertEquals(3, bse1.getRef());\r
+\r
+    }\r
+\r
+}\r