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     * A filter which simulates chrome.
028     */
029    public class ChromeFilter extends LightFilter  implements DynFiltering {
030            private float amount = 0.5f;
031            private float exposure = 1.0f;
032    
033            /**
034             * Set the amount of effect.
035             * @param amount the amount
036         * @min-value 0
037         * @max-value 1
038         * @see #getAmount
039             */
040            public void setAmount(float amount) {
041                    this.amount = amount;
042            }
043    
044            /**
045             * Get the amount of chrome.
046             * @return the amount
047         * @see #setAmount
048             */
049            public float getAmount() {
050                    return amount;
051            }
052    
053            /**
054             * Set the exppsure of the effect.
055             * @param exposure the exposure
056         * @min-value 0
057         * @max-value 1
058         * @see #getExposure
059             */
060            public void setExposure(float exposure) {
061                    this.exposure = exposure;
062            }
063            
064            /**
065             * Get the exppsure of the effect.
066             * @return the exposure
067         * @see #setExposure
068             */
069            public float getExposure() {
070                    return exposure;
071            }
072    
073        public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
074                    setColorSource( LightFilter.COLORS_CONSTANT );
075                    dst = super.filter( src, dst );
076                    TransferFilter tf = new TransferFilter() {
077                            protected float transferFunction( float v ) {
078                                    v += amount * (float)Math.sin( v * 2 * Math.PI );
079                                    return 1 - (float)Math.exp(-v * exposure);
080                            }
081                    };
082            return tf.filter( dst, dst );
083        }
084    
085            public String toString() {
086                    return "Effects/Chrome...";
087            }
088            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
089                    Object o;
090                    if((o=parameters.removeEL(KeyImpl.init("Amount")))!=null)setAmount(ImageFilterUtil.toFloatValue(o,"Amount"));
091                    if((o=parameters.removeEL(KeyImpl.init("Exposure")))!=null)setExposure(ImageFilterUtil.toFloatValue(o,"Exposure"));
092                    if((o=parameters.removeEL(KeyImpl.init("ColorSource")))!=null)setColorSource(ImageFilterUtil.toColorRGB(o,"ColorSource"));
093                    if((o=parameters.removeEL(KeyImpl.init("Material")))!=null)setMaterial(ImageFilterUtil.toLightFilter$Material(o,"Material"));
094                    if((o=parameters.removeEL(KeyImpl.init("BumpFunction")))!=null)setBumpFunction(ImageFilterUtil.toFunction2D(o,"BumpFunction"));
095                    if((o=parameters.removeEL(KeyImpl.init("BumpHeight")))!=null)setBumpHeight(ImageFilterUtil.toFloatValue(o,"BumpHeight"));
096                    if((o=parameters.removeEL(KeyImpl.init("BumpSoftness")))!=null)setBumpSoftness(ImageFilterUtil.toFloatValue(o,"BumpSoftness"));
097                    if((o=parameters.removeEL(KeyImpl.init("BumpShape")))!=null)setBumpShape(ImageFilterUtil.toIntValue(o,"BumpShape"));
098                    if((o=parameters.removeEL(KeyImpl.init("ViewDistance")))!=null)setViewDistance(ImageFilterUtil.toFloatValue(o,"ViewDistance"));
099                    if((o=parameters.removeEL(KeyImpl.init("EnvironmentMap")))!=null)setEnvironmentMap(ImageFilterUtil.toBufferedImage(o,"EnvironmentMap"));
100                    if((o=parameters.removeEL(KeyImpl.init("BumpSource")))!=null)setBumpSource(ImageFilterUtil.toIntValue(o,"BumpSource"));
101                    if((o=parameters.removeEL(KeyImpl.init("DiffuseColor")))!=null)setDiffuseColor(ImageFilterUtil.toColorRGB(o,"DiffuseColor"));
102    
103                    // check for arguments not supported
104                    if(parameters.size()>0) {
105                            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 [Amount, Exposure, ColorSource, Material, BumpFunction, BumpHeight, BumpSoftness, BumpShape, ViewDistance, EnvironmentMap, BumpSource, DiffuseColor]");
106                    }
107    
108                    return filter(src, dst);
109            }
110    }
111