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

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

fix #13159 - Move image processors out of imagery layer (patch by michael2402) - gsoc-core + fix checkstyle violations

  • Property svn:eol-style set to native
File size: 2.3 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;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.gui.layer.ImageProcessor;
11
12/**
13 * An image processor which adjusts the gamma value of an image.
14 * @since 10547
15 */
16public class GammaImageProcessor implements ImageProcessor {
17 private double gamma = 1;
18 final short[] gammaChange = new short[256];
19 private final LookupOp op3 = new LookupOp(
20 new ShortLookupTable(0, new short[][]{gammaChange, gammaChange, gammaChange}), null);
21 private final LookupOp op4 = new LookupOp(
22 new ShortLookupTable(0, new short[][]{gammaChange, gammaChange, gammaChange, gammaChange}), null);
23
24 /**
25 * Returns the currently set gamma value.
26 * @return the currently set gamma value
27 */
28 public double getGamma() {
29 return gamma;
30 }
31
32 /**
33 * Sets a new gamma value, {@code 1} stands for no correction.
34 * @param gamma new gamma value
35 */
36 public void setGamma(double gamma) {
37 this.gamma = gamma;
38 for (int i = 0; i < 256; i++) {
39 gammaChange[i] = (short) (255 * Math.pow(i / 255., gamma));
40 }
41 }
42
43 @Override
44 public BufferedImage process(BufferedImage image) {
45 if (gamma == 1) {
46 return image;
47 }
48 try {
49 final int bands = image.getRaster().getNumBands();
50 if (image.getType() != BufferedImage.TYPE_CUSTOM && bands == 3) {
51 return op3.filter(image, null);
52 } else if (image.getType() != BufferedImage.TYPE_CUSTOM && bands == 4) {
53 return op4.filter(image, null);
54 }
55 } catch (IllegalArgumentException ignore) {
56 Main.trace(ignore);
57 }
58 final int type = image.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
59 final BufferedImage to = new BufferedImage(image.getWidth(), image.getHeight(), type);
60 to.getGraphics().drawImage(image, 0, 0, null);
61 return process(to);
62 }
63
64 @Override
65 public String toString() {
66 return "GammaImageProcessor [gamma=" + gamma + ']';
67 }
68}
Note: See TracBrowser for help on using the repository browser.