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

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
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;
8import java.util.Collections;
9import java.util.Map;
10
11import org.openstreetmap.josm.gui.layer.ImageProcessor;
12import org.openstreetmap.josm.io.session.SessionAwareReadApply;
13import org.openstreetmap.josm.tools.Logging;
14import org.openstreetmap.josm.tools.Utils;
15
16/**
17 * An image processor which adjusts the gamma value of an image.
18 * @since 10547
19 */
20public class GammaImageProcessor implements ImageProcessor, SessionAwareReadApply {
21 private double gamma = 1.0;
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) {
49 if (gamma == 1.0) {
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 Logging.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
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 Logging.trace(e);
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
89 public String toString() {
90 return "GammaImageProcessor [gamma=" + gamma + ']';
91 }
92}
Note: See TracBrowser for help on using the repository browser.