source: josm/trunk/src/org/openstreetmap/josm/data/imagery/GeorefImage.java@ 3720

Last change on this file since 3720 was 3720, checked in by bastiK, 13 years ago

added missing svn:eol-style

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Color;
7import java.awt.Font;
8import java.awt.Graphics;
9import java.awt.Image;
10import java.awt.Transparency;
11import java.awt.image.BufferedImage;
12import java.io.IOException;
13import java.io.ObjectInputStream;
14import java.io.ObjectOutputStream;
15import java.io.Serializable;
16import java.lang.ref.SoftReference;
17
18import javax.imageio.ImageIO;
19
20import org.openstreetmap.josm.data.coor.EastNorth;
21import org.openstreetmap.josm.gui.NavigatableComponent;
22import org.openstreetmap.josm.gui.layer.ImageryLayer;
23import org.openstreetmap.josm.gui.layer.WMSLayer;
24
25public class GeorefImage implements Serializable {
26 private static final long serialVersionUID = 1L;
27
28 public enum State { IMAGE, NOT_IN_CACHE, FAILED};
29
30 private WMSLayer layer;
31 private State state;
32
33 private BufferedImage image;
34 private SoftReference<BufferedImage> reImg;
35 private int xIndex;
36 private int yIndex;
37
38 private static final Color transparentColor = new Color(0,0,0,0);
39 private Color fadeColor = transparentColor;
40
41 public EastNorth getMin() {
42 return layer.getEastNorth(xIndex, yIndex);
43 }
44
45 public EastNorth getMax() {
46 return layer.getEastNorth(xIndex+1, yIndex+1);
47 }
48
49
50 public GeorefImage(WMSLayer layer) {
51 this.layer = layer;
52 }
53
54 public void changePosition(int xIndex, int yIndex) {
55 if (!equalPosition(xIndex, yIndex)) {
56 this.xIndex = xIndex;
57 this.yIndex = yIndex;
58 this.image = null;
59 flushedResizedCachedInstance();
60 }
61 }
62
63 public boolean equalPosition(int xIndex, int yIndex) {
64 return this.xIndex == xIndex && this.yIndex == yIndex;
65 }
66
67 public void changeImage(State state, BufferedImage image) {
68 flushedResizedCachedInstance();
69 this.image = image;
70 this.state = state;
71
72 switch (state) {
73 case FAILED:
74 {
75 BufferedImage img = createImage();
76 layer.drawErrorTile(img);
77 this.image = img;
78 break;
79 }
80 case NOT_IN_CACHE:
81 {
82 BufferedImage img = createImage();
83 Graphics g = img.getGraphics();
84 g.setColor(Color.GRAY);
85 g.fillRect(0, 0, img.getWidth(), img.getHeight());
86 Font font = g.getFont();
87 Font tempFont = font.deriveFont(Font.PLAIN).deriveFont(36.0f);
88 g.setFont(tempFont);
89 g.setColor(Color.BLACK);
90 g.drawString(tr("Not in cache"), 10, img.getHeight()/2);
91 g.setFont(font);
92 this.image = img;
93 break;
94 }
95 default:
96 this.image = layer.sharpenImage(this.image);
97 break;
98 }
99 }
100
101 private BufferedImage createImage() {
102 return new BufferedImage(layer.getBaseImageWidth(), layer.getBaseImageHeight(), BufferedImage.TYPE_INT_RGB);
103 }
104
105 public boolean paint(Graphics g, NavigatableComponent nc, int xIndex, int yIndex, int leftEdge, int bottomEdge) {
106 if (image == null)
107 return false;
108
109 if(!(this.xIndex == xIndex && this.yIndex == yIndex))
110 return false;
111
112 int left = layer.getImageX(xIndex);
113 int bottom = layer.getImageY(yIndex);
114 int width = layer.getImageWidth(xIndex);
115 int height = layer.getImageHeight(yIndex);
116
117 int x = left - leftEdge;
118 int y = nc.getHeight() - (bottom - bottomEdge) - height;
119
120 // This happens if you zoom outside the world
121 if(width == 0 || height == 0)
122 return false;
123
124 // TODO: implement per-layer fade color
125 Color newFadeColor;
126 if (ImageryLayer.PROP_FADE_AMOUNT.get() == 0) {
127 newFadeColor = transparentColor;
128 } else {
129 newFadeColor = ImageryLayer.getFadeColorWithAlpha();
130 }
131
132 BufferedImage img = reImg == null?null:reImg.get();
133 if(img != null && img.getWidth() == width && img.getHeight() == height && fadeColor.equals(newFadeColor)) {
134 g.drawImage(img, x, y, null);
135 return true;
136 }
137
138 fadeColor = newFadeColor;
139
140 boolean alphaChannel = WMSLayer.PROP_ALPHA_CHANNEL.get() && getImage().getTransparency() != Transparency.OPAQUE;
141
142 try {
143 if(img != null) {
144 img.flush();
145 }
146 long freeMem = Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory();
147 //System.out.println("Free Memory: "+ (freeMem/1024/1024) +" MB");
148 // Notice that this value can get negative due to integer overflows
149 //System.out.println("Img Size: "+ (width*height*3/1024/1024) +" MB");
150
151 int multipl = alphaChannel ? 4 : 3;
152 // This happens when requesting images while zoomed out and then zooming in
153 // Storing images this large in memory will certainly hang up JOSM. Luckily
154 // traditional rendering is as fast at these zoom levels, so it's no loss.
155 // Also prevent caching if we're out of memory soon
156 if(width > 2000 || height > 2000 || width*height*multipl > freeMem) {
157 fallbackDraw(g, getImage(), x, y, width, height, alphaChannel);
158 } else {
159 // We haven't got a saved resized copy, so resize and cache it
160 img = new BufferedImage(width, height, alphaChannel?BufferedImage.TYPE_INT_ARGB:BufferedImage.TYPE_3BYTE_BGR);
161 img.getGraphics().drawImage(getImage(),
162 0, 0, width, height, // dest
163 0, 0, getImage().getWidth(null), getImage().getHeight(null), // src
164 null);
165 if (!alphaChannel) {
166 drawFadeRect(img.getGraphics(), 0, 0, width, height);
167 }
168 img.getGraphics().dispose();
169 g.drawImage(img, x, y, null);
170 reImg = new SoftReference<BufferedImage>(img);
171 }
172 } catch(Exception e) {
173 fallbackDraw(g, getImage(), x, y, width, height, alphaChannel);
174 }
175 return true;
176 }
177
178 private void fallbackDraw(Graphics g, Image img, int x, int y, int width, int height, boolean alphaChannel) {
179 flushedResizedCachedInstance();
180 g.drawImage(
181 img, x, y, x + width, y + height,
182 0, 0, img.getWidth(null), img.getHeight(null),
183 null);
184 if (!alphaChannel) { //FIXME: fading for layers with alpha channel currently is not supported
185 drawFadeRect(g, x, y, width, height);
186 }
187 }
188
189 private void drawFadeRect(Graphics g, int x, int y, int width, int height) {
190 if (fadeColor != transparentColor) {
191 g.setColor(fadeColor);
192 g.fillRect(x, y, width, height);
193 }
194 }
195
196 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
197 state = (State) in.readObject();
198 boolean hasImage = in.readBoolean();
199 if (hasImage) {
200 image = (ImageIO.read(ImageIO.createImageInputStream(in)));
201 } else {
202 in.readObject(); // read null from input stream
203 image = null;
204 }
205 }
206
207 private void writeObject(ObjectOutputStream out) throws IOException {
208 out.writeObject(state);
209 if(getImage() == null) {
210 out.writeBoolean(false);
211 out.writeObject(null);
212 } else {
213 out.writeBoolean(true);
214 ImageIO.write(getImage(), "png", ImageIO.createImageOutputStream(out));
215 }
216 }
217
218 public void flushedResizedCachedInstance() {
219 if (reImg != null) {
220 BufferedImage img = reImg.get();
221 if (img != null) {
222 img.flush();
223 }
224 }
225 reImg = null;
226 }
227
228
229 public BufferedImage getImage() {
230 return image;
231 }
232
233 public State getState() {
234 return state;
235 }
236
237 public int getXIndex() {
238 return xIndex;
239 }
240
241 public int getYIndex() {
242 return yIndex;
243 }
244
245 public void setLayer(WMSLayer layer) {
246 this.layer = layer;
247 }
248}
Note: See TracBrowser for help on using the repository browser.