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.scenegraph.utils;
14 import java.awt.Color;
15 import java.awt.Graphics2D;
16 import java.awt.Point;
17 import java.awt.RenderingHints;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.Rectangle2D;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.List;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.kitfox.svg.SVGDiagram;
32 * Video-ram cache suitable for rasterized PaintableSymbols scalable vector graphics.
34 * This implementation rasterizes the same symbol from different mip map levels.
37 * @author Toni Kalajainen
39 public class MipMapBufferedImage extends BufferedImage {
41 private static final Logger LOGGER = LoggerFactory.getLogger(MipMapBufferedImage.class);
43 /** Extra margin to the bounds reported by batik */
44 public static final double MARGIN_PERCENT = 3;
46 // Was 800 in VRam.. ?
47 public static final double MAX_DIMENSION = 600;
48 public static final double MIN_DIMENSION = 4;
52 Map<Double, IRaster> rasters = new HashMap<Double, IRaster>();
54 double minResolution, maxResolution;
59 * @param referenceSize
61 * FIXME: shouldn't be SVG dependent
63 public MipMapBufferedImage(SVGDiagram original, Rectangle2D imageBounds, Point referenceSize) {
64 super(original, imageBounds, referenceSize);
68 private void initializeRasters() {
69 if (LOGGER.isDebugEnabled())
70 LOGGER.debug("initializeRasters({}): diagram={}, referenceSize={}, imageBounds={}, maxres={}, minres={}", this.toString(), source, referenceSize, imageBounds, maxResolution(), minResolution());
71 List<Double> resolutions = new ArrayList<Double>();
73 if (referenceSize != null && !imageBounds.isEmpty()) {
74 // Init rasters - they are built on-demand
75 double maxResolution = maxResolution();
76 double minResolution = minResolution();
77 double fitResolution = fitResolution(referenceSize);
78 double resolution = fitResolution;
80 double next = resolution * 2;
81 if (next > maxResolution)
85 while (resolution > minResolution) {
86 IRaster r = createRaster(resolution);
87 rasters.put(resolution, r);
88 resolutions.add(resolution);
92 // Init rasters - they are built on-demand
93 double maxResolution = maxResolution();
94 double minResolution = minResolution();
95 double resolution = maxResolution;
96 while (resolution > minResolution) {
97 IRaster r = createRaster(resolution);
98 rasters.put(resolution, r);
99 resolutions.add(resolution);
104 if (LOGGER.isDebugEnabled())
105 LOGGER.debug("initializeRasters({}): resolutions={}", this.toString(), resolutions);
107 // arraylist -> array
108 this.resolutions = new double[resolutions.size()];
109 for (int i=0; i<resolutions.size(); i++)
110 this.resolutions[i] = resolutions.get(resolutions.size()-1-i);
111 this.minResolution = this.resolutions[0];
112 this.maxResolution = this.resolutions[this.resolutions.length-1];
113 //System.out.println("RESOLUTIONS: " + Arrays.toString(this.resolutions));
116 protected IRaster createRaster(double resolution) {
117 return new BufferedRaster(resolution);
120 private double fitResolution(Point p)
122 double wid = imageBounds.getWidth();
123 double hei = imageBounds.getHeight();
124 double rx = p.x / wid;
125 double ry = p.y / hei;
126 return Math.min(rx, ry);
129 private double maxResolution()
131 double wid = imageBounds.getWidth();
132 double hei = imageBounds.getHeight();
133 return MAX_DIMENSION/Math.sqrt(wid*hei);
136 private double minResolution()
138 double wid = imageBounds.getWidth();
139 double hei = imageBounds.getHeight();
140 return MIN_DIMENSION/Math.sqrt(wid*hei);
143 protected double requiredResolution(AffineTransform at)
145 double m00 = at.getScaleX();
146 double m11 = at.getScaleY();
147 double m10 = at.getShearY();
148 double m01 = at.getShearX();
149 // Project unit vector to canvas
150 double sx = Math.sqrt( m00*m00 + m10*m10 );
151 double sy = Math.sqrt( m01*m01 + m11*m11 );
152 return Math.max(sx, sy);
153 //return Math.sqrt(sx*sx+sy*sy);
156 protected double findClosestResolution(double resolution)
158 int index = Arrays.binarySearch(resolutions, resolution);
159 if (index>=0) return resolutions[index];
162 if (index>=resolutions.length) index = resolutions.length-1;
163 if (index<0) index = 0;
164 return resolutions[index];
168 public void paint(Graphics2D g) {
169 // Quality rendering requested, do not render from cache
170 //QualityHints.HIGH_QUALITY_HINTS.setQuality(g);
171 if (g.getRenderingHint(RenderingHints.KEY_RENDERING) == RenderingHints.VALUE_RENDER_QUALITY)
175 } catch (Exception e) {
176 // NOTE: Catching Exception instead of SVGException due to an
177 // NPE when encountering invalid color SVG definitions (e.g.
184 double requiredResolution = requiredResolution(g.getTransform());
185 // This scale makes the mipmapped painting use a mipmap that is smaller
186 // than the requested image pixel size in cases where the required
187 // resolution only slightly exceeds the size of an available mipmap.
188 requiredResolution *= 0.95;
189 //System.out.println("required resolution: " + requiredResolution);
191 if (requiredResolution > getRasterRenderingThresholdResolution()) {
192 Graphics2D g2d = (Graphics2D) g.create();
193 setupSourceRender(g2d);
196 } catch (Exception e) {
197 // NOTE: Catching Exception instead of SVGException due to an
198 // NPE when encountering invalid color SVG definitions (e.g.
205 Object origInterpolationHint = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION);
206 if (origInterpolationHint==null)
207 origInterpolationHint = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
209 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
211 double closestResolution = findClosestResolution(requiredResolution);
212 //System.out.println(" resolutions: " + Arrays.toString(resolutions));
213 //System.out.println(" closest resolution: " + closestResolution);
214 IRaster raster = rasters.get(closestResolution);
218 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, origInterpolationHint);
223 protected void setupSourceRender(Graphics2D g2d) {
224 g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
225 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
226 g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
227 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
228 g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
229 g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
232 protected double getRasterRenderingThresholdResolution() {
233 return maxResolution;
237 public synchronized void releaseRaster() {
238 for (IRaster r : rasters.values())
242 static interface IRaster extends Comparable<IRaster> {
243 double getResolution();
244 void paint(Graphics2D g);
248 static abstract class Raster implements IRaster {
249 protected final double resolution;
251 public Raster(double resolution) {
252 this.resolution = resolution;
255 public double getResolution() {
259 public int compareTo(IRaster o) {
260 double r = getResolution();
261 double or = o.getResolution();
270 class BufferedRaster extends Raster {
271 java.awt.image.BufferedImage image;
272 //int widMargin, heiMargin;
275 BufferedRaster(double resolution) {
277 double wid = imageBounds.getWidth();
278 double hei = imageBounds.getHeight();
279 this.wid = (int) (wid * resolution);
280 this.hei = (int) (hei * resolution);
281 // widMargin = (int) (wid * resolution * (MARGIN_PERCENT/100)) +1;
282 // heiMargin = (int) (hei * resolution * (MARGIN_PERCENT/100)) +1;
285 synchronized java.awt.image.BufferedImage getOrCreate()
287 if (image!=null) return image;
288 image = new java.awt.image.BufferedImage(
291 java.awt.image.BufferedImage.TYPE_INT_ARGB);
293 Graphics2D target = image.createGraphics();
294 target.setBackground(new Color(255,255,255,0));
295 target.clearRect(0, 0, image.getWidth(), image.getHeight());
297 target.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
298 target.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
299 target.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
300 target.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
302 // target.translate(widMargin, heiMargin);
303 target.scale(resolution, resolution);
304 target.translate(-imageBounds.getMinX(), -imageBounds.getMinY());
306 source.render(target);
307 } catch (Exception e) {
308 // TODO Auto-generated catch block
309 // NOTE: Catching Exception instead of SVGException due to an
310 // NPE when encountering invalid color SVG definitions (e.g.
315 // new GraphicsContextImpl(target, new Rectangle2D.Double(0,0, image.getWidth(), image.getHeight()), null)
322 public void paint(Graphics2D g) {
323 java.awt.image.BufferedImage image = getOrCreate();
328 } catch (Exception e) {
329 // TODO Auto-generated catch block
330 // NOTE: Catching Exception instead of SVGException due to an
331 // NPE when encountering invalid color SVG definitions (e.g.
337 AffineTransform af = g.getTransform();
338 Object rh = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION);
340 /// Bicubic interpolation is very slow with opengl pipeline
341 if (rh == RenderingHints.VALUE_INTERPOLATION_BICUBIC)
342 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
343 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
344 g.translate(imageBounds.getMinX(), imageBounds.getMinY());
345 g.scale(1/resolution, 1/resolution);
346 // g.translate(-widMargin, -heiMargin);
347 g.drawImage(image, 0, 0, null);
350 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, rh);
354 public void release() {