source: josm/trunk/src/org/openstreetmap/josm/gui/layer/imagery/GammaImageProcessor.java@ 12594

Last change on this file since 12594 was 12594, checked in by bastiK, 7 years ago

fixed #15139 - raster-image filter settings are Not Stored when saving session (color Saturation, Brightness, Sharpness)

  • Property svn:eol-style set to native
File size: 3.1 KB
RevLine 
[10547]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.imagery;
3
4import java.awt.Transparency;
5import java.awt.image.BufferedImage;
6import java.awt.image.LookupOp;
7import java.awt.image.ShortLookupTable;
[12594]8import java.util.Collections;
9import java.util.Map;
[10547]10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.gui.layer.ImageProcessor;
[12594]13import org.openstreetmap.josm.io.session.SessionAwareReadApply;
14import org.openstreetmap.josm.tools.Utils;
[10547]15
16/**
17 * An image processor which adjusts the gamma value of an image.
18 * @since 10547
19 */
[12594]20public class GammaImageProcessor implements ImageProcessor, SessionAwareReadApply {
21 private double gamma = 1.0;
[10547]22 final short[] gammaChange = new short[256];
23 private final LookupOp op3 = new LookupOp(
24 new ShortLookupTable(0, new short[][]{gammaChange, gammaChange, gammaChange}), null);
25 private final LookupOp op4 = new LookupOp(
26 new ShortLookupTable(0, new short[][]{gammaChange, gammaChange, gammaChange, gammaChange}), null);
27
28 /**
29 * Returns the currently set gamma value.
30 * @return the currently set gamma value
31 */
32 public double getGamma() {
33 return gamma;
34 }
35
36 /**
37 * Sets a new gamma value, {@code 1} stands for no correction.
38 * @param gamma new gamma value
39 */
40 public void setGamma(double gamma) {
41 this.gamma = gamma;
42 for (int i = 0; i < 256; i++) {
43 gammaChange[i] = (short) (255 * Math.pow(i / 255., gamma));
44 }
45 }
46
47 @Override
48 public BufferedImage process(BufferedImage image) {
[12594]49 if (gamma == 1.0) {
[10547]50 return image;
51 }
52 try {
53 final int bands = image.getRaster().getNumBands();
54 if (image.getType() != BufferedImage.TYPE_CUSTOM && bands == 3) {
55 return op3.filter(image, null);
56 } else if (image.getType() != BufferedImage.TYPE_CUSTOM && bands == 4) {
57 return op4.filter(image, null);
58 }
59 } catch (IllegalArgumentException ignore) {
60 Main.trace(ignore);
61 }
62 final int type = image.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
63 final BufferedImage to = new BufferedImage(image.getWidth(), image.getHeight(), type);
64 to.getGraphics().drawImage(image, 0, 0, null);
65 return process(to);
66 }
67
68 @Override
[12594]69 public void applyFromPropertiesMap(Map<String, String> properties) {
70 String cStr = properties.get("gamma");
71 if (cStr != null) {
72 try {
73 setGamma(Double.parseDouble(cStr));
74 } catch (NumberFormatException e) {
75 // nothing
76 }
77 }
78 }
79
80 @Override
81 public Map<String, String> toPropertiesMap() {
82 if (Utils.equalsEpsilon(gamma, 1.0))
83 return Collections.emptyMap();
84 else
85 return Collections.singletonMap("gamma", Double.toString(gamma));
86 }
87
88 @Override
[10547]89 public String toString() {
90 return "GammaImageProcessor [gamma=" + gamma + ']';
91 }
92}
Note: See TracBrowser for help on using the repository browser.