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    import java.awt.image.BufferedImageOp;
019    
020    import railo.runtime.engine.ThreadLocalPageContext;
021    import railo.runtime.exp.FunctionException;
022    import railo.runtime.exp.PageException;
023    import railo.runtime.img.ImageUtil;
024    import railo.runtime.type.Struct;
025    import railo.runtime.type.util.CollectionUtil;
026    
027    /**
028     * A BufferedImageOp which iterates another BufferedImageOp.
029     */
030    public class IteratedFilter extends AbstractBufferedImageOp  implements DynFiltering {
031            private BufferedImageOp filter;
032            private int iterations;
033            
034        /**
035         * Construct an IteratedFilter.
036         * @param filter the filetr to iterate
037         * @param iterations the number of iterations
038         */
039            public IteratedFilter( BufferedImageOp filter, int iterations ) {
040                    this.filter = filter;
041                    this.iterations = iterations;
042            }
043            
044            public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
045                    BufferedImage image = src;
046    
047                    for ( int i = 0; i < iterations; i++ )
048                            image = filter.filter( image, dst );
049                    
050                    return image;
051            }
052            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
053                    //Object o;
054    
055                    // check for arguments not supported
056                    if(parameters.size()>0) {
057                            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 []");
058                    }
059    
060                    return filter(src, dst);
061            }
062    }