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.Composite; 036import java.awt.Graphics2D; 037import java.awt.RenderingHints; 038import java.awt.geom.AffineTransform; 039import java.awt.image.BufferedImage; 040 041import lucee.runtime.engine.ThreadLocalPageContext; 042import lucee.runtime.exp.FunctionException; 043import lucee.runtime.exp.PageException; 044import lucee.runtime.img.ImageUtil; 045import lucee.runtime.type.KeyImpl; 046import lucee.runtime.type.Struct; 047import lucee.runtime.type.util.CollectionUtil; 048/** 049 * A filter which composites two images together with an optional transform. 050 */ 051public class CompositeFilter extends AbstractBufferedImageOp implements DynFiltering { 052 053 private Composite composite; 054 private AffineTransform transform; 055 056 /** 057 * Construct a CompositeFilter. 058 */ 059 public CompositeFilter() { 060 } 061 062 /** 063 * Construct a CompositeFilter. 064 * @param composite the composite to use 065 */ 066 public CompositeFilter( Composite composite ) { 067 this.composite = composite; 068 } 069 070 /** 071 * Construct a CompositeFilter. 072 * @param composite the composite to use 073 * @param transform a transform for the composited image 074 */ 075 public CompositeFilter( Composite composite, AffineTransform transform ) { 076 this.composite = composite; 077 this.transform = transform; 078 } 079 080 /** 081 * Set the composite. 082 * @param composite the composite to use 083 * @see #getComposite 084 */ 085 public void setComposite( Composite composite ) { 086 this.composite = composite; 087 } 088 089 /** 090 * Get the composite. 091 * @return the composite to use 092 * @see #setComposite 093 */ 094 public Composite getComposite() { 095 return composite; 096 } 097 098 /** 099 * Set the transform. 100 * @param transform the transform to use 101 * @see #getTransform 102 */ 103 public void setTransform( AffineTransform transform ) { 104 this.transform = transform; 105 } 106 107 /** 108 * Get the transform. 109 * @return the transform to use 110 * @see #setTransform 111 */ 112 public AffineTransform getTransform() { 113 return transform; 114 } 115 116 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 117 if ( dst == null ) 118 dst = createCompatibleDestImage( src, null ); 119 120 Graphics2D g = dst.createGraphics(); 121 g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); 122 g.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR ); 123 g.setComposite( composite ); 124 g.drawRenderedImage( src, transform ); 125 g.dispose(); 126 return dst; 127 } 128 129 public String toString() { 130 return "Composite"; 131 } 132 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 133 Object o; 134 if((o=parameters.removeEL(KeyImpl.init("Transform")))!=null)setTransform(ImageFilterUtil.toAffineTransform(o,"Transform")); 135 if((o=parameters.removeEL(KeyImpl.init("Composite")))!=null)setComposite(ImageFilterUtil.toComposite(o,"Composite")); 136 137 // check for arguments not supported 138 if(parameters.size()>0) { 139 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 [Transform, Composite]"); 140 } 141 142 return filter(src, dst); 143 } 144}