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    
029    /**
030     * Applies a bit mask to each ARGB pixel of an image. You can use this for, say, masking out the red channel.
031     */
032    public class MaskFilter extends PointFilter  implements DynFiltering {
033    
034            private int mask;
035    
036            public MaskFilter() {
037                    this(0xff00ffff);
038            }
039    
040            public MaskFilter(int mask) {
041                    canFilterIndexColorModel = true;
042                    setMask(mask);
043            }
044    
045            public void setMask(int mask) {
046                    this.mask = mask;
047            }
048    
049            public int getMask() {
050                    return mask;
051            }
052    
053            public int filterRGB(int x, int y, int rgb) {
054                    return rgb & mask;
055            }
056    
057            public String toString() {
058                    return "Mask";
059            }
060    
061            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
062                    Object o;
063                    if((o=parameters.removeEL(KeyImpl.init("Mask")))!=null)setMask(ImageFilterUtil.toIntValue(o,"Mask"));
064                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
065                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
066                            setDimensions(dim[0],dim[1]);
067                    }
068    
069                    // check for arguments not supported
070                    if(parameters.size()>0) {
071                            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 [Mask, Dimensions]");
072                    }
073    
074                    return filter(src, dst);
075            }
076    }