poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kiwiwi...@apache.org
Subject svn commit: r1648335 - in /poi: site/src/documentation/content/xdocs/ trunk/src/scratchpad/src/org/apache/poi/hslf/blip/ trunk/src/scratchpad/src/org/apache/poi/hslf/model/ trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/ trunk/test-data/slide...
Date Mon, 29 Dec 2014 13:35:57 GMT
Author: kiwiwings
Date: Mon Dec 29 13:35:57 2014
New Revision: 1648335

URL: http://svn.apache.org/r1648335
Log:
Bug 54541 - Add support for cropped images in Slide.draw()

Added:
    poi/trunk/test-data/slideshow/54541_cropped_bitmap.ppt   (with props)
Modified:
    poi/site/src/documentation/content/xdocs/status.xml
    poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/BitmapPainter.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/AllHSLFModelTests.java
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestPicture.java

Modified: poi/site/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/site/src/documentation/content/xdocs/status.xml?rev=1648335&r1=1648334&r2=1648335&view=diff
==============================================================================
--- poi/site/src/documentation/content/xdocs/status.xml (original)
+++ poi/site/src/documentation/content/xdocs/status.xml Mon Dec 29 13:35:57 2014
@@ -38,6 +38,7 @@
     </devs>
 
     <release version="3.12-beta1" date="2015-??-??">
+        <action dev="PD" type="add" fixes-bug="54541">Add support for cropped images
in Slide.draw()</action>
 		<action dev="PD" type="add" fixes-bug="57007">Add initial implementations of DMIN
and DGET functions</action>
         <action dev="PD" type="add">Support for Office Binary Document RC4 CryptoAPI
Encryption for HSLF</action>
         <action dev="PD" type="add" fixes-bug="57195">Support for Office Binary Document
RC4 Encryption</action>

Modified: 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=1648335&r1=1648334&r2=1648335&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/BitmapPainter.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/BitmapPainter.java Mon Dec 29 13:35:57
2014
@@ -22,6 +22,9 @@ 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
@@ -39,7 +42,9 @@ import org.apache.poi.util.POILogFactory
    limitations under the License.
 ==================================================================== */
 import javax.imageio.ImageIO;
+
 import java.awt.*;
+import java.awt.geom.AffineTransform;
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayInputStream;
 
@@ -54,15 +59,36 @@ public final class BitmapPainter impleme
     public void paint(Graphics2D graphics, PictureData pict, Picture parent) {
         BufferedImage img;
         try {
-               img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
-        }
-        catch (Exception e){
+            img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
+        } catch (Exception e) {
             logger.log(POILogger.WARN, "ImageIO failed to create image. image.type: " + pict.getType());
             return;
         }
 
+        boolean isClipped = true;
+        Insets clip = parent.getBlipClip();
+        if (clip == null) {
+            isClipped = false;
+            clip = new Insets(0,0,0,0);
+        }        
+        
+        int iw = img.getWidth();
+        int ih = img.getHeight();
+
         Rectangle anchor = parent.getLogicalAnchor2D().getBounds();
-        graphics.drawImage(img, anchor.x, anchor.y, anchor.width, anchor.height, null);
+
+        double cw = (100000-clip.left-clip.right) / 100000.0;
+        double ch = (100000-clip.top-clip.bottom) / 100000.0;
+        double sx = anchor.getWidth()/(iw*cw);
+        double sy = anchor.getHeight()/(ih*ch);
+        double tx = anchor.getX()-(iw*sx*clip.left/100000.0);
+        double ty = anchor.getY()-(ih*sy*clip.top/100000.0);
+        AffineTransform at = new AffineTransform(sx, 0, 0, sy, tx, ty) ;
+
+        Shape clipOld = graphics.getClip();
+        if (isClipped) graphics.clip(anchor.getBounds2D());
+        graphics.drawRenderedImage(img, at);
+        graphics.setClip(clipOld);
     }
 
 }

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java?rev=1648335&r1=1648334&r2=1648335&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Picture.java Mon Dec 29 13:35:57
2014
@@ -18,6 +18,7 @@
 package org.apache.poi.hslf.model;
 
 import java.awt.Graphics2D;
+import java.awt.Insets;
 import java.awt.geom.AffineTransform;
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayInputStream;
@@ -264,4 +265,40 @@ public class Picture extends SimpleShape
 
         graphics.setTransform(at);
     }
