]> source.dussan.org Git - poi.git/commitdiff
#58190 - The current picture handling uses raw integers for types and index, replace...
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 2 Aug 2015 20:21:35 +0000 (20:21 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 2 Aug 2015 20:21:35 +0000 (20:21 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1693825 13f79535-47bb-0310-9956-ffa450edef68

35 files changed:
src/examples/src/org/apache/poi/hslf/examples/DataExtraction.java
src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java
src/java/org/apache/poi/sl/usermodel/PictureData.java
src/java/org/apache/poi/sl/usermodel/SlideShow.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFRelation.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShapeContainer.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java
src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java
src/scratchpad/src/org/apache/poi/hslf/blip/Bitmap.java
src/scratchpad/src/org/apache/poi/hslf/blip/DIB.java
src/scratchpad/src/org/apache/poi/hslf/blip/EMF.java
src/scratchpad/src/org/apache/poi/hslf/blip/JPEG.java
src/scratchpad/src/org/apache/poi/hslf/blip/PICT.java
src/scratchpad/src/org/apache/poi/hslf/blip/PNG.java
src/scratchpad/src/org/apache/poi/hslf/blip/WMF.java
src/scratchpad/src/org/apache/poi/hslf/extractor/ImageExtractor.java
src/scratchpad/src/org/apache/poi/hslf/model/ActiveXShape.java
src/scratchpad/src/org/apache/poi/hslf/model/MovieShape.java
src/scratchpad/src/org/apache/poi/hslf/model/OLEShape.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFFill.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFPictureData.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFPictureShape.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java
src/scratchpad/testcases/org/apache/poi/hslf/model/TestBackground.java
src/scratchpad/testcases/org/apache/poi/hslf/model/TestMovieShape.java
src/scratchpad/testcases/org/apache/poi/hslf/model/TestOleEmbedding.java
src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPicture.java
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java

index 3c50699b804271391a075c86e80fdf05b89f2200..514c01ba08a3c6cdb40e1172ccad3badfafde556 100644 (file)
@@ -77,6 +77,7 @@ public final class DataExtraction {
                     if ("Worksheet".equals(name)) {
 
                         //read xls
+                        @SuppressWarnings({ "unused", "resource" })
                         HSSFWorkbook wb = new HSSFWorkbook(data.getData());
 
                     } else if ("Document".equals(name)) {
@@ -110,31 +111,7 @@ public final class DataExtraction {
                     picIdx++;
                     HSLFPictureShape p = (HSLFPictureShape) shape;
                     HSLFPictureData data = p.getPictureData();
-                    String name = p.getPictureName();
-                    int type = data.getType();
-                    String ext;
-                    switch (type) {
-                        case HSLFPictureShape.JPEG:
-                            ext = ".jpg";
-                            break;
-                        case HSLFPictureShape.PNG:
-                            ext = ".png";
-                            break;
-                        case HSLFPictureShape.WMF:
-                            ext = ".wmf";
-                            break;
-                        case HSLFPictureShape.EMF:
-                            ext = ".emf";
-                            break;
-                        case HSLFPictureShape.PICT:
-                            ext = ".pict";
-                            break;
-                        case HSLFPictureShape.DIB:
-                            ext = ".dib";
-                            break;
-                        default:
-                            continue;
-                    }
+                    String ext = data.getType().extension;
                     FileOutputStream out = new FileOutputStream("pict-" + picIdx + ext);
                     out.write(data.getData());
                     out.close();
index da7b980bde031b309097a66ed114d3b418e07d6a..ae134de765b5f5bff8c549af4b8bc8e97c42b773 100644 (file)
@@ -25,6 +25,7 @@ import java.io.File;
 import java.io.FileInputStream;\r
 import java.io.FileOutputStream;\r
 import java.io.IOException;\r
+import org.apache.poi.sl.usermodel.PictureData.PictureType;\r
 \r
 /**\r
  * Images\r
@@ -39,7 +40,7 @@ public class Tutorial5 {
         XSLFSlide slide = ppt.createSlide();\r
         File img = new File(System.getProperty("POI.testdata.path"), "slideshow/clock.jpg");\r
         byte[] data = IOUtils.toByteArray(new FileInputStream(img));\r
-        int pictureIndex = ppt.addPicture(data, XSLFPictureData.PICTURE_TYPE_PNG);\r
+        XSLFPictureData pictureIndex = ppt.addPicture(data, PictureType.PNG);\r
 \r
         /*XSLFPictureShape shape =*/ slide.createPicture(pictureIndex);\r
 \r
index 8697d33965fe480434255daa095dd97d7a371811..e50149f7d567878a517572de4ff234b966059f9b 100644 (file)
@@ -20,8 +20,80 @@ package org.apache.poi.sl.usermodel;
 import java.io.IOException;
 
 public interface PictureData {
-       public String getContentType();
+    
+    enum PictureType {
+        /** Extended windows meta file */
+        EMF(2,2,"image/x-emf",".emf"),
+        /** Windows Meta File */
+        WMF(3,3,"image/x-wmf",".wmf"),
+        /** Mac PICT format */
+        PICT(4,4,"image/pict",".pict"), // or image/x-pict (for HSLF) ???
+        /** JPEG format */
+        JPEG(5,5,"image/jpeg",".jpg"),
+        /** PNG format */
+        PNG(6,6,"image/png",".png"),
+        /** Device independent bitmap */
+        DIB(7,7,"image/dib",".dib"),
+        /** GIF image format */
+        GIF(-1,8,"image/gif",".gif"),
+        /** Tag Image File (.tiff) */
+        TIFF(-1,9,"image/tiff",".tif"),
+        /** Encapsulated Postscript (.eps) */
+        EPS(-1,10,"image/x-eps",".eps"),
+        /** Windows Bitmap (.bmp) */
+        BMP(-1,11,"image/x-ms-bmp",".bmp"),
+        /** WordPerfect graphics (.wpg) */
+        WPG(-1,12,"image/x-wpg",".wpg"),
+        /** Microsoft Windows Media Photo image (.wdp) */
+        WDP(-1,13,"image/vnd.ms-photo",".wdp");
+        
+        public final int nativeId, ooxmlId;
+        public final String contentType,extension;
 
-       public byte[] getData();
-       public void setData(byte[] data) throws IOException;
+        PictureType(int nativeId, int ooxmlId,String contentType,String extension) {
+            this.nativeId = nativeId;
+            this.ooxmlId = ooxmlId;
+            this.contentType = contentType;
+            this.extension = extension;
+        }
+        
+        public static PictureType forNativeID(int nativeId) {
+            for (PictureType ans : values()) {
+                if (ans.nativeId == nativeId) return ans;
+            }
+            return null;
+        }
+
+        public static PictureType forOoxmlID(int ooxmlId) {
+            for (PictureType ans : values()) {
+                if (ans.ooxmlId == ooxmlId) return ans;
+            }
+            return null;
+        }
+    }
+    
+    
+    /**
+     * Returns content type (mime type) of this picture.
+     *
+     * @return content type of this picture.
+     */
+       String getContentType();
+       
+       /**
+        * @return the picture type
+        */
+       PictureType getType();
+
+    /**
+     * Returns the binary data of this Picture
+     * @return picture data
+     */
+       byte[] getData();
+
+    /**
+     * Sets the binary picture data
+     * @param data picture data
+     */
+       void setData(byte[] data) throws IOException;
 }
index ca0ddf39182682b595d103f36df7c6c4aac5a780..5ced5b21e2d6ef639deb15863efac2d16eba5091 100644 (file)
@@ -21,6 +21,9 @@ import java.awt.Dimension;
 import java.io.IOException;
 import java.util.List;
 
+import org.apache.poi.sl.usermodel.PictureData;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
+
 public interface SlideShow {
        Slide<? extends Shape, ? extends SlideShow, ? extends Notes<?,?>> createSlide() throws IOException;
 
@@ -41,5 +44,16 @@ public interface SlideShow {
      *
      * @return the page size
      */
-    Dimension getPageSize();    
+    Dimension getPageSize();
+    
+
+    /**
+     * Adds a picture to the workbook.
+     *
+     * @param pictureData       The bytes of the picture
+     * @param format            The format of the picture.
+     *
+     * @return the new picture reference
+     */
+    PictureData addPicture(byte[] pictureData, PictureType format) throws IOException;
 }
index ae2ed3a09c41d53e58a22b48be9e2e23751b1389..ecfa5cd62596a0b26a2b6eeacdcfabf47a9f836b 100644 (file)
@@ -36,6 +36,7 @@ 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.sl.usermodel.MasterSheet;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
 import org.apache.poi.sl.usermodel.Resources;
 import org.apache.poi.sl.usermodel.SlideShow;
 import org.apache.poi.util.Beta;
@@ -192,7 +193,9 @@ public class XMLSlideShow extends POIXMLDocument implements SlideShow {
             List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/ppt/media/.*?"));
             _pictures = new ArrayList<XSLFPictureData>(mediaParts.size());
             for(PackagePart part : mediaParts){
-                _pictures.add(new XSLFPictureData(part, null));    
+                XSLFPictureData pd = new XSLFPictureData(part, null);
+                pd.setIndex(_pictures.size());
+                _pictures.add(pd);
             }
         }
         return Collections.unmodifiableList(_pictures);
@@ -442,26 +445,28 @@ public class XMLSlideShow extends POIXMLDocument implements SlideShow {
      * @see XSLFPictureData#PICTURE_TYPE_PNG
      * @see XSLFPictureData#PICTURE_TYPE_DIB
      */
-    public int addPicture(byte[] pictureData, int format) {
+    public XSLFPictureData addPicture(byte[] pictureData, PictureType format) {
         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);
+
+        if (img != null) return img;
+        
+        int imageNumber = _pictures.size();
+        XSLFRelation relType = XSLFPictureData.getRelationForType(format);
+        if (relType == null) {
+            throw new IllegalArgumentException("Picture type "+format+" is not supported.");
         }
+        img = (XSLFPictureData) createRelationship(relType, XSLFFactory.getInstance(), imageNumber + 1, true);
+        img.setIndex(imageNumber);
+        _pictures.add(img);
+        try {
+            OutputStream out = img.getPackagePart().getOutputStream();
+            out.write(pictureData);
+            out.close();
+        } catch (IOException e) {
+            throw new POIXMLException(e);
+        }
+        
+        return img;
     }
 
     /**
@@ -492,7 +497,7 @@ public class XMLSlideShow extends POIXMLDocument implements SlideShow {
         return null;
     }
 
-    public MasterSheet createMasterSheet() throws IOException {
+    public MasterSheet<XSLFShape, XMLSlideShow> createMasterSheet() throws IOException {
         // TODO: implement!
         throw new UnsupportedOperationException();
     }
index 4fc9e0f748969677d5b7ee028269a089ce482d4a..c2fb36f88567939f5baf5422cd8be6d5cec5c7b4 100644 (file)
@@ -23,7 +23,6 @@ import java.awt.geom.Rectangle2D;
 import java.util.ArrayList;\r
 import java.util.Iterator;\r
 import java.util.List;\r
-import java.util.regex.Pattern;\r
 \r
 import org.apache.poi.openxml4j.opc.PackagePart;\r
 import org.apache.poi.openxml4j.opc.PackageRelationship;\r
@@ -239,16 +238,8 @@ public class XSLFGroupShape extends XSLFShape implements XSLFShapeContainer, Gro
         return sh;\r
     }\r
 \r
-    public XSLFPictureShape createPicture(int pictureIndex){\r
-\r
-        List<PackagePart>  pics = getSheet().getPackagePart().getPackage()\r
-                .getPartsByName(Pattern.compile("/ppt/media/image" + (pictureIndex + 1) + ".*?"));\r
-\r
-        if(pics.size() == 0) {\r
-            throw new IllegalArgumentException("Picture with index=" + pictureIndex + " was not found");\r
-        }\r
-\r
-        PackagePart pic = pics.get(0);\r
+    public XSLFPictureShape createPicture(XSLFPictureData pictureData){\r
+        PackagePart pic = pictureData.getPackagePart();\r
 \r
         PackageRelationship rel = getSheet().getPackagePart().addRelationship(\r
                 pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation());\r
@@ -321,8 +312,8 @@ public class XSLFGroupShape extends XSLFShape implements XSLFShapeContainer, Gro
             } else if (shape instanceof XSLFPictureShape) {\r
                 XSLFPictureShape p = (XSLFPictureShape)shape;\r
                 XSLFPictureData pd = p.getPictureData();\r
-                int picId = getSheet().getSlideShow().addPicture(pd.getData(), pd.getPictureType());\r
-                newShape = createPicture(picId);\r
+                XSLFPictureData pdNew = getSheet().getSlideShow().addPicture(pd.getData(), pd.getType());\r
+                newShape = createPicture(pdNew);\r
             } else if (shape instanceof XSLFGroupShape) {\r
                 newShape = createGroup();\r
             } else if (shape instanceof XSLFTable) {\r
index 8e20be90b323fa1bec35f1fa9e685878a5476299..e6fc0fd4ef74ecef29cf86266aceb4f5cd6760fb 100644 (file)
@@ -25,7 +25,6 @@ import java.io.OutputStream;
 
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.POIXMLException;
-import org.apache.poi.POIXMLRelation;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.sl.usermodel.PictureData;
@@ -34,94 +33,11 @@ import org.apache.poi.util.IOUtils;
 
 /**
  * Instantiates sub-classes of POIXMLDocumentPart depending on their relationship type
- *
- * @author Yegor Kozlov
  */
 @Beta
 public final class XSLFPictureData extends POIXMLDocumentPart implements PictureData {
-    /**
-     * Extended windows meta file
-     */
-    public static final int PICTURE_TYPE_EMF = 2;
-
-    /**
-     * Windows Meta File
-     */
-    public static final int PICTURE_TYPE_WMF = 3;
-
-    /**
-     * Mac PICT format
-     */
-    public static final int PICTURE_TYPE_PICT = 4;
-
-    /**
-     * JPEG format
-     */
-    public static final int PICTURE_TYPE_JPEG = 5;
-
-    /**
-     * PNG format
-     */
-    public static final int PICTURE_TYPE_PNG = 6;
-
-    /**
-     * Device independent bitmap
-     */
-    public static final int PICTURE_TYPE_DIB = 7;
-
-    /**
-     * GIF image format
-     */
-    public static final int PICTURE_TYPE_GIF = 8;
-
-    /**
-     * Tag Image File (.tiff)
-     */
-    public static final int PICTURE_TYPE_TIFF = 9;
-
-    /**
-     * Encapsulated Postscript (.eps)
-     */
-    public static final int PICTURE_TYPE_EPS = 10;
-
-
-    /**
-     * Windows Bitmap (.bmp)
-     */
-    public static final int PICTURE_TYPE_BMP = 11;
-
-    /**
-     * WordPerfect graphics (.wpg)
-     */
-    public static final int PICTURE_TYPE_WPG = 12;
-
-    /**
-     * Microsoft Windows Media Photo image (.wdp)
-     */
-    public static final int PICTURE_TYPE_WDP = 13;
-
-    /**
-     * Relationships for each known picture type
-     */
-    protected static final POIXMLRelation[] RELATIONS;
-
-    static {
-        RELATIONS = new POIXMLRelation[14];
-        RELATIONS[PICTURE_TYPE_EMF] = XSLFRelation.IMAGE_EMF;
-        RELATIONS[PICTURE_TYPE_WMF] = XSLFRelation.IMAGE_WMF;
-        RELATIONS[PICTURE_TYPE_PICT] = XSLFRelation.IMAGE_PICT;
-        RELATIONS[PICTURE_TYPE_JPEG] = XSLFRelation.IMAGE_JPEG;
-        RELATIONS[PICTURE_TYPE_PNG] = XSLFRelation.IMAGE_PNG;
-        RELATIONS[PICTURE_TYPE_DIB] = XSLFRelation.IMAGE_DIB;
-        RELATIONS[PICTURE_TYPE_GIF] = XSLFRelation.IMAGE_GIF;
-        RELATIONS[PICTURE_TYPE_TIFF] = XSLFRelation.IMAGE_TIFF;
-        RELATIONS[PICTURE_TYPE_EPS] = XSLFRelation.IMAGE_EPS;
-        RELATIONS[PICTURE_TYPE_BMP] = XSLFRelation.IMAGE_BMP;
-        RELATIONS[PICTURE_TYPE_WPG] = XSLFRelation.IMAGE_WPG;
-        RELATIONS[PICTURE_TYPE_WDP] = XSLFRelation.IMAGE_WDP;
-    }
-
     private Long checksum = null;
+    private int index = -1;
 
     /**
      * Create a new XSLFGraphicData node
@@ -150,12 +66,12 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture
     public InputStream getInputStream() throws IOException {
         return getPackagePart().getInputStream();
     }
-    
+
     /**
      * Gets the picture data as a byte array.
      *
      * You can grab the picture data directly from the underlying package part with the {@link #getInputStream()} method
-     * 
+     *
      * @return the Picture data.
      */
     public byte[] getData() {
@@ -187,25 +103,6 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture
         return getPackagePart().getPartName().getExtension();
     }
 
-    /**
-     * Return an integer constant that specifies type of this picture
-     *
-     * @return an integer constant that specifies type of this picture
-     */
-    public int getPictureType() {
-        String contentType = getPackagePart().getContentType();
-        for (int i = 0; i < RELATIONS.length; i++) {
-            if (RELATIONS[i] == null) {
-                continue;
-            }
-
-            if (RELATIONS[i].getContentType().equals(contentType)) {
-                return i;
-            }
-        }
-        return 0;
-    }
-
     long getChecksum(){
         if(checksum == null){
             try {
@@ -218,17 +115,17 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture
     }
 
     /**
-     * *PictureData objects store the actual content in the part directly without keeping a 
+     * *PictureData objects store the actual content in the part directly without keeping a
      * copy like all others therefore we need to handle them differently.
      */
     @Override
     protected void prepareForCommit() {
         // do not clear the part here
     }
-    
+
+    @Override
     public String getContentType() {
-        POIXMLRelation rel = RELATIONS[getPictureType()];
-        return (rel == null) ? null : rel.getContentType();
+        return getPackagePart().getContentType();
     }
 
     public void setData(byte[] data) throws IOException {
@@ -238,6 +135,65 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture
         // recalculate now since we already have the data bytes available anyhow
         checksum = IOUtils.calculateChecksum(data);
     }
+
+    @Override
+    public PictureType getType() {
+        String ct = getContentType();
+        if (XSLFRelation.IMAGE_EMF.getContentType().equals(ct)) {
+            return PictureType.EMF;
+        } else if (XSLFRelation.IMAGE_WMF.getContentType().equals(ct)) {
+            return PictureType.WMF;
+        } else if (XSLFRelation.IMAGE_PICT.getContentType().equals(ct)) {
+            return PictureType.PICT;
+        } else if (XSLFRelation.IMAGE_JPEG.getContentType().equals(ct)) {
+            return PictureType.JPEG;
+        } else if (XSLFRelation.IMAGE_PNG.getContentType().equals(ct)) {
+            return PictureType.PNG;
+        } else if (XSLFRelation.IMAGE_DIB.getContentType().equals(ct)) {
+            return PictureType.DIB;
+        } else if (XSLFRelation.IMAGE_GIF.getContentType().equals(ct)) {
+            return PictureType.GIF;
+        } else if (XSLFRelation.IMAGE_EPS.getContentType().equals(ct)) {
+            return PictureType.EPS;
+        } else if (XSLFRelation.IMAGE_BMP.getContentType().equals(ct)) {
+            return PictureType.BMP;
+        } else if (XSLFRelation.IMAGE_WPG.getContentType().equals(ct)) {
+            return PictureType.WPG;
+        } else if (XSLFRelation.IMAGE_WDP.getContentType().equals(ct)) {
+            return PictureType.WDP;
+        } else {
+            return null;
+        }
+    }
     
-    
+    /* package */ static XSLFRelation getRelationForType(PictureType pt) {
+        switch (pt) {
+            case EMF: return XSLFRelation.IMAGE_EMF;
+            case WMF: return XSLFRelation.IMAGE_WMF;
+            case PICT: return XSLFRelation.IMAGE_PICT;
+            case JPEG: return XSLFRelation.IMAGE_JPEG;
+            case PNG: return XSLFRelation.IMAGE_PNG;
+            case DIB: return XSLFRelation.IMAGE_DIB;
+            case GIF: return XSLFRelation.IMAGE_GIF;
+            case EPS: return XSLFRelation.IMAGE_EPS;
+            case BMP: return XSLFRelation.IMAGE_BMP;
+            case WPG: return XSLFRelation.IMAGE_WPG;
+            case WDP: return XSLFRelation.IMAGE_WDP;
+            default: return null;
+        }
+    }
+
+    /**
+     * @return the 0-based index of this pictures within the picture parts
+     */
+    public int getIndex() {
+        return index;
+    }
+
+    /**
+     * @param index sets the 0-based index of this pictures within the picture parts
+     */
+    public void setIndex(int index) {
+        this.index = index;
+    }
 }
\ No newline at end of file
index 82e073167f6168bb36b6e36c9addfe5bf44078f7..b2e3ff2bbc548a5de74b7507bcd5b93d7b2c5f27 100644 (file)
@@ -21,15 +21,12 @@ import java.util.Map;
 
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.POIXMLRelation;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
 import org.apache.poi.util.Beta;
-import org.apache.poi.util.POILogFactory;
-import org.apache.poi.util.POILogger;
 
 @Beta
 public class XSLFRelation extends POIXMLRelation {
 
-   private static final POILogger log = POILogFactory.getLogger(XSLFRelation.class);
-
    /**
     * A map to lookup POIXMLRelation by its relation type
     */
@@ -148,76 +145,76 @@ public class XSLFRelation extends POIXMLRelation {
     );
 
     public static final XSLFRelation IMAGE_EMF = new XSLFRelation(
-          "image/x-emf",
+          PictureType.EMF.contentType,
           "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
           "/ppt/media/image#.emf",
           XSLFPictureData.class
-       );
-       public static final XSLFRelation IMAGE_WMF = new XSLFRelation(
-             "image/x-wmf",
-             "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-             "/ppt/media/image#.wmf",
-             XSLFPictureData.class
-       );
-       public static final XSLFRelation IMAGE_PICT = new XSLFRelation(
-             "image/pict",
-             "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-             "/ppt/media/image#.pict",
-             XSLFPictureData.class
-       );
-       public static final XSLFRelation IMAGE_JPEG = new XSLFRelation(
-             "image/jpeg",
-             "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-             "/ppt/media/image#.jpeg",
-             XSLFPictureData.class
-       );
-       public static final XSLFRelation IMAGE_PNG = new XSLFRelation(
-             "image/png",
-             "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-             "/ppt/media/image#.png",
-             XSLFPictureData.class
-       );
-       public static final XSLFRelation IMAGE_DIB = new XSLFRelation(
-             "image/dib",
-             "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-             "/ppt/media/image#.dib",
-             XSLFPictureData.class
-       );
-       public static final XSLFRelation IMAGE_GIF = new XSLFRelation(
-             "image/gif",
-             "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-             "/ppt/media/image#.gif",
-             XSLFPictureData.class
-       );
+    );
+    public static final XSLFRelation IMAGE_WMF = new XSLFRelation(
+          PictureType.WMF.contentType,
+          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+          "/ppt/media/image#.wmf",
+          XSLFPictureData.class
+    );
+    public static final XSLFRelation IMAGE_PICT = new XSLFRelation(
+          PictureType.PICT.contentType,
+          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+          "/ppt/media/image#.pict",
+          XSLFPictureData.class
+    );
+    public static final XSLFRelation IMAGE_JPEG = new XSLFRelation(
+          PictureType.JPEG.contentType,
+          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+          "/ppt/media/image#.jpeg",
+          XSLFPictureData.class
+    );
+    public static final XSLFRelation IMAGE_PNG = new XSLFRelation(
+          PictureType.PNG.contentType,
+          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+          "/ppt/media/image#.png",
+          XSLFPictureData.class
+    );
+    public static final XSLFRelation IMAGE_DIB = new XSLFRelation(
+          PictureType.DIB.contentType,
+          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+          "/ppt/media/image#.dib",
+          XSLFPictureData.class
+    );
+    public static final XSLFRelation IMAGE_GIF = new XSLFRelation(
+          PictureType.GIF.contentType,
+          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+          "/ppt/media/image#.gif",
+          XSLFPictureData.class
+    );
     public static final XSLFRelation IMAGE_TIFF = new XSLFRelation(
-            "image/tiff",
-            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-            "/ppt/media/image#.tiff",
-            XSLFPictureData.class
+          PictureType.TIFF.contentType,
+          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+          "/ppt/media/image#.tiff",
+          XSLFPictureData.class
     );
     public static final XSLFRelation IMAGE_EPS = new XSLFRelation(
-            "image/x-eps",
-            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-            "/ppt/media/image#.eps",
-            XSLFPictureData.class
+          PictureType.EPS.contentType,
+          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+          "/ppt/media/image#.eps",
+          XSLFPictureData.class
     );
     public static final XSLFRelation IMAGE_BMP = new XSLFRelation(
-            "image/x-ms-bmp",
-            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-            "/ppt/media/image#.bmp",
-            XSLFPictureData.class
+          PictureType.BMP.contentType,
+          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+          "/ppt/media/image#.bmp",
+          XSLFPictureData.class
     );
     public static final XSLFRelation IMAGE_WPG = new XSLFRelation(
-            "image/x-wpg",
-            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-            "/ppt/media/image#.wpg",
-            XSLFPictureData.class
+          PictureType.WPG.contentType,
+          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+          "/ppt/media/image#.wpg",
+          XSLFPictureData.class
     );
     public static final XSLFRelation IMAGE_WDP = new XSLFRelation(
-            "image/vnd.ms-photo",
-            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
-            "/ppt/media/image#.wdp",
-            XSLFPictureData.class
+          PictureType.WDP.contentType,
+          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+          "/ppt/media/image#.wdp",
+          XSLFPictureData.class
     );
 
     public static final XSLFRelation IMAGES = new XSLFRelation(
index e1d00c58d416657c5be38377dd4467b2ce9af0fa..f7078029b61be1f8d9c5194cc36bcf378aa3bc21 100644 (file)
@@ -55,7 +55,7 @@ public interface XSLFShapeContainer extends ShapeContainer<XSLFShape> {
     /**
      * create a picture belonging to this container
      */
-    XSLFPictureShape createPicture(int pictureIndex);
+    XSLFPictureShape createPicture(XSLFPictureData pictureData);
 
     /**
      * Removes all of the elements from this container (optional operation).
index 9549baf4693f8acd05adb20769c89261746ca84e..fb6ea5446c70f1855d7de0815b0bfd7298ff7793 100644 (file)
@@ -25,7 +25,6 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.regex.Pattern;
 
 import javax.xml.namespace.QName;
 
@@ -182,15 +181,8 @@ public abstract class XSLFSheet extends POIXMLDocumentPart implements XSLFShapeC
         return sh;
     }
 
-    public XSLFPictureShape createPicture(int pictureIndex){
-        List<PackagePart>  pics = getPackagePart().getPackage()
-                .getPartsByName(Pattern.compile("/ppt/media/image" + (pictureIndex + 1) + ".*?"));
-
-        if(pics.size() == 0) {
-            throw new IllegalArgumentException("Picture with index=" + pictureIndex + " was not found");
-        }
-
-        PackagePart pic = pics.get(0);
+    public XSLFPictureShape createPicture(XSLFPictureData pictureData){
+        PackagePart pic = pictureData.getPackagePart();
 
         PackageRelationship rel = getPackagePart().addRelationship(
                 pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation());
@@ -525,8 +517,8 @@ public abstract class XSLFSheet extends POIXMLDocumentPart implements XSLFShapeC
         XSLFPictureData data = new XSLFPictureData(blipPart, null);
 
         XMLSlideShow ppt = getSlideShow();
-        int pictureIdx = ppt.addPicture(data.getData(), data.getPictureType());
-        PackagePart pic = ppt.getAllPictures().get(pictureIdx).getPackagePart();
+        XSLFPictureData pictureData = ppt.addPicture(data.getData(), data.getType());
+        PackagePart pic = pictureData.getPackagePart();
 
         PackageRelationship rel = getPackagePart().addRelationship(
                 pic.getPartName(), TargetMode.INTERNAL, blipRel.getRelationshipType());
index 35f30a36d0b87d208ee19fe645f18080f7eeb252..e7bed2a1e21d1b26a9985b68d0c9576d81d216df 100644 (file)
@@ -37,6 +37,7 @@ import javax.imageio.ImageIO;
 
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
 import org.apache.poi.xslf.usermodel.DrawingParagraph;
 import org.apache.poi.xslf.usermodel.DrawingTextBody;
 import org.apache.poi.xslf.usermodel.XMLSlideShow;
@@ -48,6 +49,8 @@ import org.apache.poi.xslf.usermodel.XSLFSlide;
 import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
 import org.junit.Ignore;
 import org.junit.Test;
+
+
 public class TestXSLFBugs {
 
     @Test
@@ -56,7 +59,6 @@ public class TestXSLFBugs {
        XMLSlideShow ss = XSLFTestDataSamples.openSampleDocument("51187.pptx");
        
        assertEquals(1, ss.getSlides().size());
-       XSLFSlide slide = ss.getSlides().get(0);
        
        // Check the relations on it
        // Note - rId3 is a self reference
@@ -289,11 +291,11 @@ public class TestXSLFBugs {
         
         // Add a few pictures
         for (int i=0; i<10; i++) {
-            int idx = ss.addPicture(pics[i], XSLFPictureData.PICTURE_TYPE_JPEG);
-            assertEquals(i, idx);
+            XSLFPictureData data = ss.addPicture(pics[i], PictureType.JPEG);
+            assertEquals(i, data.getIndex());
             assertEquals(i+1, ss.getAllPictures().size());
             
-            XSLFPictureShape shape = slide.createPicture(idx);
+            XSLFPictureShape shape = slide.createPicture(data);
             assertNotNull(shape.getPictureData());
             assertArrayEquals(pics[i], shape.getPictureData().getData());
             assertEquals(i+2, slide.getShapes().size());
@@ -307,11 +309,11 @@ public class TestXSLFBugs {
         
         // Add past 10
         for (int i=10; i<15; i++) {
-            int idx = ss.addPicture(pics[i], XSLFPictureData.PICTURE_TYPE_JPEG);
-            assertEquals(i, idx);
+            XSLFPictureData data = ss.addPicture(pics[i], PictureType.JPEG);
+            assertEquals(i, data.getIndex());
             assertEquals(i+1, ss.getAllPictures().size());
             
-            XSLFPictureShape shape = slide.createPicture(idx);
+            XSLFPictureShape shape = slide.createPicture(data);
             assertNotNull(shape.getPictureData());
             assertArrayEquals(pics[i], shape.getPictureData().getData());
             assertEquals(i+2, slide.getShapes().size());
@@ -324,11 +326,11 @@ public class TestXSLFBugs {
         }
         
         // Add a duplicate, check the right one is picked
-        int idx = ss.addPicture(pics[3], XSLFPictureData.PICTURE_TYPE_JPEG);
-        assertEquals(3, idx);
+        XSLFPictureData data = ss.addPicture(pics[3], PictureType.JPEG);
+        assertEquals(3, data.getIndex());
         assertEquals(15, ss.getAllPictures().size());
         
-        XSLFPictureShape shape = slide.createPicture(idx);
+        XSLFPictureShape shape = slide.createPicture(data);
         assertNotNull(shape.getPictureData());
         assertArrayEquals(pics[3], shape.getPictureData().getData());
         assertEquals(17, slide.getShapes().size());
@@ -351,11 +353,11 @@ public class TestXSLFBugs {
         assertArrayEquals(pics[3], shape.getPictureData().getData());
         
         // Add another duplicate
-        idx = ss.addPicture(pics[5], XSLFPictureData.PICTURE_TYPE_JPEG);
-        assertEquals(5, idx);
+        data = ss.addPicture(pics[5], PictureType.JPEG);
+        assertEquals(5, data.getIndex());
         assertEquals(15, ss.getAllPictures().size());
         
-        shape = slide.createPicture(idx);
+        shape = slide.createPicture(data);
         assertNotNull(shape.getPictureData());
         assertArrayEquals(pics[5], shape.getPictureData().getData());
         assertEquals(18, slide.getShapes().size());
index 0ec9b972a07421c6a1a089f58d7cb9295b798a73..12dde34afe9818b19459f17b1d1918eda49d7b5d 100644 (file)
 ==================================================================== */\r
 package org.apache.poi.xslf.usermodel;\r
 \r
-import static org.junit.Assert.*;\r
+import static org.junit.Assert.assertArrayEquals;\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertFalse;\r
+import static org.junit.Assert.assertNotNull;\r
 \r
-import java.util.*;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
 \r
 import org.apache.poi.xslf.XSLFTestDataSamples;\r
+import org.apache.poi.sl.usermodel.PictureData.PictureType;\r
 import org.junit.Test;\r
 import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;\r
 \r
@@ -35,21 +41,21 @@ public class TestXSLFPictureShape {
         assertEquals(0, ppt.getAllPictures().size());\r
         byte[] data1 = new byte[100];\r
         for(int i = 0;i < 100;i++) { data1[i] = (byte)i; }\r
-        int idx1 = ppt.addPicture(data1, XSLFPictureData.PICTURE_TYPE_JPEG);\r
-        assertEquals(0, idx1);\r
+        XSLFPictureData pdata1 = ppt.addPicture(data1, PictureType.JPEG);\r
+        assertEquals(0, pdata1.getIndex());\r
         assertEquals(1, ppt.getAllPictures().size());\r
 \r
         XSLFSlide slide = ppt.createSlide();\r
-        XSLFPictureShape shape1 = slide.createPicture(idx1);\r
+        XSLFPictureShape shape1 = slide.createPicture(pdata1);\r
         assertNotNull(shape1.getPictureData());\r
         assertArrayEquals(data1, shape1.getPictureData().getData());\r
 \r
         byte[] data2 = new byte[200];\r
         for(int i = 0;i < 200;i++) { data2[i] = (byte)i; }\r
-        int idx2 = ppt.addPicture(data2, XSLFPictureData.PICTURE_TYPE_PNG);\r
-        XSLFPictureShape shape2 = slide.createPicture(idx2);\r
+        XSLFPictureData pdata2 = ppt.addPicture(data2, PictureType.PNG);\r
+        XSLFPictureShape shape2 = slide.createPicture(pdata2);\r
         assertNotNull(shape2.getPictureData());\r
-        assertEquals(1, idx2);\r
+        assertEquals(1, pdata2.getIndex());\r
         assertEquals(2, ppt.getAllPictures().size());\r
         assertArrayEquals(data2, shape2.getPictureData().getData());\r
 \r
@@ -75,12 +81,11 @@ public class TestXSLFPictureShape {
         // first add 20 images to the slide\r
         for (int i = 0; i < 20; i++, pictureIndex++) {\r
             byte[] data = new byte[]{(byte)pictureIndex};\r
-            int elementIndex = ppt.addPicture(data,\r
-                    XSLFPictureData.PICTURE_TYPE_PNG);\r
-            assertEquals(pictureIndex, elementIndex);   // added images have indexes 0,1,2....19\r
-            XSLFPictureShape picture = slide1.createPicture(elementIndex);\r
+            XSLFPictureData elementData = ppt.addPicture(data, PictureType.PNG);\r
+            assertEquals(pictureIndex, elementData.getIndex());   // added images have indexes 0,1,2....19\r
+            XSLFPictureShape picture = slide1.createPicture(elementData);\r
             // POI saves images as image1.png, image2.png, etc.\r
-            String fileName = "image" + (elementIndex + 1) + ".png";\r
+            String fileName = "image" + (elementData.getIndex()+1) + ".png";\r
             assertEquals(fileName, picture.getPictureData().getFileName());\r
             assertArrayEquals(data, picture.getPictureData().getData());\r
         }\r
@@ -88,11 +93,10 @@ public class TestXSLFPictureShape {
         // and then add next 20 images to a group\r
         for (int i = 0; i < 20; i++, pictureIndex++) {\r
             byte[] data = new byte[]{(byte)pictureIndex};\r
-            int elementIndex = ppt.addPicture(data,\r
-                    XSLFPictureData.PICTURE_TYPE_PNG);\r
-            XSLFPictureShape picture = group1.createPicture(elementIndex);\r
+            XSLFPictureData elementData = ppt.addPicture(data, PictureType.PNG);\r
+            XSLFPictureShape picture = group1.createPicture(elementData);\r
             // POI saves images as image1.png, image2.png, etc.\r
-            assertEquals(pictureIndex, elementIndex);   // added images have indexes 0,1,2....19\r
+            assertEquals(pictureIndex, elementData.getIndex());   // added images have indexes 0,1,2....19\r
             String fileName = "image" + (pictureIndex + 1) + ".png";\r
             assertEquals(fileName, picture.getPictureData().getFileName());\r
             assertArrayEquals(data, picture.getPictureData().getData());\r
@@ -122,13 +126,13 @@ public class TestXSLFPictureShape {
         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
+        XSLFPictureData pdata1 = ppt.addPicture(img1, PictureType.PNG);\r
+        assertEquals(0, pdata1.getIndex());\r
+        assertEquals(0, ppt.addPicture(img1, PictureType.PNG).getIndex());\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
+        XSLFPictureData idx2 = ppt.addPicture(img2, PictureType.PNG);\r
+        assertEquals(1, idx2.getIndex());\r
+        assertEquals(1, ppt.addPicture(img2, PictureType.PNG).getIndex());\r
 \r
         XSLFSlide slide1 = ppt.createSlide();\r
         assertNotNull(slide1);\r
@@ -141,10 +145,10 @@ public class TestXSLFPictureShape {
     public void testMerge() {\r
         XMLSlideShow ppt1 = new XMLSlideShow();\r
         byte[] data1 = new byte[100];\r
-        int idx1 = ppt1.addPicture(data1, XSLFPictureData.PICTURE_TYPE_JPEG);\r
+        XSLFPictureData pdata1 = ppt1.addPicture(data1, PictureType.JPEG);\r
 \r
         XSLFSlide slide1 = ppt1.createSlide();\r
-        XSLFPictureShape shape1 = slide1.createPicture(idx1);\r
+        XSLFPictureShape shape1 = slide1.createPicture(pdata1);\r
         CTPicture ctPic1 = (CTPicture)shape1.getXmlObject();\r
         ctPic1.getNvPicPr().getNvPr().addNewCustDataLst().addNewTags().setId("rId99");\r
 \r
index 9755d46d2739f4848227fe2d9999a0dc91f51d7b..416315a2f598fcdc1ed486d4ef148649d6a079e6 100644 (file)
@@ -25,11 +25,10 @@ import java.io.ByteArrayOutputStream;
 /**
  * Represents a bitmap picture data:  JPEG or PNG.
  * The data is not compressed and the exact file content is written in the stream.
- *
- * @author Yegor Kozlov
  */
-public abstract  class Bitmap extends HSLFPictureData {
+public abstract class Bitmap extends HSLFPictureData {
 
+    @Override
     public byte[] getData(){
         byte[] rawdata = getRawData();
         int prefixLen = 16*uidInstanceCount+1;
@@ -38,6 +37,7 @@ public abstract  class Bitmap extends HSLFPictureData {
         return imgdata;
     }
 
+    @Override
     public void setData(byte[] data) throws IOException {
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         for (int i=0; i<uidInstanceCount; i++) {
index e49fb160ad8d086ce85d9a005bdc177dea936c2d..a0283aabd5e4b6153efd25b94572da9680fe8264 100644 (file)
 
 package org.apache.poi.hslf.blip;
 
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
-import org.apache.poi.util.LittleEndian;
-
 import java.io.IOException;
 
+import org.apache.poi.util.LittleEndian;
+
 /**
  * Represents a DIB picture data in a PPT file
- *
- * @author Yegor Kozlov
  */
 public final class DIB extends Bitmap {
     /**
@@ -33,12 +30,9 @@ public final class DIB extends Bitmap {
      */
     private static final int HEADER_SIZE = 14;
 
-    /**
-     * @return type of  this picture
-     * @see  org.apache.poi.hslf.usermodel.HSLFPictureShape#DIB
-     */
-    public int getType(){
-        return HSLFPictureShape.DIB;
+    @Override
+    public PictureType getType(){
+        return PictureType.DIB;
     }
 
     /**
@@ -66,6 +60,7 @@ public final class DIB extends Bitmap {
         }        
     }    
     
+    @Override
     public byte[] getData(){
         return addBMPHeader ( super.getData() );
     }
@@ -99,14 +94,11 @@ public final class DIB extends Bitmap {
         return dib;
     }
 
+    @Override
     public void setData(byte[] data) throws IOException {
         //cut off the bitmap file-header
         byte[] dib = new byte[data.length-HEADER_SIZE];
         System.arraycopy(data, HEADER_SIZE, dib, 0, dib.length);
         super.setData(dib);
     }
-
-    public String getContentType() {
-        return "image/bmp";
-    }
 }
index 582d9ab45352511a08d9aac2b287a2cba2c359c1..3c814893d9abf96798d63212ec1c654c256e0a71 100644 (file)
@@ -24,19 +24,14 @@ import java.io.InputStream;
 import java.util.zip.InflaterInputStream;
 
 import org.apache.poi.hslf.exceptions.HSLFException;
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
 import org.apache.poi.util.Units;
 
 /**
  * Represents EMF (Windows Enhanced Metafile) picture data.
- *
- * @author Yegor Kozlov
  */
 public final class EMF extends Metafile {
 
-    /**
-     * Extract compressed EMF data from a ppt
-     */
+    @Override
     public byte[] getData(){
         try {
             byte[] rawdata = getRawData();
@@ -60,6 +55,7 @@ public final class EMF extends Metafile {
         }
     }
 
+    @Override
     public void setData(byte[] data) throws IOException {
         byte[] compressed = compress(data, 0, data.length);
 
@@ -79,8 +75,9 @@ public final class EMF extends Metafile {
         setRawData(out.toByteArray());
     }
 
-    public int getType(){
-        return HSLFPictureShape.EMF;
+    @Override
+    public PictureType getType(){
+        return PictureType.EMF;
     }
 
     /**
@@ -107,8 +104,4 @@ public final class EMF extends Metafile {
                 throw new IllegalArgumentException(signature+" is not a valid instance/signature value for EMF");
         }        
     }
-
-    public String getContentType() {
-        return "image/x-emf";
-    }
 }
index ed436beb28ffbc5edff1113a283828aaa53079ac..c7b94a68451dc287bf7b9cd154258c52cdfbce4c 100644 (file)
 
 package org.apache.poi.hslf.blip;
 
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
 
 /**
  * Represents a JPEG picture data in a PPT file
- *
- * @author Yegor Kozlov
  */
 public final class JPEG extends Bitmap {
 
@@ -30,12 +27,9 @@ public final class JPEG extends Bitmap {
     
     private ColorSpace colorSpace = ColorSpace.rgb;
     
-    /**
-     * @return type of  this picture
-     * @see  org.apache.poi.hslf.usermodel.HSLFPictureShape#JPEG
-     */
-    public int getType(){
-        return HSLFPictureShape.JPEG;
+    @Override
+    public PictureType getType(){
+        return PictureType.JPEG;
     }
 
     public ColorSpace getColorSpace() {
@@ -82,8 +76,4 @@ public final class JPEG extends Bitmap {
                 throw new IllegalArgumentException(signature+" is not a valid instance/signature value for JPEG");
         }        
     }
-
-    public String getContentType() {
-        return "image/jpeg";
-    }
 }
index da478c9ddd7f140c9becac85ed90aff6c30dec74..a21e0fe6b011221eaba2119825882c00bace9453 100644 (file)
@@ -23,19 +23,14 @@ import java.io.IOException;
 import java.util.zip.InflaterInputStream;
 
 import org.apache.poi.hslf.exceptions.HSLFException;
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
 import org.apache.poi.util.Units;
 
 /**
  * Represents Macintosh PICT picture data.
- *
- * @author Yegor Kozlov
  */
 public final class PICT extends Metafile {
 
-    /**
-     * Extract compressed PICT data from a ppt
-     */
+    @Override
     public byte[] getData(){
         byte[] rawdata = getRawData();
         try {
@@ -74,6 +69,7 @@ public final class PICT extends Metafile {
         return out.toByteArray();
     }
 
+    @Override
     public void setData(byte[] data) throws IOException {
         int pos = 512; //skip the first 512 bytes - they are MAC specific crap
         byte[] compressed = compress(data, pos, data.length-pos);
@@ -97,11 +93,9 @@ public final class PICT extends Metafile {
         setRawData(out.toByteArray());
     }
 
-    /**
-     * @see org.apache.poi.hslf.usermodel.HSLFPictureShape#PICT
-     */
-    public int getType(){
-        return HSLFPictureShape.PICT;
+    @Override
+    public PictureType getType(){
+        return PictureType.PICT;
     }
 
     /**
@@ -128,9 +122,4 @@ public final class PICT extends Metafile {
                 throw new IllegalArgumentException(signature+" is not a valid instance/signature value for PICT");
         }        
     }
-
-    public String getContentType() {
-        return "image/x-pict";
-    }
-
 }
index cd61a9bd6a48d5ab201e6273ac91478a3a5fa5c9..b338d9d171550e4ab8c236b63e77b59da32e748e 100644 (file)
 
 package org.apache.poi.hslf.blip;
 
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
 import org.apache.poi.util.PngUtils;
 
 /**
  * Represents a PNG picture data in a PPT file
- *
- * @author Yegor Kozlov
  */
 public final class PNG extends Bitmap {
 
-    /**
-     * @return PNG data
-     */
+    @Override
     public byte[] getData() {
         byte[] data = super.getData();
 
@@ -44,12 +39,9 @@ public final class PNG extends Bitmap {
         return data;
     }
 
-    /**
-     * @return type of  this picture
-     * @see  org.apache.poi.hslf.usermodel.HSLFPictureShape#PNG
-     */
-    public int getType(){
-        return HSLFPictureShape.PNG;
+    @Override
+    public PictureType getType(){
+        return PictureType.PNG;
     }
 
     /**
@@ -76,8 +68,4 @@ public final class PNG extends Bitmap {
                 throw new IllegalArgumentException(signature+" is not a valid instance/signature value for PNG");
         }        
     }
-
-    public String getContentType() {
-        return "image/png";
-    }
 }
index a6d52ad8b7efce7b99444ed2cd12803a4ee38ccd..14d1b2652fbbc342e2f71dae2f1ebb6fe421c408 100644 (file)
@@ -25,21 +25,16 @@ import java.io.OutputStream;
 import java.util.zip.InflaterInputStream;
 
 import org.apache.poi.hslf.exceptions.HSLFException;
-import org.apache.poi.hslf.usermodel.HSLFPictureShape;
 import org.apache.poi.util.LittleEndian;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.util.Units;
 
 /**
  * Represents a WMF (Windows Metafile) picture data.
- *
- * @author Yegor Kozlov
  */
 public final class WMF extends Metafile {
 
-    /**
-     * Extract compressed WMF data from a ppt
-     */
+    @Override
     public byte[] getData(){
         try {
             byte[] rawdata = getRawData();
@@ -70,6 +65,7 @@ public final class WMF extends Metafile {
         }
     }
 
+    @Override
     public void setData(byte[] data) throws IOException {
         int pos = 0;
         AldusHeader aldus = new AldusHeader();
@@ -97,11 +93,9 @@ public final class WMF extends Metafile {
         setRawData(out.toByteArray());
     }
 
-    /**
-     * We are of type <code>Picture.WMF</code>
-     */
-    public int getType(){
-        return HSLFPictureShape.WMF;
+    @Override
+    public PictureType getType(){
+        return PictureType.WMF;
     }
 
     /**
@@ -208,10 +202,4 @@ public final class WMF extends Metafile {
             return 22;
         }
     }
-
-
-    public String getContentType() {
-        return "image/x-wmf";
-    }
-
 }
index e90abbbcb106001e01739005ca5b3509542118ea..97b822a9912d25f950495602e7ee734059fe6880 100644 (file)
@@ -18,6 +18,7 @@
 package org.apache.poi.hslf.extractor;
 
 import org.apache.poi.hslf.usermodel.*;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
 
 import java.io.IOException;
 import java.io.FileOutputStream;
@@ -44,31 +45,8 @@ public final class ImageExtractor {
             // picture data
             byte[] data = pict.getData();
 
-            int type = pict.getType();
-            String ext;
-            switch (type) {
-                case HSLFPictureShape.JPEG:
-                    ext = ".jpg";
-                    break;
-                case HSLFPictureShape.PNG:
-                    ext = ".png";
-                    break;
-                case HSLFPictureShape.WMF:
-                    ext = ".wmf";
-                    break;
-                case HSLFPictureShape.EMF:
-                    ext = ".emf";
-                    break;
-                case HSLFPictureShape.PICT:
-                    ext = ".pict";
-                    break;
-                case HSLFPictureShape.DIB:
-                    ext = ".dib";
-                    break;
-                default:
-                    continue;
-            }
-            FileOutputStream out = new FileOutputStream("pict_" + i + ext);
+            PictureType type = pict.getType();
+            FileOutputStream out = new FileOutputStream("pict_" + i + type.extension);
             out.write(data);
             out.close();
         }
index 567b8297e08bd68dc697cb45b2ecd654858874ee..25bb1aa13154e4cf3687d648ba7f729145724cb2 100644 (file)
@@ -52,10 +52,10 @@ public final class ActiveXShape extends HSLFPictureShape {
     /**
      * Create a new <code>Picture</code>
      *
-    * @param pictureIdx the index of the picture
+    * @param pictureData the picture data
      */
-    public ActiveXShape(int movieIdx, int pictureIdx){
-        super(pictureIdx, null);
+    public ActiveXShape(int movieIdx, HSLFPictureData pictureData){
+        super(pictureData, null);
         setActiveXIndex(movieIdx);
     }
 
index 27b00005372383561b719203c30c3b011a634c2b..0e35a0e2984aa5990af92157ac4a6effe25ad3c7 100644 (file)
@@ -41,10 +41,10 @@ public final class MovieShape extends HSLFPictureShape {
     /**
      * Create a new <code>Picture</code>
      *
-    * @param pictureIdx the index of the picture
+    * @param pictureData the picture data
      */
-    public MovieShape(int movieIdx, int pictureIdx){
-        super(pictureIdx, null);
+    public MovieShape(int movieIdx, HSLFPictureData pictureData){
+        super(pictureData, null);
         setMovieIndex(movieIdx);
         setAutoPlay(true);
     }
@@ -52,11 +52,11 @@ public final class MovieShape extends HSLFPictureShape {
     /**
      * Create a new <code>Picture</code>
      *
-     * @param idx the index of the picture
+     * @param pictureData the picture data
      * @param parent the parent shape
      */
-    public MovieShape(int movieIdx, int idx, ShapeContainer<HSLFShape> parent) {
-        super(idx, parent);
+    public MovieShape(int movieIdx, HSLFPictureData pictureData, ShapeContainer<HSLFShape> parent) {
+        super(pictureData, parent);
         setMovieIndex(movieIdx);
     }
 
index be680bfd815b3d2935b58d2186ccd1413d50636f..3e3436eb87c80e13781c3b3e0a5dbfd9c4aad3a9 100644 (file)
@@ -39,10 +39,10 @@ public final class OLEShape extends HSLFPictureShape {
     /**
      * Create a new <code>OLEShape</code>
      *
-    * @param idx the index of the picture
+    * @param data the picture data
      */
-    public OLEShape(int idx){
-        super(idx);
+    public OLEShape(HSLFPictureData data){
+        super(data);
     }
 
     /**
@@ -51,8 +51,8 @@ public final class OLEShape extends HSLFPictureShape {
      * @param idx the index of the picture
      * @param parent the parent shape
      */
-    public OLEShape(int idx, ShapeContainer<HSLFShape> parent) {
-        super(idx, parent);
+    public OLEShape(HSLFPictureData data, ShapeContainer<HSLFShape> parent) {
+        super(data, parent);
     }
 
     /**
index 09d97a3b746921cd8a159654b7348ec93695af2d..77cb18e21efb561dbefe45f21eb566bcf4cb3655 100644 (file)
@@ -295,16 +295,14 @@ public final class HSLFFill {
     /**
      * Assign picture used to fill the underlying shape.
      *
-     * @param idx 0-based index of the picture added to this ppt by <code>SlideShow.addPicture</code> method.
+     * @param data the picture data added to this ppt by {@link HSLFSlideShow#addPicture(byte[], org.apache.poi.sl.usermodel.PictureData.PictureType)} method.
      */
-    public void setPictureData(int idx){
+    public void setPictureData(HSLFPictureData data){
         EscherOptRecord opt = shape.getEscherOptRecord();
-        HSLFShape.setEscherProperty(opt, (short)(EscherProperties.FILL__PATTERNTEXTURE + 0x4000), idx);
-        if( idx != 0 ) {
-            if( shape.getSheet() != null ) {
-                EscherBSERecord bse = getEscherBSERecord(idx);
-                bse.setRef(bse.getRef() + 1);
-            }
+        HSLFShape.setEscherProperty(opt, (short)(EscherProperties.FILL__PATTERNTEXTURE + 0x4000), (data == null ? 0 : data.getIndex()));
+        if(data != null && shape.getSheet() != null) {
+            EscherBSERecord bse = getEscherBSERecord(data.getIndex());
+            bse.setRef(bse.getRef() + 1);
         }
     }
 
index aa7e8a32eb27d78617cd1875a70dca298e43c48c..a2c27e51c196408c85a72a273b806e05107b4f8a 100644 (file)
@@ -54,34 +54,12 @@ public abstract class HSLFPictureData implements PictureData {
      * The instance type/signatures defines if one or two UID instances will be included
      */
     protected int uidInstanceCount = 1;
-    
-    /**
-     * Returns type of this picture.
-     * Must be one of the static constants defined in the <code>Picture<code> class.
-     *
-     * @return type of this picture.
-     */
-    public abstract int getType();
-
 
     /**
-     * Returns content type (mime type) of this picture.
-     *
-     * @return content type of this picture.
+     * The 1-based index within the pictures stream 
      */
-    public abstract String getContentType();
+    protected int index = -1;
     
-    /**
-     * Returns the binary data of this Picture
-     * @return picture data
-     */
-    public abstract byte[] getData();
-
-    /**
-     *  Set picture data
-     */
-    public abstract void setData(byte[] data) throws IOException;
-
     /**
      * Blip signature.
      */
@@ -159,7 +137,8 @@ public abstract class HSLFPictureData implements PictureData {
         out.write(data);
 
         data = new byte[LittleEndian.SHORT_SIZE];
-        LittleEndian.putUShort(data, 0, getType() + 0xF018);
+        PictureType pt = getType();
+        LittleEndian.putUShort(data, 0, pt.nativeId + 0xF018);
         out.write(data);
 
         byte[] rawdata = getRawData();
@@ -178,27 +157,15 @@ public abstract class HSLFPictureData implements PictureData {
      * Must be one of the static constants defined in the <code>Picture<code> class.
      * @return concrete instance of <code>PictureData</code>
      */
-     public static HSLFPictureData create(int type){
+     public static HSLFPictureData create(PictureType type){
         HSLFPictureData pict;
         switch (type){
-            case HSLFPictureShape.EMF:
-                pict = new EMF();
-                break;
-            case HSLFPictureShape.WMF:
-                pict = new WMF();
-                break;
-            case HSLFPictureShape.PICT:
-                pict = new PICT();
-                break;
-            case HSLFPictureShape.JPEG:
-                pict = new JPEG();
-                break;
-            case HSLFPictureShape.PNG:
-                pict = new PNG();
-                break;
-            case HSLFPictureShape.DIB:
-                pict = new DIB();
-                break;
+            case EMF: pict = new EMF(); break;
+            case WMF: pict = new WMF(); break;
+            case PICT: pict = new PICT(); break;
+            case JPEG: pict = new JPEG(); break;
+            case PNG: pict = new PNG(); break;
+            case DIB: pict = new DIB(); break;
             default:
                 throw new IllegalArgumentException("Unsupported picture type: " + type);
         }
@@ -231,4 +198,23 @@ public abstract class HSLFPictureData implements PictureData {
     public int getSize(){
         return getData().length;
     }
+
+    /**
+     * @return the 1-based index of this pictures within the pictures stream
+     */
+    public int getIndex() {
+        return index;
+    }
+
+    /**
+     * @param index sets the 1-based index of this pictures within the pictures stream
+     */
+    public void setIndex(int index) {
+        this.index = index;
+    }
+    
+    @Override
+    public final String getContentType() {
+        return getType().contentType;
+    }
 }
index a6857937c0b74b1a2ef20ed61f00adf3e81c6dcf..807af0d5f160c18e6f6f07728ef69deb70c8c0d0 100644 (file)
@@ -51,43 +51,13 @@ import org.apache.poi.util.Units;
  */
 public class HSLFPictureShape extends HSLFSimpleShape implements PictureShape {
 
-    /**
-    *  Windows Enhanced Metafile (EMF)
-    */
-    public static final int EMF = 2;
-
-    /**
-    *  Windows Metafile (WMF)
-    */
-    public static final int WMF = 3;
-
-    /**
-    * Macintosh PICT
-    */
-    public static final int PICT = 4;
-
-    /**
-    *  JPEG
-    */
-    public static final int JPEG = 5;
-
-    /**
-    *  PNG
-    */
-    public static final int PNG = 6;
-
-    /**
-     * Windows DIB (BMP)
-     */
-    public static final byte DIB = 7;
-
     /**
      * Create a new <code>Picture</code>
      *
     * @param idx the index of the picture
      */
-    public HSLFPictureShape(int idx){
-        this(idx, null);
+    public HSLFPictureShape(HSLFPictureData data){
+        this(data, null);
     }
 
     /**
@@ -96,9 +66,9 @@ public class HSLFPictureShape extends HSLFSimpleShape implements PictureShape {
      * @param idx the index of the picture
      * @param parent the parent shape
      */
-    public HSLFPictureShape(int idx, ShapeContainer<HSLFShape> parent) {
+    public HSLFPictureShape(HSLFPictureData data, ShapeContainer<HSLFShape> parent) {
         super(null, parent);
-        _escherContainer = createSpContainer(idx, parent instanceof HSLFGroupShape);
+        _escherContainer = createSpContainer(data.getIndex(), parent instanceof HSLFGroupShape);
     }
 
     /**
index 0fbc38a9ee207ffe618ece94f7af44407ac0b250..9f4d311982ba7ec82d40317d1a14f38fa6baa9c5 100644 (file)
@@ -25,46 +25,31 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 import org.apache.poi.ddf.EscherBSERecord;
 import org.apache.poi.ddf.EscherContainerRecord;
 import org.apache.poi.ddf.EscherOptRecord;
-import org.apache.poi.ddf.EscherRecord;
 import org.apache.poi.hpsf.ClassID;
 import org.apache.poi.hslf.exceptions.CorruptPowerPointFileException;
 import org.apache.poi.hslf.exceptions.HSLFException;
-import org.apache.poi.hslf.model.*;
-import org.apache.poi.hslf.record.Document;
-import org.apache.poi.hslf.record.DocumentAtom;
-import org.apache.poi.hslf.record.ExAviMovie;
-import org.apache.poi.hslf.record.ExControl;
-import org.apache.poi.hslf.record.ExEmbed;
-import org.apache.poi.hslf.record.ExEmbedAtom;
-import org.apache.poi.hslf.record.ExHyperlink;
-import org.apache.poi.hslf.record.ExHyperlinkAtom;
-import org.apache.poi.hslf.record.ExMCIMovie;
-import org.apache.poi.hslf.record.ExObjList;
-import org.apache.poi.hslf.record.ExObjListAtom;
-import org.apache.poi.hslf.record.ExOleObjAtom;
-import org.apache.poi.hslf.record.ExOleObjStg;
-import org.apache.poi.hslf.record.ExVideoContainer;
-import org.apache.poi.hslf.record.FontCollection;
-import org.apache.poi.hslf.record.FontEntityAtom;
-import org.apache.poi.hslf.record.HeadersFootersContainer;
-import org.apache.poi.hslf.record.PersistPtrHolder;
-import org.apache.poi.hslf.record.PositionDependentRecord;
-import org.apache.poi.hslf.record.PositionDependentRecordContainer;
-import org.apache.poi.hslf.record.Record;
-import org.apache.poi.hslf.record.RecordContainer;
-import org.apache.poi.hslf.record.RecordTypes;
-import org.apache.poi.hslf.record.SlideListWithText;
+import org.apache.poi.hslf.model.HeadersFooters;
+import org.apache.poi.hslf.model.MovieShape;
+import org.apache.poi.hslf.model.PPFont;
+import org.apache.poi.hslf.record.*;
 import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
-import org.apache.poi.hslf.record.SlidePersistAtom;
-import org.apache.poi.hslf.record.UserEditAtom;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-import org.apache.poi.sl.usermodel.*;
+import org.apache.poi.sl.usermodel.MasterSheet;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
+import org.apache.poi.sl.usermodel.Resources;
+import org.apache.poi.sl.usermodel.Shape;
+import org.apache.poi.sl.usermodel.SlideShow;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.util.Units;
@@ -752,19 +737,20 @@ public final class HSLFSlideShow implements SlideShow {
                return slide;
        }
 
-       /**
-        * Adds a picture to this presentation and returns the associated index.
-        *
-        * @param data
-        *            picture data
-        * @param format
-        *            the format of the picture. One of constans defined in the
-        *            <code>Picture</code> class.
-        * @return the index to this picture (1 based).
-        */
-       public int addPicture(byte[] data, int format) throws IOException {
-               byte[] uid = HSLFPictureData.getChecksum(data);
+       @Override
+       public HSLFPictureData addPicture(byte[] data, PictureType format) throws IOException {
+           if (format == null || format.nativeId == -1) {
+               throw new IllegalArgumentException("Unsupported picture format: " + format); 
+           }
+           
+           byte[] uid = HSLFPictureData.getChecksum(data);
 
+               for (HSLFPictureData pd : getPictureData()) {
+                   if (Arrays.equals(pd.getUID(), uid)) {
+                       return pd;
+                   }
+               }
+               
                EscherContainerRecord bstore;
 
                EscherContainerRecord dggContainer = _documentRecord.getPPDrawingGroup().getDggContainer();
@@ -775,14 +761,6 @@ public final class HSLFSlideShow implements SlideShow {
                        bstore.setRecordId(EscherContainerRecord.BSTORE_CONTAINER);
 
                        dggContainer.addChildBefore(bstore, EscherOptRecord.RECORD_ID);
-               } else {
-                       Iterator<EscherRecord> iter = bstore.getChildIterator();
-                       for (int i = 0; iter.hasNext(); i++) {
-                               EscherBSERecord bse = (EscherBSERecord) iter.next();
-                               if (Arrays.equals(bse.getUid(), uid)) {
-                                       return i + 1;
-                               }
-                       }
                }
 
                HSLFPictureData pict = HSLFPictureData.create(format);
@@ -792,19 +770,20 @@ public final class HSLFSlideShow implements SlideShow {
 
                EscherBSERecord bse = new EscherBSERecord();
                bse.setRecordId(EscherBSERecord.RECORD_ID);
-               bse.setOptions((short) (0x0002 | (format << 4)));
+               bse.setOptions((short) (0x0002 | (format.nativeId << 4)));
                bse.setSize(pict.getRawData().length + 8);
                bse.setUid(uid);
 
-               bse.setBlipTypeMacOS((byte) format);
-               bse.setBlipTypeWin32((byte) format);
+               bse.setBlipTypeMacOS((byte) format.nativeId);
+               bse.setBlipTypeWin32((byte) format.nativeId);
 
-               if (format == HSLFPictureShape.EMF)
-                       bse.setBlipTypeMacOS((byte) HSLFPictureShape.PICT);
-               else if (format == HSLFPictureShape.WMF)
-                       bse.setBlipTypeMacOS((byte) HSLFPictureShape.PICT);
-               else if (format == HSLFPictureShape.PICT)
-                       bse.setBlipTypeWin32((byte) HSLFPictureShape.WMF);
+               if (format == PictureType.EMF) {
+                       bse.setBlipTypeMacOS((byte) PictureType.PICT.nativeId);
+               } else if (format == PictureType.WMF) {
+                       bse.setBlipTypeMacOS((byte) PictureType.PICT.nativeId);
+               } else if (format == PictureType.PICT) {
+                       bse.setBlipTypeWin32((byte) PictureType.WMF.nativeId);
+               }
 
                bse.setRef(0);
                bse.setOffset(offset);
@@ -814,7 +793,7 @@ public final class HSLFSlideShow implements SlideShow {
                int count = bstore.getChildRecords().size();
                bstore.setOptions((short) ((count << 4) | 0xF));
 
-               return count;
+               return pict;
        }
 
        /**
@@ -827,7 +806,7 @@ public final class HSLFSlideShow implements SlideShow {
         *            <code>Picture</code> class.
         * @return the index to this picture (1 based).
         */
-       public int addPicture(File pict, int format) throws IOException {
+       public HSLFPictureData addPicture(File pict, PictureType format) throws IOException {
                int length = (int) pict.length();
                byte[] data = new byte[length];
         FileInputStream is = null;
index c02fe22c95c30b1c9bb0d07b2679fa625d6da433..e0f7479d6882c680e387e3594ad1f2c8fe349aa5 100644 (file)
@@ -52,6 +52,7 @@ import org.apache.poi.poifs.filesystem.DocumentInputStream;
 import org.apache.poi.poifs.filesystem.EntryUtils;
 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
 import org.apache.poi.util.LittleEndian;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
@@ -407,13 +408,14 @@ public final class HSLFSlideShowImpl extends POIDocument {
                        }
 
                        // If they type (including the bonus 0xF018) is 0, skip it
-                       if(type == 0) {
+                       PictureType pt = PictureType.forNativeID(type - 0xF018);
+                       if(type == 0 || pt == null) {
                                logger.log(POILogger.ERROR, "Problem reading picture: Invalid image type 0, on picture with length " + imgsize + ".\nYou document will probably become corrupted if you save it!");
                                logger.log(POILogger.ERROR, "" + pos);
                        } else {
                                // Build the PictureData object from the data
-                               try {
-                                       HSLFPictureData pict = HSLFPictureData.create(type - 0xF018);
+                try {
+                                       HSLFPictureData pict = HSLFPictureData.create(pt);
 
                     // Copy the data, ready to pass to PictureData
                     byte[] imgdata = new byte[imgsize];
@@ -421,6 +423,7 @@ public final class HSLFSlideShowImpl extends POIDocument {
                     pict.setRawData(imgdata);
 
                     pict.setOffset(offset);
+                    pict.setIndex(_pictures.size());
                                        _pictures.add(pict);
                                } catch(IllegalArgumentException e) {
                                        logger.log(POILogger.ERROR, "Problem reading picture: " + e + "\nYou document will probably become corrupted if you save it!");
@@ -711,6 +714,7 @@ public final class HSLFSlideShowImpl extends POIDocument {
              offset = prev.getOffset() + prev.getRawData().length + 8;
           }
           img.setOffset(offset);
+          img.setIndex(_pictures.size()+1);
           _pictures.add(img);
           return offset;
    }
index 92eca1ae5ddc4f6644ab67fea941b8ce8ee1583b..eaebd5c167c81fe11fe110f63b967a52fb919501 100644 (file)
@@ -33,7 +33,15 @@ import org.apache.poi.ddf.EscherProperties;
 import org.apache.poi.ddf.EscherRecord;
 import org.apache.poi.ddf.EscherSimpleProperty;
 import org.apache.poi.hslf.record.Document;
-import org.apache.poi.hslf.usermodel.*;
+import org.apache.poi.hslf.usermodel.HSLFAutoShape;
+import org.apache.poi.hslf.usermodel.HSLFFill;
+import org.apache.poi.hslf.usermodel.HSLFPictureData;
+import org.apache.poi.hslf.usermodel.HSLFShape;
+import org.apache.poi.hslf.usermodel.HSLFSheet;
+import org.apache.poi.hslf.usermodel.HSLFSlide;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
+import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
 import org.apache.poi.sl.usermodel.ShapeType;
 import org.junit.Test;
 
@@ -104,15 +112,15 @@ public final class TestBackground {
         HSLFSlide slide;
         HSLFFill fill;
         HSLFShape shape;
-        int idx;
+        HSLFPictureData data;
 
         //slide 1
         slide = ppt.createSlide();
         slide.setFollowMasterBackground(false);
         fill = slide.getBackground().getFill();
-        idx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
+        data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
         fill.setFillType(HSLFFill.FILL_PICTURE);
-        fill.setPictureData(idx);
+        fill.setPictureData(data);
 
         shape = new HSLFAutoShape(ShapeType.RECT);
         shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
@@ -124,9 +132,9 @@ public final class TestBackground {
         slide = ppt.createSlide();
         slide.setFollowMasterBackground(false);
         fill = slide.getBackground().getFill();
-        idx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
+        data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
         fill.setFillType(HSLFFill.FILL_PATTERN);
-        fill.setPictureData(idx);
+        fill.setPictureData(data);
         fill.setBackgroundColor(Color.green);
         fill.setForegroundColor(Color.red);
 
@@ -140,16 +148,16 @@ public final class TestBackground {
         slide = ppt.createSlide();
         slide.setFollowMasterBackground(false);
         fill = slide.getBackground().getFill();
-        idx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
+        data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
         fill.setFillType(HSLFFill.FILL_TEXTURE);
-        fill.setPictureData(idx);
+        fill.setPictureData(data);
 
         shape = new HSLFAutoShape(ShapeType.RECT);
         shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
         fill = shape.getFill();
         fill.setFillType(HSLFFill.FILL_PICTURE);
-        idx = ppt.addPicture(_slTests.readFile("clock.jpg"), HSLFPictureShape.JPEG);
-        fill.setPictureData(idx);
+        data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
+        fill.setPictureData(data);
         slide.addShape(shape);
 
         // slide 4
@@ -198,7 +206,6 @@ public final class TestBackground {
         assertEquals(HSLFFill.FILL_SHADE_CENTER, fill.getFillType());
         shape = slides.get(3).getShapes().get(0);
         assertEquals(HSLFFill.FILL_SHADE, shape.getFill().getFillType());
-
     }
 
     private int getFillPictureRefCount(HSLFShape shape, HSLFFill fill) {
index a83d8201236c667a1a17178ad3699e339ce26365..df696d021d63ed395df8887b8067417a1f2b06be 100644 (file)
 
 package org.apache.poi.hslf.model;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import java.awt.geom.Rectangle2D;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 
 import org.apache.poi.POIDataSamples;
-import org.apache.poi.hslf.usermodel.*;
+import org.apache.poi.hslf.usermodel.HSLFPictureData;
+import org.apache.poi.hslf.usermodel.HSLFSlide;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
 import org.junit.Test;
 
 /**
@@ -44,9 +49,9 @@ public final class TestMovieShape {
 
         String path = "/test-movie.mpg";
         int movieIdx = ppt.addMovie(path, MovieShape.MOVIE_MPEG);
-        int thumbnailIdx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
+        HSLFPictureData thumbnailData = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
 
-        MovieShape shape = new MovieShape(movieIdx, thumbnailIdx);
+        MovieShape shape = new MovieShape(movieIdx, thumbnailData);
         shape.setAnchor(new Rectangle2D.Float(300,225,120,90));
         slide.addShape(shape);
 
index f58949117f2d20c9236750ecd28ac49531528ce4..834f1520b13ff82797ef2b2aaedef47c2e40b248 100644 (file)
@@ -30,6 +30,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hwpf.HWPFDocument;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.IOUtils;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
 import org.junit.Test;
 
 public final class TestOleEmbedding {
@@ -95,7 +96,7 @@ public final class TestOleEmbedding {
        HSLFSlideShow ppt = new HSLFSlideShow(_hslfSlideShow);
        
        File pict = POIDataSamples.getSlideShowInstance().getFile("clock.jpg");
-       int pictId = ppt.addPicture(pict, HSLFPictureShape.JPEG);
+       HSLFPictureData pictData = ppt.addPicture(pict, PictureType.JPEG);
        
        InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("Employee.xls");
        POIFSFileSystem poiData1 = new POIFSFileSystem(is);
@@ -104,14 +105,14 @@ public final class TestOleEmbedding {
        int oleObjectId1 = ppt.addEmbed(poiData1);
        
        HSLFSlide slide1 = ppt.createSlide();
-       OLEShape oleShape1 = new OLEShape(pictId);
+       OLEShape oleShape1 = new OLEShape(pictData);
        oleShape1.setObjectID(oleObjectId1);
        slide1.addShape(oleShape1);
        oleShape1.setAnchor(new Rectangle2D.Double(100,100,100,100));
        
        // add second slide with different order in object creation
        HSLFSlide slide2 = ppt.createSlide();
-       OLEShape oleShape2 = new OLEShape(pictId);
+       OLEShape oleShape2 = new OLEShape(pictData);
 
         is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleWithImages.xls");
         POIFSFileSystem poiData2 = new POIFSFileSystem(is);
index 19f0d5b0344c4434f67069cc0dcaad251cdd023e..ed9ca3819d0b7de81d01fb9d8b40bb42c9f2149a 100644 (file)
 
 package org.apache.poi.hslf.model;
 
-import static org.junit.Assert.*;
-
-import java.awt.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Rectangle;
 import java.awt.geom.Rectangle2D;
-import java.io.*;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.poi.POIDataSamples;
-import org.apache.poi.ddf.*;
-import org.apache.poi.hslf.usermodel.*;
+import org.apache.poi.ddf.EscherDgRecord;
+import org.apache.poi.ddf.EscherDggRecord;
+import org.apache.poi.ddf.EscherOptRecord;
+import org.apache.poi.ddf.EscherProperties;
+import org.apache.poi.ddf.EscherSimpleProperty;
+import org.apache.poi.hslf.usermodel.HSLFAutoShape;
+import org.apache.poi.hslf.usermodel.HSLFGroupShape;
+import org.apache.poi.hslf.usermodel.HSLFPictureData;
+import org.apache.poi.hslf.usermodel.HSLFPictureShape;
+import org.apache.poi.hslf.usermodel.HSLFShape;
+import org.apache.poi.hslf.usermodel.HSLFSimpleShape;
+import org.apache.poi.hslf.usermodel.HSLFSlide;
+import org.apache.poi.hslf.usermodel.HSLFSlideShow;
+import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
+import org.apache.poi.hslf.usermodel.HSLFTextBox;
+import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
+import org.apache.poi.hslf.usermodel.HSLFTextRun;
+import org.apache.poi.hslf.usermodel.HSLFTextShape;
 import org.apache.poi.sl.usermodel.ShapeType;
 import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -303,8 +329,8 @@ public final class TestShapes {
         group.setAnchor(new Rectangle(0, 0, (int)pgsize.getWidth(), (int)pgsize.getHeight()));
         slide.addShape(group);
 
-        int idx = ppt.addPicture(_slTests.readFile("clock.jpg"), HSLFPictureShape.JPEG);
-        HSLFPictureShape pict = new HSLFPictureShape(idx, group);
+        HSLFPictureData data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
+        HSLFPictureShape pict = new HSLFPictureShape(data, group);
         pict.setAnchor(new Rectangle(0, 0, 200, 200));
         group.addShape(pict);
 
index b7dba80b82cc10aa7049346e517b631b72b549aa..1dbd0f457f9f9c72108a438a63420360c14c6a9f 100644 (file)
 
 package org.apache.poi.hslf.usermodel;
 
-import static org.junit.Assert.*;
-
-import java.awt.Color;
-import java.awt.Rectangle;
-import java.io.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
@@ -39,15 +44,16 @@ import org.apache.poi.ddf.EscherOptRecord;
 import org.apache.poi.ddf.EscherProperties;
 import org.apache.poi.hslf.HSLFTestDataSamples;
 import org.apache.poi.hslf.exceptions.OldPowerPointFormatException;
-import org.apache.poi.hslf.model.*;
-import org.apache.poi.hslf.model.textproperties.TextPropCollection;
-import org.apache.poi.hslf.model.textproperties.TextPropCollection.TextPropType;
-import org.apache.poi.hslf.record.*;
+import org.apache.poi.hslf.model.HeadersFooters;
+import org.apache.poi.hslf.record.Document;
+import org.apache.poi.hslf.record.Record;
+import org.apache.poi.hslf.record.SlideListWithText;
 import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
-import org.apache.poi.poifs.filesystem.DocumentEntry;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-import org.apache.poi.util.*;
-import org.junit.Ignore;
+import org.apache.poi.hslf.record.TextHeaderAtom;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
+import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.StringUtil;
+import org.apache.poi.util.Units;
 import org.junit.Test;
 
 /**
@@ -73,8 +79,8 @@ public final class TestBugs {
 
         HSLFPictureData[] pict = ppt.getPictureData();
         assertEquals(2, pict.length);
-        assertEquals(HSLFPictureShape.JPEG, pict[0].getType());
-        assertEquals(HSLFPictureShape.JPEG, pict[1].getType());
+        assertEquals(PictureType.JPEG, pict[0].getType());
+        assertEquals(PictureType.JPEG, pict[1].getType());
     }
 
     /**
@@ -354,7 +360,7 @@ public final class TestBugs {
 
         HSLFPictureData pict = f.getPictureData();
         assertNotNull(pict);
-        assertEquals(HSLFPictureShape.JPEG, pict.getType());
+        assertEquals(PictureType.JPEG, pict.getType());
     }
 
     /**
index 5ede8b094d26635dfcc6e115ffa7e8afa0e84d13..d7adf4000e1627799ec611d86e63cd9cc7283c75 100644 (file)
 
 package org.apache.poi.hslf.usermodel;
 
-import static org.junit.Assert.*;
-
-import java.awt.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+import java.awt.BasicStroke;
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Graphics2D;
+import java.awt.Rectangle;
 import java.awt.image.BufferedImage;
-import java.io.*;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.InputStream;
 import java.lang.reflect.Constructor;
-import java.util.*;
+import java.util.BitSet;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.imageio.ImageIO;
 
@@ -31,6 +42,7 @@ import org.apache.poi.POIDataSamples;
 import org.apache.poi.ddf.EscherBSERecord;
 import org.apache.poi.hssf.usermodel.DummyGraphics2d;
 import org.apache.poi.sl.draw.Drawable;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
 import org.apache.poi.sl.usermodel.Slide;
 import org.apache.poi.sl.usermodel.SlideShow;
 import org.apache.poi.util.JvmBugs;
@@ -58,10 +70,10 @@ public final class TestPicture {
         HSLFSlide s2 = ppt.createSlide();
         HSLFSlide s3 = ppt.createSlide();
 
-        int idx = ppt.addPicture(_slTests.readFile("clock.jpg"), HSLFPictureShape.JPEG);
-        HSLFPictureShape pict = new HSLFPictureShape(idx);
-        HSLFPictureShape pict2 = new HSLFPictureShape(idx);
-        HSLFPictureShape pict3 = new HSLFPictureShape(idx);
+        HSLFPictureData data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
+        HSLFPictureShape pict = new HSLFPictureShape(data);
+        HSLFPictureShape pict2 = new HSLFPictureShape(data);
+        HSLFPictureShape pict3 = new HSLFPictureShape(data);
 
         pict.setAnchor(new Rectangle(10,10,100,100));
         s.addShape(pict);
@@ -89,8 +101,9 @@ public final class TestPicture {
     public void bug46122() {
         HSLFSlideShow ppt = new HSLFSlideShow();
         HSLFSlide slide = ppt.createSlide();
-
-        HSLFPictureShape pict = new HSLFPictureShape(-1); //index to non-existing picture data
+        HSLFPictureData pd = HSLFPictureData.create(PictureType.PNG);
+        
+        HSLFPictureShape pict = new HSLFPictureShape(pd); //index to non-existing picture data
         pict.setSheet(slide);
         HSLFPictureData data = pict.getPictureData();
         assertNull(data);
@@ -125,15 +138,19 @@ public final class TestPicture {
                 null            // EMF
         };
 
-        for (int i = 0; i < pictures.length; i++) {
-            BufferedImage image = ImageIO.read(new ByteArrayInputStream(pictures[i].getData()));
-
-            if (pictures[i].getType() != HSLFPictureShape.WMF && pictures[i].getType() != HSLFPictureShape.EMF) {
-                assertNotNull(image);
-
-                int[] dimensions = expectedSizes[i];
-                assertEquals(dimensions[0], image.getWidth());
-                assertEquals(dimensions[1], image.getHeight());
+        int i = 0;
+        for (HSLFPictureData pd : pictures) {
+            int[] dimensions = expectedSizes[i++];
+            BufferedImage image = ImageIO.read(new ByteArrayInputStream(pd.getData()));
+            switch (pd.getType()) {
+                case WMF:
+                case EMF:
+                    break;
+                default:
+                    assertNotNull(image);
+                    assertEquals(dimensions[0], image.getWidth());
+                    assertEquals(dimensions[1], image.getHeight());
+                    break;
             }
         }
     }
index 61c309106d8b84698b7aba959a849064a3e10194..6f9de093ac09a4e195bf5bdfb401605afcac2db2 100644 (file)
@@ -28,6 +28,7 @@ import junit.framework.TestCase;
 
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.hslf.blip.*;
+import org.apache.poi.sl.usermodel.PictureData.PictureType;
 
 /**
  * Test adding/reading pictures
@@ -47,9 +48,9 @@ public final class TestPictures extends TestCase{
 
         HSLFSlide slide = ppt.createSlide();
         byte[] src_bytes = slTests.readFile("cow.pict");
-        int idx = ppt.addPicture(src_bytes, HSLFPictureShape.PICT);
-        HSLFPictureShape pict = new HSLFPictureShape(idx);
-        assertEquals(idx, pict.getPictureIndex());
+        HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.PICT);
+        HSLFPictureShape pict = new HSLFPictureShape(data);
+        assertEquals(data.getIndex(), pict.getPictureIndex());
         slide.addShape(pict);
 
         //serialize and read again
@@ -63,7 +64,7 @@ public final class TestPictures extends TestCase{
         List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
         assertEquals(1, sh.size());
         pict = (HSLFPictureShape)sh.get(0);
-        assertEquals(idx, pict.getPictureIndex());
+        assertEquals(data.getIndex(), pict.getPictureIndex());
 
         //check picture data
         HSLFPictureData[] pictures = ppt.getPictureData();
@@ -71,7 +72,7 @@ public final class TestPictures extends TestCase{
         assertEquals(pict.getPictureData(), pictures[0]);
 
         assertEquals(1, pictures.length);
-        assertEquals(HSLFPictureShape.PICT, pictures[0].getType());
+        assertEquals(PictureType.PICT, pictures[0].getType());
         assertTrue(pictures[0] instanceof PICT);
         //compare the content of the initial file with what is stored in the PictureData
         byte[] ppt_bytes = pictures[0].getData();
@@ -92,9 +93,9 @@ public final class TestPictures extends TestCase{
 
         HSLFSlide slide = ppt.createSlide();
         byte[] src_bytes = slTests.readFile("santa.wmf");
-        int idx = ppt.addPicture(src_bytes, HSLFPictureShape.WMF);
-        HSLFPictureShape pict = new HSLFPictureShape(idx);
-        assertEquals(idx, pict.getPictureIndex());
+        HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.WMF);
+        HSLFPictureShape pict = new HSLFPictureShape(data);
+        assertEquals(data.getIndex(), pict.getPictureIndex());
         slide.addShape(pict);
 
         //serialize and read again
@@ -108,7 +109,7 @@ public final class TestPictures extends TestCase{
         List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
         assertEquals(1, sh.size());
         pict = (HSLFPictureShape)sh.get(0);
-        assertEquals(idx, pict.getPictureIndex());
+        assertEquals(data.getIndex(), pict.getPictureIndex());
 
         //check picture data
         HSLFPictureData[] pictures = ppt.getPictureData();
@@ -116,7 +117,7 @@ public final class TestPictures extends TestCase{
         assertEquals(pict.getPictureData(), pictures[0]);
 
         assertEquals(1, pictures.length);
-        assertEquals(HSLFPictureShape.WMF, pictures[0].getType());
+        assertEquals(PictureType.WMF, pictures[0].getType());
         assertTrue(pictures[0] instanceof WMF);
         //compare the content of the initial file with what is stored in the PictureData
         byte[] ppt_bytes = pictures[0].getData();
@@ -137,10 +138,10 @@ public final class TestPictures extends TestCase{
 
         HSLFSlide slide = ppt.createSlide();
         byte[] src_bytes = slTests.readFile("wrench.emf");
-        int idx = ppt.addPicture(src_bytes, HSLFPictureShape.EMF);
+        HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.EMF);
 
-        HSLFPictureShape pict = new HSLFPictureShape(idx);
-        assertEquals(idx, pict.getPictureIndex());
+        HSLFPictureShape pict = new HSLFPictureShape(data);
+        assertEquals(data.getIndex(), pict.getPictureIndex());
         slide.addShape(pict);
 
         //serialize and read again
@@ -154,7 +155,7 @@ public final class TestPictures extends TestCase{
         List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
         assertEquals(1, sh.size());
         pict = (HSLFPictureShape)sh.get(0);
-        assertEquals(idx, pict.getPictureIndex());
+        assertEquals(data.getIndex(), pict.getPictureIndex());
 
         //check picture data
         HSLFPictureData[] pictures = ppt.getPictureData();
@@ -162,7 +163,7 @@ public final class TestPictures extends TestCase{
         assertEquals(pict.getPictureData(), pictures[0]);
 
         assertEquals(1, pictures.length);
-        assertEquals(HSLFPictureShape.EMF, pictures[0].getType());
+        assertEquals(PictureType.EMF, pictures[0].getType());
         assertTrue(pictures[0] instanceof EMF);
         //compare the content of the initial file with what is stored in the PictureData
         byte[] ppt_bytes = pictures[0].getData();
@@ -177,9 +178,9 @@ public final class TestPictures extends TestCase{
 
         HSLFSlide slide = ppt.createSlide();
         byte[] src_bytes = slTests.readFile("tomcat.png");
-        int idx = ppt.addPicture(src_bytes, HSLFPictureShape.PNG);
-        HSLFPictureShape pict = new HSLFPictureShape(idx);
-        assertEquals(idx, pict.getPictureIndex());
+        HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.PNG);
+        HSLFPictureShape pict = new HSLFPictureShape(data);
+        assertEquals(data.getIndex(), pict.getPictureIndex());
         slide.addShape(pict);
 
         //serialize and read again
@@ -193,7 +194,7 @@ public final class TestPictures extends TestCase{
         List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
         assertEquals(1, sh.size());
         pict = (HSLFPictureShape)sh.get(0);
-        assertEquals(idx, pict.getPictureIndex());
+        assertEquals(data.getIndex(), pict.getPictureIndex());
 
         //check picture data
         HSLFPictureData[] pictures = ppt.getPictureData();
@@ -201,7 +202,7 @@ public final class TestPictures extends TestCase{
         assertEquals(pict.getPictureData(), pictures[0]);
 
         assertEquals(1, pictures.length);
-        assertEquals(HSLFPictureShape.PNG, pictures[0].getType());
+        assertEquals(PictureType.PNG, pictures[0].getType());
         assertTrue(pictures[0] instanceof PNG);
         //compare the content of the initial file with what is stored in the PictureData
         byte[] ppt_bytes = pictures[0].getData();
@@ -216,10 +217,10 @@ public final class TestPictures extends TestCase{
 
         HSLFSlide slide = ppt.createSlide();
         byte[] src_bytes = slTests.readFile("clock.jpg");
-        int idx = ppt.addPicture(src_bytes, HSLFPictureShape.JPEG);
+        HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.JPEG);
 
-        HSLFPictureShape pict = new HSLFPictureShape(idx);
-        assertEquals(idx, pict.getPictureIndex());
+        HSLFPictureShape pict = new HSLFPictureShape(data);
+        assertEquals(data.getIndex(), pict.getPictureIndex());
         slide.addShape(pict);
 
         //serialize and read again
@@ -233,7 +234,7 @@ public final class TestPictures extends TestCase{
         List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
         assertEquals(1, sh.size());
         pict = (HSLFPictureShape)sh.get(0);
-        assertEquals(idx, pict.getPictureIndex());
+        assertEquals(data.getIndex(), pict.getPictureIndex());
 
         //check picture data
         HSLFPictureData[] pictures = ppt.getPictureData();
@@ -241,7 +242,7 @@ public final class TestPictures extends TestCase{
         assertEquals(pict.getPictureData(), pictures[0]);
 
         assertEquals(1, pictures.length);
-        assertEquals(HSLFPictureShape.JPEG, pictures[0].getType());
+        assertEquals(PictureType.JPEG, pictures[0].getType());
         assertTrue(pictures[0] instanceof JPEG);
         //compare the content of the initial file with what is stored in the PictureData
         byte[] ppt_bytes = pictures[0].getData();
@@ -256,9 +257,9 @@ public final class TestPictures extends TestCase{
 
         HSLFSlide slide = ppt.createSlide();
         byte[] src_bytes = slTests.readFile("clock.dib");
-        int idx = ppt.addPicture(src_bytes, HSLFPictureShape.DIB);
-        HSLFPictureShape pict = new HSLFPictureShape(idx);
-        assertEquals(idx, pict.getPictureIndex());
+        HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.DIB);
+        HSLFPictureShape pict = new HSLFPictureShape(data);
+        assertEquals(data.getIndex(), pict.getPictureIndex());
         slide.addShape(pict);
 
         //serialize and read again
@@ -272,7 +273,7 @@ public final class TestPictures extends TestCase{
         List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
         assertEquals(1, sh.size());
         pict = (HSLFPictureShape)sh.get(0);
-        assertEquals(idx, pict.getPictureIndex());
+        assertEquals(data.getIndex(), pict.getPictureIndex());
 
         //check picture data
         HSLFPictureData[] pictures = ppt.getPictureData();
@@ -280,7 +281,7 @@ public final class TestPictures extends TestCase{
         assertEquals(pict.getPictureData(), pictures[0]);
 
         assertEquals(1, pictures.length);
-        assertEquals(HSLFPictureShape.DIB, pictures[0].getType());
+        assertEquals(PictureType.DIB, pictures[0].getType());
         assertTrue(pictures[0] instanceof DIB);
         //compare the content of the initial file with what is stored in the PictureData
         byte[] ppt_bytes = pictures[0].getData();
@@ -304,7 +305,7 @@ public final class TestPictures extends TestCase{
         pict = (HSLFPictureShape)slides.get(0).getShapes().get(0); //the first slide contains JPEG
         pdata = pict.getPictureData();
         assertTrue(pdata instanceof JPEG);
-        assertEquals(HSLFPictureShape.JPEG, pdata.getType());
+        assertEquals(PictureType.JPEG, pdata.getType());
         src_bytes = pdata.getData();
         ppt_bytes = slTests.readFile("clock.jpg");
         assertArrayEquals(src_bytes, ppt_bytes);
@@ -312,7 +313,7 @@ public final class TestPictures extends TestCase{
         pict = (HSLFPictureShape)slides.get(1).getShapes().get(0); //the second slide contains PNG
         pdata = pict.getPictureData();
         assertTrue(pdata instanceof PNG);
-        assertEquals(HSLFPictureShape.PNG, pdata.getType());
+        assertEquals(PictureType.PNG, pdata.getType());
         src_bytes = pdata.getData();
         ppt_bytes = slTests.readFile("tomcat.png");
         assertArrayEquals(src_bytes, ppt_bytes);
@@ -320,7 +321,7 @@ public final class TestPictures extends TestCase{
         pict = (HSLFPictureShape)slides.get(2).getShapes().get(0); //the third slide contains WMF
         pdata = pict.getPictureData();
         assertTrue(pdata instanceof WMF);
-        assertEquals(HSLFPictureShape.WMF, pdata.getType());
+        assertEquals(PictureType.WMF, pdata.getType());
         src_bytes = pdata.getData();
         ppt_bytes = slTests.readFile("santa.wmf");
         assertEquals(src_bytes.length, ppt_bytes.length);
@@ -334,7 +335,7 @@ public final class TestPictures extends TestCase{
         pict = (HSLFPictureShape)slides.get(3).getShapes().get(0); //the forth slide contains PICT
         pdata = pict.getPictureData();
         assertTrue(pdata instanceof PICT);
-        assertEquals(HSLFPictureShape.PICT, pdata.getType());
+        assertEquals(PictureType.PICT, pdata.getType());
         src_bytes = pdata.getData();
         ppt_bytes = slTests.readFile("cow.pict");
         assertEquals(src_bytes.length, ppt_bytes.length);
@@ -348,7 +349,7 @@ public final class TestPictures extends TestCase{
         pict = (HSLFPictureShape)slides.get(4).getShapes().get(0); //the fifth slide contains EMF
         pdata = pict.getPictureData();
         assertTrue(pdata instanceof EMF);
-        assertEquals(HSLFPictureShape.EMF, pdata.getType());
+        assertEquals(PictureType.EMF, pdata.getType());
         src_bytes = pdata.getData();
         ppt_bytes = slTests.readFile("wrench.emf");
         assertArrayEquals(src_bytes, ppt_bytes);
@@ -365,8 +366,8 @@ public final class TestPictures extends TestCase{
                // Should still have 2 real pictures
                assertEquals(2, hslf.getPictures().length);
                // Both are real pictures, both WMF
-               assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[0].getType());
-               assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[1].getType());
+               assertEquals(PictureType.WMF, hslf.getPictures()[0].getType());
+               assertEquals(PictureType.WMF, hslf.getPictures()[1].getType());
 
                // Now test what happens when we use the SlideShow interface
                HSLFSlideShow ppt = new HSLFSlideShow(hslf);
@@ -381,12 +382,12 @@ public final class TestPictures extends TestCase{
         pict = (HSLFPictureShape)slides.get(0).getShapes().get(1); // 2nd object on 1st slide
         pdata = pict.getPictureData();
         assertTrue(pdata instanceof WMF);
-        assertEquals(HSLFPictureShape.WMF, pdata.getType());
+        assertEquals(PictureType.WMF, pdata.getType());
 
         pict = (HSLFPictureShape)slides.get(0).getShapes().get(2); // 3rd object on 1st slide
         pdata = pict.getPictureData();
         assertTrue(pdata instanceof WMF);
-        assertEquals(HSLFPictureShape.WMF, pdata.getType());
+        assertEquals(PictureType.WMF, pdata.getType());
        }
 
     /**
@@ -401,8 +402,8 @@ public final class TestPictures extends TestCase{
                // Should still have 2 real pictures
                assertEquals(2, hslf.getPictures().length);
                // Both are real pictures, both WMF
-               assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[0].getType());
-               assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[1].getType());
+               assertEquals(PictureType.WMF, hslf.getPictures()[0].getType());
+               assertEquals(PictureType.WMF, hslf.getPictures()[1].getType());
 
                // Now test what happens when we use the SlideShow interface
                HSLFSlideShow ppt = new HSLFSlideShow(hslf);
@@ -417,12 +418,12 @@ public final class TestPictures extends TestCase{
         pict = (HSLFPictureShape)slides.get(6).getShapes().get(13);
         pdata = pict.getPictureData();
         assertTrue(pdata instanceof WMF);
-        assertEquals(HSLFPictureShape.WMF, pdata.getType());
+        assertEquals(PictureType.WMF, pdata.getType());
 
         pict = (HSLFPictureShape)slides.get(7).getShapes().get(13);
         pdata = pict.getPictureData();
         assertTrue(pdata instanceof WMF);
-        assertEquals(HSLFPictureShape.WMF, pdata.getType());
+        assertEquals(PictureType.WMF, pdata.getType());
 
         //add a new picture, it should be correctly appended to the Pictures stream
         ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -431,7 +432,7 @@ public final class TestPictures extends TestCase{
 
         int streamSize = out.size();
 
-        HSLFPictureData data = HSLFPictureData.create(HSLFPictureShape.JPEG);
+        HSLFPictureData data = HSLFPictureData.create(PictureType.JPEG);
         data.setData(new byte[100]);
         int offset = hslf.addPicture(data);
         assertEquals(streamSize, offset);
@@ -452,8 +453,8 @@ public final class TestPictures extends TestCase{
 
         HSLFSlide slide = ppt.createSlide();
         byte[] img = slTests.readFile("tomcat.png");
-        int idx = ppt.addPicture(img, HSLFPictureShape.PNG);
-        HSLFPictureShape pict = new HSLFPictureShape(idx);
+        HSLFPictureData data = ppt.addPicture(img, PictureType.PNG);
+        HSLFPictureShape pict = new HSLFPictureShape(data);
         pict.setPictureName("tomcat.png");
         slide.addShape(pict);