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.type.KeyImpl;
023    import railo.runtime.type.Struct;
024    import railo.runtime.type.util.CollectionUtil;
025    /**
026     * A Filter to pixellate images.
027     */
028    public class BlockFilter extends TransformFilter  implements DynFiltering {
029            
030            private int blockSize = 2;
031    
032            /**
033             * Set the pixel block size.
034             * @param blockSize the number of pixels along each block edge
035         * @min-value 1
036         * @max-value 100+
037         * @see #getBlockSize
038             */
039            public void setBlockSize(int blockSize) {
040                    this.blockSize = blockSize;
041            }
042    
043            /**
044             * Get the pixel block size.
045             * @return the number of pixels along each block edge
046         * @see #setBlockSize
047             */
048            public int getBlockSize() {
049                    return blockSize;
050            }
051    
052    
053            public BlockFilter() {
054            }
055    
056            protected void transformInverse(int x, int y, float[] out) {
057                    out[0] = (x / blockSize) * blockSize;
058                    out[1] = (y / blockSize) * blockSize;
059            }
060    
061            public String toString() {
062                    return "Stylize/Mosaic...";
063            }
064            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=null;//ImageUtil.createBufferedImage(src);
065                    Object o;
066                    if((o=parameters.removeEL(KeyImpl.init("BlockSize")))!=null)setBlockSize(ImageFilterUtil.toIntValue(o,"BlockSize"));
067                    if((o=parameters.removeEL(KeyImpl.init("EdgeAction")))!=null)setEdgeAction(ImageFilterUtil.toString(o,"EdgeAction"));
068                    if((o=parameters.removeEL(KeyImpl.init("Interpolation")))!=null)setInterpolation(ImageFilterUtil.toString(o,"Interpolation"));
069    
070                    // check for arguments not supported
071                    if(parameters.size()>0) {
072                            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 [BlockSize, EdgeAction, Interpolation]");
073                    }
074    
075                    return filter(src, dst);
076            }
077    }
078