| | 87 | }*/ |
| | 88 | |
| | 89 | try { |
| | 90 | if(reImg != null) reImg.flush(); |
| | 91 | long freeMem = Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory(); |
| | 92 | //System.out.println("Free Memory: "+ (freeMem/1024/1024) +" MB"); |
| | 93 | // Notice that this value can get negative due to integer overflows |
| | 94 | //System.out.println("Img Size: "+ (width*height*3/1024/1024) +" MB"); |
| | 95 | |
| | 96 | // This happens when requesting images while zoomed out and then zooming in |
| | 97 | // Storing images this large in memory will certainly hang up JOSM. Luckily |
| | 98 | // traditional rendering is as fast at these zoom levels, so it's no loss. |
| | 99 | // Also prevent caching if we're out of memory soon |
| | 100 | if(width > 10000 || height > 10000 || width*height*3 > freeMem) { |
| | 101 | fallbackDraw(g, image, minPt, maxPt); |
| | 102 | } else { |
| | 103 | // We haven't got a saved resized copy, so resize and cache it |
| | 104 | reImg = new BufferedImage(width, height, |
| | 105 | alphaChannel |
| | 106 | ? BufferedImage.TYPE_INT_ARGB |
| | 107 | : BufferedImage.TYPE_3BYTE_BGR // This removes alpha, too |
| | 108 | ); |
| | 109 | reImg.getGraphics().drawImage(image, |
| | 110 | 0, 0, width, height, // dest |
| | 111 | 0, 0, image.getWidth(null), image.getHeight(null), // src |
| | 112 | null); |
| | 113 | reImg.getGraphics().dispose(); |
| | 114 | |
| | 115 | reImgHash.setSize(width, height); |
| | 116 | g.drawImage(reImg, minPt.x, maxPt.y, null); |
| | 117 | } |
| | 118 | } catch(Exception e) { |
| | 119 | fallbackDraw(g, image, minPt, maxPt); |
| 86 | | |
| 87 | | // We haven't got a saved resized copy, so resize and cache it |
| 88 | | reImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
| 89 | | reImg.getGraphics().drawImage(ppImg, |
| 90 | | 0, 0, width, height, // dest |
| 91 | | 0, 0, ppImg.getWidth(null), ppImg.getHeight(null), // src |
| 92 | | null); |
| 93 | | |
| 94 | | reImgHash.setSize(width, height); |
| 95 | | g.drawImage(reImg, minPt.x, maxPt.y, null); |
| | 124 | private void fallbackDraw(Graphics g, Image img, Point min, Point max) { |
| | 125 | if(reImg != null) reImg.flush(); |
| | 126 | reImg = null; |
| | 127 | g.drawImage(img, |
| | 128 | min.x, max.y, max.x, min.y, // dest |
| | 129 | 0, 0, img.getWidth(null), img.getHeight(null), // src |
| | 130 | null); |
| | 131 | } |
| | 132 | |