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 converts it to its outline, replacing all interior pixels with the 'new' color. 030 */ 031 public class OutlineFilter extends BinaryFilter implements DynFiltering { 032 033 public OutlineFilter() { 034 newColor = 0xffffffff; 035 } 036 037 protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) { 038 int index = 0; 039 int[] outPixels = new int[width * height]; 040 041 for (int y = 0; y < height; y++) { 042 for (int x = 0; x < width; x++) { 043 int pixel = inPixels[y*width+x]; 044 if (blackFunction.isBlack(pixel)) { 045 int neighbours = 0; 046 047 for (int dy = -1; dy <= 1; dy++) { 048 int iy = y+dy; 049 int ioffset; 050 if (0 <= iy && iy < height) { 051 ioffset = iy*width; 052 for (int dx = -1; dx <= 1; dx++) { 053 int ix = x+dx; 054 if (!(dy == 0 && dx == 0) && 0 <= ix && ix < width) { 055 int rgb = inPixels[ioffset+ix]; 056 if (blackFunction.isBlack(rgb)) 057 neighbours++; 058 } else 059 neighbours++; 060 } 061 } 062 } 063 064 if (neighbours == 9) 065 pixel = newColor; 066 } 067 outPixels[index++] = pixel; 068 } 069 070 } 071 return outPixels; 072 } 073 074 public String toString() { 075 return "Binary/Outline..."; 076 } 077 078 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 079 Object o; 080 if((o=parameters.removeEL(KeyImpl.init("Iterations")))!=null)setIterations(ImageFilterUtil.toIntValue(o,"Iterations")); 081 if((o=parameters.removeEL(KeyImpl.init("Colormap")))!=null)setColormap(ImageFilterUtil.toColormap(o,"Colormap")); 082 if((o=parameters.removeEL(KeyImpl.init("NewColor")))!=null)setNewColor(ImageFilterUtil.toColorRGB(o,"NewColor")); 083 084 // check for arguments not supported 085 if(parameters.size()>0) { 086 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 [Iterations, Colormap, NewColor, BlackFunction]"); 087 } 088 089 return filter(src, dst); 090 } 091 } 092