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.Rectangle;
018    import java.awt.image.BufferedImage;
019    import java.awt.image.ColorModel;
020    import java.awt.image.WritableRaster;
021    
022    import railo.runtime.engine.ThreadLocalPageContext;
023    import railo.runtime.exp.FunctionException;
024    import railo.runtime.exp.PageException;
025    import railo.runtime.img.ImageUtil;
026    import railo.runtime.type.List;
027    import railo.runtime.type.Struct;
028    
029    
030    /**
031     * A filter which acts as a superclass for filters which need to have the whole image in memory
032     * to do their stuff.
033     */
034    public abstract class WholeImageFilter extends AbstractBufferedImageOp  implements DynFiltering {
035    
036            /**
037         * The output image bounds.
038         */
039        protected Rectangle transformedSpace;
040    
041            /**
042         * The input image bounds.
043         */
044            protected Rectangle originalSpace;
045            
046            /**
047             * Construct a WholeImageFilter.
048             */
049            public WholeImageFilter() {
050            }
051    
052        public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
053            int width = src.getWidth();
054            int height = src.getHeight();
055                    int type = src.getType();
056                    WritableRaster srcRaster = src.getRaster();
057    
058                    originalSpace = new Rectangle(0, 0, width, height);
059                    transformedSpace = new Rectangle(0, 0, width, height);
060                    transformSpace(transformedSpace);
061    
062            if ( dst == null ) {
063                ColorModel dstCM = src.getColorModel();
064                            dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height), dstCM.isAlphaPremultiplied(), null);
065                    }
066                    WritableRaster dstRaster = dst.getRaster();
067    
068                    int[] inPixels = getRGB( src, 0, 0, width, height, null );
069                    inPixels = filterPixels( width, height, inPixels, transformedSpace );
070                    setRGB( dst, 0, 0, transformedSpace.width, transformedSpace.height, inPixels );
071    
072            return dst;
073        }
074    
075            /**
076         * Calculate output bounds for given input bounds.
077         * @param rect input and output rectangle
078         */
079            protected void transformSpace(Rectangle rect) {
080            }
081            
082            /**
083         * Actually filter the pixels.
084         * @param width the image width
085         * @param height the image height
086         * @param inPixels the image pixels
087         * @param transformedSpace the output bounds
088         * @return the output pixels
089         */
090            protected abstract int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace );
091            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
092                    Object o;
093    
094                    // check for arguments not supported
095                    if(parameters.size()>0) {
096                            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 []");
097                    }
098    
099                    return filter(src, dst);
100            }
101    }
102