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 bi-cubic interpolation, which can't be done with AffineTransformOp.
024 */
025public class BicubicScaleFilter extends AbstractBufferedImageOp {
026
027        private int width;
028        private int height;
029
030        /**
031     * Construct a BicubicScaleFilter which resizes to 32x32 pixels.
032     */
033    public BicubicScaleFilter() {
034                this(32, 32);
035        }
036
037        /**
038         * Constructor for a filter which scales the input image to the given width and height using bicubic interpolation.
039         * Unfortunately, it appears that bicubic actually looks worse than bilinear interpolation on most Java implementations,
040         * but you can be the judge.
041     * @param width the width of the output image
042     * @param height the height of the output image
043         */
044        public BicubicScaleFilter( int width, int height ) {
045                this.width = width;
046                this.height = height;
047        }
048
049    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
050        int w = src.getWidth();
051        int h = src.getHeight();
052
053                if ( dst == null ) {
054                        ColorModel dstCM = src.getColorModel();
055                        dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(width, height), dstCM.isAlphaPremultiplied(), null);
056                }
057
058                Graphics2D g = dst.createGraphics();
059                g.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC );
060                g.drawImage( src, 0, 0, width, height, null );
061                g.dispose();
062
063        return dst;
064    }
065
066        public String toString() {
067                return "Distort/Bicubic Scale";
068        }
069
070}