]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/image/ImageUtils.java
G2DParentNode handles "undefined" child bounds separately
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / ImageUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.image;
13
14 import java.awt.GraphicsConfiguration;
15
16 import org.simantics.g2d.image.Image.Feature;
17 import org.simantics.g2d.image.impl.AWTImage;
18 import org.simantics.g2d.image.impl.ImageProxy;
19 import org.simantics.g2d.image.impl.MipMapBufferedImage;
20 import org.simantics.g2d.image.impl.MipMapVRamBufferedImage;
21 import org.simantics.g2d.image.impl.Shadow;
22 import org.simantics.g2d.image.impl.VRamBufferedImage;
23 import org.simantics.g2d.image.impl.Shadow.ShadowParameters;
24
25 /**
26  * Image utilities.
27  * 
28  * @See {@link ProviderUtils}
29  * @See {@link Image}
30  * @author Toni Kalajainen
31  */
32 public class ImageUtils {
33
34     /**
35      * Adapt AWT {@link java.awt.image.BufferedImage} to {@link Image}
36      * @param awtImage
37      * @return
38      */
39     public static Image adaptAWT(java.awt.image.BufferedImage awtImage)
40     {
41         return new AWTImage(awtImage);
42     }
43
44     /**
45      * Creates a memory backed buffer for an Image.
46      * Use this with vector images and shadows.
47      * 
48      * @param source
49      * @return video-ram optimized version of the symbol
50      */
51     public static Image createBuffer(Image source)
52     {
53         return new MipMapBufferedImage(source);
54     }
55
56     /**
57      * Creates a proxy of the source image. Proxy has a strong reference to the
58      * source but weak from source to proxy and thus a proxy prevents garbage
59      * collection of the source image.
60      * 
61      * Example:
62      *  <pre>
63      *   Image i = ProviderUtils.at("https://www.simantics.org/simantics/simantics/logo.jpg");
64      *   Image p1 = ImageUtils.proxy(i);
65      *   Image p2 = ImageUtils.proxy(i);
66      *   i = null;
67      * 
68      *   deliver p1 and p2 to user interface components.
69      * 
70      *   i stays in memory until p1 and p2 are garbage collected.
71      *  </pre>
72      * 
73      * @param source
74      * @return
75      */
76     public static Image createProxy(Image source)
77     {
78         return new ImageProxy(source);
79     }
80
81     /**
82      * Creates an instance of Image that reflects to the shadow of the source Image.
83      * <p>
84      * Shadow is created every time the image is painted. Usage of memory buffer
85      * is recommended (see createBuffer() ).
86      * 
87      * @param source
88      * @param shadow
89      * @return
90      */
91     public static Image createShadow(Image source, ShadowParameters shadow)
92     {
93         return new Shadow(source, shadow);
94     }
95
96     public static Image createShadow(Image source, ShadowParameters shadow, int width, int height)
97     {
98         return new Shadow(source, shadow, width, height);
99     }
100
101     /**
102      * Raster symbol to video memory (lazy).
103      * 
104      * @param ps
105      * @param gc Graphics configuration
106      * @return video-ram optimized version of the symbol
107      */
108     public static Image createVideoMemoryBuffer(Image ps, GraphicsConfiguration gc)
109     {
110         if (gc==null) throw new IllegalArgumentException("null arg");
111         if (ps instanceof VRamBufferedImage)
112         {
113             VRamBufferedImage p = (VRamBufferedImage) ps;
114             if (p.getGraphicsConfiguration()!=gc) ps = p.getSource(); else return ps;
115         }
116         if (ps instanceof MipMapVRamBufferedImage)
117         {
118             MipMapVRamBufferedImage p = (MipMapVRamBufferedImage) ps;
119             if (p.getGraphicsConfiguration()!=gc) ps = p.getSource(); else return ps;
120         }
121         if (ps.getFeatures().contains(Feature.Vector))
122             return new MipMapVRamBufferedImage(ps, gc);
123         else
124             return new VRamBufferedImage(ps, gc);
125     }
126
127
128
129
130 }
131