]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/image/ImageUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / ImageUtils.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/image/ImageUtils.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/image/ImageUtils.java
new file mode 100644 (file)
index 0000000..1f1c215
--- /dev/null
@@ -0,0 +1,131 @@
+/*******************************************************************************\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;\r
+\r
+import java.awt.GraphicsConfiguration;\r
+\r
+import org.simantics.g2d.image.Image.Feature;\r
+import org.simantics.g2d.image.impl.AWTImage;\r
+import org.simantics.g2d.image.impl.ImageProxy;\r
+import org.simantics.g2d.image.impl.MipMapBufferedImage;\r
+import org.simantics.g2d.image.impl.MipMapVRamBufferedImage;\r
+import org.simantics.g2d.image.impl.Shadow;\r
+import org.simantics.g2d.image.impl.VRamBufferedImage;\r
+import org.simantics.g2d.image.impl.Shadow.ShadowParameters;\r
+\r
+/**\r
+ * Image utilities.\r
+ * \r
+ * @See {@link ProviderUtils}\r
+ * @See {@link Image}\r
+ * @author Toni Kalajainen\r
+ */\r
+public class ImageUtils {\r
+\r
+    /**\r
+     * Adapt AWT {@link java.awt.image.BufferedImage} to {@link Image}\r
+     * @param awtImage\r
+     * @return\r
+     */\r
+    public static Image adaptAWT(java.awt.image.BufferedImage awtImage)\r
+    {\r
+        return new AWTImage(awtImage);\r
+    }\r
+\r
+    /**\r
+     * Creates a memory backed buffer for an Image.\r
+     * Use this with vector images and shadows.\r
+     * \r
+     * @param source\r
+     * @return video-ram optimized version of the symbol\r
+     */\r
+    public static Image createBuffer(Image source)\r
+    {\r
+        return new MipMapBufferedImage(source);\r
+    }\r
+\r
+    /**\r
+     * Creates a proxy of the source image. Proxy has a strong reference to the\r
+     * source but weak from source to proxy and thus a proxy prevents garbage\r
+     * collection of the source image.\r
+     * \r
+     * Example:\r
+     *  <pre>\r
+     *   Image i = ProviderUtils.at("https://www.simantics.org/simantics/simantics/logo.jpg");\r
+     *   Image p1 = ImageUtils.proxy(i);\r
+     *   Image p2 = ImageUtils.proxy(i);\r
+     *   i = null;\r
+     * \r
+     *   deliver p1 and p2 to user interface components.\r
+     * \r
+     *   i stays in memory until p1 and p2 are garbage collected.\r
+     *  </pre>\r
+     * \r
+     * @param source\r
+     * @return\r
+     */\r
+    public static Image createProxy(Image source)\r
+    {\r
+        return new ImageProxy(source);\r
+    }\r
+\r
+    /**\r
+     * Creates an instance of Image that reflects to the shadow of the source Image.\r
+     * <p>\r
+     * Shadow is created every time the image is painted. Usage of memory buffer\r
+     * is recommended (see createBuffer() ).\r
+     * \r
+     * @param source\r
+     * @param shadow\r
+     * @return\r
+     */\r
+    public static Image createShadow(Image source, ShadowParameters shadow)\r
+    {\r
+        return new Shadow(source, shadow);\r
+    }\r
+\r
+    public static Image createShadow(Image source, ShadowParameters shadow, int width, int height)\r
+    {\r
+        return new Shadow(source, shadow, width, height);\r
+    }\r
+\r
+    /**\r
+     * Raster symbol to video memory (lazy).\r
+     * \r
+     * @param ps\r
+     * @param gc Graphics configuration\r
+     * @return video-ram optimized version of the symbol\r
+     */\r
+    public static Image createVideoMemoryBuffer(Image ps, GraphicsConfiguration gc)\r
+    {\r
+        if (gc==null) throw new IllegalArgumentException("null arg");\r
+        if (ps instanceof VRamBufferedImage)\r
+        {\r
+            VRamBufferedImage p = (VRamBufferedImage) ps;\r
+            if (p.getGraphicsConfiguration()!=gc) ps = p.getSource(); else return ps;\r
+        }\r
+        if (ps instanceof MipMapVRamBufferedImage)\r
+        {\r
+            MipMapVRamBufferedImage p = (MipMapVRamBufferedImage) ps;\r
+            if (p.getGraphicsConfiguration()!=gc) ps = p.getSource(); else return ps;\r
+        }\r
+        if (ps.getFeatures().contains(Feature.Vector))\r
+            return new MipMapVRamBufferedImage(ps, gc);\r
+        else\r
+            return new VRamBufferedImage(ps, gc);\r
+    }\r
+\r
+\r
+\r
+\r
+}\r
+\r