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