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     * A filter which replaces one color by another in an image. This is frankly, not often useful, but has its occasional
031     * uses when dealing with GIF transparency and the like.
032     */
033    public class MapColorsFilter extends PointFilter  implements DynFiltering {
034    
035            private int oldColor;
036            private int newColor;
037            
038            /**
039         * Construct a MapColorsFilter.
040         */
041        public MapColorsFilter() {
042                    this( 0xffffffff, 0xff000000 );
043            }
044            
045            /**
046         * Construct a MapColorsFilter.
047         * @param oldColor the color to replace
048         * @param newColor the color to replace it with
049         */
050            public MapColorsFilter(int oldColor, int newColor) {
051                    canFilterIndexColorModel = true;
052                    this.oldColor = oldColor;
053                    this.newColor = newColor;
054            }
055    
056            public int filterRGB(int x, int y, int rgb) {
057                    if (rgb == oldColor)
058                            return newColor;
059                    return rgb;
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("Dimensions")))!=null){
064                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
065                            setDimensions(dim[0],dim[1]);
066                    }
067    
068                    // check for arguments not supported
069                    if(parameters.size()>0) {
070                            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 [Dimensions]");
071                    }
072    
073                    return filter(src, dst);
074            }
075    }
076