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 simply multiplies pixel values by a given scale factor.
031     */
032    public class RescaleFilter extends TransferFilter  implements DynFiltering {
033            
034            private float scale = 1.0f;
035            
036            public RescaleFilter() {
037        }
038        
039            public RescaleFilter(float scale) {
040                    this.scale = scale;
041        }
042        
043        protected float transferFunction( float v ) {
044                    return v * scale;
045            }
046    
047            /**
048         * Specifies the scale factor.
049         * @param scale the scale factor.
050         * @min-value 1
051         * @max-value 5+
052         * @see #getScale
053         */
054            public void setScale(float scale) {
055                    this.scale = scale;
056                    initialized = false;
057            }
058            
059            /**
060         * Returns the scale factor.
061         * @return the scale factor.
062         * @see #setScale
063         */
064            public float getScale() {
065                    return scale;
066            }
067    
068            public String toString() {
069                    return "Colors/Rescale...";
070            }
071    
072            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
073                    Object o;
074                    if((o=parameters.removeEL(KeyImpl.init("Scale")))!=null)setScale(ImageFilterUtil.toFloatValue(o,"Scale"));
075                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
076                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
077                            setDimensions(dim[0],dim[1]);
078                    }
079    
080                    // check for arguments not supported
081                    if(parameters.size()>0) {
082                            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 [Scale, Dimensions]");
083                    }
084    
085                    return filter(src, dst);
086            }
087    }
088