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.img.math.BinaryFunction;
024    import railo.runtime.img.math.BlackFunction;
025    import railo.runtime.type.KeyImpl;
026    import railo.runtime.type.Struct;
027    import railo.runtime.type.util.CollectionUtil;
028    /**
029     * The superclass for some of the filters which work on binary images.
030     */
031    public abstract class BinaryFilter extends WholeImageFilter  implements DynFiltering {
032    
033            protected int newColor = 0xff000000;
034            protected BinaryFunction blackFunction = new BlackFunction();
035            protected int iterations = 1;
036            protected Colormap colormap;
037    
038            /**
039             * Set the number of iterations the effect is performed.
040             * @param iterations the number of iterations
041         * @min-value 0
042         * @see #getIterations
043             */
044            public void setIterations(int iterations) {
045                    this.iterations = iterations;
046            }
047    
048            /**
049             * Get the number of iterations the effect is performed.
050             * @return the number of iterations
051         * @see #setIterations
052             */
053            public int getIterations() {
054                    return iterations;
055            }
056    
057        /**
058         * Set the colormap to be used for the filter.
059         * @param colormap the colormap
060         * @see #getColormap
061         */
062            public void setColormap(Colormap colormap) {
063                    this.colormap = colormap;
064            }
065    
066        /**
067         * Get the colormap to be used for the filter.
068         * @return the colormap
069         * @see #setColormap
070         */
071            public Colormap getColormap() {
072                    return colormap;
073            }
074    
075            public void setNewColor(int newColor) {
076                    this.newColor = newColor;
077            }
078    
079            public int getNewColor() {
080                    return newColor;
081            }
082    
083            public void setBlackFunction(BinaryFunction blackFunction) {
084                    this.blackFunction = blackFunction;
085            }
086    
087            public BinaryFunction getBlackFunction() {
088                    return blackFunction;
089            }
090    
091            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
092                    Object o;
093                    if((o=parameters.removeEL(KeyImpl.init("Iterations")))!=null)setIterations(ImageFilterUtil.toIntValue(o,"Iterations"));
094                    if((o=parameters.removeEL(KeyImpl.init("Colormap")))!=null)setColormap(ImageFilterUtil.toColormap(o,"Colormap"));
095                    if((o=parameters.removeEL(KeyImpl.init("NewColor")))!=null)setNewColor(ImageFilterUtil.toIntValue(o,"NewColor"));
096                    
097                    // check for arguments not supported
098                    if(parameters.size()>0) {
099                            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 [Iterations, Colormap, NewColor, BlackFunction]");
100                    }
101    
102                    return filter(src, dst);
103            }
104    }
105