import org.apache.poi.util.LittleEndian;
import org.apache.poi.hslf.record.*;
+import org.apache.poi.hslf.usermodel.Picture;
/**
* This class contains the main functionality for the Powerpoint file
* Fetch the Current User Atom of the document
*/
public CurrentUserAtom getCurrentUserAtom() { return currentUser; }
+
+ /**
+ * Read pictures contained in this presentation
+ *
+ * @return array with the read pictures ot <code>null</code> if the
+ * presentation doesn't contain pictures.
+ */
+ public Picture[] getPictures() throws IOException {
+ byte[] pictstream;
+
+ try {
+ DocumentEntry entry = (DocumentEntry)filesystem.getRoot().getEntry("Pictures");
+ pictstream = new byte[entry.getSize()];
+ DocumentInputStream is = filesystem.createDocumentInputStream("Pictures");
+ is.read(pictstream);
+ } catch (FileNotFoundException e){
+ //silently catch exceptions if the presentation doesn't contain pictures
+ return null;
+ }
+
+ ArrayList p = new ArrayList();
+ int pos = 0;
+ while (pos < pictstream.length) {
+ Picture pict = new Picture(pictstream, pos);
+ p.add(pict);
+ pos += Picture.HEADER_SIZE + pict.getSize();
+ }
+
+ return (Picture[])p.toArray(new Picture[p.size()]);
+ }
}
--- /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.usermodel;\r
+\r
+import org.apache.poi.util.LittleEndian;\r
+\r
+/**\r
+ * Represents a picture in a PowerPoint document.\r
+ * <p>\r
+ * The information about an image in PowerPoint document is stored in \r
+ * two places:\r
+ * <li> EscherBSE container in the Document keeps information about image \r
+ * type, image index to refer by slides etc.\r
+ * <li> "Pictures" OLE stream holds the actual data of the image.\r
+ * </p>\r
+ * <p>\r
+ * Data in the "Pictures" OLE stream is organized as follows:<br>\r
+ * For each image there is an entry: 25 byte header + image data.\r
+ * Image data is the exact content of the JPEG file, i.e. PowerPoint\r
+ * puts the whole jpeg file there without any modifications.<br>\r
+ * Header format:\r
+ * <li> 2 byte: image type. For JPEGs it is 0x46A0, for PNG it is 0x6E00.\r
+ * <li> 2 byte: unknown.\r
+ * <li> 4 byte : image size + 17. Looks like shift from the end of \r
+ * header but why to add it to the image size?\r
+ * <li> next 16 bytes. Unique identifier of this image which is used by \r
+ * EscherBSE record.\r
+ * </p>\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class Picture {\r
+\r
+ /**\r
+ * Windows Metafile\r
+ */\r
+ public static final int WMF = 0x2160;\r
+\r
+ /**\r
+ * Macintosh PICT\r
+ */\r
+ public static final int PICT = 0x5420;\r
+\r
+ /**\r
+ * JPEG\r
+ */\r
+ public static final int JPEG = 0x46A0;\r
+\r
+ /**\r
+ * PNG\r
+ */\r
+ public static final int PNG = 0x6E00;\r
+\r
+ /**\r
+ * Windows DIB (BMP)\r
+ */\r
+ public static final int DIB = 0x7A80;\r
+\r
+ /**\r
+ * The size of the header\r
+ */\r
+ public static final int HEADER_SIZE = 25;\r
+\r
+ /**\r
+ * Binary data of the picture\r
+ */\r
+ protected byte[] pictdata;\r
+\r
+ /**\r
+ * Header which holds information about this picture\r
+ */\r
+ protected byte[] header;\r
+\r
+ /**\r
+ * Read a picture from "Pictures" OLE stream\r
+ *\r
+ * @param pictstream the bytes to read\r
+ * @param offset the index of the first byte to read\r
+ */\r
+ public Picture(byte[] pictstream, int offset){\r
+ header = new byte[Picture.HEADER_SIZE];\r
+ System.arraycopy(pictstream, offset, header, 0, header.length);\r
+\r
+ int size = LittleEndian.getInt(header, 4) - 17;\r
+ pictdata = new byte[size];\r
+ System.arraycopy(pictstream, offset + Picture.HEADER_SIZE, pictdata, 0, pictdata.length);\r
+ }\r
+\r
+ /**\r
+ * @return the binary data of this picture\r
+ */\r
+ public byte[] getData(){\r
+ return pictdata;\r
+ }\r
+\r
+ /**\r
+ * Return image size in bytes\r
+ *\r
+ * @return the size of the picture in bytes\r
+ */\r
+ public int getSize(){\r
+ return pictdata.length;\r
+ }\r
+\r
+ /**\r
+ * Returns the unique identifier (UID) of this picture.\r
+ * The UID is a checksum of the picture data. Its length is 16 bytes\r
+ * and it must be unique across the presentation.\r
+ *\r
+ * @return the unique identifier of this picture\r
+ */\r
+ public byte[] getUID(){\r
+ byte[] uid = new byte[16];\r
+ System.arraycopy(header, 8, uid, 0, uid.length);\r
+ return uid;\r
+ }\r
+\r
+ /**\r
+ * Returns the type of this picture. Must be one of the static constans defined in this class.\r
+ *\r
+ * @return type of this picture.\r
+ */\r
+ public int getType(){\r
+ int type = LittleEndian.getShort(header, 0);\r
+ return type;\r
+ }\r
+}\r