Example usage for java.awt RenderingHints VALUE_INTERPOLATION_BICUBIC

List of usage examples for java.awt RenderingHints VALUE_INTERPOLATION_BICUBIC

Introduction

In this page you can find the example usage for java.awt RenderingHints VALUE_INTERPOLATION_BICUBIC.

Prototype

Object VALUE_INTERPOLATION_BICUBIC

To view the source code for java.awt RenderingHints VALUE_INTERPOLATION_BICUBIC.

Click Source Link

Document

Interpolation hint value -- the color samples of 9 nearby integer coordinate samples in the image are interpolated using a cubic function in both X and Y to produce a color sample.

Usage

From source file:com.sketchy.image.ImageProcessingThread.java

public static BufferedImage rotateImage(BufferedImage image, RotateOption rotateOption) {

    if (rotateOption == RotateOption.ROTATE_NONE)
        return image;

    int degrees = 0;
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    BufferedImage rotatedImage = null;
    if (rotateOption == RotateOption.ROTATE_90) {
        degrees = 90;/*from  w  w  w.j  a v a  2  s.  c  om*/
        rotatedImage = new BufferedImage(imageHeight, imageWidth, BufferedImage.TYPE_INT_ARGB);
    } else if (rotateOption == RotateOption.ROTATE_180) {
        degrees = 180;
        rotatedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
    } else if (rotateOption == RotateOption.ROTATE_270) {
        degrees = 270;
        rotatedImage = new BufferedImage(imageHeight, imageWidth, BufferedImage.TYPE_INT_ARGB);
    }

    Graphics2D g = rotatedImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    g.rotate(Math.toRadians(degrees), 0, 0);

    if (degrees == 90) {
        g.drawImage(image, null, 0, -imageHeight);
    } else if (degrees == 180) {
        g.drawImage(image, null, -imageWidth, -imageHeight);
    } else if (degrees == 270) {
        g.drawImage(image, null, -imageWidth, 0);
    }
    g.dispose();
    return rotatedImage;
}

From source file:com.fluidops.iwb.deepzoom.DZConvert.java

/**
 * Returns resized image//from  w  w w .jav  a  2s .  c om
 * NB - useful reference on high quality image resizing can be found here:
 *   http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
 * @param width the required width
 * @param height the frequired height
 * @param img the image to be resized
 */
private static BufferedImage resizeImage(BufferedImage img, double width, double height) {
    int w = (int) width;
    int h = (int) height;
    BufferedImage result = new BufferedImage(w, h, getType(img));
    Graphics2D g = result.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.drawImage(img, 0, 0, w, h, 0, 0, img.getWidth(), img.getHeight(), null);
    return result;
}

From source file:com.simiacryptus.util.Util.java

/**
 * Resize buffered image.// w  w w  .j  av a  2 s.  c  o m
 *
 * @param image the image
 * @return the buffered image
 */
@Nullable
public static BufferedImage resize(@Nullable final BufferedImage image) {
    if (null == image)
        return image;
    final int width = Math.min(image.getWidth(), 800);
    if (width == image.getWidth())
        return image;
    final int height = image.getHeight() * width / image.getWidth();
    @javax.annotation.Nonnull
    final BufferedImage rerender = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics gfx = rerender.getGraphics();
    @javax.annotation.Nonnull
    final RenderingHints hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    ((Graphics2D) gfx).setRenderingHints(hints);
    gfx.drawImage(image, 0, 0, rerender.getWidth(), rerender.getHeight(), null);
    return rerender;
}

From source file:com.sketchy.image.ImageProcessingThread.java

public static BufferedImage flipImage(BufferedImage image, FlipOption flipOption) {

    if (flipOption == FlipOption.FLIP_NONE)
        return image;

    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    BufferedImage flippedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = flippedImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    int startX = 0;
    int startY = 0;
    int endX = imageWidth;
    int endY = imageHeight;
    if ((flipOption == FlipOption.FLIP_HORIZONTAL) || (flipOption == FlipOption.FLIP_BOTH)) {
        startX = imageWidth;/*from   w w w .  ja v a  2  s  . c om*/
        endX = -imageWidth;
    }
    if ((flipOption == FlipOption.FLIP_VERTICAL) || (flipOption == FlipOption.FLIP_BOTH)) {
        startY = imageHeight;
        endY = -imageHeight;
    }

    g.drawImage(image, startX, startY, endX, endY, null);
    g.dispose();
    return flippedImage;
}

