001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019/* 020* 021 022Licensed under the Apache License, Version 2.0 (the "License"); 023you may not use this file except in compliance with the License. 024You may obtain a copy of the License at 025 026 http://www.apache.org/licenses/LICENSE-2.0 027 028Unless required by applicable law or agreed to in writing, software 029distributed under the License is distributed on an "AS IS" BASIS, 030WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 031See the License for the specific language governing permissions and 032limitations under the License. 033*/ 034 035package lucee.runtime.img.filter;import java.awt.Rectangle; 036import java.awt.image.BufferedImage; 037 038import lucee.runtime.engine.ThreadLocalPageContext; 039import lucee.runtime.exp.FunctionException; 040import lucee.runtime.exp.PageException; 041import lucee.runtime.img.ImageUtil; 042import lucee.runtime.type.KeyImpl; 043import lucee.runtime.type.Struct; 044import lucee.runtime.type.util.CollectionUtil; 045 046/** 047 * Given a binary image, this filter performs binary dilation, setting all added pixels to the given 'new' color. 048 */ 049public class DilateFilter extends BinaryFilter implements DynFiltering { 050 051 private int threshold = 2; 052 053 public DilateFilter() { 054 } 055 056 /** 057 * Set the threshold - the number of neighbouring pixels for dilation to occur. 058 * @param threshold the new threshold 059 * @see #getThreshold 060 */ 061 public void setThreshold(int threshold) { 062 this.threshold = threshold; 063 } 064 065 /** 066 * Return the threshold - the number of neighbouring pixels for dilation to occur. 067 * @return the current threshold 068 * @see #setThreshold 069 */ 070 public int getThreshold() { 071 return threshold; 072 } 073 074 protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) { 075 int[] outPixels = new int[width * height]; 076 077 for (int i = 0; i < iterations; i++) { 078 int index = 0; 079 080 if (i > 0) { 081 int[] t = inPixels; 082 inPixels = outPixels; 083 outPixels = t; 084 } 085 for (int y = 0; y < height; y++) { 086 for (int x = 0; x < width; x++) { 087 int pixel = inPixels[y*width+x]; 088 if (!blackFunction.isBlack(pixel)) { 089 int neighbours = 0; 090 091 for (int dy = -1; dy <= 1; dy++) { 092 int iy = y+dy; 093 int ioffset; 094 if (0 <= iy && iy < height) { 095 ioffset = iy*width; 096 for (int dx = -1; dx <= 1; dx++) { 097 int ix = x+dx; 098 if (!(dy == 0 && dx == 0) && 0 <= ix && ix < width) { 099 int rgb = inPixels[ioffset+ix]; 100 if (blackFunction.isBlack(rgb)) 101 neighbours++; 102 } 103 } 104 } 105 } 106 107 if (neighbours >= threshold) { 108 if (colormap != null) 109 pixel = colormap.getColor((float)i/iterations); 110 else 111 pixel = newColor; 112 } 113 } 114 outPixels[index++] = pixel; 115 } 116 } 117 } 118 return outPixels; 119 } 120 121 public String toString() { 122 return "Binary/Dilate..."; 123 } 124 125 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 126 Object o; 127 if((o=parameters.removeEL(KeyImpl.init("Threshold")))!=null)setThreshold(ImageFilterUtil.toIntValue(o,"Threshold")); 128 if((o=parameters.removeEL(KeyImpl.init("Iterations")))!=null)setIterations(ImageFilterUtil.toIntValue(o,"Iterations")); 129 if((o=parameters.removeEL(KeyImpl.init("Colormap")))!=null)setColormap(ImageFilterUtil.toColormap(o,"Colormap")); 130 if((o=parameters.removeEL(KeyImpl.init("NewColor")))!=null)setNewColor(ImageFilterUtil.toColorRGB(o,"NewColor")); 131 132 // check for arguments not supported 133 if(parameters.size()>0) { 134 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]"); 135 } 136 137 return filter(src, dst); 138 } 139} 140