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