]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/VRamBufferedImage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / impl / VRamBufferedImage.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/VRamBufferedImage.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/VRamBufferedImage.java
new file mode 100644 (file)
index 0000000..962e0bb
--- /dev/null
@@ -0,0 +1,99 @@
+/*******************************************************************************\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.RenderingHints;\r
+import java.awt.Shape;\r
+import java.awt.Transparency;\r
+import java.awt.geom.Rectangle2D;\r
+import java.awt.image.VolatileImage;\r
+\r
+import org.simantics.g2d.image.Image;\r
+\r
+/**\r
+ * Video-ram cache suitable for rasterized PaintableSymbols.\r
+ * \r
+ * @see VRamImage\r
+ * @author Toni Kalajainen\r
+ */\r
+public class VRamBufferedImage extends ImageProxy {\r
+\r
+    Shape outline;\r
+    final GraphicsConfiguration gc;\r
+    VolatileImage image;\r
+    int wid, hei;\r
+\r
+    public VRamBufferedImage(Image original, GraphicsConfiguration gc) {\r
+        super(original);\r
+        assert(gc!=null);\r
+        this.gc = gc;\r
+        Rectangle2D bounds = original.getBounds();\r
+        wid = (int) Math.ceil( bounds.getWidth() );\r
+        hei = (int) Math.ceil( bounds.getHeight() );\r
+    }\r
+\r
+    synchronized VolatileImage restore()\r
+    {\r
+        if (image==null) {\r
+            image = gc.createCompatibleVolatileImage(\r
+                    wid, hei,\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
+            Rectangle2D bounds = source.getBounds();\r
+            outline = source.getOutline();\r
+            Graphics2D target = image.createGraphics();\r
+            target.setBackground(new Color(255,255,255,0));\r
+            target.clearRect(0, 0, image.getWidth(), image.getHeight());\r
+            target.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);\r
+            target.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);\r
+            target.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);\r
+            target.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);\r
+            target.translate(-bounds.getMinX(), -bounds.getMinY());\r
+            source.init(null);//new GraphicsContextImpl(new Rectangle2D.Double(0,0, image.getWidth(), image.getHeight()), null) );\r
+            target.dispose();\r
+            return image;\r
+        }\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return gc.hashCode() ^source.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.source.equals(source);\r
+    }\r
+\r
+    public Image getSource() {\r
+        return source;\r
+    }\r
+\r
+    public GraphicsConfiguration getGraphicsConfiguration() {\r
+        return gc;\r
+    }\r
+\r
+}\r