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 which changes the exposure of an image.
031     */
032    public class ExposureFilter extends TransferFilter  implements DynFiltering {
033    
034            private float exposure = 1.0f;
035    
036            protected float transferFunction( float f ) {
037                    return 1 - (float)Math.exp(-f * exposure);
038            }
039    
040        /**
041         * Set the exposure level.
042         * @param exposure the exposure level
043         * @min-value 0
044         * @max-value 5+
045         * @see #getExposure
046         */
047            public void setExposure(float exposure) {
048                    this.exposure = exposure;
049                    initialized = false;
050            }
051            
052        /**
053         * Get the exposure level.
054         * @return the exposure level
055         * @see #setExposure
056         */
057            public float getExposure() {
058                    return exposure;
059            }
060    
061            public String toString() {
062                    return "Colors/Exposure...";
063            }
064    
065            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
066                    Object o;
067                    if((o=parameters.removeEL(KeyImpl.init("Exposure")))!=null)setExposure(ImageFilterUtil.toFloatValue(o,"Exposure"));
068                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
069                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
070                            setDimensions(dim[0],dim[1]);
071                    }
072    
073                    // check for arguments not supported
074                    if(parameters.size()>0) {
075                            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 [Exposure, Dimensions]");
076                    }
077    
078                    return filter(src, dst);
079            }
080    }
081