001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/*
020*
021
022Licensed under the Apache License, Version 2.0 (the "License");
023you may not use this file except in compliance with the License.
024You may obtain a copy of the License at
025
026   http://www.apache.org/licenses/LICENSE-2.0
027
028Unless required by applicable law or agreed to in writing, software
029distributed under the License is distributed on an "AS IS" BASIS,
030WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
031See the License for the specific language governing permissions and
032limitations under the License.
033*/
034
035package lucee.runtime.img.filter;import java.awt.image.BufferedImage;
036import java.awt.image.WritableRaster;
037
038import lucee.runtime.engine.ThreadLocalPageContext;
039import lucee.runtime.exp.FunctionException;
040import lucee.runtime.exp.PageException;
041import lucee.runtime.img.ImageUtil;
042import lucee.runtime.type.KeyImpl;
043import lucee.runtime.type.Struct;
044import lucee.runtime.type.util.CollectionUtil;
045
046/**
047 * An abstract superclass for point filters. The interface is the same as the old RGBImageFilter.
048 */
049public abstract class PointFilter extends AbstractBufferedImageOp  implements DynFiltering {
050
051        protected boolean canFilterIndexColorModel = false;
052
053    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
054        int width = src.getWidth();
055        int height = src.getHeight();
056                int type = src.getType();
057                WritableRaster srcRaster = src.getRaster();
058
059        if ( dst == null )
060            dst = createCompatibleDestImage( src, null );
061                WritableRaster dstRaster = dst.getRaster();
062
063        setDimensions( width, height);
064
065                int[] inPixels = new int[width];
066        for ( int y = 0; y < height; y++ ) {
067                        // We try to avoid calling getRGB on images as it causes them to become unmanaged, causing horrible performance problems.
068                        if ( type == BufferedImage.TYPE_INT_ARGB ) {
069                                srcRaster.getDataElements( 0, y, width, 1, inPixels );
070                                for ( int x = 0; x < width; x++ )
071                                        inPixels[x] = filterRGB( x, y, inPixels[x] );
072                                dstRaster.setDataElements( 0, y, width, 1, inPixels );
073                        } else {
074                                src.getRGB( 0, y, width, 1, inPixels, 0, width );
075                                for ( int x = 0; x < width; x++ )
076                                        inPixels[x] = filterRGB( x, y, inPixels[x] );
077                                dst.setRGB( 0, y, width, 1, inPixels, 0, width );
078                        }
079        }
080
081        return dst;
082    }
083
084        public void setDimensions(int width, int height) {
085        }
086
087        public abstract int filterRGB(int x, int y, int rgb);
088        public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
089                Object o;
090                if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
091                        int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
092                        setDimensions(dim[0],dim[1]);
093                }
094
095                // check for arguments not supported
096                if(parameters.size()>0) {
097                        throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "the parameter"+(parameters.size()>1?"s":"")+" ["+CollectionUtil.getKeyList(parameters,", ")+"] "+(parameters.size()>1?"are":"is")+" not allowed, only the following parameters are supported [Dimensions]");
098                }
099
100                return filter(src, dst);
101        }
102}