1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.imagery;
|
---|
3 |
|
---|
4 | import java.awt.image.BufferedImage;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.io.session.SessionAwareReadApply;
|
---|
9 | import org.openstreetmap.josm.tools.ImageProcessor;
|
---|
10 | import org.openstreetmap.josm.tools.Logging;
|
---|
11 | import org.openstreetmap.josm.tools.Utils;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Adds or removes the colorfulness of the image.
|
---|
15 | *
|
---|
16 | * @author Michael Zangl
|
---|
17 | * @since 10547
|
---|
18 | */
|
---|
19 | public class ColorfulImageProcessor implements ImageProcessor, SessionAwareReadApply {
|
---|
20 | private ColorfulFilter op;
|
---|
21 | private double colorfulness = 1.0;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Gets the colorfulness value.
|
---|
25 | * @return The value
|
---|
26 | */
|
---|
27 | public double getColorfulness() {
|
---|
28 | return colorfulness;
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Sets the colorfulness value. Clamps it to 0+
|
---|
33 | * @param colorfulness The value
|
---|
34 | */
|
---|
35 | public void setColorfulness(double colorfulness) {
|
---|
36 | if (colorfulness < 0) {
|
---|
37 | this.colorfulness = 0;
|
---|
38 | } else {
|
---|
39 | this.colorfulness = colorfulness;
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (this.colorfulness < .95 || this.colorfulness > 1.05) {
|
---|
43 | op = new ColorfulFilter(this.colorfulness);
|
---|
44 | } else {
|
---|
45 | op = null;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public BufferedImage process(BufferedImage image) {
|
---|
51 | if (op != null) {
|
---|
52 | return op.filter(image, null);
|
---|
53 | } else {
|
---|
54 | return image;
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public void applyFromPropertiesMap(Map<String, String> properties) {
|
---|
60 | String cStr = properties.get("colorfulness");
|
---|
61 | if (cStr != null) {
|
---|
62 | try {
|
---|
63 | setColorfulness(Double.parseDouble(cStr));
|
---|
64 | } catch (NumberFormatException e) {
|
---|
65 | Logging.trace(e);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public Map<String, String> toPropertiesMap() {
|
---|
72 | if (Utils.equalsEpsilon(colorfulness, 1.0))
|
---|
73 | return Collections.emptyMap();
|
---|
74 | else
|
---|
75 | return Collections.singletonMap("colorfulness", Double.toString(colorfulness));
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public String toString() {
|
---|
80 | return "ColorfulImageProcessor [colorfulness=" + colorfulness + ']';
|
---|
81 | }
|
---|
82 | }
|
---|