-}
+
+    /**
+     * Returns the clipping values as percent ratio relatively to the image size.
+     * The anchor specified by {@link #getLogicalAnchor2D()} is the displayed size,
+     * i.e. the size of the already clipped image
+     * 
+     * @return the clipping as insets converted/scaled to 100000 (=100%) 
+     */
+    public Insets getBlipClip() {
+        EscherOptRecord opt = getEscherOptRecord();
+        
+        double top    = getFractProp(opt, EscherProperties.BLIP__CROPFROMTOP);
+        double bottom = getFractProp(opt, EscherProperties.BLIP__CROPFROMBOTTOM);
+        double left   = getFractProp(opt, EscherProperties.BLIP__CROPFROMLEFT);
+        double right  = getFractProp(opt, EscherProperties.BLIP__CROPFROMRIGHT);
+        
+        // if all crop values are zero (the default) then no crop rectangle is set, return
null
+        return (top==0 && bottom==0 && left==0 && right==0)
+            ? null
+            : new Insets((int)(top*100000), (int)(left*100000), (int)(bottom*100000), (int)(right*100000));
+    }
+    
+    /**
+     * @return the fractional property or 0 if not defined
+     *
+     * @see <a href="http://msdn.microsoft.com/en-us/library/dd910765(v=office.12).aspx">2.2.1.6
FixedPoint</a>
+     */
+    private static double getFractProp(EscherOptRecord opt, short propertyId) {
+        EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, propertyId);
+        if (prop == null) return 0;
+        int fixedPoint = prop.getPropertyValue();
+        int i = (fixedPoint >> 16);
+        int f = (fixedPoint >> 0) & 0xFFFF;
+        double fp = i + f/65536.0;
+        return fp;
+    }
+}
\ No newline at end of file

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/AllHSLFModelTests.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/AllHSLFModelTests.java?rev=1648335&r1=1648334&r2=1648335&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/AllHSLFModelTests.java (original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/AllHSLFModelTests.java Mon
Dec 29 13:35:57 2014
@@ -17,39 +17,35 @@
 
 package org.apache.poi.hslf.model;
 
-import junit.framework.Test;
-import junit.framework.TestSuite;
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
 
 /**
  * Collects all tests from the package <tt>org.apache.poi.hslf.model</tt>.
- * 
- * @author Josh Micich
  */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+    TestBackground.class,
+    TestFreeform.class,
+    TestHeadersFooters.class,
+    TestHyperlink.class,
+    TestImagePainter.class,
+    TestLine.class,
+    TestMovieShape.class,
+    TestOleEmbedding.class,
+    TestPPFont.class,
+    TestPPGraphics2D.class,
+    TestPicture.class,
+    TestSetBoldItalic.class,
+    TestShapes.class,
+    TestSheet.class,
+    TestSlideChangeNotes.class,
+    TestSlideMaster.class,
+    TestSlides.class,
+    TestTable.class,
+    TestTextRun.class,
+    TestTextRunReWrite.class,
+    TestTextShape.class
+})
 public class AllHSLFModelTests {
-	
-	public static Test suite() {
-		TestSuite result = new TestSuite(AllHSLFModelTests.class.getName());
-		result.addTestSuite(TestBackground.class);
-		result.addTestSuite(TestFreeform.class);
-		result.addTestSuite(TestHeadersFooters.class);
-		result.addTestSuite(TestHyperlink.class);
-		result.addTestSuite(TestImagePainter.class);
-		result.addTestSuite(TestLine.class);
-		result.addTestSuite(TestMovieShape.class);
-		result.addTestSuite(TestOleEmbedding.class);
-		result.addTestSuite(TestPPFont.class);
-		result.addTestSuite(TestPPGraphics2D.class);
-		result.addTestSuite(TestPicture.class);
-		result.addTestSuite(TestSetBoldItalic.class);
-		result.addTestSuite(TestShapes.class);
-		result.addTestSuite(TestSheet.class);
-		result.addTestSuite(TestSlideChangeNotes.class);
-		result.addTestSuite(TestSlideMaster.class);
-		result.addTestSuite(TestSlides.class);
-		result.addTestSuite(TestTable.class);
-		result.addTestSuite(TestTextRun.class);
-		result.addTestSuite(TestTextRunReWrite.class);
-		result.addTestSuite(TestTextShape.class);
-		return result;
-	}
 }

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestPicture.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestPicture.java?rev=1648335&r1=1648334&r2=1648335&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestPicture.java (original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestPicture.java Mon Dec
29 13:35:57 2014
@@ -17,26 +17,38 @@
 
 package org.apache.poi.hslf.model;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+import java.awt.Dimension;
 import java.awt.Graphics2D;
 import java.awt.Rectangle;
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayInputStream;
-import javax.imageio.ImageIO;
+import java.io.File;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
 
-import junit.framework.TestCase;
+import javax.imageio.ImageIO;
 
+import org.apache.poi.POIDataSamples;
 import org.apache.poi.ddf.EscherBSERecord;
 import org.apache.poi.hslf.HSLFSlideShow;
 import org.apache.poi.hslf.usermodel.PictureData;
 import org.apache.poi.hslf.usermodel.SlideShow;
-import org.apache.poi.POIDataSamples;
+import org.apache.poi.util.JvmBugs;
+import org.junit.Ignore;
+import org.junit.Test;
 
 /**
  * Test Picture shape.
  *
  * @author Yegor Kozlov
  */
-public final class TestPicture extends TestCase {
+public final class TestPicture {
     private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
 
     /**
@@ -44,7 +56,8 @@ public final class TestPicture extends T
      * This is important when the same image appears multiple times in a slide show.
      *
      */
-    public void testMultiplePictures() throws Exception {
+    @Test
+    public void multiplePictures() throws Exception {
         SlideShow ppt = new SlideShow();
 
         Slide s = ppt.createSlide();
@@ -78,7 +91,8 @@ public final class TestPicture extends T
      * Picture#getEscherBSERecord threw NullPointerException if EscherContainerRecord.BSTORE_CONTAINER
      * was not found. The correct behaviour is to return null.
      */
-    public void test46122() {
+    @Test
+    public void bug46122() {
         SlideShow ppt = new SlideShow();
         Slide slide = ppt.createSlide();
 
@@ -92,7 +106,8 @@ public final class TestPicture extends T
         pict.draw(graphics);
     }
 
-    public void testMacImages() throws Exception {
+    @Test
+    public void macImages() throws Exception {
         HSLFSlideShow hss = new HSLFSlideShow(_slTests.openResourceAsStream("53446.ppt"));
 
         PictureData[] pictures = hss.getPictures();
@@ -128,4 +143,47 @@ public final class TestPicture extends T
             }
         }
     }
+
+    @Test
+    @Ignore("Just for visual validation - antialiasing is different on various systems")
+    public void bug54541() throws Exception {
+//        InputStream xis = _slTests.openResourceAsStream("54542_cropped_bitmap.pptx");
+//        XMLSlideShow xss = new XMLSlideShow(xis);
+//        xis.close();
+//        
+//        Dimension xpg = xss.getPageSize();
+//        for(XSLFSlide slide : xss.getSlides()) {
+//            BufferedImage img = new BufferedImage(xpg.width, xpg.height, BufferedImage.TYPE_INT_RGB);
+//            Graphics2D graphics = img.createGraphics();
+//            fixFonts(graphics);
+//            slide.draw(graphics);
+//            ImageIO.write(img, "PNG", new File("testx.png"));
+//        }
+//
+//        System.out.println("########################");
+        
+        InputStream is = _slTests.openResourceAsStream("54541_cropped_bitmap.ppt");
+        SlideShow ss = new SlideShow(is);
+        is.close();
+        
+        Dimension pg = ss.getPageSize();
+        int i=1;
+        for(Slide slide : ss.getSlides()) {
+            BufferedImage img = new BufferedImage(pg.width, pg.height, BufferedImage.TYPE_INT_RGB);
+            Graphics2D graphics = img.createGraphics();
+            fixFonts(graphics);
+            slide.draw(graphics);
+            ImageIO.write(img, "PNG", new File("test"+(i++)+".png"));
+        }
+    }
+    
+    @SuppressWarnings("unchecked")
+    private void fixFonts(Graphics2D graphics) {
+        if (!JvmBugs.hasLineBreakMeasurerBug()) return;
+        Map<String,String> fontMap = (Map<String,String>)graphics.getRenderingHint(TextPainter.KEY_FONTMAP);
+        if (fontMap == null) fontMap = new HashMap<String,String>();
+        fontMap.put("Calibri", "Lucida Sans");
+        fontMap.put("Cambria", "Lucida Bright");
+        graphics.setRenderingHint(TextPainter.KEY_FONTMAP, fontMap);        
+    }
 }

Added: poi/trunk/test-data/slideshow/54541_cropped_bitmap.ppt
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/slideshow/54541_cropped_bitmap.ppt?rev=1648335&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/slideshow/54541_cropped_bitmap.ppt
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



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


Mime
View raw message