diff options
| author | Pierre-Charles David | 2015-09-17 14:26:32 +0000 |
|---|---|---|
| committer | Pierre-Charles David | 2016-02-17 08:12:36 +0000 |
| commit | 84c5183ef0e3296da08828a2b1ed48db7d05ca26 (patch) | |
| tree | 6d06892e32ef22ce6e6f846962f53d09106a8032 | |
| parent | 208418a8db5681c36c52800da01163464e589409 (diff) | |
| download | org.eclipse.sirius-84c5183ef0e3296da08828a2b1ed48db7d05ca26.tar.gz org.eclipse.sirius-84c5183ef0e3296da08828a2b1ed48db7d05ca26.tar.xz org.eclipse.sirius-84c5183ef0e3296da08828a2b1ed48db7d05ca26.zip | |
[442268] Move utility method into internal helper class
This is technically an API change, as it removed a protected method from
a non-final public class, but the code changes so much more in the next
patches of this series that it is not worth documenting each and every
intermediate API change. Only the final, aggegrate changes will be
documented.
Bug: 442268
Change-Id: I2ff793edfed14d7f89d0a786ca7c1521880a1d79
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
3 files changed, 45 insertions, 41 deletions
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/AbstractCachedSVGFigure.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/AbstractCachedSVGFigure.java index 03cb3478ad..1e17e8e225 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/AbstractCachedSVGFigure.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/AbstractCachedSVGFigure.java @@ -21,6 +21,7 @@ import org.eclipse.sirius.common.tools.api.util.StringUtil; import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin; import org.eclipse.sirius.diagram.ui.tools.internal.figure.TransparentFigureGraphicsModifier; import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.ImageCache; +import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.SVGUtils; import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.SimpleImageTranscoder; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; @@ -141,7 +142,7 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style updateRenderingHints(graphics); BufferedImage awtImage = getTranscoder().getBufferedImage(); if (awtImage != null) { - result = toSWT(Display.getCurrent(), awtImage); + result = SVGUtils.toSWT(Display.getCurrent(), awtImage); AbstractCachedSVGFigure.CACHE.put(key, result); } } diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java index 756d1434b3..398b81e954 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java @@ -15,7 +15,6 @@ package org.eclipse.sirius.diagram.ui.tools.api.figure; import java.awt.RenderingHints; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; -import java.awt.image.WritableRaster; import java.io.IOException; import java.util.WeakHashMap; @@ -32,12 +31,10 @@ import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.sirius.diagram.DiagramPlugin; import org.eclipse.sirius.diagram.ui.provider.Messages; import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.InferringNamespaceContext; +import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.SVGUtils; import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.SimpleImageTranscoder; import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.Device; import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.ImageData; -import org.eclipse.swt.graphics.PaletteData; import org.eclipse.swt.widgets.Display; import org.w3c.dom.Document; import org.w3c.dom.NodeList; @@ -183,7 +180,7 @@ public class SVGFigure extends Figure { updateRenderingHints(graphics); BufferedImage awtImage = transcoder.getBufferedImage(); if (awtImage != null) { - image = toSWT(Display.getCurrent(), awtImage); + image = SVGUtils.toSWT(Display.getCurrent(), awtImage); graphics.drawImage(image, r.x, r.y); } } finally { @@ -236,41 +233,6 @@ public class SVGFigure extends Figure { } } - /** - * Converts an AWT based buffered image into an SWT <code>Image</code>. This - * will always return an <code>Image</code> that has 24 bit depth regardless - * of the type of AWT buffered image that is passed into the method. - * - * @param awtImage - * the {@link java.awt.image.BufferedImage} to be converted to an - * <code>Image</code> - * @return an <code>Image</code> that represents the same image data as the - * AWT <code>BufferedImage</code> type. - */ - protected static org.eclipse.swt.graphics.Image toSWT(Device device, BufferedImage awtImage) { - // We can force bitdepth to be 24 bit because BufferedImage getRGB - // allows us to always retrieve 24 bit data regardless of source color - // depth. - PaletteData palette = new PaletteData(0xFF0000, 0xFF00, 0xFF); - ImageData swtImageData = new ImageData(awtImage.getWidth(), awtImage.getHeight(), 24, palette); - // Ensure scansize is aligned on 32 bit. - int scansize = (((awtImage.getWidth() * 3) + 3) * 4) / 4; - WritableRaster alphaRaster = awtImage.getAlphaRaster(); - byte[] alphaBytes = new byte[awtImage.getWidth()]; - for (int y = 0; y < awtImage.getHeight(); y++) { - int[] buff = awtImage.getRGB(0, y, awtImage.getWidth(), 1, null, 0, scansize); - swtImageData.setPixels(0, y, awtImage.getWidth(), buff, 0); - if (alphaRaster != null) { - int[] alpha = alphaRaster.getPixels(0, y, awtImage.getWidth(), 1, (int[]) null); - for (int i = 0; i < awtImage.getWidth(); i++) { - alphaBytes[i] = (byte) alpha[i]; - } - swtImageData.setAlphas(0, y, awtImage.getWidth(), alphaBytes, 0); - } - } - return new org.eclipse.swt.graphics.Image(device, swtImageData); - } - public final Rectangle2D getAreaOfInterest() { getDocument(); return transcoder == null ? null : transcoder.getCanvasAreaOfInterest(); diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SVGUtils.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SVGUtils.java index dac3b0116f..9648d95839 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SVGUtils.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SVGUtils.java @@ -12,10 +12,16 @@ */ package org.eclipse.sirius.diagram.ui.tools.internal.figure.svg; +import java.awt.image.BufferedImage; +import java.awt.image.WritableRaster; + import org.apache.batik.svggen.SVGColor; import org.apache.batik.svggen.SVGGeneratorContext; import org.apache.batik.svggen.SVGPaintDescriptor; import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Device; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.PaletteData; import org.eclipse.swt.widgets.Display; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -52,5 +58,40 @@ public class SVGUtils { } return null; } + + /** + * Converts an AWT based buffered image into an SWT <code>Image</code>. This + * will always return an <code>Image</code> that has 24 bit depth regardless + * of the type of AWT buffered image that is passed into the method. + * + * @param awtImage + * the {@link java.awt.image.BufferedImage} to be converted to an + * <code>Image</code> + * @return an <code>Image</code> that represents the same image data as the + * AWT <code>BufferedImage</code> type. + */ + public static org.eclipse.swt.graphics.Image toSWT(Device device, BufferedImage awtImage) { + // We can force bitdepth to be 24 bit because BufferedImage getRGB + // allows us to always retrieve 24 bit data regardless of source color + // depth. + PaletteData palette = new PaletteData(0xFF0000, 0xFF00, 0xFF); + ImageData swtImageData = new ImageData(awtImage.getWidth(), awtImage.getHeight(), 24, palette); + // Ensure scansize is aligned on 32 bit. + int scansize = (((awtImage.getWidth() * 3) + 3) * 4) / 4; + WritableRaster alphaRaster = awtImage.getAlphaRaster(); + byte[] alphaBytes = new byte[awtImage.getWidth()]; + for (int y = 0; y < awtImage.getHeight(); y++) { + int[] buff = awtImage.getRGB(0, y, awtImage.getWidth(), 1, null, 0, scansize); + swtImageData.setPixels(0, y, awtImage.getWidth(), buff, 0); + if (alphaRaster != null) { + int[] alpha = alphaRaster.getPixels(0, y, awtImage.getWidth(), 1, (int[]) null); + for (int i = 0; i < awtImage.getWidth(); i++) { + alphaBytes[i] = (byte) alpha[i]; + } + swtImageData.setAlphas(0, y, awtImage.getWidth(), alphaBytes, 0); + } + } + return new org.eclipse.swt.graphics.Image(device, swtImageData); + } } // CHECKSTYLE:ON |
