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

Last change on this file since 7025 was 7025, checked in by Don-vip, 12 years ago

Sonar - fix various issues

  • Property svn:eol-style set to native
File size: 8.0 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, PARTLY_IN_CACHE}
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 BufferedImage imgFailed = createImage();
75 layer.drawErrorTile(imgFailed);
76 this.image = imgFailed;
77 break;
78 case NOT_IN_CACHE:
79 BufferedImage img = createImage();
80 Graphics g = img.getGraphics();
81 g.setColor(Color.GRAY);
82 g.fillRect(0, 0, img.getWidth(), img.getHeight());
83 Font font = g.getFont();
84 Font tempFont = font.deriveFont(Font.PLAIN).deriveFont(36.0f);
85 g.setFont(tempFont);
86 g.setColor(Color.BLACK);
87 String text = tr("Not in cache");
88 g.drawString(text, (img.getWidth() - g.getFontMetrics().stringWidth(text)) / 2, img.getHeight()/2);
89 g.setFont(font);
90 this.image = img;
91 break;
92 default:
93 if (this.image != null) {
94 this.image = layer.sharpenImage(this.image);
95 }
96 break;
97 }
98 }
99
100 private BufferedImage createImage() {
101 return new BufferedImage(layer.getImageSize(), layer.getImageSize(), BufferedImage.TYPE_INT_RGB);
102 }
103
104 public boolean paint(Graphics g, NavigatableComponent nc, int xIndex, int yIndex, int leftEdge, int bottomEdge) {
105 if (image == null)
106 return false;
107
108 if(!(this.xIndex == xIndex && this.yIndex == yIndex))
109 return false;
110
111 int left = layer.getImageX(xIndex);
112 int bottom = layer.getImageY(yIndex);
113 int width = layer.getImageWidth(xIndex);
114 int height = layer.getImageHeight(yIndex);
115
116 int x = left - leftEdge;
117 int y = nc.getHeight() - (bottom - bottomEdge) - height;
118
119 // This happens if you zoom outside the world
120 if(width == 0 || height == 0)
121 return false;
122
123 // TODO: implement per-layer fade color
124 Color newFadeColor;
125 if (ImageryLayer.PROP_FADE_AMOUNT.get() == 0) {
126 newFadeColor = transparentColor;
127 } else {
128 newFadeColor = ImageryLayer.getFadeColorWithAlpha();
129 }
130
131 BufferedImage img = reImg == null?null:reImg.get();
132 if(img != null && img.getWidth() == width && img.getHeight() == height && fadeColor.equals(newFadeColor)) {
133 g.drawImage(img, x, y, null);
134 return true;
135 }
136
137 fadeColor = newFadeColor;
138
139 boolean alphaChannel = WMSLayer.PROP_ALPHA_CHANNEL.get() && getImage().getTransparency() != Transparency.OPAQUE;
140
141 try {
142 if(img != null) {
143 img.flush();
144 }
145 long freeMem = Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory();
146 // Notice that this value can get negative due to integer overflows
147
148 int multipl = alphaChannel ? 4 : 3;
149 // This happens when requesting images while zoomed out and then zooming in
150 // Storing images this large in memory will certainly hang up JOSM. Luckily
151 // traditional rendering is as fast at these zoom levels, so it's no loss.
152 // Also prevent caching if we're out of memory soon
153 if(width > 2000 || height > 2000 || width*height*multipl > freeMem) {
154 fallbackDraw(g, getImage(), x, y, width, height, alphaChannel);
155 } else {
156 // We haven't got a saved resized copy, so resize and cache it
157 img = new BufferedImage(width, height, alphaChannel?BufferedImage.TYPE_INT_ARGB:BufferedImage.TYPE_3BYTE_BGR);
158 img.getGraphics().drawImage(getImage(),
159 0, 0, width, height, // dest
160 0, 0, getImage().getWidth(null), getImage().getHeight(null), // src
161 null);
162 if (!alphaChannel) {
163 drawFadeRect(img.getGraphics(), 0, 0, width, height);
164 }
165 img.getGraphics().dispose();
166 g.drawImage(img, x, y, null);
167 reImg = new SoftReference<>(img);
168 }
169 } catch(Exception e) {
170 fallbackDraw(g, getImage(), x, y, width, height, alphaChannel);
171 }
172 return true;
173 }
174
175 private void fallbackDraw(Graphics g, Image img, int x, int y, int width, int height, boolean alphaChannel) {
176 flushedResizedCachedInstance();
177 g.drawImage(
178 img, x, y, x + width, y + height,
179 0, 0, img.getWidth(null), img.getHeight(null),
180 null);
181 if (!alphaChannel) { //FIXME: fading for layers with alpha channel currently is not supported
182 drawFadeRect(g, x, y, width, height);
183 }
184 }
185
186 private void drawFadeRect(Graphics g, int x, int y, int width, int height) {
187 if (fadeColor != transparentColor) {
188 g.setColor(fadeColor);
189 g.fillRect(x, y, width, height);
190 }
191 }
192
193 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
194 state = (State) in.readObject();
195 boolean hasImage = in.readBoolean();
196 if (hasImage) {
197 image = (ImageIO.read(ImageIO.createImageInputStream(in)));
198 } else {
199 in.readObject(); // read null from input stream
200 image = null;
201 }
202 }
203
204 private void writeObject(ObjectOutputStream out) throws IOException {
205 out.writeObject(state);
206 if(getImage() == null) {
207 out.writeBoolean(false);
208 out.writeObject(null);
209 } else {
210 out.writeBoolean(true);
211 ImageIO.write(getImage(), "png", ImageIO.createImageOutputStream(out));
212 }
213 }
214
215 public void flushedResizedCachedInstance() {
216 if (reImg != null) {
217 BufferedImage img = reImg.get();
218 if (img != null) {
219 img.flush();
220 }
221 }
222 reImg = null;
223 }
224
225
226 public BufferedImage getImage() {
227 return image;
228 }
229
230 public State getState() {
231 return state;
232 }
233
234 public int getXIndex() {
235 return xIndex;
236 }
237
238 public int getYIndex() {
239 return yIndex;
240 }
241
242 public void setLayer(WMSLayer layer) {
243 this.layer = layer;
244 }
245}
Note: See TracBrowser for help on using the repository browser.