mirror of https://github.com/apache/poi.git
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
This commit is contained in:
parent
826805b0c5
commit
6141b9b40c
|
@ -24,6 +24,7 @@ import java.awt.Insets;
|
||||||
import java.awt.Shape;
|
import java.awt.Shape;
|
||||||
import java.awt.geom.AffineTransform;
|
import java.awt.geom.AffineTransform;
|
||||||
import java.awt.geom.Rectangle2D;
|
import java.awt.geom.Rectangle2D;
|
||||||
|
import java.awt.image.AffineTransformOp;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.awt.image.RescaleOp;
|
import java.awt.image.RescaleOp;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
@ -69,14 +70,25 @@ public class BitmapImageRenderer implements ImageRenderer {
|
||||||
return bi;
|
return bi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
/**
|
|
||||||
* @return the buffered image
|
|
||||||
*/
|
|
||||||
public BufferedImage getImage() {
|
public BufferedImage getImage() {
|
||||||
return img;
|
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
|
@Override
|
||||||
public Dimension getDimension() {
|
public Dimension getDimension() {
|
||||||
return (img == null)
|
return (img == null)
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
package org.apache.poi.sl.draw;
|
package org.apache.poi.sl.draw;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
import java.awt.Dimension;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.LinearGradientPaint;
|
import java.awt.LinearGradientPaint;
|
||||||
import java.awt.MultipleGradientPaint.ColorSpaceType;
|
import java.awt.MultipleGradientPaint.ColorSpaceType;
|
||||||
|
@ -153,12 +154,20 @@ public class DrawPaint {
|
||||||
renderer.setAlpha(alpha/100000.f);
|
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) {
|
if(image == null) {
|
||||||
LOG.log(POILogger.ERROR, "Can't load image data");
|
LOG.log(POILogger.ERROR, "Can't load image data");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Rectangle2D textAnchor = shape.getAnchor();
|
|
||||||
Paint paint = new java.awt.TexturePaint(image, textAnchor);
|
Paint paint = new java.awt.TexturePaint(image, textAnchor);
|
||||||
|
|
||||||
return paint;
|
return paint;
|
||||||
|
|
|
@ -408,6 +408,9 @@ public class DrawSimpleShape extends DrawShape {
|
||||||
|
|
||||||
presets.put(cusName, new CustomGeometry(cusGeom));
|
presets.put(cusName, new CustomGeometry(cusGeom));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
staxFiltRd.close();
|
||||||
|
staxReader.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("Unable to load preset geometries.", e);
|
throw new RuntimeException("Unable to load preset geometries.", e);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -18,17 +18,13 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.poi.sl.draw;
|
package org.apache.poi.sl.draw;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.Dimension;
|
||||||
import java.awt.geom.AffineTransform;
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.Insets;
|
||||||
import java.awt.geom.Rectangle2D;
|
import java.awt.geom.Rectangle2D;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.awt.image.RescaleOp;
|
import java.io.IOException;
|
||||||
import java.io.*;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
|
|
||||||
import org.apache.poi.util.POILogFactory;
|
|
||||||
import org.apache.poi.util.POILogger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Classes can implement this interfaces to support other formats, for
|
* Classes can implement this interfaces to support other formats, for
|
||||||
|
@ -110,6 +106,14 @@ public interface ImageRenderer {
|
||||||
*/
|
*/
|
||||||
BufferedImage getImage();
|
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
|
* Render picture data into the supplied graphics
|
||||||
*
|
*
|
||||||
|
|
|
@ -160,6 +160,9 @@ public class PPTX2PNG {
|
||||||
File outfile = new File(outdir, outname);
|
File outfile = new File(outdir, outname);
|
||||||
ImageIO.write(img, format, outfile);
|
ImageIO.write(img, format, outfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
graphics.dispose();
|
||||||
|
img.flush();
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
ss.close();
|
ss.close();
|
||||||
|
|
|
@ -72,18 +72,22 @@ public class HwmfSLImageRenderer implements ImageRenderer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BufferedImage getImage() {
|
public BufferedImage getImage() {
|
||||||
|
return getImage(getDimension());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BufferedImage getImage(Dimension dim) {
|
||||||
if (image == null) {
|
if (image == null) {
|
||||||
return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
|
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);
|
BufferedImage bufImg = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
||||||
Graphics2D g = bufImg.createGraphics();
|
Graphics2D g = bufImg.createGraphics();
|
||||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
||||||
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||||
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
|
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();
|
g.dispose();
|
||||||
|
|
||||||
if (alpha != 0) {
|
if (alpha != 0) {
|
||||||
|
|
Loading…
Reference in New Issue