001/* 002Copyright 2006 Jerry Huxtable 003 004Licensed under the Apache License, Version 2.0 (the "License"); 005you may not use this file except in compliance with the License. 006You may obtain a copy of the License at 007 008 http://www.apache.org/licenses/LICENSE-2.0 009 010Unless required by applicable law or agreed to in writing, software 011distributed under the License is distributed on an "AS IS" BASIS, 012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013See the License for the specific language governing permissions and 014limitations under the License. 015*/ 016 017package com.jhlabs.image; 018 019import java.awt.*; 020import java.awt.image.*; 021 022/** 023 * Scales an image using the area-averaging algorithm, which can't be done with AffineTransformOp. 024 */ 025public class ScaleFilter extends AbstractBufferedImageOp { 026 027 private int width; 028 private int height; 029 030 /** 031 * Construct a ScaleFilter. 032 */ 033 public ScaleFilter() { 034 this(32, 32); 035 } 036 037 /** 038 * Construct a ScaleFilter. 039 * @param width the width to scale to 040 * @param height the height to scale to 041 */ 042 public ScaleFilter( int width, int height ) { 043 this.width = width; 044 this.height = height; 045 } 046 047 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 048 if ( dst == null ) { 049 ColorModel dstCM = src.getColorModel(); 050 dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster( width, height ), dstCM.isAlphaPremultiplied(), null); 051 } 052 053 Image scaleImage = src.getScaledInstance( width, height, Image.SCALE_AREA_AVERAGING ); 054 Graphics2D g = dst.createGraphics(); 055 g.drawImage( scaleImage, 0, 0, width, height, null ); 056 g.dispose(); 057 058 return dst; 059 } 060 061 public String toString() { 062 return "Distort/Scale"; 063 } 064 065}