--- /dev/null
+/*******************************************************************************\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