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.KeyImpl; 025 import railo.runtime.type.Struct; 026 import railo.runtime.type.util.CollectionUtil; 027 028 /** 029 * Given a binary image, this filter performs binary erosion, setting all removed pixels to the given 'new' color. 030 */ 031 public class ErodeFilter extends BinaryFilter implements DynFiltering { 032 033 private int threshold = 2; 034 035 public ErodeFilter() { 036 newColor = 0xffffffff; 037 } 038 039 /** 040 * Set the threshold - the number of neighbouring pixels for dilation to occur. 041 * @param threshold the new threshold 042 * @see #getThreshold 043 */ 044 public void setThreshold(int threshold) { 045 this.threshold = threshold; 046 } 047 048 /** 049 * Return the threshold - the number of neighbouring pixels for dilation to occur. 050 * @return the current threshold 051 * @see #setThreshold 052 */ 053 public int getThreshold() { 054 return threshold; 055 } 056 057 protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) { 058 int[] outPixels = new int[width * height]; 059 060 for (int i = 0; i < iterations; i++) { 061 int index = 0; 062 063 if (i > 0) { 064 int[] t = inPixels; 065 inPixels = outPixels; 066 outPixels = t; 067 } 068 for (int y = 0; y < height; y++) { 069 for (int x = 0; x < width; x++) { 070 int pixel = inPixels[y*width+x]; 071 if (blackFunction.isBlack(pixel)) { 072 int neighbours = 0; 073 074 for (int dy = -1; dy <= 1; dy++) { 075 int iy = y+dy; 076 int ioffset; 077 if (0 <= iy && iy < height) { 078 ioffset = iy*width; 079 for (int dx = -1; dx <= 1; dx++) { 080 int ix = x+dx; 081 if (!(dy == 0 && dx == 0) && 0 <= ix && ix < width) { 082 int rgb = inPixels[ioffset+ix]; 083 if (!blackFunction.isBlack(rgb)) 084 neighbours++; 085 } 086 } 087 } 088 } 089 090 if (neighbours >= threshold) { 091 if (colormap != null) 092 pixel = colormap.getColor((float)i/iterations); 093 else 094 pixel = newColor; 095 } 096 } 097 outPixels[index++] = pixel; 098 } 099 } 100 } 101 102 return outPixels; 103 } 104 105 public String toString() { 106 return "Binary/Erode..."; 107 } 108 109 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 110 Object o; 111 if((o=parameters.removeEL(KeyImpl.init("Threshold")))!=null)setThreshold(ImageFilterUtil.toIntValue(o,"Threshold")); 112 if((o=parameters.removeEL(KeyImpl.init("Iterations")))!=null)setIterations(ImageFilterUtil.toIntValue(o,"Iterations")); 113 if((o=parameters.removeEL(KeyImpl.init("Colormap")))!=null)setColormap(ImageFilterUtil.toColormap(o,"Colormap")); 114 if((o=parameters.removeEL(KeyImpl.init("NewColor")))!=null)setNewColor(ImageFilterUtil.toColorRGB(o,"NewColor")); 115 116 // check for arguments not supported 117 if(parameters.size()>0) { 118 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 [Threshold, Iterations, Colormap, NewColor, BlackFunction]"); 119 } 120 121 return filter(src, dst); 122 } 123 } 124