Example usage for java.awt.image BufferedImageOp filter

List of usage examples for java.awt.image BufferedImageOp filter

Introduction

In this page you can find the example usage for java.awt.image BufferedImageOp filter.

Prototype

public BufferedImage filter(BufferedImage src, BufferedImage dest);

Source Link

Document

Performs a single-input/single-output operation on a BufferedImage .

Usage

From source file:ucar.unidata.idv.ui.ImageGenerator.java

/**
 * Scale the given {@code source} {@link Image}.
 *
 * @param source Source image./*from ww  w  . jav  a2  s .c  om*/
 * @param width New width.
 * @param height New height.
 *
 * @return Scaled {@code source} image (uses bilinear interpolation).
 */
public static BufferedImage getScaledImage(Image source, int width, int height) {
    // convert the given Image into a BufferedImage if needed--makes things a
    // little easier.
    BufferedImage image;
    if (source instanceof BufferedImage) {
        image = (BufferedImage) source;
    } else {
        image = new BufferedImage(source.getWidth(null), source.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(source, 0, 0, null);
        g.dispose();
    }

    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();
    double scaleX = (double) width / imageWidth;
    double scaleY = (double) height / imageHeight;
    AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
    BufferedImageOp op = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
    return op.filter(image, new BufferedImage(width, height, image.getType()));
}