Ignore:
Timestamp:
2009-02-12T09:24:58+01:00 (16 years ago)
Author:
stoecker
Message:

close #2164 and #1954. patch by xeen

Location:
applications/editors/josm/plugins/wmsplugin/src/wmsplugin
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wmsplugin/src/wmsplugin/GeorefImage.java

    r13667 r13673  
    5858            return false;
    5959        }
    60        
     60
    6161        // Width and height flicker about 2 pixels due to rounding errors, typically only 1
    6262        int width = Math.abs(maxPt.x-minPt.x);
     
    6464        int diffx = reImgHash.width - width;
    6565        int diffy = reImgHash.height - height;
    66        
     66
    6767        // This happens if you zoom outside the world
    6868        if(width == 0 || height == 0)
    6969            return false;
    70        
     70
    7171        // We still need to re-render if the requested size is larger (otherwise we'll have black lines)
    7272        // If it's only up to two pixels smaller, just draw the old image, the errors are minimal
     
    7979
    8080        boolean alphaChannel = Main.pref.getBoolean("wmsplugin.alpha_channel");
    81         Image ppImg = image;
    82         if(!alphaChannel) {
     81        // We don't need this, as simply saving the image to RGB instead of ARGB removes alpha
     82        // But we do get a black instead of white background.
     83        //Image ppImg = image;
     84        /*if(!alphaChannel) {
    8385            // set alpha value to 255
    8486            ppImg = clearAlpha(image);
     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);
    85120        }
    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
     121        return true;
     122    }
     123
     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
    92130            null);
    93 
    94         reImgHash.setSize(width, height);       
    95         g.drawImage(reImg, minPt.x, maxPt.y, null);
    96         return true;
    97131    }
    98132
     
    124158                return out_img;
    125159        }
    126    
     160
    127161    public void flushedResizedCachedInstance() {
    128162        reImg = null;
  • applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Grabber.java

    r13667 r13673  
    5353        g.drawString(tr("Exception occurred"), 10, height()/2);
    5454        image.image = img;
     55        image.flushedResizedCachedInstance();
    5556        image.failed = true;
    5657        g.setFont(font);
  • applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSGrabber.java

    r13645 r13673  
    5656            image.max = proj.latlon2eastNorth(b.max);
    5757
    58             if(image.isVisible(mv)) //don't download, if the image isn't visible already
     58            if(image.isVisible(mv)) { //don't download, if the image isn't visible already
    5959                image.image = grab(url);
     60                image.flushedResizedCachedInstance();
     61            }
    6062            image.downloadingStarted = false;
    6163        } catch(Exception e) {
  • applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSLayer.java

    r13667 r13673  
    172172                    img.downloadingStarted = true;
    173173                    img.image = null;
     174                    img.flushedResizedCachedInstance();
    174175                    Grabber gr = WMSPlugin.getGrabber(baseURL, XYtoBounds(x,y), Main.main.proj, pixelPerDegree, img, mv, this);
    175176                    executor.submit(gr);
     
    250251                    GeorefImage img = images[modulo(x,dax)][modulo(y,day)];
    251252                    img.image = null;
     253                    img.flushedResizedCachedInstance();
    252254                    img.downloadingStarted = false;
    253255                    img.failed = false;
Note: See TracChangeset for help on using the changeset viewer.