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.Rectangle;
018    import java.awt.image.BufferedImage;
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 filter which replcaes each pixel by the mimimum of itself and its eight neightbours.
029     */
030    public class MinimumFilter extends WholeImageFilter  implements DynFiltering {
031            
032            public MinimumFilter() {
033            }
034            
035            protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) {
036                    int index = 0;
037                    int[] outPixels = new int[width * height];
038    
039                    for (int y = 0; y < height; y++) {
040                            for (int x = 0; x < width; x++) {
041                                    int pixel = 0xffffffff;
042                                    for (int dy = -1; dy <= 1; dy++) {
043                                            int iy = y+dy;
044                                            int ioffset;
045                                            if (0 <= iy && iy < height) {
046                                                    ioffset = iy*width;
047                                                    for (int dx = -1; dx <= 1; dx++) {
048                                                            int ix = x+dx;
049                                                            if (0 <= ix && ix < width) {
050                                                                    pixel = PixelUtils.combinePixels(pixel, inPixels[ioffset+ix], PixelUtils.MIN);
051                                                            }
052                                                    }
053                                            }
054                                    }
055                                    outPixels[index++] = pixel;
056                            }
057                    }
058                    return outPixels;
059            }
060    
061            public String toString() {
062                    return "Blur/Minimum";
063            }
064    
065            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
066                    //Object o;
067    
068                    // check for arguments not supported
069                    if(parameters.size()>0) {
070                            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 []");
071                    }
072    
073                    return filter(src, dst);
074            }
075    }
076