]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/VRamBufferedImage.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / impl / VRamBufferedImage.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.impl;
13
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;
22
23 import org.simantics.g2d.image.Image;
24
25 /**
26  * Video-ram cache suitable for rasterized PaintableSymbols.
27  * 
28  * @see VRamImage
29  * @author Toni Kalajainen
30  */
31 public class VRamBufferedImage extends ImageProxy {
32
33     Shape outline;
34     final GraphicsConfiguration gc;
35     VolatileImage image;
36     int wid, hei;
37
38     public VRamBufferedImage(Image original, GraphicsConfiguration gc) {
39         super(original);
40         assert(gc!=null);
41         this.gc = gc;
42         Rectangle2D bounds = original.getBounds();
43         wid = (int) Math.ceil( bounds.getWidth() );
44         hei = (int) Math.ceil( bounds.getHeight() );
45     }
46
47     synchronized VolatileImage restore()
48     {
49         if (image==null) {
50             image = gc.createCompatibleVolatileImage(
51                     wid, hei,
52                     Transparency.TRANSLUCENT);
53         }
54         int validateResult = image.validate(gc);
55         if (validateResult == VolatileImage.IMAGE_INCOMPATIBLE)
56             return null;
57
58         if (validateResult == VolatileImage.IMAGE_OK)
59             return image;
60
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) );
73             target.dispose();
74             return image;
75         }
76         return null;
77     }
78
79     @Override
80     public int hashCode() {
81         return gc.hashCode() ^source.hashCode();
82     }
83
84     @Override
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);
89     }
90
91     public Image getSource() {
92         return source;
93     }
94
95     public GraphicsConfiguration getGraphicsConfiguration() {
96         return gc;
97     }
98
99 }