Ignore:
Timestamp:
2015-09-04T10:32:05+02:00 (11 years ago)
Author:
simon04
Message:

fix #10417 - Selectable Gamma value for background imagery

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java

    r8723 r8729  
    2020import java.awt.image.ConvolveOp;
    2121import java.awt.image.Kernel;
     22import java.awt.image.LookupOp;
     23import java.awt.image.ShortLookupTable;
    2224import java.text.AttributedCharacterIterator;
    2325import java.text.AttributedString;
     
    7577    protected double dy = 0.0;
    7678
     79    protected GammaImageProcessor gammaImageProcessor = new GammaImageProcessor();
     80
    7781    private final ImageryAdjustAction adjustAction = new ImageryAdjustAction(this);
    7882
     
    9296        }
    9397        addImageProcessor(createSharpener(PROP_SHARPEN_LEVEL.get()));
     98        addImageProcessor(gammaImageProcessor);
    9499    }
    95100
     
    247252        BufferedImageOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
    248253        return createImageProcessor(op, false);
     254    }
     255
     256    /**
     257     * An image processor which adjusts the gamma value of an image.
     258     */
     259    public static class GammaImageProcessor implements ImageProcessor {
     260        private double gamma = 1;
     261        final short[] gammaChange = new short[256];
     262        private LookupOp op3 = new LookupOp(new ShortLookupTable(0, new short[][]{gammaChange, gammaChange, gammaChange}), null);
     263        private LookupOp op4 = new LookupOp(new ShortLookupTable(0, new short[][]{gammaChange, gammaChange, gammaChange, gammaChange}), null);
     264
     265        /**
     266         * Returns the currently set gamma value.
     267         */
     268        public double getGamma() {
     269            return gamma;
     270        }
     271
     272        /**
     273         * Sets a new gamma value, {@code 1} stands for no correction.
     274         */
     275        public void setGamma(double gamma) {
     276            this.gamma = gamma;
     277            for (int i = 0; i < 256; i++) {
     278                gammaChange[i] = (short) (255 * Math.pow(i / 255., gamma));
     279            }
     280        }
     281
     282        private LookupOp getOp(int bands) {
     283            if (gamma == 1) {
     284                return null;
     285            } else if (bands == 3) {
     286                return op3;
     287            } else if (bands == 4) {
     288                return op4;
     289            } else {
     290                return null;
     291            }
     292        }
     293
     294        @Override
     295        public BufferedImage process(BufferedImage image) {
     296            final LookupOp op = getOp(image.getRaster().getNumBands());
     297            final BufferedImage to = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
     298            return op == null ? image : op.filter(image, to);
     299        }
     300    }
     301
     302    /**
     303     * Returns the currently set gamma value.
     304     */
     305    public double getGamma() {
     306        return gammaImageProcessor.getGamma();
     307    }
     308
     309    /**
     310     * Sets a new gamma value, {@code 1} stands for no correction.
     311     */
     312    public void setGamma(double gamma) {
     313        gammaImageProcessor.setGamma(gamma);
    249314    }
    250315
Note: See TracChangeset for help on using the changeset viewer.