1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.image;
14 import java.awt.GraphicsConfiguration;
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;
28 * @See {@link ProviderUtils}
30 * @author Toni Kalajainen
32 public class ImageUtils {
35 * Adapt AWT {@link java.awt.image.BufferedImage} to {@link Image}
39 public static Image adaptAWT(java.awt.image.BufferedImage awtImage)
41 return new AWTImage(awtImage);
45 * Creates a memory backed buffer for an Image.
46 * Use this with vector images and shadows.
49 * @return video-ram optimized version of the symbol
51 public static Image createBuffer(Image source)
53 return new MipMapBufferedImage(source);
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.
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);
68 * deliver p1 and p2 to user interface components.
70 * i stays in memory until p1 and p2 are garbage collected.
76 public static Image createProxy(Image source)
78 return new ImageProxy(source);
82 * Creates an instance of Image that reflects to the shadow of the source Image.
84 * Shadow is created every time the image is painted. Usage of memory buffer
85 * is recommended (see createBuffer() ).
91 public static Image createShadow(Image source, ShadowParameters shadow)
93 return new Shadow(source, shadow);
96 public static Image createShadow(Image source, ShadowParameters shadow, int width, int height)
98 return new Shadow(source, shadow, width, height);
102 * Raster symbol to video memory (lazy).
105 * @param gc Graphics configuration
106 * @return video-ram optimized version of the symbol
108 public static Image createVideoMemoryBuffer(Image ps, GraphicsConfiguration gc)
110 if (gc==null) throw new IllegalArgumentException("null arg");
111 if (ps instanceof VRamBufferedImage)
113 VRamBufferedImage p = (VRamBufferedImage) ps;
114 if (p.getGraphicsConfiguration()!=gc) ps = p.getSource(); else return ps;
116 if (ps instanceof MipMapVRamBufferedImage)
118 MipMapVRamBufferedImage p = (MipMapVRamBufferedImage) ps;
119 if (p.getGraphicsConfiguration()!=gc) ps = p.getSource(); else return ps;
121 if (ps.getFeatures().contains(Feature.Vector))
122 return new MipMapVRamBufferedImage(ps, gc);
124 return new VRamBufferedImage(ps, gc);