]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/utils/MipMapBufferedImage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / utils / MipMapBufferedImage.java
diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/utils/MipMapBufferedImage.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/utils/MipMapBufferedImage.java
new file mode 100644 (file)
index 0000000..10a8dec
--- /dev/null
@@ -0,0 +1,349 @@
+/*******************************************************************************\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