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.awt.image.ConvolveOp;
|
---|
6 | import java.awt.image.Kernel;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.gui.layer.ImageProcessor;
|
---|
12 | import org.openstreetmap.josm.io.session.SessionAwareReadApply;
|
---|
13 | import org.openstreetmap.josm.tools.Utils;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Sharpens or blurs the image, depending on the sharpen value.
|
---|
17 | * <p>
|
---|
18 | * A positive sharpen level means that we sharpen the image.
|
---|
19 | * <p>
|
---|
20 | * A negative sharpen level let's us blur the image. -1 is the most useful value there.
|
---|
21 | *
|
---|
22 | * @author Michael Zangl
|
---|
23 | * @since 10547
|
---|
24 | */
|
---|
25 | public class SharpenImageProcessor implements ImageProcessor, SessionAwareReadApply {
|
---|
26 | private float sharpenLevel = 1.0f;
|
---|
27 | private ConvolveOp op;
|
---|
28 |
|
---|
29 | private static final float[] KERNEL_IDENTITY = new float[] {
|
---|
30 | 0, 0, 0,
|
---|
31 | 0, 1, 0,
|
---|
32 | 0, 0, 0
|
---|
33 | };
|
---|
34 |
|
---|
35 | private static final float[] KERNEL_BLUR = new float[] {
|
---|
36 | 1f / 16, 2f / 16, 1f / 16,
|
---|
37 | 2f / 16, 4f / 16, 2f / 16,
|
---|
38 | 1f / 16, 2f / 16, 1f / 16
|
---|
39 | };
|
---|
40 |
|
---|
41 | private static final float[] KERNEL_SHARPEN = new float[] {
|
---|
42 | -.5f, -1f, -.5f,
|
---|
43 | -1f, 7, -1f,
|
---|
44 | -.5f, -1f, -.5f
|
---|
45 | };
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Gets the current sharpen level.
|
---|
49 | * @return The level.
|
---|
50 | */
|
---|
51 | public float getSharpenLevel() {
|
---|
52 | return sharpenLevel;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Sets the sharpening level.
|
---|
57 | * @param sharpenLevel The level. Clamped to be positive or 0.
|
---|
58 | */
|
---|
59 | public void setSharpenLevel(float sharpenLevel) {
|
---|
60 | if (sharpenLevel < 0) {
|
---|
61 | this.sharpenLevel = 0;
|
---|
62 | } else {
|
---|
63 | this.sharpenLevel = sharpenLevel;
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (this.sharpenLevel < 0.95) {
|
---|
67 | op = generateMixed(this.sharpenLevel, KERNEL_IDENTITY, KERNEL_BLUR);
|
---|
68 | } else if (this.sharpenLevel > 1.05) {
|
---|
69 | op = generateMixed(this.sharpenLevel - 1, KERNEL_SHARPEN, KERNEL_IDENTITY);
|
---|
70 | } else {
|
---|
71 | op = null;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private static ConvolveOp generateMixed(float aFactor, float[] a, float[] b) {
|
---|
76 | if (a.length != 9 || b.length != 9) {
|
---|
77 | throw new IllegalArgumentException("Illegal kernel array length.");
|
---|
78 | }
|
---|
79 | float[] values = new float[9];
|
---|
80 | for (int i = 0; i < values.length; i++) {
|
---|
81 | values[i] = aFactor * a[i] + (1 - aFactor) * b[i];
|
---|
82 | }
|
---|
83 | return new ConvolveOp(new Kernel(3, 3, values), ConvolveOp.EDGE_NO_OP, null);
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override
|
---|
87 | public BufferedImage process(BufferedImage image) {
|
---|
88 | if (op != null) {
|
---|
89 | return op.filter(image, null);
|
---|
90 | } else {
|
---|
91 | return image;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public void applyFromPropertiesMap(Map<String, String> properties) {
|
---|
97 | String vStr = properties.get("sharpenlevel");
|
---|
98 | if (vStr != null) {
|
---|
99 | try {
|
---|
100 | setSharpenLevel(Float.parseFloat(vStr));
|
---|
101 | } catch (NumberFormatException e) {
|
---|
102 | if (Main.isTraceEnabled()) {
|
---|
103 | Main.trace(e);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public Map<String, String> toPropertiesMap() {
|
---|
111 | if (Utils.equalsEpsilon(sharpenLevel, 1.0))
|
---|
112 | return Collections.emptyMap();
|
---|
113 | else
|
---|
114 | return Collections.singletonMap("sharpenlevel", Float.toString(sharpenLevel));
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public String toString() {
|
---|
119 | return "SharpenImageProcessor [sharpenLevel=" + sharpenLevel + ']';
|
---|
120 | }
|
---|
121 | }
|
---|