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.List;
025    import railo.runtime.type.Struct;
026    
027    
028    
029    /**
030     * A filter to change the brightness and contrast of an image.
031     */
032    public class ContrastFilter extends TransferFilter  implements DynFiltering {
033    
034            private float brightness = 1.0f;
035            private float contrast = 1.0f;
036            
037            protected float transferFunction( float f ) {
038                    f = f*brightness;
039                    f = (f-0.5f)*contrast+0.5f;
040                    return f;
041            }
042    
043        /**
044         * Set the filter brightness.
045         * @param brightness the brightness in the range 0 to 1
046         * @min-value 0
047         * @max-value 0
048         * @see #getBrightness
049         */
050            public void setBrightness(float brightness) {
051                    this.brightness = brightness;
052                    initialized = false;
053            }
054            
055        /**
056         * Get the filter brightness.
057         * @return the brightness in the range 0 to 1
058         * @see #setBrightness
059         */
060            public float getBrightness() {
061                    return brightness;
062            }
063    
064        /**
065         * Set the filter contrast.
066         * @param contrast the contrast in the range 0 to 1
067         * @min-value 0
068         * @max-value 0
069         * @see #getContrast
070         */
071            public void setContrast(float contrast) {
072                    this.contrast = contrast;
073                    initialized = false;
074            }
075            
076        /**
077         * Get the filter contrast.
078         * @return the contrast in the range 0 to 1
079         * @see #setContrast
080         */
081            public float getContrast() {
082                    return contrast;
083            }
084    
085            public String toString() {
086                    return "Colors/Contrast...";
087            }
088    
089            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
090                    Object o;
091                    if((o=parameters.removeEL(KeyImpl.init("Brightness")))!=null)setBrightness(ImageFilterUtil.toFloatValue(o,"Brightness"));
092                    if((o=parameters.removeEL(KeyImpl.init("Contrast")))!=null)setContrast(ImageFilterUtil.toFloatValue(o,"Contrast"));
093                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
094                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
095                            setDimensions(dim[0],dim[1]);
096                    }
097    
098                    // check for arguments not supported
099                    if(parameters.size()>0) {
100                            throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "the parameter"+(parameters.size()>1?"s":"")+" ["+List.arrayToList(parameters.keysAsString(),", ")+"] "+(parameters.size()>1?"are":"is")+" not allowed, only the following parameters are supported [Brightness, Contrast, Dimensions]");
101                    }
102    
103                    return filter(src, dst);
104            }
105    }
106