--- /dev/null
+/* ====================================================================\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
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