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.impl;
14 import java.awt.Color;
15 import java.awt.Graphics2D;
16 import java.awt.GraphicsConfiguration;
17 import java.awt.RenderingHints;
18 import java.awt.Shape;
19 import java.awt.Transparency;
20 import java.awt.geom.Rectangle2D;
21 import java.awt.image.VolatileImage;
23 import org.simantics.g2d.image.Image;
26 * Video-ram cache suitable for rasterized PaintableSymbols.
29 * @author Toni Kalajainen
31 public class VRamBufferedImage extends ImageProxy {
34 final GraphicsConfiguration gc;
38 public VRamBufferedImage(Image original, GraphicsConfiguration gc) {
42 Rectangle2D bounds = original.getBounds();
43 wid = (int) Math.ceil( bounds.getWidth() );
44 hei = (int) Math.ceil( bounds.getHeight() );
47 synchronized VolatileImage restore()
50 image = gc.createCompatibleVolatileImage(
52 Transparency.TRANSLUCENT);
54 int validateResult = image.validate(gc);
55 if (validateResult == VolatileImage.IMAGE_INCOMPATIBLE)
58 if (validateResult == VolatileImage.IMAGE_OK)
61 if (validateResult == VolatileImage.IMAGE_RESTORED /*raster.contentsLost()*/) {
62 Rectangle2D bounds = source.getBounds();
63 outline = source.getOutline();
64 Graphics2D target = image.createGraphics();
65 target.setBackground(new Color(255,255,255,0));
66 target.clearRect(0, 0, image.getWidth(), image.getHeight());
67 target.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
68 target.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
69 target.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
70 target.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
71 target.translate(-bounds.getMinX(), -bounds.getMinY());
72 source.init(null);//new GraphicsContextImpl(new Rectangle2D.Double(0,0, image.getWidth(), image.getHeight()), null) );
80 public int hashCode() {
81 return gc.hashCode() ^source.hashCode();
85 public boolean equals(Object obj) {
86 if (!(obj instanceof VRamBufferedImage)) return false;
87 VRamBufferedImage o = (VRamBufferedImage) obj;
88 return o.gc == gc && o.source.equals(source);
91 public Image getSource() {
95 public GraphicsConfiguration getGraphicsConfiguration() {