From source file:com.funambol.foundation.util.MediaUtils.java

/**
 * Rotates given buffered image by given amount of degree.
 * The valid degree values are 0, 90, 180, 270.
 * If the image is a jpg, the rotation is lossless, exif data are preserved
 * and image size is almost the same./* ww  w.  j a va 2  s . c  om*/
 *
 * @param bufImage the buffered image
 * @param degree amount of degree to apply
 * @return a buffered image containing rotated image data
 * @throws PicturesException if amount of degree is invalid or if an
 *         IOException occurs
 */
private static BufferedImage rotateImage(BufferedImage bufImage, int degree)
        throws FileDataObjecyUtilsException {

    degree = degree % 360;
    int h;
    int w;

    switch (degree) {
    case 0:
    case 180:
        h = bufImage.getHeight();
        w = bufImage.getWidth();
        break;
    case 90:
    case 270:
        h = bufImage.getWidth();
        w = bufImage.getHeight();
        break;
    default:
        throw new FileDataObjecyUtilsException(
                "Error rotating image since the '" + degree + "' degree value is unsupported");
    }

    BufferedImage out = null;

    int bufImageType = bufImage.getType();
    if (BufferedImage.TYPE_BYTE_INDEXED == bufImageType || BufferedImage.TYPE_BYTE_BINARY == bufImageType) {

        IndexColorModel model = (IndexColorModel) bufImage.getColorModel();
        out = new BufferedImage(w, h, bufImage.getType(), model);

    } else if (BufferedImage.TYPE_CUSTOM == bufImageType) {

        // we don't know what type of image it can be

        // there's a bug in some VM that cause some PNG images to have 
        // type custom: this should take care of this issue

        //check if we need to have alpha channel
        boolean alpha = bufImage.getTransparency() != BufferedImage.OPAQUE;

        if (alpha) {
            // TYPE_INT_ARGB_PRE gives you smaller output images
            // than TYPE_INT_ARGB
            out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
        } else {
            out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        }

    } else {

        out = new BufferedImage(w, h, bufImage.getType());
    }

    Graphics2D g2d = out.createGraphics();

    Map renderingHints = new HashMap();

    renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    g2d.setRenderingHints(renderingHints);
    g2d.rotate(Math.toRadians(degree));

    switch (degree) {
    case 90:
        g2d.translate(0, -w);
        break;
    case 180:
        g2d.translate(-w, -h);
        break;
    case 270:
        g2d.translate(-h, 0);
        break;
    }

    g2d.drawImage(bufImage, null, 0, 0);
    g2d.dispose();

    return out;
}

From source file:lucee.runtime.img.Image.java

public void paste(Image topImage, int x, int y) throws ExpressionException {
    RenderingHints interp = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    BorderExtender extender = BorderExtender.createInstance(1);
    Graphics2D g = getGraphics();
    g.addRenderingHints(new RenderingHints(JAI.KEY_BORDER_EXTENDER, extender));
    g.drawImage(topImage.image(), (new AffineTransformOp(AffineTransform.getTranslateInstance(x, y), interp)),
            0, 0);//  w  ww  .j a v a  2s. com

}

From source file:lucee.runtime.img.Image.java

