001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer.imagery;
003
004import java.awt.image.BufferedImage;
005import java.util.Collections;
006import java.util.Map;
007
008import org.openstreetmap.josm.io.session.SessionAwareReadApply;
009import org.openstreetmap.josm.tools.ImageProcessor;
010import org.openstreetmap.josm.tools.Logging;
011import org.openstreetmap.josm.tools.Utils;
012
013/**
014 * Adds or removes the colorfulness of the image.
015 *
016 * @author Michael Zangl
017 * @since 10547
018 */
019public class ColorfulImageProcessor implements ImageProcessor, SessionAwareReadApply {
020    private ColorfulFilter op;
021    private double colorfulness = 1.0;
022
023    /**
024     * Gets the colorfulness value.
025     * @return The value
026     */
027    public double getColorfulness() {
028        return colorfulness;
029    }
030
031    /**
032     * Sets the colorfulness value. Clamps it to 0+
033     * @param colorfulness The value
034     */
035    public void setColorfulness(double colorfulness) {
036        if (colorfulness < 0) {
037            this.colorfulness = 0;
038        } else {
039            this.colorfulness = colorfulness;
040        }
041
042        if (this.colorfulness < .95 || this.colorfulness > 1.05) {
043            op = new ColorfulFilter(this.colorfulness);
044        } else {
045            op = null;
046        }
047    }
048
049    @Override
050    public BufferedImage process(BufferedImage image) {
051        if (op != null) {
052            return op.filter(image, null);
053        } else {
054            return image;
055        }
056    }
057
058    @Override
059    public void applyFromPropertiesMap(Map<String, String> properties) {
060        String cStr = properties.get("colorfulness");
061        if (cStr != null) {
062            try {
063                setColorfulness(Double.parseDouble(cStr));
064            } catch (NumberFormatException e) {
065                Logging.trace(e);
066            }
067        }
068    }
069
070    @Override
071    public Map<String, String> toPropertiesMap() {
072        if (Utils.equalsEpsilon(colorfulness, 1.0))
073            return Collections.emptyMap();
074        else
075            return Collections.singletonMap("colorfulness", Double.toString(colorfulness));
076    }
077
078    @Override
079    public String toString() {
080        return "ColorfulImageProcessor [colorfulness=" + colorfulness + ']';
081    }
082}