]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/MipMapVRamBufferedImage.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / impl / MipMapVRamBufferedImage.java
index e5e4ebe0d24dc293a407f66ed4acf589e013dc0b..b75ee90d918a205b561615cc7a7428a5d77c1128 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.g2d.image.impl;\r
-\r
-import java.awt.Color;\r
-import java.awt.Graphics2D;\r
-import java.awt.GraphicsConfiguration;\r
-import java.awt.Shape;\r
-import java.awt.Transparency;\r
-import java.awt.geom.AffineTransform;\r
-import java.awt.geom.Rectangle2D;\r
-import java.awt.image.VolatileImage;\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.EnumSet;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import org.simantics.g2d.image.Image;\r
-import org.simantics.scenegraph.Node;\r
-import org.simantics.scenegraph.g2d.G2DParentNode;\r
-import org.simantics.scenegraph.utils.QualityHints;\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 MipMapVRamBufferedImage extends ImageProxy implements Image {\r
-\r
-    /** Extra margin to the bounds reported by batik*/\r
-    public static final double MARGIN_PERCENT = 3;\r
-\r
-    public static final double MAX_DIMENSION = 800;\r
-    public static final double MIN_DIMENSION = 4;\r
-\r
-    final GraphicsConfiguration gc;\r
-    Shape outline;\r
-\r
-    double [] resolutions;\r
-    Map<Double, Raster> rasters = new HashMap<Double, Raster>();\r
-    double minResolution, maxResolution;\r
-    EnumSet<Feature> caps;\r
-\r
-    public MipMapVRamBufferedImage(Image original, GraphicsConfiguration gc)\r
-    {\r
-        super(original);\r
-        if(gc==null)\r
-            throw new IllegalArgumentException("Argument is null.");\r
-        this.source = original;\r
-        this.gc = gc;\r
-\r
-        // Init rasters - they are built on-demand\r
-        double maxResolution = maxResolution();\r
-        double minResolution = minResolution();\r
-        double resolution = maxResolution;\r
-        List<Double> resolutions = new ArrayList<Double>();\r
-        while (resolution>minResolution)\r
-        {\r
-            Raster r = new Raster(resolution);\r
-            rasters.put(resolution, r);\r
-            resolutions.add(resolution);\r
-            resolution /= 2;\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
-\r
-        if (original.getFeatures().contains(Feature.Volatile))\r
-            caps = EnumSet.noneOf(Feature.class);\r
-        else\r
-            caps = EnumSet.of(Feature.Vector);\r
-    }\r
-\r
-    private double maxResolution()\r
-    {\r
-        Rectangle2D bounds = source.getBounds();\r
-        double wid = bounds.getWidth();\r
-        double hei = bounds.getHeight();\r
-        return MAX_DIMENSION/Math.sqrt(wid*hei);\r
-    }\r
-\r
-    private double minResolution()\r
-    {\r
-        Rectangle2D bounds = source.getBounds();\r
-        double wid = bounds.getWidth();\r
-        double hei = bounds.getHeight();\r
-        return MIN_DIMENSION/Math.sqrt(wid*hei);\r
-    }\r
-\r
-    private 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.sqrt(sx*sx+sy*sy);\r
-    }\r
-\r
-    private 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 Node init(G2DParentNode parent) {\r
-        return null;\r
-//             Graphics2D g = gc.getGraphics2D();\r
-//             // Quality rendering requested, do not render from cache\r
-//             if (g.getRenderingHint(RenderingHints.KEY_RENDERING) == RenderingHints.VALUE_RENDER_QUALITY)\r
-//             {\r
-//                     source.paint(gc);\r
-//                     return;\r
-//             }\r
-//\r
-//             double requiredResolution = requiredResolution(g.getTransform());\r
-//             if (requiredResolution > maxResolution)\r
-//             {\r
-//                     g = gc.createClone();\r
-//                     g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);\r
-//                     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);\r
-//                     g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);\r
-//                     g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);\r
-//                     g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);\r
-//                     g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);\r
-//                     gc = new GraphicsContextImpl(g, gc.getBounds(), gc.getNode());\r
-//                     source.paint(gc);\r
-//                     return;\r
-//             }\r
-//\r
-//             double closestResolution = findClosestResolution(requiredResolution);\r
-//\r
-//             Raster raster = rasters.get(closestResolution);\r
-//\r
-//             Object origInterpolationHint = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION);\r
-//             if (origInterpolationHint==null)\r
-//                     origInterpolationHint = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;\r
-//             g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);\r
-//             try {\r
-//                     raster.paint( gc );\r
-//             } finally {\r
-//                     g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, origInterpolationHint);\r
-//             }\r
-    }\r
-\r
-    public Image getOriginalPaintableSymbol()\r
-    {\r
-        return source;\r
-    }\r
-\r
-    synchronized void releaseRaster() {\r
-        for (Raster r : rasters.values())\r
-            r.image = null;\r
-\r
-    }\r
-\r
-    @Override\r
-    protected void notifyChanged() {\r
-        releaseRaster();\r
-        super.notifyChanged();\r
-    }\r
-\r
-    class Raster implements Comparable<Raster> {\r
-        VolatileImage image;\r
-        double resolution;\r
-        int widMargin, heiMargin;\r
-        int wid, hei;\r
-\r
-        Raster(double resolution) {\r
-            Rectangle2D bounds = source.getBounds();\r
-            this.resolution = resolution;\r
-            double wid = bounds.getWidth();\r
-            double hei = bounds.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 VolatileImage restore()\r
-        {\r
-            Rectangle2D bounds = source.getBounds();\r
-            if (image==null) {\r
-                image = gc.createCompatibleVolatileImage(\r
-                        wid+widMargin*2,\r
-                        hei+heiMargin*2,\r
-                        Transparency.TRANSLUCENT);\r
-            }\r
-            int validateResult = image.validate(gc);\r
-            if (validateResult == VolatileImage.IMAGE_INCOMPATIBLE)\r
-                return null;\r
-\r
-            if (validateResult == VolatileImage.IMAGE_OK)\r
-                return image;\r
-\r
-            if (validateResult == VolatileImage.IMAGE_RESTORED /*raster.contentsLost()*/) {\r
-                Graphics2D target = image.createGraphics();\r
-                target.setBackground(new Color(255,255,255,0));\r
-                target.clearRect(0, 0, image.getWidth(), image.getHeight());\r
-                QualityHints.HIGH_QUALITY_HINTS.setQuality(target);\r
-                target.translate(widMargin, heiMargin);\r
-                target.scale(resolution, resolution);\r
-                target.translate(-bounds.getMinX(), -bounds.getMinY());\r
-                source.init(null);\r
-//                                             new GraphicsContextImpl(new Rectangle2D.Double(0,0, image.getWidth(), image.getHeight()), null)\r
-//                                             );\r
-                target.dispose();\r
-                return image;\r
-            }\r
-            return null;\r
-        }\r
-\r
-//        public void paint(GraphicsContext gc) {\r
-//                     Rectangle2D bounds = source.getBounds();\r
-//                     VolatileImage image = restore();\r
-//                     if (image==null)\r
-//                     {\r
-//                             QualityHints.LOW_QUALITY_HINTS.setQuality(gc.getGraphics2D());\r
-//                             source.paint(gc);\r
-//                             return;\r
-//                     }\r
-//                     Graphics2D g = gc.getGraphics2D();\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(bounds.getMinX(), bounds.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
-        @Override\r
-        public int compareTo(Raster other) {\r
-            if (other.resolution<resolution)\r
-                return -1;\r
-            if (other.resolution>resolution)\r
-                return 1;\r
-            return 0;\r
-        }\r
-    }\r
-    /*\r
-       @Override\r
-       public int hashCode() {\r
-               return gc.hashCode() ^original.hashCode();\r
-       }\r
-\r
-       @Override\r
-       public boolean equals(Object obj) {\r
-               if (!(obj instanceof VRamBufferedImage)) return false;\r
-               VRamBufferedImage o = (VRamBufferedImage) obj;\r
-               return o.gc == gc && o.original.equals(original);\r
-       }\r
-     */\r
-    @Override\r
-    public EnumSet<Feature> getFeatures() {\r
-        return caps;\r
-    }\r
-\r
-    public Image getSource() {\r
-        return source;\r
-    }\r
-\r
-    public GraphicsConfiguration getGraphicsConfiguration() {\r
-        return gc;\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.g2d.image.impl;
+
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.GraphicsConfiguration;
+import java.awt.Shape;
+import java.awt.Transparency;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.VolatileImage;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.simantics.g2d.image.Image;
+import org.simantics.scenegraph.Node;
+import org.simantics.scenegraph.g2d.G2DParentNode;
+import org.simantics.scenegraph.utils.QualityHints;
+
+/**
+ * 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 MipMapVRamBufferedImage extends ImageProxy implements Image {
+
+    /** Extra margin to the bounds reported by batik*/
+    public static final double MARGIN_PERCENT = 3;
+
+    public static final double MAX_DIMENSION = 800;
+    public static final double MIN_DIMENSION = 4;
+
+    final GraphicsConfiguration gc;
+    Shape outline;
+
+    double [] resolutions;
+    Map<Double, Raster> rasters = new HashMap<Double, Raster>();
+    double minResolution, maxResolution;
+    EnumSet<Feature> caps;
+
+    public MipMapVRamBufferedImage(Image original, GraphicsConfiguration gc)
+    {
+        super(original);
+        if(gc==null)
+            throw new IllegalArgumentException("Argument is null.");
+        this.source = original;
+        this.gc = gc;
+
+        // Init rasters - they are built on-demand
+        double maxResolution = maxResolution();
+        double minResolution = minResolution();
+        double resolution = maxResolution;
+        List<Double> resolutions = new ArrayList<Double>();
+        while (resolution>minResolution)
+        {
+            Raster r = new Raster(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];
+
+        if (original.getFeatures().contains(Feature.Volatile))
+            caps = EnumSet.noneOf(Feature.class);
+        else
+            caps = EnumSet.of(Feature.Vector);
+    }
+
+    private double maxResolution()
+    {
+        Rectangle2D bounds = source.getBounds();
+        double wid = bounds.getWidth();
+        double hei = bounds.getHeight();
+        return MAX_DIMENSION/Math.sqrt(wid*hei);
+    }
+
+    private double minResolution()
+    {
+        Rectangle2D bounds = source.getBounds();
+        double wid = bounds.getWidth();
+        double hei = bounds.getHeight();
+        return MIN_DIMENSION/Math.sqrt(wid*hei);
+    }
+
+    private 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.sqrt(sx*sx+sy*sy);
+    }
+
+    private 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 Node init(G2DParentNode parent) {
+        return null;
+//             Graphics2D g = gc.getGraphics2D();
+//             // Quality rendering requested, do not render from cache
+//             if (g.getRenderingHint(RenderingHints.KEY_RENDERING) == RenderingHints.VALUE_RENDER_QUALITY)
+//             {
+//                     source.paint(gc);
+//                     return;
+//             }
+//
+//             double requiredResolution = requiredResolution(g.getTransform());
+//             if (requiredResolution > maxResolution)
+//             {
+//                     g = gc.createClone();
+//                     g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
+//                     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
+//                     g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+//                     g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
+//                     g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
+//                     g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
+//                     gc = new GraphicsContextImpl(g, gc.getBounds(), gc.getNode());
+//                     source.paint(gc);
+//                     return;
+//             }
+//
+//             double closestResolution = findClosestResolution(requiredResolution);
+//
+//             Raster raster = rasters.get(closestResolution);
+//
+//             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);
+//             try {
+//                     raster.paint( gc );
+//             } finally {
+//                     g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, origInterpolationHint);
+//             }
+    }
+
+    public Image getOriginalPaintableSymbol()
+    {
+        return source;
+    }
+
+    synchronized void releaseRaster() {
+        for (Raster r : rasters.values())
+            r.image = null;
+
+    }
+
+    @Override
+    protected void notifyChanged() {
+        releaseRaster();
+        super.notifyChanged();
+    }
+
+    class Raster implements Comparable<Raster> {
+        VolatileImage image;
+        double resolution;
+        int widMargin, heiMargin;
+        int wid, hei;
+
+        Raster(double resolution) {
+            Rectangle2D bounds = source.getBounds();
+            this.resolution = resolution;
+            double wid = bounds.getWidth();
+            double hei = bounds.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 VolatileImage restore()
+        {
+            Rectangle2D bounds = source.getBounds();
+            if (image==null) {
+                image = gc.createCompatibleVolatileImage(
+                        wid+widMargin*2,
+                        hei+heiMargin*2,
+                        Transparency.TRANSLUCENT);
+            }
+            int validateResult = image.validate(gc);
+            if (validateResult == VolatileImage.IMAGE_INCOMPATIBLE)
+                return null;
+
+            if (validateResult == VolatileImage.IMAGE_OK)
+                return image;
+
+            if (validateResult == VolatileImage.IMAGE_RESTORED /*raster.contentsLost()*/) {
+                Graphics2D target = image.createGraphics();
+                target.setBackground(new Color(255,255,255,0));
+                target.clearRect(0, 0, image.getWidth(), image.getHeight());
+                QualityHints.HIGH_QUALITY_HINTS.setQuality(target);
+                target.translate(widMargin, heiMargin);
+                target.scale(resolution, resolution);
+                target.translate(-bounds.getMinX(), -bounds.getMinY());
+                source.init(null);
+//                                             new GraphicsContextImpl(new Rectangle2D.Double(0,0, image.getWidth(), image.getHeight()), null)
+//                                             );
+                target.dispose();
+                return image;
+            }
+            return null;
+        }
+
+//        public void paint(GraphicsContext gc) {
+//                     Rectangle2D bounds = source.getBounds();
+//                     VolatileImage image = restore();
+//                     if (image==null)
+//                     {
+//                             QualityHints.LOW_QUALITY_HINTS.setQuality(gc.getGraphics2D());
+//                             source.paint(gc);
+//                             return;
+//                     }
+//                     Graphics2D g = gc.getGraphics2D();
+//                     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(bounds.getMinX(), bounds.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);
+//                     }
+//        }
+
+        @Override
+        public int compareTo(Raster other) {
+            if (other.resolution<resolution)
+                return -1;
+            if (other.resolution>resolution)
+                return 1;
+            return 0;
+        }
+    }
+    /*
+       @Override
+       public int hashCode() {
+               return gc.hashCode() ^original.hashCode();
+       }
+
+       @Override
+       public boolean equals(Object obj) {
+               if (!(obj instanceof VRamBufferedImage)) return false;
+               VRamBufferedImage o = (VRamBufferedImage) obj;
+               return o.gc == gc && o.original.equals(original);
+       }
+     */
+    @Override
+    public EnumSet<Feature> getFeatures() {
+        return caps;
+    }
+
+    public Image getSource() {
+        return source;
+    }
+
+    public GraphicsConfiguration getGraphicsConfiguration() {
+        return gc;
+    }
+
+}
+