001/*
002Copyright 2006 Jerry Huxtable
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008   http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015*/
016
017package com.jhlabs.image;
018
019import java.awt.*;
020import java.awt.image.*;
021
022/**
023 * A filter for changing the gamma of an image.
024 */
025public class GammaFilter extends TransferFilter {
026
027        private float rGamma, gGamma, bGamma;
028
029    /**
030     * Construct a GammaFilter.
031     */
032        public GammaFilter() {
033                this(1.0f);
034        }
035
036    /**
037     * Construct a GammaFilter.
038     * @param gamma the gamma level for all RGB channels
039     */
040        public GammaFilter(float gamma) {
041                this(gamma, gamma, gamma);
042        }
043
044    /**
045     * Construct a GammaFilter.
046     * @param rGamma the gamma level for the red channel
047     * @param gGamma the gamma level for the blue channel
048     * @param bGamma the gamma level for the green channel
049     */
050        public GammaFilter(float rGamma, float gGamma, float bGamma) {
051                setGamma(rGamma, gGamma, bGamma);
052        }
053
054    /**
055     * Set the gamma levels.
056     * @param rGamma the gamma level for the red channel
057     * @param gGamma the gamma level for the blue channel
058     * @param bGamma the gamma level for the green channel
059     * @see #getGamma
060     */
061        public void setGamma(float rGamma, float gGamma, float bGamma) {
062                this.rGamma = rGamma;
063                this.gGamma = gGamma;
064                this.bGamma = bGamma;
065                initialized = false;
066        }
067
068    /**
069     * Set the gamma level.
070     * @param gamma the gamma level for all RGB channels
071     * @see #getGamma
072     */
073        public void setGamma(float gamma) {
074                setGamma(gamma, gamma, gamma);
075        }
076        
077    /**
078     * Get the gamma level.
079     * @return the gamma level for all RGB channels
080     * @see #setGamma
081     */
082        public float getGamma() {
083                return rGamma;
084        }
085        
086    protected void initialize() {
087                rTable = makeTable(rGamma);
088
089                if (gGamma == rGamma)
090                        gTable = rTable;
091                else
092                        gTable = makeTable(gGamma);
093
094                if (bGamma == rGamma)
095                        bTable = rTable;
096                else if (bGamma == gGamma)
097                        bTable = gTable;
098                else
099                        bTable = makeTable(bGamma);
100        }
101
102        private int[] makeTable(float gamma) {
103                int[] table = new int[256];
104                for (int i = 0; i < 256; i++) {
105                        int v = (int) ((255.0 * Math.pow(i/255.0, 1.0 / gamma)) + 0.5);
106                        if (v > 255)
107                                v = 255;
108                        table[i] = v;
109                }
110                return table;
111        }
112
113        public String toString() {
114                return "Colors/Gamma...";
115        }
116
117}
118