public void shear(float shear, ShearDir direction, Object interpolation) throws ExpressionException {
    ParameterBlock params = new ParameterBlock();
    params.addSource(image());/*from   www.  j  a v a2 s .c o  m*/
    params.add(shear);
    params.add(direction);
    params.add(0.0F);
    params.add(0.0F);
    RenderingHints hints = null;

    if (interpolation == RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)
        params.add(Interpolation.getInstance(0));
    else if (interpolation == RenderingHints.VALUE_INTERPOLATION_BILINEAR) {
        params.add(Interpolation.getInstance(1));
        BorderExtender extender = BorderExtender.createInstance(1);
        hints = new RenderingHints(JAI.KEY_BORDER_EXTENDER, extender);
    } else if (interpolation == RenderingHints.VALUE_INTERPOLATION_BICUBIC) {
        params.add(Interpolation.getInstance(2));
        BorderExtender extender = BorderExtender.createInstance(1);
        hints = new RenderingHints(JAI.KEY_BORDER_EXTENDER, extender);
    }
    // TODO
    Color bg = getGraphics().getBackground();
    params.add(new double[] { bg.getRed(), bg.getGreen(), bg.getBlue() });
    image(JAI.create("shear", params, hints).getAsBufferedImage());
}

From source file:au.com.gaiaresources.bdrs.controller.test.TestDataCreator.java

private byte[] getAndScaleImageData(int targetWidth, int targetHeight, File file) throws IOException {
    if (file == null) {
        return null;
    }//from   w w w .j av a 2s .  c o m

    BufferedImage sourceImage = ImageIO.read(file);
    BufferedImage targetImage;
    if (targetWidth > -1 && targetHeight > -1) {
        // Resize the image as required to fit the space
        targetImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2_scaled = targetImage.createGraphics();
        // Better scaling
        g2_scaled.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);

        g2_scaled.setBackground(Color.WHITE);
        g2_scaled.clearRect(0, 0, targetWidth, targetHeight);

        int sourceWidth = sourceImage.getWidth();
        int sourceHeight = sourceImage.getHeight();

        double widthRatio = (double) targetWidth / (double) sourceWidth;
        double heightRatio = (double) targetHeight / (double) targetHeight;

        if (heightRatio > widthRatio) {
            int scaledHeight = (int) Math.round(widthRatio * sourceHeight);
            g2_scaled.drawImage(sourceImage, 0, (targetImage.getHeight() - scaledHeight) / 2,
                    targetImage.getWidth(), scaledHeight, g2_scaled.getBackground(), null);
        } else {
            int scaledWidth = (int) Math.round(heightRatio * sourceWidth);
            g2_scaled.drawImage(sourceImage, (targetImage.getWidth() - scaledWidth) / 2, 0, scaledWidth,
                    targetImage.getHeight(), new Color(0, 0, 0, 255), null);
        }
    } else {
        targetImage = sourceImage;
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream(targetWidth * targetHeight);
    ImageIO.write(targetImage, "png", baos);
    baos.flush();
    byte[] data = baos.toByteArray();
    baos.close();

    return data;
}

From source file:de.tor.tribes.ui.panels.MinimapPanel.java

protected BufferedImage getScaledImage(int width, int height) {
    BufferedImage tempImg = ImageUtils.createCompatibleBufferedImage(width, height,
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = (Graphics2D) tempImg.getGraphics();
    g2d.setColor(new Color(35, 125, 0));
    g2d.fillRect(0, 0, tempImg.getWidth(null), tempImg.getHeight(null));
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.drawImage(mBuffer, 0, 0, width, height, 0, 0, mBuffer.getWidth(), mBuffer.getHeight(), null);
    g2d.dispose();//from www . jav a2  s  . c o m
    return tempImg;
}

From source file:lucee.runtime.img.Image.java

public void resizeImage(int width, int height, int interpolation) throws ExpressionException {
    Object ip;// www.  j a  va 2 s . c  om
    if (interpolation == IPC_NEAREST)
        ip = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
    else if (interpolation == IPC_BICUBIC)
        ip = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
    else if (interpolation == IPC_BILINEAR)
        ip = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
    else
        throw new ExpressionException("invalid interpoltion definition");

    BufferedImage dst = new BufferedImage(width, height, image().getType());
    Graphics2D graphics = dst.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, ip);
    graphics.drawImage(image(), 0, 0, width, height, null);
    graphics.dispose();
    image(dst);

}