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