]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/utils/MipMapBufferedImage.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / utils / MipMapBufferedImage.java
index 10a8decb39bb7b63ca86d776251ce78f7917fd17..d76b9ec05a1063c437dd1b2c26a1edd9ebb221db 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.scenegraph.utils;\r
-\r
-import java.awt.Color;\r
-import java.awt.Graphics2D;\r
-import java.awt.Point;\r
-import java.awt.RenderingHints;\r
-import java.awt.geom.AffineTransform;\r
-import java.awt.geom.Rectangle2D;\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import com.kitfox.svg.SVGDiagram;\r
-\r
-/**\r
- * Video-ram cache suitable for rasterized PaintableSymbols scalable vector graphics.\r
- * <p>\r
- * This implementation rasterizes the same symbol from different mip map levels.\r
- * \r
- * @see VRamImage\r
- * @author Toni Kalajainen\r
- */\r
-public class MipMapBufferedImage extends BufferedImage {\r
-\r
-    /** Extra margin to the bounds reported by batik */\r
-    public static final double MARGIN_PERCENT = 3;\r
-\r
-    // Was 800 in VRam.. ?\r
-    public static final double MAX_DIMENSION  = 600;\r
-    public static final double MIN_DIMENSION  = 4;\r
-\r
-    //Shape outline;\r
-\r
-    Map<Double, IRaster>       rasters        = new HashMap<Double, IRaster>();\r
-    double[]                   resolutions;\r
-    double                     minResolution, maxResolution;\r
-\r
-    /**\r
-     * @param original\r
-     * @param imageBounds\r
-     * @param referenceSize\r
-     * \r
-     * FIXME: shouldn't be SVG dependent\r
-     */\r
-    public MipMapBufferedImage(SVGDiagram original, Rectangle2D imageBounds, Point referenceSize) {\r
-        super(original, imageBounds, referenceSize);\r
-        initializeRasters();\r
-    }\r
-\r
-    private void initializeRasters() {\r
-        List<Double> resolutions = new ArrayList<Double>();\r
-\r
-        if (referenceSize != null && !imageBounds.isEmpty()) {\r
-            // Init rasters - they are built on-demand\r
-            double maxResolution = maxResolution();\r
-            double minResolution = minResolution();\r
-            double fitResolution = fitResolution(referenceSize);\r
-            double resolution = fitResolution;\r
-            while (true) {\r
-                double next = resolution * 2;\r
-                if (next > maxResolution)\r
-                    break;\r
-                resolution = next;\r
-            }\r
-            while (resolution > minResolution) {\r
-                IRaster r = createRaster(resolution);\r
-                rasters.put(resolution, r);\r
-                resolutions.add(resolution);\r
-                resolution /= 2;\r
-            }\r
-        } else {\r
-            // Init rasters - they are built on-demand\r
-            double maxResolution = maxResolution();\r
-            double minResolution = minResolution();\r
-            double resolution = maxResolution;\r
-            while (resolution > minResolution) {\r
-                IRaster r = createRaster(resolution);\r
-                rasters.put(resolution, r);\r
-                resolutions.add(resolution);\r
-                resolution /= 2;\r
-            }\r
-        }\r
-\r
-        // arraylist -> array\r
-        this.resolutions = new double[resolutions.size()];\r
-        for (int i=0; i<resolutions.size(); i++)\r
-            this.resolutions[i] = resolutions.get(resolutions.size()-1-i);\r
-        this.minResolution = this.resolutions[0];\r
-        this.maxResolution = this.resolutions[this.resolutions.length-1];\r
-        //System.out.println("RESOLUTIONS: " + Arrays.toString(this.resolutions));\r
-    }\r
-\r
-    protected IRaster createRaster(double resolution) {\r
-        return new BufferedRaster(resolution);\r
-    }\r
-\r
-    private double fitResolution(Point p)\r
-    {\r
-        double wid = imageBounds.getWidth();\r
-        double hei = imageBounds.getHeight();\r
-        double rx = p.x / wid;\r
-        double ry = p.y / hei;\r
-        return Math.min(rx, ry);\r
-    }\r
-\r
-    private double maxResolution()\r
-    {\r
-        double wid = imageBounds.getWidth();\r
-        double hei = imageBounds.getHeight();\r
-        return MAX_DIMENSION/Math.sqrt(wid*hei);\r
-    }\r
-\r
-    private double minResolution()\r
-    {\r
-        double wid = imageBounds.getWidth();\r
-        double hei = imageBounds.getHeight();\r
-        return MIN_DIMENSION/Math.sqrt(wid*hei);\r
-    }\r
-\r
-    protected double requiredResolution(AffineTransform at)\r
-    {\r
-        double m00 = at.getScaleX();\r
-        double m11 = at.getScaleY();\r
-        double m10 = at.getShearY();\r
-        double m01 = at.getShearX();\r
-        // Project unit vector to canvas\r
-        double sx = Math.sqrt( m00*m00 + m10*m10 );\r
-        double sy = Math.sqrt( m01*m01 + m11*m11 );\r
-        return Math.max(sx, sy);\r
-        //return Math.sqrt(sx*sx+sy*sy);\r
-    }\r
-\r
-    protected double findClosestResolution(double resolution)\r
-    {\r
-        int index = Arrays.binarySearch(resolutions, resolution);\r
-        if (index>=0) return resolutions[index];\r
-        index  = -(index+1);\r
-\r
-        if (index>=resolutions.length) index = resolutions.length-1;\r
-        if (index<0) index = 0;\r
-        return resolutions[index];\r
-    }\r
-\r
-    @Override\r
-    public void paint(Graphics2D g) {\r
-        // Quality rendering requested, do not render from cache\r
-        //QualityHints.HIGH_QUALITY_HINTS.setQuality(g);\r
-        if (g.getRenderingHint(RenderingHints.KEY_RENDERING) == RenderingHints.VALUE_RENDER_QUALITY)\r
-        {\r
-            try {\r
-                source.render(g);\r
-            } catch (Exception e) {\r
-                // NOTE: Catching Exception instead of SVGException due to an\r
-                // NPE when encountering invalid color SVG definitions (e.g.\r
-                // rgb(256,-1,0))\r
-                e.printStackTrace();\r
-            }\r
-            return;\r
-        }\r
-\r
-        double requiredResolution = requiredResolution(g.getTransform());\r
-        // This scale makes the mipmapped painting use a mipmap that is smaller\r
-        // than the requested image pixel size in cases where the required\r
-        // resolution only slightly exceeds the size of an available mipmap.\r
-        requiredResolution *= 0.95;\r
-        //System.out.println("required resolution: " + requiredResolution);\r
-\r
-        if (requiredResolution > getRasterRenderingThresholdResolution()) {\r
-            Graphics2D g2d = (Graphics2D) g.create();\r
-            setupSourceRender(g2d);\r
-            try {\r
-                source.render(g2d);\r
-            } catch (Exception e) {\r
-                // NOTE: Catching Exception instead of SVGException due to an\r
-                // NPE when encountering invalid color SVG definitions (e.g.\r
-                // rgb(256,-1,0))\r
-                e.printStackTrace();\r
-            } finally {\r
-                g2d.dispose();\r
-            }\r
-        } else {\r
-            Object origInterpolationHint = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION);\r
-            if (origInterpolationHint==null)\r
-                origInterpolationHint = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;\r
-\r
-            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);\r
-\r
-            double closestResolution = findClosestResolution(requiredResolution);\r
-            //System.out.println("  resolutions: " + Arrays.toString(resolutions));\r
-            //System.out.println("  closest resolution: " + closestResolution);\r
-            IRaster raster = rasters.get(closestResolution);\r
-            try {\r
-                raster.paint( g );\r
-            } finally {\r
-                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, origInterpolationHint);\r
-            }\r
-        }\r
-    }\r
-\r
-    protected void setupSourceRender(Graphics2D g2d) {\r
-        g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);\r
-        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);\r
-        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);\r
-        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);\r
-        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);\r
-        g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);\r
-    }\r
-\r
-    protected double getRasterRenderingThresholdResolution() {\r
-        return maxResolution;\r
-    }\r
-\r
-    @Override\r
-    public synchronized void releaseRaster() {\r
-        for (IRaster r : rasters.values())\r
-            r.release();\r
-    }\r
-\r
-    static interface IRaster extends Comparable<IRaster> {\r
-        double getResolution();\r
-        void paint(Graphics2D g);\r
-        void release();\r
-    }\r
-\r
-    static abstract class Raster implements IRaster {\r
-        protected final double resolution;\r
-\r
-        public Raster(double resolution) {\r
-            this.resolution = resolution;\r
-        }\r
-\r
-        public double getResolution() {\r
-            return resolution;\r
-        }\r
-\r
-        public int compareTo(IRaster o) {\r
-            double r = getResolution();\r
-            double or = o.getResolution();\r
-            if (or < r)\r
-                return -1;\r
-            if (or > r)\r
-                return 1;\r
-            return 0;\r
-        }\r
-    }\r
-\r
-    class BufferedRaster extends Raster {\r
-        java.awt.image.BufferedImage image;\r
-        //int widMargin, heiMargin;\r
-        int wid, hei;\r
-\r
-        BufferedRaster(double resolution) {\r
-            super(resolution);\r
-            double wid = imageBounds.getWidth();\r
-            double hei = imageBounds.getHeight();\r
-            this.wid = (int) (wid * resolution);\r
-            this.hei = (int) (hei * resolution);\r
-//            widMargin = (int) (wid * resolution * (MARGIN_PERCENT/100)) +1;\r
-//            heiMargin = (int) (hei * resolution * (MARGIN_PERCENT/100)) +1;\r
-        }\r
-\r
-        synchronized java.awt.image.BufferedImage getOrCreate()\r
-        {\r
-            if (image!=null) return image;\r
-            image = new java.awt.image.BufferedImage(\r
-                    (wid+0*2+1),\r
-                    (hei+0*2+1),\r
-                    java.awt.image.BufferedImage.TYPE_INT_ARGB);\r
-\r
-            Graphics2D target = image.createGraphics();\r
-            target.setBackground(new Color(255,255,255,0));\r
-            target.clearRect(0, 0, image.getWidth(), image.getHeight());\r
-\r
-            target.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);\r
-            target.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);\r
-            target.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);\r
-            target.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);\r
-\r
-//            target.translate(widMargin, heiMargin);\r
-            target.scale(resolution, resolution);\r
-            target.translate(-imageBounds.getMinX(), -imageBounds.getMinY());\r
-            try {\r
-                source.render(target);\r
-            } catch (Exception e) {\r
-                // TODO Auto-generated catch block\r
-                // NOTE: Catching Exception instead of SVGException due to an\r
-                // NPE when encountering invalid color SVG definitions (e.g.\r
-                // rgb(256,-1,0))\r
-                e.printStackTrace();\r
-            }\r
-//            source.paint(\r
-//                    new GraphicsContextImpl(target, new Rectangle2D.Double(0,0, image.getWidth(), image.getHeight()), null)\r
-//            );\r
-            target.dispose();\r
-\r
-            return image;\r
-        }\r
-\r
-        public void paint(Graphics2D g) {\r
-            java.awt.image.BufferedImage image = getOrCreate();\r
-            if (image==null)\r
-            {\r
-                try {\r
-                    source.render(g);\r
-                } catch (Exception e) {\r
-                    // TODO Auto-generated catch block\r
-                    // NOTE: Catching Exception instead of SVGException due to an\r
-                    // NPE when encountering invalid color SVG definitions (e.g.\r
-                    // rgb(256,-1,0))\r
-                    e.printStackTrace();\r
-                }\r
-                return;\r
-            }\r
-            AffineTransform af = g.getTransform();\r
-            Object rh = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION);\r
-            try {\r
-                /// Bicubic interpolation is very slow with opengl pipeline\r
-                if (rh == RenderingHints.VALUE_INTERPOLATION_BICUBIC)\r
-                    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,\r
-                            RenderingHints.VALUE_INTERPOLATION_BILINEAR);\r
-                g.translate(imageBounds.getMinX(), imageBounds.getMinY());\r
-                g.scale(1/resolution, 1/resolution);\r
-//                g.translate(-widMargin, -heiMargin);\r
-                g.drawImage(image, 0, 0, null);\r
-            } finally {\r
-                g.setTransform(af);\r
-                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, rh);\r
-            }\r
-        }\r
-\r
-        public void release() {\r
-            image = null;\r
-        }\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.scenegraph.utils;
+
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.Point;
+import java.awt.RenderingHints;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.kitfox.svg.SVGDiagram;
+
+/**
+ * Video-ram cache suitable for rasterized PaintableSymbols scalable vector graphics.
+ * <p>
+ * This implementation rasterizes the same symbol from different mip map levels.
+ * 
+ * @see VRamImage
+ * @author Toni Kalajainen
+ */
+public class MipMapBufferedImage extends BufferedImage {
+
+    /** Extra margin to the bounds reported by batik */
+    public static final double MARGIN_PERCENT = 3;
+
+    // Was 800 in VRam.. ?
+    public static final double MAX_DIMENSION  = 600;
+    public static final double MIN_DIMENSION  = 4;
+
+    //Shape outline;
+
+    Map<Double, IRaster>       rasters        = new HashMap<Double, IRaster>();
+    double[]                   resolutions;
+    double                     minResolution, maxResolution;
+
+    /**
+     * @param original
+     * @param imageBounds
+     * @param referenceSize
+     * 
+     * FIXME: shouldn't be SVG dependent
+     */
+    public MipMapBufferedImage(SVGDiagram original, Rectangle2D imageBounds, Point referenceSize) {
+        super(original, imageBounds, referenceSize);
+        initializeRasters();
+    }
+
+    private void initializeRasters() {
+        List<Double> resolutions = new ArrayList<Double>();
+
+        if (referenceSize != null && !imageBounds.isEmpty()) {
+            // Init rasters - they are built on-demand
+            double maxResolution = maxResolution();
+            double minResolution = minResolution();
+            double fitResolution = fitResolution(referenceSize);
+            double resolution = fitResolution;
+            while (true) {
+                double next = resolution * 2;
+                if (next > maxResolution)
+                    break;
+                resolution = next;
+            }
+            while (resolution > minResolution) {
+                IRaster r = createRaster(resolution);
+                rasters.put(resolution, r);
+                resolutions.add(resolution);
+                resolution /= 2;
+            }
+        } else {
+            // Init rasters - they are built on-demand
+            double maxResolution = maxResolution();
+            double minResolution = minResolution();
+            double resolution = maxResolution;
+            while (resolution > minResolution) {
+                IRaster r = createRaster(resolution);
+                rasters.put(resolution, r);
+                resolutions.add(resolution);
+                resolution /= 2;
+            }
+        }
+
+        // arraylist -> array
+        this.resolutions = new double[resolutions.size()];
+        for (int i=0; i<resolutions.size(); i++)
+            this.resolutions[i] = resolutions.get(resolutions.size()-1-i);
+        this.minResolution = this.resolutions[0];
+        this.maxResolution = this.resolutions[this.resolutions.length-1];
+        //System.out.println("RESOLUTIONS: " + Arrays.toString(this.resolutions));
+    }
+
+    protected IRaster createRaster(double resolution) {
+        return new BufferedRaster(resolution);
+    }
+
+    private double fitResolution(Point p)
+    {
+        double wid = imageBounds.getWidth();
+        double hei = imageBounds.getHeight();
+        double rx = p.x / wid;
+        double ry = p.y / hei;
+        return Math.min(rx, ry);
+    }
+
+    private double maxResolution()
+    {
+        double wid = imageBounds.getWidth();
+        double hei = imageBounds.getHeight();
+        return MAX_DIMENSION/Math.sqrt(wid*hei);
+    }
+
+    private double minResolution()
+    {
+        double wid = imageBounds.getWidth();
+        double hei = imageBounds.getHeight();
+        return MIN_DIMENSION/Math.sqrt(wid*hei);
+    }
+
+    protected double requiredResolution(AffineTransform at)
+    {
+        double m00 = at.getScaleX();
+        double m11 = at.getScaleY();
+        double m10 = at.getShearY();
+        double m01 = at.getShearX();
+        // Project unit vector to canvas
+        double sx = Math.sqrt( m00*m00 + m10*m10 );
+        double sy = Math.sqrt( m01*m01 + m11*m11 );
+        return Math.max(sx, sy);
+        //return Math.sqrt(sx*sx+sy*sy);
+    }
+
+    protected double findClosestResolution(double resolution)
+    {
+        int index = Arrays.binarySearch(resolutions, resolution);
+        if (index>=0) return resolutions[index];
+        index  = -(index+1);
+
+        if (index>=resolutions.length) index = resolutions.length-1;
+        if (index<0) index = 0;
+        return resolutions[index];
+    }
+
+    @Override
+    public void paint(Graphics2D g) {
+        // Quality rendering requested, do not render from cache
+        //QualityHints.HIGH_QUALITY_HINTS.setQuality(g);
+        if (g.getRenderingHint(RenderingHints.KEY_RENDERING) == RenderingHints.VALUE_RENDER_QUALITY)
+        {
+            try {
+                source.render(g);
+            } catch (Exception e) {
+                // NOTE: Catching Exception instead of SVGException due to an
+                // NPE when encountering invalid color SVG definitions (e.g.
+                // rgb(256,-1,0))
+                e.printStackTrace();
+            }
+            return;
+        }
+
+        double requiredResolution = requiredResolution(g.getTransform());
+        // This scale makes the mipmapped painting use a mipmap that is smaller
+        // than the requested image pixel size in cases where the required
+        // resolution only slightly exceeds the size of an available mipmap.
+        requiredResolution *= 0.95;
+        //System.out.println("required resolution: " + requiredResolution);
+
+        if (requiredResolution > getRasterRenderingThresholdResolution()) {
+            Graphics2D g2d = (Graphics2D) g.create();
+            setupSourceRender(g2d);
+            try {
+                source.render(g2d);
+            } catch (Exception e) {
+                // NOTE: Catching Exception instead of SVGException due to an
+                // NPE when encountering invalid color SVG definitions (e.g.
+                // rgb(256,-1,0))
+                e.printStackTrace();
+            } finally {
+                g2d.dispose();
+            }
+        } else {
+            Object origInterpolationHint = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION);
+            if (origInterpolationHint==null)
+                origInterpolationHint = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
+
+            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
+
+            double closestResolution = findClosestResolution(requiredResolution);
+            //System.out.println("  resolutions: " + Arrays.toString(resolutions));
+            //System.out.println("  closest resolution: " + closestResolution);
+            IRaster raster = rasters.get(closestResolution);
+            try {
+                raster.paint( g );
+            } finally {
+                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, origInterpolationHint);
+            }
+        }
+    }
+
+    protected void setupSourceRender(Graphics2D g2d) {
+        g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
+        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
+        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
+        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
+        g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
+    }
+
+    protected double getRasterRenderingThresholdResolution() {
+        return maxResolution;
+    }
+
+    @Override
+    public synchronized void releaseRaster() {
+        for (IRaster r : rasters.values())
+            r.release();
+    }
+
+    static interface IRaster extends Comparable<IRaster> {
+        double getResolution();
+        void paint(Graphics2D g);
+        void release();
+    }
+
+    static abstract class Raster implements IRaster {
+        protected final double resolution;
+
+        public Raster(double resolution) {
+            this.resolution = resolution;
+        }
+
+        public double getResolution() {
+            return resolution;
+        }
+
+        public int compareTo(IRaster o) {
+            double r = getResolution();
+            double or = o.getResolution();
+            if (or < r)
+                return -1;
+            if (or > r)
+                return 1;
+            return 0;
+        }
+    }
+
+    class BufferedRaster extends Raster {
+        java.awt.image.BufferedImage image;
+        //int widMargin, heiMargin;
+        int wid, hei;
+
+        BufferedRaster(double resolution) {
+            super(resolution);
+            double wid = imageBounds.getWidth();
+            double hei = imageBounds.getHeight();
+            this.wid = (int) (wid * resolution);
+            this.hei = (int) (hei * resolution);
+//            widMargin = (int) (wid * resolution * (MARGIN_PERCENT/100)) +1;
+//            heiMargin = (int) (hei * resolution * (MARGIN_PERCENT/100)) +1;
+        }
+
+        synchronized java.awt.image.BufferedImage getOrCreate()
+        {
+            if (image!=null) return image;
+            image = new java.awt.image.BufferedImage(
+                    (wid+0*2+1),
+                    (hei+0*2+1),
+                    java.awt.image.BufferedImage.TYPE_INT_ARGB);
+
+            Graphics2D target = image.createGraphics();
+            target.setBackground(new Color(255,255,255,0));
+            target.clearRect(0, 0, image.getWidth(), image.getHeight());
+
+            target.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
+            target.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+            target.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+            target.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
+
+//            target.translate(widMargin, heiMargin);
+            target.scale(resolution, resolution);
+            target.translate(-imageBounds.getMinX(), -imageBounds.getMinY());
+            try {
+                source.render(target);
+            } catch (Exception e) {
+                // TODO Auto-generated catch block
+                // NOTE: Catching Exception instead of SVGException due to an
+                // NPE when encountering invalid color SVG definitions (e.g.
+                // rgb(256,-1,0))
+                e.printStackTrace();
+            }
+//            source.paint(
+//                    new GraphicsContextImpl(target, new Rectangle2D.Double(0,0, image.getWidth(), image.getHeight()), null)
+//            );
+            target.dispose();
+
+            return image;
+        }
+
+        public void paint(Graphics2D g) {
+            java.awt.image.BufferedImage image = getOrCreate();
+            if (image==null)
+            {
+                try {
+                    source.render(g);
+                } catch (Exception e) {
+                    // TODO Auto-generated catch block
+                    // NOTE: Catching Exception instead of SVGException due to an
+                    // NPE when encountering invalid color SVG definitions (e.g.
+                    // rgb(256,-1,0))
+                    e.printStackTrace();
+                }
+                return;
+            }
+            AffineTransform af = g.getTransform();
+            Object rh = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION);
+            try {
+                /// Bicubic interpolation is very slow with opengl pipeline
+                if (rh == RenderingHints.VALUE_INTERPOLATION_BICUBIC)
+                    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+                            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+                g.translate(imageBounds.getMinX(), imageBounds.getMinY());
+                g.scale(1/resolution, 1/resolution);
+//                g.translate(-widMargin, -heiMargin);
+                g.drawImage(image, 0, 0, null);
+            } finally {
+                g.setTransform(af);
+                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, rh);
+            }
+        }
+
+        public void release() {
+            image = null;
+        }
+    }
+
+}