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.img.ImageUtil; 023 import railo.runtime.type.KeyImpl; 024 import railo.runtime.type.List; 025 import railo.runtime.type.Struct; 026 027 /** 028 * A filter which adds Gaussian blur to an image, producing a glowing effect. 029 * 030 */ 031 public class GlowFilter extends GaussianFilter implements DynFiltering { 032 033 private float amount = 0.5f; 034 035 public GlowFilter() { 036 radius = 2; 037 } 038 039 /** 040 * Set the amount of glow. 041 * @param amount the amount 042 * @min-value 0 043 * @max-value 1 044 * @see #getAmount 045 */ 046 public void setAmount( float amount ) { 047 this.amount = amount; 048 } 049 050 /** 051 * Get the amount of glow. 052 * @return the amount 053 * @see #setAmount 054 */ 055 public float getAmount() { 056 return amount; 057 } 058 059 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 060 int width = src.getWidth(); 061 int height = src.getHeight(); 062 063 if ( dst == null ) 064 dst = createCompatibleDestImage( src, null ); 065 066 int[] inPixels = new int[width*height]; 067 int[] outPixels = new int[width*height]; 068 src.getRGB( 0, 0, width, height, inPixels, 0, width ); 069 070 if ( radius > 0 ) { 071 convolveAndTranspose(kernel, inPixels, outPixels, width, height, alpha, alpha && premultiplyAlpha, false, CLAMP_EDGES); 072 convolveAndTranspose(kernel, outPixels, inPixels, height, width, alpha, false, alpha && premultiplyAlpha, CLAMP_EDGES); 073 } 074 075 src.getRGB( 0, 0, width, height, outPixels, 0, width ); 076 077 float a = 4*amount; 078 079 int index = 0; 080 for ( int y = 0; y < height; y++ ) { 081 for ( int x = 0; x < width; x++ ) { 082 int rgb1 = outPixels[index]; 083 int r1 = (rgb1 >> 16) & 0xff; 084 int g1 = (rgb1 >> 8) & 0xff; 085 int b1 = rgb1 & 0xff; 086 087 int rgb2 = inPixels[index]; 088 int r2 = (rgb2 >> 16) & 0xff; 089 int g2 = (rgb2 >> 8) & 0xff; 090 int b2 = rgb2 & 0xff; 091 092 r1 = PixelUtils.clamp( (int)(r1 + a * r2) ); 093 g1 = PixelUtils.clamp( (int)(g1 + a * g2) ); 094 b1 = PixelUtils.clamp( (int)(b1 + a * b2) ); 095 096 inPixels[index] = (rgb1 & 0xff000000) | (r1 << 16) | (g1 << 8) | b1; 097 index++; 098 } 099 } 100 101 dst.setRGB( 0, 0, width, height, inPixels, 0, width ); 102 return dst; 103 } 104 105 public String toString() { 106 return "Blur/Glow..."; 107 } 108 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 109 Object o; 110 if((o=parameters.removeEL(KeyImpl.init("Amount")))!=null)setAmount(ImageFilterUtil.toFloatValue(o,"Amount")); 111 if((o=parameters.removeEL(KeyImpl.init("Radius")))!=null)setRadius(ImageFilterUtil.toFloatValue(o,"Radius")); 112 if((o=parameters.removeEL(KeyImpl.init("EdgeAction")))!=null)setEdgeAction(ImageFilterUtil.toString(o,"EdgeAction")); 113 if((o=parameters.removeEL(KeyImpl.init("UseAlpha")))!=null)setUseAlpha(ImageFilterUtil.toBooleanValue(o,"UseAlpha")); 114 if((o=parameters.removeEL(KeyImpl.init("PremultiplyAlpha")))!=null)setPremultiplyAlpha(ImageFilterUtil.toBooleanValue(o,"PremultiplyAlpha")); 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":"")+" ["+List.arrayToList(parameters.keysAsString(),", ")+"] "+(parameters.size()>1?"are":"is")+" not allowed, only the following parameters are supported [Amount, Radius, Kernel, EdgeAction, UseAlpha, PremultiplyAlpha]"); 119 } 120 121 return filter(src, dst); 122 } 123 }