001    /*
002    *
003    
004    Licensed under the Apache License, Version 2.0 (the "License");
005    you may not use this file except in compliance with the License.
006    You may obtain a copy of the License at
007    
008       http://www.apache.org/licenses/LICENSE-2.0
009    
010    Unless required by applicable law or agreed to in writing, software
011    distributed under the License is distributed on an "AS IS" BASIS,
012    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    See the License for the specific language governing permissions and
014    limitations under the License.
015    */
016    
017    package railo.runtime.img.filter;import java.awt.image.BufferedImage;
018    
019    import railo.runtime.engine.ThreadLocalPageContext;
020    import railo.runtime.exp.FunctionException;
021    import railo.runtime.exp.PageException;
022    import railo.runtime.img.ImageUtil;
023    import railo.runtime.type.KeyImpl;
024    import railo.runtime.type.Struct;
025    import railo.runtime.type.util.CollectionUtil;
026    
027    
028    
029    /**
030     * A filter for changing the gamma of an image.
031     */
032    public class GammaFilter extends TransferFilter  implements DynFiltering {
033    
034            private float rGamma, gGamma, bGamma;
035    
036        /**
037         * Construct a GammaFilter.
038         */
039            public GammaFilter() {
040                    this(1.0f);
041            }
042    
043        /**
044         * Construct a GammaFilter.
045         * @param gamma the gamma level for all RGB channels
046         */
047            public GammaFilter(float gamma) {
048                    this(gamma, gamma, gamma);
049            }
050    
051        /**
052         * Construct a GammaFilter.
053         * @param rGamma the gamma level for the red channel
054         * @param gGamma the gamma level for the blue channel
055         * @param bGamma the gamma level for the green channel
056         */
057            public GammaFilter(float rGamma, float gGamma, float bGamma) {
058                    setGamma(rGamma, gGamma, bGamma);
059            }
060    
061        /**
062         * Set the gamma levels.
063         * @param rGamma the gamma level for the red channel
064         * @param gGamma the gamma level for the blue channel
065         * @param bGamma the gamma level for the green channel
066         * @see #getGamma
067         */
068            public void setGamma(float rGamma, float gGamma, float bGamma) {
069                    this.rGamma = rGamma;
070                    this.gGamma = gGamma;
071                    this.bGamma = bGamma;
072                    initialized = false;
073            }
074    
075        /**
076         * Set the gamma level.
077         * @param gamma the gamma level for all RGB channels
078         * @see #getGamma
079         */
080            public void setGamma(float gamma) {
081                    setGamma(gamma, gamma, gamma);
082            }
083            
084        /**
085         * Get the gamma level.
086         * @return the gamma level for all RGB channels
087         * @see #setGamma
088         */
089            public float getGamma() {
090                    return rGamma;
091            }
092            
093        protected void initialize() {
094                    rTable = makeTable(rGamma);
095    
096                    if (gGamma == rGamma)
097                            gTable = rTable;
098                    else
099                            gTable = makeTable(gGamma);
100    
101                    if (bGamma == rGamma)
102                            bTable = rTable;
103                    else if (bGamma == gGamma)
104                            bTable = gTable;
105                    else
106                            bTable = makeTable(bGamma);
107            }
108    
109            private int[] makeTable(float gamma) {
110                    int[] table = new int[256];
111                    for (int i = 0; i < 256; i++) {
112                            int v = (int) ((255.0 * Math.pow(i/255.0, 1.0 / gamma)) + 0.5);
113                            if (v > 255)
114                                    v = 255;
115                            table[i] = v;
116                    }
117                    return table;
118            }
119    
120            public String toString() {
121                    return "Colors/Gamma...";
122            }
123    
124            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
125                    Object o;
126                    if((o=parameters.removeEL(KeyImpl.init("Gamma")))!=null)setGamma(ImageFilterUtil.toFloatValue(o,"Gamma"));
127                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
128                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
129                            setDimensions(dim[0],dim[1]);
130                    }
131    
132                    // check for arguments not supported
133                    if(parameters.size()>0) {
134                            throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "the parameter"+(parameters.size()>1?"s":"")+" ["+CollectionUtil.getKeyList(parameters,", ")+"] "+(parameters.size()>1?"are":"is")+" not allowed, only the following parameters are supported [Gamma, Gamma, Dimensions]");
135                    }
136    
137                    return filter(src, dst);
138            }
139    }
140