You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by Artyom Nikiforov <ar...@arcusys.fi> on 2014/12/16 15:23:37 UTC

Cropping background images.

Hi, folks!
I'm trying to convert pptx slides to images with the code
<http://poi.apache.org/slideshow/how-to-shapes.html>:
        FileInputStream is = new FileInputStream("slideshow.pptx");
        XMLSlideShow ppt = new XMLSlideShow(is);
        is.close();

        Dimension pgsize = ppt.getPageSize();

        Slide[] slide = ppt.getSlides();
        for (int i = 0; i &lt; slide.length; i++) {

            BufferedImage img = new BufferedImage(pgsize.width,
pgsize.height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            //clear the drawing area
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,
pgsize.height));

            //render
            slide[i].draw(graphics);

            //save the output
            FileOutputStream out = new FileOutputStream("slide-"  + (i+1) +
".png");
            javax.imageio.ImageIO.write(img, "png", out);
            out.close();
        }

If background image is bigger than a slide, then it's cropped to a slide
width and height. So you get left top corner of an image as a result. If
background image is smaller, then it takes only a part of slide's area(see
the attached files). I haven't found that error in bugtracker
<https://issues.apache.org/bugzilla/buglist.cgi?product=POI>. So the
intention of this letter is only to submit a bug.

BTW, I've made theese changes to eliminate the issue:

>From 45ff683c719eb84612808d120e0a8498710bd791 Mon Sep 17 00:00:00 2001
From: Artyom Nikiforov <ar...@arcusys.fi>
Date: Mon, 15 Dec 2014 11:58:18 +0300
Subject: [PATCH] Resize background picture to slide size.

---
 .../apache/poi/xslf/usermodel/XSLFBackground.java  | 26 ++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFBackground.java
b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFBackground.java
index 604c9df..f1cbec1 100644
--- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFBackground.java
+++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFBackground.java
@@ -23,6 +23,10 @@ import
org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTStyleMatrixReference;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
 import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground;
+import java.awt.image.AffineTransformOp;
+import java.awt.geom.AffineTransform;
+import java.awt.image.BufferedImage;
+import java.awt.TexturePaint;

 import java.awt.Color;
 import java.awt.Dimension;
@@ -81,9 +85,31 @@ public class XSLFBackground extends XSLFSimpleShape {
             fill = rShape.selectPaint(graphics, bgStyle, phClr,
theme.getPackagePart());
         }

+        if(fill.getClass().getName().equals("java.awt.TexturePaint")) {
+            BufferedImage bufImage2 =
this.transformImageToSlideSize(((TexturePaint)fill).getImage());
+            fill = new TexturePaint(bufImage2, this.getAnchor());
+        }
+
         return fill;
     }

