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.img.math.Function2D;
024    import railo.runtime.type.KeyImpl;
025    import railo.runtime.type.List;
026    import railo.runtime.type.Struct;
027    
028    public class MapFilter extends TransformFilter  implements DynFiltering {
029    
030            private Function2D xMapFunction;
031            private Function2D yMapFunction;
032    
033            public MapFilter() {
034            }
035            
036            public void setXMapFunction(Function2D xMapFunction) {
037                    this.xMapFunction = xMapFunction;
038            }
039    
040            public Function2D getXMapFunction() {
041                    return xMapFunction;
042            }
043    
044            public void setYMapFunction(Function2D yMapFunction) {
045                    this.yMapFunction = yMapFunction;
046            }
047    
048            public Function2D getYMapFunction() {
049                    return yMapFunction;
050            }
051            
052            protected void transformInverse(int x, int y, float[] out) {
053                    float xMap, yMap;
054                    xMap = xMapFunction.evaluate(x, y);
055                    yMap = yMapFunction.evaluate(x, y);
056                    out[0] = xMap * transformedSpace.width;
057                    out[1] = yMap * transformedSpace.height;
058            }
059    
060            public String toString() {
061                    return "Distort/Map Coordinates...";
062            }
063            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
064                    Object o;
065                    if((o=parameters.removeEL(KeyImpl.init("XMapFunction")))!=null)setXMapFunction(ImageFilterUtil.toFunction2D(o,"XMapFunction"));
066                    if((o=parameters.removeEL(KeyImpl.init("YMapFunction")))!=null)setYMapFunction(ImageFilterUtil.toFunction2D(o,"YMapFunction"));
067                    if((o=parameters.removeEL(KeyImpl.init("EdgeAction")))!=null)setEdgeAction(ImageFilterUtil.toString(o,"EdgeAction"));
068                    if((o=parameters.removeEL(KeyImpl.init("Interpolation")))!=null)setInterpolation(ImageFilterUtil.toString(o,"Interpolation"));
069    
070                    // check for arguments not supported
071                    if(parameters.size()>0) {
072                            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 [XMapFunction, YMapFunction, EdgeAction, Interpolation]");
073                    }
074    
075                    return filter(src, dst);
076            }
077    }