--- /dev/null
+package org.apache.poi.hslf.blip;\r
+\r
+import org.apache.poi.hslf.usermodel.PictureData;\r
+import org.apache.poi.hslf.model.Picture;\r
+import org.apache.poi.util.POILogger;\r
+import org.apache.poi.util.POILogFactory;\r
+\r
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+import javax.imageio.ImageIO;\r
+import java.awt.*;\r
+import java.awt.image.BufferedImage;\r
+import java.io.ByteArrayInputStream;\r
+\r
+/**\r
+ * Creates BufferedImage using javax.imageio.ImageIO and draws it in the specified graphics.\r
+ *\r
+ * @author Yegor Kozlov.\r
+ */\r
+public class BitmapPainter implements ImagePainter {\r
+ protected POILogger logger = POILogFactory.getLogger(this.getClass());\r
+\r
+ public void paint(Graphics2D graphics, PictureData pict, Picture parent) {\r
+ BufferedImage img;\r
+ try {\r
+ img = ImageIO.read(new ByteArrayInputStream(pict.getData()));\r
+ }\r
+ catch (Exception e){\r
+ logger.log(POILogger.WARN, "ImageIO failed to create image. image.type: " + pict.getType());\r
+ return;\r
+ }\r
+ Rectangle anchor = parent.getAnchor();\r
+ Image scaledImg = img.getScaledInstance(anchor.width, anchor.height, Image.SCALE_SMOOTH);\r
+ graphics.drawImage(scaledImg, anchor.x, anchor.y, null);\r
+ }\r
+\r
+}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.hslf.blip;\r
+\r
+import org.apache.poi.hslf.model.Picture;\r
+import org.apache.poi.hslf.usermodel.PictureData;\r
+\r
+import java.awt.*;\r
+\r
+/**\r
+ * A common interface for objects that can render ppt picture data.\r
+ * <p>\r
+ * Subclasses can redefine it and use third-party libraries for actual rendering,\r
+ * for example, Bitmaps can be rendered using javax.imageio.* , WMF can be rendered using Apache Batik,\r
+ * PICT can be rendered using Apple QuickTime API for Java, etc.\r
+ * </p>\r
+ *\r
+ * A typical usage is as follows:\r
+ * <code>\r
+ * public WMFPaiter implements ImagePainter{\r
+ * public void paint(Graphics2D graphics, PictureData pict, Picture parent){\r
+ * DataInputStream is = new DataInputStream(new ByteArrayInputStream(pict.getData()));\r
+ * org.apache.batik.transcoder.wmf.tosvg.WMFRecordStore wmfStore =\r
+ * new org.apache.batik.transcoder.wmf.tosvg.WMFRecordStore();\r
+ * try {\r
+ * wmfStore.read(is);\r
+ * } catch (IOException e){\r
+ * return;\r
+ * }\r
+ *\r
+ * Rectangle anchor = parent.getAnchor();\r
+ * float scale = (float)anchor.width/wmfStore.getWidthPixels();\r
+ *\r
+ * org.apache.batik.transcoder.wmf.tosvg.WMFPainter painter =\r
+ * new org.apache.batik.transcoder.wmf.tosvg.WMFPainter(wmfStore, 0, 0, scale);\r
+ * graphics.translate(anchor.x, anchor.y);\r
+ * painter.paint(graphics);\r
+ * }\r
+ * }\r
+ * PictureData.setImagePainter(Picture.WMF, new WMFPaiter());\r
+ * ...\r
+ * </code>\r
+ * Subsequenet calls of Slide.draw(Graphics gr) will use WMFPaiter for WMF images.\r
+ *\r
+ * @author Yegor Kozlov.\r
+ */\r
+public interface ImagePainter {\r
+\r
+ /**\r
+ * Paints the specified picture data\r
+ *\r
+ * @param graphics the graphics to paintb into\r
+ * @param pict the data to paint\r
+ * @param parent the shapes that owns the picture data\r
+ */\r
+ public void paint(Graphics2D graphics, PictureData pict, Picture parent);\r
+}\r
package org.apache.poi.hslf.usermodel;
import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.POILogger;
+import org.apache.poi.util.POILogFactory;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.blip.*;
import org.apache.poi.hslf.exceptions.HSLFException;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import java.awt.*;
/**
* A class that represents image data contained in a slide show.
*/
public abstract class PictureData {
+ protected POILogger logger = POILogFactory.getLogger(this.getClass());
+
/**
* Size of the image checksum calculated using MD5 algorithm.
*/
protected static final int CHECKSUM_SIZE = 16;
- /**
- * Binary data of the picture
- */
+ /**
+ * Binary data of the picture
+ */
private byte[] rawdata;
- /**
- * The offset to the picture in the stream
- */
- protected int offset;
+ /**
+ * The offset to the picture in the stream
+ */
+ protected int offset;
/**
* Returns type of this picture.
*/
protected abstract int getSignature();
+ protected static ImagePainter[] painters = new ImagePainter[8];
+ static {
+ PictureData.setImagePainter(Picture.PNG, new BitmapPainter());
+ PictureData.setImagePainter(Picture.JPEG, new BitmapPainter());
+ PictureData.setImagePainter(Picture.DIB, new BitmapPainter());
+ }
+
/**
* Returns the raw binary data of this Picture excluding the first 8 bytes
* which hold image signature and size of the image data.
return getData().length;
}
+ public void draw(Graphics2D graphics, Picture parent){
+ ImagePainter painter = painters[getType()];
+ if(painter != null) painter.paint(graphics, this, parent);
+ else logger.log(POILogger.WARN, "Rendering is not supported: " + getClass().getName());
+ }
+
+ /**
+ * Register ImagePainter for the specified image type
+ *
+ * @param type image type, must be one of the static constants defined in the <code>Picture<code> class.
+ * @param painter
+ */
+ public static void setImagePainter(int type, ImagePainter painter){
+ painters[type] = painter;
+ }
+
+ /**
+ * Return ImagePainter for the specified image type
+ *
+ * @param type blip type, must be one of the static constants defined in the <code>Picture<code> class.
+ * @return ImagePainter for the specified image type
+ */
+ public static ImagePainter getImagePainter(int type){
+ return painters[type];
+ }
+
}
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.hslf.model;\r
+\r
+import junit.framework.*;\r
+\r
+import java.io.FileOutputStream;\r
+import java.io.File;\r
+import java.awt.*;\r
+\r
+import org.apache.poi.hslf.usermodel.SlideShow;\r
+import org.apache.poi.hslf.usermodel.PictureData;\r
+import org.apache.poi.hslf.HSLFSlideShow;\r
+import org.apache.poi.hslf.blip.ImagePainter;\r
+import org.apache.poi.hslf.blip.BitmapPainter;\r
+import org.apache.poi.ddf.EscherBSERecord;\r
+\r
+/**\r
+ * Test Picture shape.\r
+ * \r
+ * @author Yegor Kozlov\r
+ */\r
+public class TestImagePainter extends TestCase {\r
+\r
+ private static class CustomImagePainer implements ImagePainter{\r
+ public void paint(Graphics2D graphics, PictureData pict, Picture parent){\r
+ //do noting\r
+ }\r
+\r
+ }\r
+\r
+ public void testImagePainter() throws Exception {\r
+\r
+ ImagePainter pntr = PictureData.getImagePainter(Picture.PNG);\r
+ assertTrue(PictureData.getImagePainter(Picture.PNG) instanceof BitmapPainter);\r
+ assertTrue(PictureData.getImagePainter(Picture.JPEG) instanceof BitmapPainter);\r
+ assertTrue(PictureData.getImagePainter(Picture.DIB) instanceof BitmapPainter);\r
+\r
+ PictureData.setImagePainter(Picture.WMF, new CustomImagePainer());\r
+ assertTrue(PictureData.getImagePainter(Picture.WMF) instanceof CustomImagePainer);\r
+ }\r
+\r
+}\r