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    import java.awt.image.WritableRaster;
019    
020    import railo.runtime.engine.ThreadLocalPageContext;
021    import railo.runtime.exp.FunctionException;
022    import railo.runtime.exp.PageException;
023    import railo.runtime.img.ImageUtil;
024    import railo.runtime.type.KeyImpl;
025    import railo.runtime.type.List;
026    import railo.runtime.type.Struct;
027    
028    /**
029     * An abstract superclass for point filters. The interface is the same as the old RGBImageFilter.
030     */
031    public abstract class PointFilter extends AbstractBufferedImageOp  implements DynFiltering {
032    
033            protected boolean canFilterIndexColorModel = false;
034    
035        public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
036            int width = src.getWidth();
037            int height = src.getHeight();
038                    int type = src.getType();
039                    WritableRaster srcRaster = src.getRaster();
040    
041            if ( dst == null )
042                dst = createCompatibleDestImage( src, null );
043                    WritableRaster dstRaster = dst.getRaster();
044    
045            setDimensions( width, height);
046    
047                    int[] inPixels = new int[width];
048            for ( int y = 0; y < height; y++ ) {
049                            // We try to avoid calling getRGB on images as it causes them to become unmanaged, causing horrible performance problems.
050                            if ( type == BufferedImage.TYPE_INT_ARGB ) {
051                                    srcRaster.getDataElements( 0, y, width, 1, inPixels );
052                                    for ( int x = 0; x < width; x++ )
053                                            inPixels[x] = filterRGB( x, y, inPixels[x] );
054                                    dstRaster.setDataElements( 0, y, width, 1, inPixels );
055                            } else {
056                                    src.getRGB( 0, y, width, 1, inPixels, 0, width );
057                                    for ( int x = 0; x < width; x++ )
058                                            inPixels[x] = filterRGB( x, y, inPixels[x] );
059                                    dst.setRGB( 0, y, width, 1, inPixels, 0, width );
060                            }
061            }
062    
063            return dst;
064        }
065    
066            public void setDimensions(int width, int height) {
067            }
068    
069            public abstract int filterRGB(int x, int y, int rgb);
070            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
071                    Object o;
072                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
073                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
074                            setDimensions(dim[0],dim[1]);
075                    }
076    
077                    // check for arguments not supported
078                    if(parameters.size()>0) {
079                            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]");
080                    }
081    
082                    return filter(src, dst);
083            }
084    }