+    private BufferedImage transformImageToSlideSize(BufferedImage inBufImage) {
+        Dimension pg = this.getSheet().getSlideShow().getPageSize();
+        double outHeight = pg.getHeight();
+        double outWidth = pg.getWidth();
+        double inHeight = inBufImage.getHeight();
+        double inWeight = inBufImage.getWidth();
+
+        double scaleX = outWidth  / inWeight;
+        double scaleY = outHeight / inHeight;
+
+        BufferedImage outBufImage = new BufferedImage((int)outWidth,
(int)outHeight, BufferedImage.TYPE_INT_ARGB);
+        AffineTransform at = new AffineTransform();
+        at.scale(scaleX, scaleY);
+        AffineTransformOp scaleOp = new AffineTransformOp(at,
AffineTransformOp.TYPE_BILINEAR);
+        return scaleOp.filter(inBufImage, outBufImage);
+    }
+
     @Override
     public Color getFillColor(){
         Paint p = getPaint(null);
-- 
2.1.3

Re: Cropping background images.

Posted by Nick Burch <ap...@gagravarr.org>.
Any chance you could open an bug in bugzilla, and attach the patch there? 
That way it won't get forgotten about
http://issues.apache.org/bugzilla/buglist.cgi?product=POI

Thanks
Nick

On Tue, 16 Dec 2014, Artyom Nikiforov wrote:
> Hi, folks!
> I'm trying to convert pptx slides to images with the code
> <http://poi.apache.org/slideshow/how-to-shapes.html>:
>        FileInputStream is = new FileInputStream("slideshow.pptx");
>        XMLSlideShow ppt = new XMLSlideShow(is);
>        is.close();
>
>        Dimension pgsize = ppt.getPageSize();
>
>        Slide[] slide = ppt.getSlides();
>        for (int i = 0; i &lt; slide.length; i++) {
>
>            BufferedImage img = new BufferedImage(pgsize.width,
> pgsize.height, BufferedImage.TYPE_INT_RGB);
>            Graphics2D graphics = img.createGraphics();
>            //clear the drawing area
>            graphics.setPaint(Color.white);
>            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,
> pgsize.height));
>
>            //render
>            slide[i].draw(graphics);
>
>            //save the output
>            FileOutputStream out = new FileOutputStream("slide-"  + (i+1) +
> ".png");
>            javax.imageio.ImageIO.write(img, "png", out);
>            out.close();
>        }
>
> If background image is bigger than a slide, then it's cropped to a slide
> width and height. So you get left top corner of an image as a result. If
> background image is smaller, then it takes only a part of slide's area(see
> the attached files). I haven't found that error in bugtracker
> <https://issues.apache.org/bugzilla/buglist.cgi?product=POI>. So the
> intention of this letter is only to submit a bug.
>
> BTW, I've made theese changes to eliminate the issue:
>
>> From 45ff683c719eb84612808d120e0a8498710bd791 Mon Sep 17 00:00:00 2001
> From: Artyom Nikiforov <ar...@arcusys.fi>
> Date: Mon, 15 Dec 2014 11:58:18 +0300
> Subject: [PATCH] Resize background picture to slide size.
>
> ---
> .../apache/poi/xslf/usermodel/XSLFBackground.java  | 26 ++++++++++++++++++++++
> 1 file changed, 26 insertions(+)
>
> diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFBackground.java
> b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFBackground.java
> index 604c9df..f1cbec1 100644
> --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFBackground.java
> +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFBackground.java
> @@ -23,6 +23,10 @@ import
> org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
> import org.openxmlformats.schemas.drawingml.x2006.main.CTStyleMatrixReference;
> import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
> import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground;
> +import java.awt.image.AffineTransformOp;
> +import java.awt.geom.AffineTransform;
> +import java.awt.image.BufferedImage;
> +import java.awt.TexturePaint;
>
> import java.awt.Color;
> import java.awt.Dimension;
> @@ -81,9 +85,31 @@ public class XSLFBackground extends XSLFSimpleShape {
>             fill = rShape.selectPaint(graphics, bgStyle, phClr,
> theme.getPackagePart());
>         }
>
> +        if(fill.getClass().getName().equals("java.awt.TexturePaint")) {
> +            BufferedImage bufImage2 =
> this.transformImageToSlideSize(((TexturePaint)fill).getImage());
> +            fill = new TexturePaint(bufImage2, this.getAnchor());
> +        }
> +
>         return fill;
>     }
>
> +    private BufferedImage transformImageToSlideSize(BufferedImage inBufImage) {
> +        Dimension pg = this.getSheet().getSlideShow().getPageSize();
> +        double outHeight = pg.getHeight();
> +        double outWidth = pg.getWidth();
> +        double inHeight = inBufImage.getHeight();
> +        double inWeight = inBufImage.getWidth();
> +
> +        double scaleX = outWidth  / inWeight;
> +        double scaleY = outHeight / inHeight;
> +
> +        BufferedImage outBufImage = new BufferedImage((int)outWidth,
> (int)outHeight, BufferedImage.TYPE_INT_ARGB);
> +        AffineTransform at = new AffineTransform();
> +        at.scale(scaleX, scaleY);
> +        AffineTransformOp scaleOp = new AffineTransformOp(at,
> AffineTransformOp.TYPE_BILINEAR);
> +        return scaleOp.filter(inBufImage, outBufImage);
> +    }
> +
>     @Override
>     public Color getFillColor(){
>         Paint p = getPaint(null);
>

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