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.Graphics2D; 018 import java.awt.Image; 019 import java.awt.image.BufferedImage; 020 import java.awt.image.ColorModel; 021 022 import railo.runtime.exp.PageException; 023 import railo.runtime.img.ImageUtil; 024 import railo.runtime.op.Caster; 025 import railo.runtime.type.KeyImpl; 026 import railo.runtime.type.Struct; 027 028 029 /** 030 * Scales an image using the area-averaging algorithm, which can't be done with AffineTransformOp. 031 */ 032 public class ScaleFilter extends AbstractBufferedImageOp implements DynFiltering { 033 034 private int width; 035 private int height; 036 037 /** 038 * Construct a ScaleFilter. 039 */ 040 public ScaleFilter() { 041 this(32, 32); 042 } 043 044 /** 045 * @param width the width to set 046 */ 047 public void setWidth(int width) { 048 this.width = width; 049 } 050 051 /** 052 * @param height the height to set 053 */ 054 public void setHeight(int height) { 055 this.height = height; 056 } 057 058 /** 059 * Construct a ScaleFilter. 060 * @param width the width to scale to 061 * @param height the height to scale to 062 */ 063 public ScaleFilter( int width, int height ) { 064 this.width = width; 065 this.height = height; 066 } 067 068 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 069 int w = src.getWidth(); 070 int h = src.getHeight(); 071 072 if ( dst == null ) { 073 ColorModel dstCM = src.getColorModel(); 074 dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(w, h), dstCM.isAlphaPremultiplied(), null); 075 } 076 077 Image scaleImage = src.getScaledInstance( w, h, Image.SCALE_AREA_AVERAGING ); 078 Graphics2D g = dst.createGraphics(); 079 g.drawImage( src, 0, 0, width, height, null ); 080 g.dispose(); 081 082 return dst; 083 } 084 085 public String toString() { 086 return "Distort/Scale"; 087 } 088 089 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 090 int width=Caster.toIntValue(parameters.get(KeyImpl.init("Width"))); 091 int height=Caster.toIntValue(parameters.get(KeyImpl.init("Height"))); 092 setHeight(height); 093 setWidth(width); 094 095 dst=ImageUtil.createBufferedImage(dst,width,height); 096 097 098 return filter(src, dst); 099 } 100 }