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