001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.tools;
003
004import java.awt.Dimension;
005import java.awt.Graphics;
006import java.awt.Image;
007import java.awt.image.BufferedImage;
008
009import javax.swing.ImageIcon;
010
011/** class to describe how image overlay
012 * @since 8095
013 */
014public class ImageOverlay {
015    /** the image ressource to use as overlay */
016    public ImageProvider image;
017    /** offset of the image from left border, values between 0 and 1 */
018    private double offsetLeft;
019    /** offset of the image from top border, values between 0 and 1 */
020    private double offsetRight;
021    /** offset of the image from right border, values between 0 and 1*/
022    private double offsetTop;
023    /** offset of the image from bottom border, values between 0 and 1 */
024    private double offsetBottom;
025    
026    /**
027     * Create an overlay info. All values are relative sizes between 0 and 1. Size of the image
028     * is the result of the difference between left/right and top/bottom.
029     *
030     * @param image imager provider for the overlay icon
031     * @param offsetLeft offset of the image from left border, values between 0 and 1, -1 for auto-calculation
032     * @param offsetTop offset of the image from top border, values between 0 and 1, -1 for auto-calculation
033     * @param offsetRight offset of the image from right border, values between 0 and 1, -1 for auto-calculation
034     * @param offsetBottom offset of the image from bottom border, values between 0 and 1, -1 for auto-calculation
035     * @since 8095
036     */
037    public ImageOverlay(ImageProvider image, double offsetLeft, double offsetTop, double offsetRight, double offsetBottom) {
038        this.image = image;
039        this.offsetLeft = offsetLeft;
040        this.offsetTop = offsetTop;
041        this.offsetRight = offsetRight;
042        this.offsetBottom = offsetBottom;
043    }
044    
045    /**
046     * Create an overlay in southeast corner. All values are relative sizes between 0 and 1.
047     * Size of the image is the result of the difference between left/right and top/bottom.
048     * Right and bottom values are set to 1.
049     *
050     * @param image imager provider for the overlay icon
051     * @see #ImageOverlay(ImageProvider, double, double, double, double)
052     * @since 8095
053     */
054    public ImageOverlay(ImageProvider image) {
055        this.image = image;
056        this.offsetLeft = -1.0;
057        this.offsetTop = -1.0;
058        this.offsetRight = 1.0;
059        this.offsetBottom = 1.0;
060    }
061
062    /**
063     * Handle overlay. The image passed as argument is modified!
064     *
065     * @param ground the base image for the overlay (gets modified!)
066     * @return the modified image (same as argument)
067     * @since 8095
068     */
069    public BufferedImage apply(BufferedImage ground) {
070        /* get base dimensions for calculation */
071        int w = ground.getWidth();
072        int h = ground.getHeight();
073        int width = -1;
074        int height = -1;
075        if (offsetRight > 0 && offsetLeft > 0) {
076            width = new Double(w*(offsetRight-offsetLeft)).intValue();
077        }
078        if (offsetTop > 0 && offsetBottom > 0) {
079            width = new Double(h*(offsetBottom-offsetTop)).intValue();
080        }
081        ImageIcon overlay;
082        if(width != -1 || height != -1) {
083            image = new ImageProvider(image).resetMaxSize(new Dimension(width, height));
084        }
085        overlay = image.get();
086        int x, y;
087        if (width == -1 && offsetLeft < 0) {
088            x = new Double(w*offsetRight).intValue() - overlay.getIconWidth();
089        } else {
090            x = new Double(w*offsetLeft).intValue();
091        }
092        if (height == -1 && offsetTop < 0) {
093            y = new Double(h*offsetBottom).intValue() - overlay.getIconHeight();
094        } else {
095            y = new Double(h*offsetTop).intValue();
096        }
097        overlay.paintIcon(null, ground.getGraphics(), x, y);
098        return ground;
099    }
100}