Ticket #1954: CacheResizedImageInMemory.patch

File CacheResizedImageInMemory.patch, 2.5 KB (added by xeen, 16 years ago)
  • src/wmsplugin/GeorefImage.java

     
    11package wmsplugin;
    22
     3import java.awt.Dimension;
    34import java.awt.Graphics;
    45import java.awt.Point;
    56import java.awt.image.BufferedImage;
     
    1516
    1617public class GeorefImage implements Serializable {
    1718    public BufferedImage image = null;
     19    private BufferedImage reImg = null;
     20    private Dimension reImgHash = new Dimension(0, 0);
    1821    public EastNorth min, max;
    1922    public boolean downloadingStarted;
    2023
     
    4346        EastNorth ma = new EastNorth(max.east()+dx, max.north()+dy);
    4447        Point minPt = nc.getPoint(mi), maxPt = nc.getPoint(ma);
    4548
     49        // downloadAndPaintVisible in WMSLayer.java requests visible images only
     50        // so this path is never hit.
    4651        /* this is isVisible() but taking dx, dy into account */
    47         if(!(g.hitClip(minPt.x, maxPt.y,
    48                 maxPt.x - minPt.x, minPt.y - maxPt.y)))
     52        /*if(!(g.hitClip(minPt.x, maxPt.y, maxPt.x - minPt.x, minPt.y - maxPt.y))) {
    4953            return false;
     54        }*/
     55       
     56        // Width and height flicker about 2 pixels due to rounding errors, typically only 1
     57        int width = Math.abs(maxPt.x-minPt.x);
     58        int height = Math.abs(minPt.y-maxPt.y);
     59        int diffx = reImgHash.width - width;
     60        int diffy = reImgHash.height - height;
     61       
     62        // We still need to re-render if the requested size is larger (otherwise we'll have black lines)
     63        // If it's only up to two pixels smaller, just draw the old image, the errors are minimal
     64        // but the performance improvements when moving are huge
     65        // Zooming is still slow because the images need to be resized
     66        if(diffx >= 0 && diffx <= 2 && diffy >= 0 && diffy <= 2 && reImg != null) {
     67            g.drawImage(reImg, minPt.x, maxPt.y, null);
     68            return true;
     69        }
    5070
    51         g.drawImage(image,
    52             minPt.x, maxPt.y, maxPt.x, minPt.y, // dest
     71        // We haven't got a saved resized copy, so resize and cache it       
     72        reImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
     73        reImg.getGraphics().drawImage(image,
     74            0, 0, width, height, // dest
    5375            0, 0, image.getWidth(), image.getHeight(), // src
    5476            null);
    5577
     78        reImgHash.setSize(width, height);       
     79        g.drawImage(reImg, minPt.x, maxPt.y, null);
    5680        return true;
    5781    }
    5882