poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ye...@apache.org
Subject svn commit: r664491 - in /poi/trunk/src/scratchpad: src/org/apache/poi/hslf/blip/BitmapPainter.java src/org/apache/poi/hslf/blip/ImagePainter.java src/org/apache/poi/hslf/usermodel/PictureData.java testcases/org/apache/poi/hslf/model/TestImagePainter.java
Date Sun, 08 Jun 2008 12:31:20 GMT
Author: yegor
Date: Sun Jun  8 05:31:19 2008
New Revision: 664491

URL: http://svn.apache.org/viewvc?rev=664491&view=rev
Log:
Support custom image renderers

Added:
    poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/BitmapPainter.java   (with props)
    poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/ImagePainter.java   (with props)
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestImagePainter.java   (with
props)
Modified:
    poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/PictureData.java

Added: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/BitmapPainter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/BitmapPainter.java?rev=664491&view=auto
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/BitmapPainter.java (added)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/BitmapPainter.java Sun Jun  8 05:31:19
2008
@@ -0,0 +1,51 @@
+package org.apache.poi.hslf.blip;
+
+import org.apache.poi.hslf.usermodel.PictureData;
+import org.apache.poi.hslf.model.Picture;
+import org.apache.poi.util.POILogger;
+import org.apache.poi.util.POILogFactory;
+
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+
+/**
+ * Creates BufferedImage using javax.imageio.ImageIO and draws it in the specified graphics.
+ *
+ * @author  Yegor Kozlov.
+ */
+public class BitmapPainter implements ImagePainter {
+    protected POILogger logger = POILogFactory.getLogger(this.getClass());
+
+    public void paint(Graphics2D graphics, PictureData pict, Picture parent) {
+        BufferedImage img;
+        try {
+               img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
+        }
+        catch (Exception e){
+            logger.log(POILogger.WARN, "ImageIO failed to create image. image.type: " + pict.getType());
+            return;
+        }
+        Rectangle anchor = parent.getAnchor();
+        Image scaledImg = img.getScaledInstance(anchor.width, anchor.height, Image.SCALE_SMOOTH);
+        graphics.drawImage(scaledImg, anchor.x, anchor.y, null);
+    }
+
+}

Propchange: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/BitmapPainter.java
------------------------------------------------------------------------------
    svn:executable = *

Added: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/ImagePainter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/ImagePainter.java?rev=664491&view=auto
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/ImagePainter.java (added)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/ImagePainter.java Sun Jun  8 05:31:19
2008
@@ -0,0 +1,71 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.hslf.blip;
+
+import org.apache.poi.hslf.model.Picture;
+import org.apache.poi.hslf.usermodel.PictureData;
+
+import java.awt.*;
+
+/**
+ * A common interface for objects that can render ppt picture data.
+ * <p>
+ * Subclasses can redefine it and use third-party libraries for actual rendering,
+ * for example, Bitmaps can be rendered using javax.imageio.* , WMF can be rendered using
Apache Batik,
+ * PICT can be rendered using Apple QuickTime API for Java, etc.
+ * </p>
+ *
+ * A typical usage is as follows:
+ * <code>
+ * public WMFPaiter implements ImagePainter{
+ *   public void paint(Graphics2D graphics, PictureData pict, Picture parent){
+ *       DataInputStream is = new DataInputStream(new ByteArrayInputStream(pict.getData()));
+ *       org.apache.batik.transcoder.wmf.tosvg.WMFRecordStore wmfStore =
+ *               new org.apache.batik.transcoder.wmf.tosvg.WMFRecordStore();
+ *       try {
+ *           wmfStore.read(is);
+ *       } catch (IOException e){
+ *           return;
+ *       }
+ *
+ *       Rectangle anchor = parent.getAnchor();
+ *       float scale = (float)anchor.width/wmfStore.getWidthPixels();
+ *
+ *       org.apache.batik.transcoder.wmf.tosvg.WMFPainter painter =
+ *               new org.apache.batik.transcoder.wmf.tosvg.WMFPainter(wmfStore, 0, 0, scale);
+ *       graphics.translate(anchor.x, anchor.y);
+ *       painter.paint(graphics);
+ *   }
+ * }
+ * PictureData.setImagePainter(Picture.WMF, new WMFPaiter());
+ * ...
+ * </code>
+ * Subsequenet calls of Slide.draw(Graphics gr) will use WMFPaiter for WMF images.
+ *
+ * @author  Yegor Kozlov.
+ */
+public interface ImagePainter {
+
+    /**
+     * Paints the specified picture data
+     *
+     * @param graphics  the graphics to paintb into
+     * @param pict      the data to paint
+     * @param parent    the shapes that owns the picture data
+     */
+    public void paint(Graphics2D graphics, PictureData pict, Picture parent);
+}

Propchange: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/ImagePainter.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/PictureData.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/PictureData.java?rev=664491&r1=664490&r2=664491&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/PictureData.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/PictureData.java Sun Jun  8
05:31:19 2008
@@ -17,6 +17,8 @@
 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;
@@ -25,6 +27,7 @@
 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.
@@ -33,19 +36,21 @@
  */
 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.
@@ -71,6 +76,13 @@
      */
     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.
@@ -212,4 +224,30 @@
         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];
+    }
+
 }

Added: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestImagePainter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestImagePainter.java?rev=664491&view=auto
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestImagePainter.java (added)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestImagePainter.java Sun
Jun  8 05:31:19 2008
@@ -0,0 +1,57 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.hslf.model;
+
+import junit.framework.*;
+
+import java.io.FileOutputStream;
+import java.io.File;
+import java.awt.*;
+
+import org.apache.poi.hslf.usermodel.SlideShow;
+import org.apache.poi.hslf.usermodel.PictureData;
+import org.apache.poi.hslf.HSLFSlideShow;
+import org.apache.poi.hslf.blip.ImagePainter;
+import org.apache.poi.hslf.blip.BitmapPainter;
+import org.apache.poi.ddf.EscherBSERecord;
+
+/**
+ * Test Picture shape.
+ * 
+ * @author Yegor Kozlov
+ */
+public class TestImagePainter extends TestCase {
+
+    private static class CustomImagePainer implements ImagePainter{
+        public void paint(Graphics2D graphics, PictureData pict, Picture parent){
+            //do noting
+        }
+
+    }
+
+    public void testImagePainter() throws Exception {
+
+        ImagePainter pntr = PictureData.getImagePainter(Picture.PNG);
+        assertTrue(PictureData.getImagePainter(Picture.PNG) instanceof BitmapPainter);
+        assertTrue(PictureData.getImagePainter(Picture.JPEG) instanceof BitmapPainter);
+        assertTrue(PictureData.getImagePainter(Picture.DIB) instanceof BitmapPainter);
+
+        PictureData.setImagePainter(Picture.WMF, new CustomImagePainer());
+        assertTrue(PictureData.getImagePainter(Picture.WMF) instanceof CustomImagePainer);
+    }
+
+}

Propchange: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestImagePainter.java
------------------------------------------------------------------------------
    svn:executable = *



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message