From e18eef07b19eb173867ba41dcd179b8dc2ca4e1d Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Mon, 22 Jul 2019 21:30:12 +0000 Subject: [PATCH] Refactor alpha handling git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1863601 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/sl/draw/BitmapImageRenderer.java | 24 ++++++++++++------- .../poi/hemf/draw/HemfImageRenderer.java | 13 +++------- .../poi/hwmf/draw/HwmfImageRenderer.java | 14 +++-------- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/java/org/apache/poi/sl/draw/BitmapImageRenderer.java b/src/java/org/apache/poi/sl/draw/BitmapImageRenderer.java index 3643b976c0..e6a28a3c23 100644 --- a/src/java/org/apache/poi/sl/draw/BitmapImageRenderer.java +++ b/src/java/org/apache/poi/sl/draw/BitmapImageRenderer.java @@ -244,9 +244,9 @@ public class BitmapImageRenderer implements ImageRenderer { } 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(); + BufferedImage scaled = new BufferedImage((int)w_new, (int)h_new, BufferedImage.TYPE_INT_ARGB); AffineTransform at = new AffineTransform(); at.scale(w_new/w_old, h_new/h_old); AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); @@ -263,16 +263,22 @@ public class BitmapImageRenderer implements ImageRenderer { @Override public void setAlpha(double alpha) { - if (img == null) return; + img = setAlpha(img, alpha); + } + + public static BufferedImage setAlpha(BufferedImage image, double alpha) { + if (image == null) { + return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); + } - Dimension dim = getDimension(); - BufferedImage newImg = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB); - Graphics2D g = newImg.createGraphics(); - RescaleOp op = new RescaleOp(new float[]{1.0f, 1.0f, 1.0f, (float)alpha}, new float[]{0,0,0,0}, null); - g.drawImage(img, op, 0, 0); - g.dispose(); + if (alpha == 0) { + return image; + } - img = newImg; + float[] scalefactors = {1, 1, 1, (float)alpha}; + float[] offsets = {0,0,0,0}; + RescaleOp op = new RescaleOp(scalefactors, offsets, null); + return op.filter(image, null); } diff --git a/src/scratchpad/src/org/apache/poi/hemf/draw/HemfImageRenderer.java b/src/scratchpad/src/org/apache/poi/hemf/draw/HemfImageRenderer.java index 3a4ea04c4c..39db61492b 100644 --- a/src/scratchpad/src/org/apache/poi/hemf/draw/HemfImageRenderer.java +++ b/src/scratchpad/src/org/apache/poi/hemf/draw/HemfImageRenderer.java @@ -30,10 +30,12 @@ import java.io.IOException; import java.io.InputStream; import org.apache.poi.hemf.usermodel.HemfPicture; +import org.apache.poi.sl.draw.BitmapImageRenderer; import org.apache.poi.sl.draw.ImageRenderer; import org.apache.poi.sl.usermodel.PictureData; import org.apache.poi.util.Units; +@SuppressWarnings("unused") public class HemfImageRenderer implements ImageRenderer { HemfPicture image; double alpha; @@ -89,16 +91,7 @@ public class HemfImageRenderer implements ImageRenderer { image.draw(g, new Rectangle2D.Double(0,0,dim.getWidth(),dim.getHeight())); g.dispose(); - if (alpha != 0) { - BufferedImage newImg = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB); - g = newImg.createGraphics(); - RescaleOp op = new RescaleOp(new float[]{1.0f, 1.0f, 1.0f, (float)alpha}, new float[]{0,0,0,0}, null); - g.drawImage(bufImg, op, 0, 0); - g.dispose(); - bufImg = newImg; - } - - return bufImg; + return BitmapImageRenderer.setAlpha(bufImg, alpha); } @Override diff --git a/src/scratchpad/src/org/apache/poi/hwmf/draw/HwmfImageRenderer.java b/src/scratchpad/src/org/apache/poi/hwmf/draw/HwmfImageRenderer.java index f30bdc34d2..bc96032f03 100644 --- a/src/scratchpad/src/org/apache/poi/hwmf/draw/HwmfImageRenderer.java +++ b/src/scratchpad/src/org/apache/poi/hwmf/draw/HwmfImageRenderer.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.io.InputStream; import org.apache.poi.hwmf.usermodel.HwmfPicture; +import org.apache.poi.sl.draw.BitmapImageRenderer; import org.apache.poi.sl.draw.DrawPictureShape; import org.apache.poi.sl.draw.ImageRenderer; import org.apache.poi.sl.usermodel.PictureData.PictureType; @@ -93,17 +94,8 @@ public class HwmfImageRenderer implements ImageRenderer { g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); image.draw(g, new Rectangle2D.Double(0,0,dim.getWidth(),dim.getHeight())); g.dispose(); - - if (alpha != 0) { - BufferedImage newImg = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB); - g = newImg.createGraphics(); - RescaleOp op = new RescaleOp(new float[]{1.0f, 1.0f, 1.0f, (float)alpha}, new float[]{0,0,0,0}, null); - g.drawImage(bufImg, op, 0, 0); - g.dispose(); - bufImg = newImg; - } - - return bufImg; + + return BitmapImageRenderer.setAlpha(bufImg, alpha); } @Override -- 2.39.5