]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/BufferedImage.java
Merge "ShapeNode with separate stroke and fill paints"
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / impl / BufferedImage.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.geom.AffineTransform;
15 import java.util.EnumSet;
16
17 import org.simantics.g2d.image.Image;
18 import org.simantics.g2d.image.ImageUtils;
19 import org.simantics.scenegraph.Node;
20 import org.simantics.scenegraph.g2d.G2DParentNode;
21
22 /**
23  * System-memory stored raster of an image.
24  * Suitable for vector based images.
25  * <p>
26  * This implementation determines the required raster quality from the
27  * given graphics2D context. If the buffered raster is not good enough quality,
28  * the image will be re-rendered. If the buffer to be rendered is larger than
29  * maxDimension * maxDimension, original image will be used instead.
30  * <p>
31  * Disposing buffer does not dipose the original image.
32  * 
33  * @See {@link ImageUtils}
34  * @author Toni Kalajainen
35  */
36 public class BufferedImage extends ImageProxy implements Image {
37
38     public static final double MAX_DIMENSION = 800;
39
40     java.awt.image.BufferedImage raster;
41     double rasterResolution = 1.0;
42     double maxDimension;
43     EnumSet<Feature> caps;
44
45     public BufferedImage(Image source)
46     {
47         this(source, MAX_DIMENSION);
48     }
49
50     public BufferedImage(Image source, double maxDimension)
51     {
52         super(source);
53         this.maxDimension = maxDimension;
54         boolean vector = source.getFeatures().contains(Feature.Vector);
55         boolean vola = source.getFeatures().contains(Feature.Volatile);
56         if (vola) {
57             source.addImageListener(new ImageListener() {
58                 @Override
59                 public void onContentChangedNotification(Image image) {
60                     releaseRaster();
61                 }});
62             this.caps = EnumSet.of(Feature.Volatile);
63         } else {
64             this.caps = EnumSet.noneOf(Feature.class);
65         }
66     }
67
68     private double requiredResolution(AffineTransform af)
69     {
70         double reso = Math.max(af.getScaleX(), af.getScaleY());
71         // Add some extra information for rotations & anti-aliasing
72         reso *= 1.3;
73         return reso;
74     }
75
76     private synchronized void releaseRaster()
77     {
78         raster = null;
79     }
80
81     @Override
82     public synchronized Node init(G2DParentNode parent) {
83         return null;
84 //              Graphics2D g = gc.getGraphics2D();
85 //              // Quality rendering requested, do not render from cache
86 //              if (g.getRenderingHint(RenderingHints.KEY_RENDERING) == RenderingHints.VALUE_RENDER_QUALITY)
87 //              {
88 //                      source.paint(gc);
89 //                      return;
90 //              }
91 //              Rectangle2D bounds = getBounds();
92 //
93 //              double requiredResolution = requiredResolution(g.getTransform());
94 //              double wid = bounds.getWidth();
95 //              double hei = bounds.getHeight();
96 //              wid *= requiredResolution * 1.05;
97 //              hei *= requiredResolution * 1.05;
98 //
99 //              // too detailed, render with the original image
100 //              if (wid*hei > maxDimension*maxDimension)
101 //              {
102 //                      source.paint(gc);
103 //                      return;
104 //              }
105 //
106 //              if (raster!=null && requiredResolution>rasterResolution )
107 //              {
108 //                      releaseRaster();
109 //              }
110 //              if (raster==null) {
111 //
112 //                      rasterResolution = requiredResolution;
113 //
114 //                      raster = new java.awt.image.BufferedImage(
115 //                                      (int)(wid+1),
116 //                                      (int)(hei+1),
117 //                                      java.awt.image.BufferedImage.TYPE_INT_ARGB);
118 //                      Graphics2D target = raster.createGraphics();
119 //                      target.setBackground(new Color(255,255,255,0));
120 //                      target.clearRect(0, 0, raster.getWidth(), raster.getHeight());
121 //                      target.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
122 //                      target.scale(rasterResolution, rasterResolution);
123 //                      target.translate(-bounds.getX(), -bounds.getY());
124 //
125 //                      QualityHints.HIGH_QUALITY_HINTS.setQuality(target);
126 //                      target.setRenderingHint( RenderingHintsKeyExt.KEY_BUFFERED_IMAGE, new WeakReference<java.awt.image.BufferedImage>(raster) );
127 //
128 //                      source.paint(new GraphicsContextImpl(new Rectangle2D.Double(0,0, raster.getWidth(), raster.getHeight()), null)  );
129 //                      target.dispose();
130 //              }
131 //
132 //              AffineTransform af = g.getTransform();
133 //              try {
134 //                      g.translate(bounds.getX(), bounds.getY());
135 //                      g.scale(1/rasterResolution, 1/rasterResolution);
136 //                      g.drawImage(raster, 0, 0, null);
137 //                      g.setColor(Color.BLACK);
138 //              } finally {
139 //                      g.setTransform(af);
140 //              }
141     }
142
143     @Override
144     public EnumSet<Feature> getFeatures() {
145         return caps;
146     }
147
148 }
149