]> source.dussan.org Git - poi.git/commitdiff
Support for DIB picture types, from Bug 40740
authorNick Burch <nick@apache.org>
Tue, 17 Oct 2006 18:12:43 +0000 (18:12 +0000)
committerNick Burch <nick@apache.org>
Tue, 17 Oct 2006 18:12:43 +0000 (18:12 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@464984 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hslf/blip/DIB.java [new file with mode: 0644]
src/scratchpad/src/org/apache/poi/hslf/extractor/ImageExtractor.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/PictureData.java
src/scratchpad/testcases/org/apache/poi/hslf/data/sci_cec.dib [new file with mode: 0644]
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java

diff --git a/src/scratchpad/src/org/apache/poi/hslf/blip/DIB.java b/src/scratchpad/src/org/apache/poi/hslf/blip/DIB.java
new file mode 100644 (file)
index 0000000..362bbcb
--- /dev/null
@@ -0,0 +1,81 @@
+/* ====================================================================\r
+   Copyright 2002-2004   Apache Software Foundation\r
+\r
+   Licensed under the Apache License, Version 2.0 (the "License");\r
+   you may not use this file except in compliance with the License.\r
+   You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.hslf.blip;\r
+\r
+import org.apache.poi.hslf.model.Picture;\r
+import org.apache.poi.util.LittleEndian;\r
+\r
+import java.io.IOException;\r
+\r
+/**\r
+ * Represents a DIB picture data in a PPT file\r
+ * \r
+ * @author Yegor Kozlov\r
+ */\r
+public class DIB extends Bitmap {\r
+    /**\r
+     * Size of the BITMAPFILEHEADER structure preceding the actual DIB bytes\r
+     */\r
+    public static final int HEADER_SIZE = 14;\r
+\r
+    /**\r
+     * @return type of  this picture\r
+     * @see  org.apache.poi.hslf.model.Picture#DIB\r
+     */\r
+    public int getType(){\r
+        return Picture.DIB;\r
+    }\r
+\r
+    /**\r
+     * DIB signature is <code>0x7A80</code>\r
+     *\r
+     * @return DIB signature (<code>0x7A80</code>)\r
+     */\r
+    public int getSignature(){\r
+        return 0x7A80;\r
+    }\r
+\r
+    public byte[] getData(){\r
+        byte[] data = super.getData();\r
+\r
+        // bitmap file-header, corresponds to a\r
+        // Windows  BITMAPFILEHEADER structure\r
+        // (For more information, consult the Windows API Programmer's reference )\r
+        byte[] header = new byte[HEADER_SIZE];\r
+        //Specifies the file type. It must be set to the signature word BM (0x4D42) to indicate bitmap.\r
+        LittleEndian.putInt(header, 0, 0x4D42);\r
+        //Specifies the size, in bytes, of the bitmap file.\r
+        LittleEndian.putInt(header, 2, data.length); //DIB length including the header\r
+        //Reserved; set to zero\r
+        LittleEndian.putInt(header, 6, 0);\r
+        //the offset, in bytes, from the header to the bitmap bits (looks like it is always 2)\r
+        LittleEndian.putInt(header, 10, 2);\r
+\r
+        //DIB data is the header + dib bytes\r
+        byte[] dib = new byte[header.length + data.length];\r
+        System.arraycopy(header, 0, dib, 0, header.length);\r
+        System.arraycopy(data, 0, dib, header.length, data.length);\r
+\r
+        return dib;\r
+    }\r
+\r
+    public void setData(byte[] data) throws IOException {\r
+        //cut off the bitmap file-header\r
+        byte[] dib = new byte[data.length-HEADER_SIZE];\r
+        System.arraycopy(data, HEADER_SIZE, dib, 0, dib.length);\r
+        super.setData(dib);\r
+    }\r
+}\r
index 85fd16fae971cee9de655d78bac9a9740f3ab929..e778f313ed3acba63940cc7499f8b31f72e0b69b 100644 (file)
@@ -64,6 +64,9 @@ public class ImageExtractor {
                 case Picture.PICT:\r
                     ext = ".pict";\r
                     break;\r
+                case Picture.DIB:\r
+                    ext = ".dib";\r
+                    break;\r
                 default:\r
                     continue;\r
             }\r
index 4b82e780ded30ad8b70263c9e00cb7e6244fa0c1..2c9f714244c52b5f38e83d870f5eb48b9ef9ed4f 100644 (file)
@@ -174,6 +174,9 @@ public abstract class PictureData {
             case Picture.PNG:\r
                 pict = new PNG();\r
                 break;\r
+            case Picture.DIB:\r
+                pict = new DIB();\r
+                break;\r
             default:\r
                 throw new IllegalArgumentException("Unsupported picture type: " + type);\r
         }\r
diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/data/sci_cec.dib b/src/scratchpad/testcases/org/apache/poi/hslf/data/sci_cec.dib
new file mode 100644 (file)
index 0000000..61bfb62
Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hslf/data/sci_cec.dib differ
index 861be0c2c6e884fed12ce8779877a9e91e966660..6767a299b0920871da85deef119e6135370cc9c8 100644 (file)
@@ -249,6 +249,46 @@ public class TestPictures extends TestCase{
         assertTrue(Arrays.equals(src_bytes, ppt_bytes));\r
     }\r
 \r
+    /**\r
+     * Test read/write DIB\r
+     */\r
+    public void testDIB() throws Exception {\r
+        SlideShow ppt = new SlideShow();\r
+\r
+        Slide slide = ppt.createSlide();\r
+        File img = new File(cwd, "sci_cec.dib");\r
+        int idx = ppt.addPicture(img, Picture.DIB);\r
+        Picture pict = new Picture(idx);\r
+        assertEquals(idx, pict.getPictureIndex());\r
+        slide.addShape(pict);\r
+\r
+        //serialize and read again\r
+        ByteArrayOutputStream out = new ByteArrayOutputStream();\r
+        ppt.write(out);\r
+        out.close();\r
+\r
+        ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));\r
+\r
+        //make sure we can read this picture shape and it refers to the correct picture data\r
+        Shape[] sh = ppt.getSlides()[0].getShapes();\r
+        assertEquals(1, sh.length);\r
+        pict = (Picture)sh[0];\r
+        assertEquals(idx, pict.getPictureIndex());\r
+\r
+        //check picture data\r
+        PictureData[] pictures = ppt.getPictureData();\r
+        //the Picture shape refers to the PictureData object in the Presentation\r
+        assertEquals(pict.getPictureData(), pictures[0]);\r
+\r
+        assertEquals(1, pictures.length);\r
+        assertEquals(Picture.DIB, pictures[0].getType());\r
+        assertTrue(pictures[0] instanceof DIB);\r
+        //compare the content of the initial file with what is stored in the PictureData\r
+        byte[] src_bytes = read(img);\r
+        byte[] ppt_bytes = pictures[0].getData();\r
+        assertTrue(Arrays.equals(src_bytes, ppt_bytes));\r
+    }\r
+\r
     /**\r
      * Read file into a byte array\r
      */\r