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    public class ErodeAlphaFilter extends PointFilter  implements DynFiltering {
028    
029            private float threshold;
030            private float softness = 0;
031        protected float radius = 5;
032            private float lowerThreshold;
033            private float upperThreshold;
034    
035            public ErodeAlphaFilter() {
036                    this( 3, 0.75f, 0 );
037            }
038    
039            public ErodeAlphaFilter( float radius, float threshold, float softness ) {
040                    this.radius = radius;
041                    this.threshold = threshold;
042                    this.softness = softness;
043            }
044    
045            public void setRadius(float radius) {
046                    this.radius = radius;
047            }
048            
049            public float getRadius() {
050                    return radius;
051            }
052    
053            public void setThreshold(float threshold) {
054                    this.threshold = threshold;
055            }
056            
057            public float getThreshold() {
058                    return threshold;
059            }
060            
061            public void setSoftness(float softness) {
062                    this.softness = softness;
063            }
064    
065            public float getSoftness() {
066                    return softness;
067            }
068    
069        public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
070            dst = new GaussianFilter( (int)radius ).filter( src, (BufferedImage)null );
071            lowerThreshold = 255*(threshold - softness*0.5f);
072            upperThreshold = 255*(threshold + softness*0.5f);
073                    return super.filter(dst, dst);
074            }
075    
076            public int filterRGB(int x, int y, int rgb) {
077                    int a = (rgb >> 24) & 0xff;
078                    int r = (rgb >> 16) & 0xff;
079                    int g = (rgb >> 8) & 0xff;
080                    int b = rgb & 0xff;
081                    if ( a == 255 )
082                return 0xffffffff;
083            float f = ImageMath.smoothStep(lowerThreshold, upperThreshold, a);
084            a = (int)(f * 255);
085            if ( a < 0 )
086                a = 0;
087            else if ( a > 255 )
088                a = 255;
089            return (a << 24) | 0xffffff;
090            }
091    
092            public String toString() {
093                    return "Alpha/Erode...";
094            }
095            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
096                    Object o;
097                    if((o=parameters.removeEL(KeyImpl.init("Radius")))!=null)setRadius(ImageFilterUtil.toFloatValue(o,"Radius"));
098                    if((o=parameters.removeEL(KeyImpl.init("Softness")))!=null)setSoftness(ImageFilterUtil.toFloatValue(o,"Softness"));
099                    if((o=parameters.removeEL(KeyImpl.init("Threshold")))!=null)setThreshold(ImageFilterUtil.toFloatValue(o,"Threshold"));
100                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
101                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
102                            setDimensions(dim[0],dim[1]);
103                    }
104    
105                    // check for arguments not supported
106                    if(parameters.size()>0) {
107                            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 [Radius, Softness, Threshold, Dimensions]");
108                    }
109    
110                    return filter(src, dst);
111            }
112    }