diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2016-05-16 10:09:22 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2016-05-16 10:09:22 +0000 |
commit | 6141b9b40ca0f5b953bedd9522e43c631623a44c (patch) | |
tree | 16010b4ea199a9436b5f2fc6743b83f999430a40 /src | |
parent | 826805b0c583f80fb24f8bdf249f7818f20d5975 (diff) | |
download | poi-6141b9b40ca0f5b953bedd9522e43c631623a44c.tar.gz poi-6141b9b40ca0f5b953bedd9522e43c631623a44c.zip |
Close resources in Common SL rendering classes and minimize memory consumption by using the image anchor dimensions instead of the WMF-included dimensions
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744007 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
6 files changed, 53 insertions, 18 deletions
diff --git a/src/java/org/apache/poi/sl/draw/BitmapImageRenderer.java b/src/java/org/apache/poi/sl/draw/BitmapImageRenderer.java index 5099246e60..73e45d5dba 100644 --- a/src/java/org/apache/poi/sl/draw/BitmapImageRenderer.java +++ b/src/java/org/apache/poi/sl/draw/BitmapImageRenderer.java @@ -24,6 +24,7 @@ import java.awt.Insets; import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
+import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.ByteArrayInputStream;
@@ -69,15 +70,26 @@ public class BitmapImageRenderer implements ImageRenderer { return bi;
}
-
- /**
- * @return the buffered image
- */
+ @Override
public BufferedImage getImage() {
return img;
}
@Override
+ public BufferedImage getImage(Dimension dim) {
+ double w_old = img.getWidth();
+ double h_old = img.getHeight();
+ BufferedImage scaled = new BufferedImage((int)w_old, (int)h_old, BufferedImage.TYPE_INT_ARGB);
+ double w_new = dim.getWidth();
+ double h_new = dim.getHeight();
+ AffineTransform at = new AffineTransform();
+ at.scale(w_new/w_old, h_new/h_old);
+ AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
+ scaleOp.filter(img, scaled);
+ return scaled;
+ }
+
+ @Override
public Dimension getDimension() {
return (img == null)
? new Dimension(0,0)
diff --git a/src/java/org/apache/poi/sl/draw/DrawPaint.java b/src/java/org/apache/poi/sl/draw/DrawPaint.java index 7362a0e4a5..2bebffc203 100644 --- a/src/java/org/apache/poi/sl/draw/DrawPaint.java +++ b/src/java/org/apache/poi/sl/draw/DrawPaint.java @@ -18,6 +18,7 @@ package org.apache.poi.sl.draw;
import java.awt.Color;
+import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.ColorSpaceType;
@@ -153,12 +154,20 @@ public class DrawPaint { renderer.setAlpha(alpha/100000.f);
}
- BufferedImage image = renderer.getImage();
+ Rectangle2D textAnchor = shape.getAnchor();
+ BufferedImage image;
+ if ("image/x-wmf".equals(fill.getContentType())) {
+ // don't rely on wmf dimensions, use dimension of anchor
+ // TODO: check pixels vs. points for image dimension
+ image = renderer.getImage(new Dimension((int)textAnchor.getWidth(), (int)textAnchor.getHeight()));
+ } else {
+ image = renderer.getImage();
+ }
+
if(image == null) {
LOG.log(POILogger.ERROR, "Can't load image data");
return null;
}
- Rectangle2D textAnchor = shape.getAnchor();
Paint paint = new java.awt.TexturePaint(image, textAnchor);
return paint;
diff --git a/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java b/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java index 7206996660..748b691dba 100644 --- a/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java +++ b/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java @@ -408,6 +408,9 @@ public class DrawSimpleShape extends DrawShape { presets.put(cusName, new CustomGeometry(cusGeom));
}
+
+ staxFiltRd.close();
+ staxReader.close();
} catch (Exception e) {
throw new RuntimeException("Unable to load preset geometries.", e);
} finally {
diff --git a/src/java/org/apache/poi/sl/draw/ImageRenderer.java b/src/java/org/apache/poi/sl/draw/ImageRenderer.java index fed5d078cc..6b8c49a143 100644 --- a/src/java/org/apache/poi/sl/draw/ImageRenderer.java +++ b/src/java/org/apache/poi/sl/draw/ImageRenderer.java @@ -18,17 +18,13 @@ */
package org.apache.poi.sl.draw;
-import java.awt.*;
-import java.awt.geom.AffineTransform;
+import java.awt.Dimension;
+import java.awt.Graphics2D;
+import java.awt.Insets;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
-import java.awt.image.RescaleOp;
-import java.io.*;
-
-import javax.imageio.ImageIO;
-
-import org.apache.poi.util.POILogFactory;
-import org.apache.poi.util.POILogger;
+import java.io.IOException;
+import java.io.InputStream;
/**
* Classes can implement this interfaces to support other formats, for
@@ -109,6 +105,14 @@ public interface ImageRenderer { * @return the image as buffered image
*/
BufferedImage getImage();
+
+ /**
+ * @param dim the dimension in pixels of the returned image
+ * @return the image as buffered image
+ *
+ * @since POI 3.15-beta2
+ */
+ BufferedImage getImage(Dimension dim);
/**
* Render picture data into the supplied graphics
diff --git a/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java b/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java index 7fb12eac8d..83d944bf22 100644 --- a/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java +++ b/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java @@ -160,6 +160,9 @@ public class PPTX2PNG { File outfile = new File(outdir, outname);
ImageIO.write(img, format, outfile);
}
+
+ graphics.dispose();
+ img.flush();
}
} finally {
ss.close();
diff --git a/src/scratchpad/src/org/apache/poi/hwmf/draw/HwmfSLImageRenderer.java b/src/scratchpad/src/org/apache/poi/hwmf/draw/HwmfSLImageRenderer.java index ad6cacbdc5..ab69ea6d53 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/draw/HwmfSLImageRenderer.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/draw/HwmfSLImageRenderer.java @@ -72,18 +72,22 @@ public class HwmfSLImageRenderer implements ImageRenderer { @Override
public BufferedImage getImage() {
+ return getImage(getDimension());
+ }
+
+ @Override
+ public BufferedImage getImage(Dimension dim) {
if (image == null) {
return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
}
- Dimension dim = getDimension();
BufferedImage bufImg = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bufImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
- image.draw(g);
+ image.draw(g, new Rectangle2D.Double(0,0,dim.getWidth(),dim.getHeight()));
g.dispose();
if (alpha != 0) {
@@ -97,7 +101,7 @@ public class HwmfSLImageRenderer implements ImageRenderer { return bufImg;
}
-
+
@Override
public boolean drawImage(Graphics2D graphics, Rectangle2D anchor) {
return drawImage(graphics, anchor, null);
|