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 fills an image with a given color. Normally you would just call Graphics.fillRect but it can sometimes be useful
031     * to go via a filter to fit in with an existing API.
032     */
033    public class FillFilter extends PointFilter  implements DynFiltering {
034    
035            private int fillColor;
036    
037        /**
038         * Construct a FillFilter.
039         */
040            public FillFilter() {
041                    this(0xff000000);
042            }
043    
044        /**
045         * Construct a FillFilter.
046         * @param color the fill color
047         */
048            public FillFilter(int color) {
049                    this.fillColor = color;
050            }
051    
052        /**
053         * Set the fill color.
054         * @param fillColor the fill color
055         * @see #getFillColor
056         */
057            public void setFillColor(int fillColor) {
058                    this.fillColor = fillColor;
059            }
060    
061        /**
062         * Get the fill color.
063         * @return the fill color
064         * @see #setFillColor
065         */
066            public int getFillColor() {
067                    return fillColor;
068            }
069    
070            public int filterRGB(int x, int y, int rgb) {
071                    return fillColor;
072            }
073            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
074                    Object o;
075                    if((o=parameters.removeEL(KeyImpl.init("FillColor")))!=null)setFillColor(ImageFilterUtil.toColorRGB(o,"FillColor"));
076                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
077                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
078                            setDimensions(dim[0],dim[1]);
079                    }
080    
081                    // check for arguments not supported
082                    if(parameters.size()>0) {
083                            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 [FillColor, Dimensions]");
084                    }
085    
086                    return filter(src, dst);
087            }
088    }
089