Example usage for java.awt RenderingHints KEY_INTERPOLATION

List of usage examples for java.awt RenderingHints KEY_INTERPOLATION

Introduction

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

Prototype

Key KEY_INTERPOLATION

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

Click Source Link

Document

Interpolation hint key.

Usage

From source file:gr.iti.mklab.reveal.forensics.util.Util.java

public static BufferedImage scaleImage(BufferedImage image, int width, int height) {
    assert (width > 0 && height > 0);
    // create image of new size
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    //  ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(image, 0, 0, img.getWidth(), img.getHeight(), null);
    return img;//from   w w w  .  ja  v a2  s  .c om
}

From source file:ImageOpByRomain.java

/**
 * <p>/*ww w. j a v  a  2  s.co m*/
 * Returns a thumbnail of a source image. <code>newSize</code> defines the
 * length of the longest dimension of the thumbnail. The other dimension is
 * then computed according to the dimensions ratio of the original picture.
 * </p>
 * <p>
 * This method offers a good trade-off between speed and quality. The result
 * looks better than
 * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when the
 * new size is less than half the longest dimension of the source image, yet
 * the rendering speed is almost similar.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int)
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int, int)
 * @param image
 *            the source image
 * @param newSize
 *            the length of the largest dimension of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newSize</code> is larger than the largest dimension
 *             of <code>image</code> or &lt;= 0
 */
public static BufferedImage createThumbnail(BufferedImage image, int newSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    boolean isWidthGreater = width > height;

    if (isWidthGreater) {
        if (newSize >= width) {
            throw new IllegalArgumentException("newSize must be lower than" + " the image width");
        }
    } else if (newSize >= height) {
        throw new IllegalArgumentException("newSize must be lower than" + " the image height");
    }

    if (newSize <= 0) {
        throw new IllegalArgumentException("newSize must" + " be greater than 0");
    }

    float ratioWH = (float) width / (float) height;
    float ratioHW = (float) height / (float) width;

    BufferedImage thumb = image;

    do {
        if (isWidthGreater) {
            width /= 2;
            if (width < newSize) {
                width = newSize;
            }
            height = (int) (width / ratioWH);
        } else {
            height /= 2;
            if (height < newSize) {
                height = newSize;
            }
            width = (int) (height / ratioHW);
        }

        BufferedImage temp = createCompatibleImage(image, width, height);
        Graphics2D g2 = temp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
        g2.dispose();

        thumb = temp;
    } while (newSize != (isWidthGreater ? width : height));

    return thumb;
}

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  www.j  a v  a  2s .c  om

    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:ImageOpByRomain.java

/**
 * <p>//  ww  w .  j a  v a  2 s.c om
 * Returns a thumbnail of a source image.
 * </p>
 * <p>
 * This method offers a good trade-off between speed and quality. The result
 * looks better than
 * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when the
 * new size is less than half the longest dimension of the source image, yet
 * the rendering speed is almost similar.
 * </p>
 * 
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int)
 * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int)
 * @see #createThumbnail(java.awt.image.BufferedImage, int)
 * @param image
 *            the source image
 * @param newWidth
 *            the width of the thumbnail
 * @param newHeight
 *            the height of the thumbnail
 * @return a new compatible <code>BufferedImage</code> containing a
 *         thumbnail of <code>image</code>
 * @throws IllegalArgumentException
 *             if <code>newWidth</code> is larger than the width of
 *             <code>image</code> or if code>newHeight</code> is larger
 *             than the height of <code>image or if one the dimensions is not
 *             &gt; 0</code>
 */
public static BufferedImage createThumbnail(BufferedImage image, int newWidth, int newHeight) {
    int width = image.getWidth();
    int height = image.getHeight();

    if (newWidth >= width || newHeight >= height) {
        throw new IllegalArgumentException(
                "newWidth and newHeight cannot" + " be greater than the image" + " dimensions");
    } else if (newWidth <= 0 || newHeight <= 0) {
        throw new IllegalArgumentException("newWidth and newHeight must" + " be greater than 0");
    }

    BufferedImage thumb = image;

    do {
        if (width > newWidth) {
            width /= 2;
            if (width < newWidth) {
                width = newWidth;
            }
        }

        if (height > newHeight) {
            height /= 2;
            if (height < newHeight) {
                height = newHeight;
            }
        }

        BufferedImage temp = createCompatibleImage(image, width, height);
        Graphics2D g2 = temp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
        g2.dispose();

        thumb = temp;
    } while (width != newWidth || height != newHeight);

    return thumb;
}

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();/*  ww w. j  a v a  2s.  c  om*/
    return tempImg;
}

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

/**
  * Convenience method that returns a scaled instance of the
  * provided {@code BufferedImage}./*from  ww  w . j  ava 2s.  com*/
  *
  * @param img the original image to be scaled
  * @param targetWidth the desired width of the scaled instance,
  *    in pixels
  * @param targetHeight the desired height of the scaled instance,
  *    in pixels
  * @param hint one of the rendering hints that corresponds to
  *    {@code RenderingHints.KEY_INTERPOLATION} (e.g.
  *    {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
  *    {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
  *    {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
  * @param higherQuality if true, this method will use a multi-step
  *    scaling technique that provides higher quality than the usual
  *    one-step technique (only useful in downscaling cases, where
  *    {@code targetWidth} or {@code targetHeight} is
  *    smaller than the original dimensions, and generally only when
  *    the {@code BILINEAR} hint is specified)
  * @return a scaled version of the original {@code BufferedImage}
  */
private BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    // functionality not supported in java 1.4
    int transparency = Transparency.OPAQUE;
    try {
        transparency = img.getTransparency();
    } catch (Throwable t) {
    }
    int type = (transparency == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;

    BufferedImage ret = img;
    int w, h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:net.geoprism.dashboard.DashboardMap.java

private BufferedImage getLegendTitleImage(DashboardLayer layer) {

    FontMetrics fm;//  w  ww .j a v a2s .  c  o  m
    int textWidth;
    int textHeight;
    int textBoxHorizontalPadding = 4;
    int textBoxVerticalPadding = 4;
    int borderWidth = 2;
    int paddedTitleHeight;
    int paddedTitleWidth;
    int titleLeftPadding = textBoxHorizontalPadding;
    BufferedImage newLegendTitleBase;
    Graphics2D newLegendTitleBaseGraphic = null;

    try {
        // Build the Font object
        Font titleFont = new Font(layer.getName(), Font.BOLD, 14);

        // Build variables for base legend graphic construction
        try {
            newLegendTitleBase = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
            newLegendTitleBaseGraphic = newLegendTitleBase.createGraphics();

            newLegendTitleBaseGraphic.setFont(titleFont);

            fm = newLegendTitleBaseGraphic.getFontMetrics();
            textHeight = fm.getHeight();
            textWidth = fm.stringWidth(layer.getName());

            paddedTitleWidth = textWidth + (textBoxHorizontalPadding * 2) + (borderWidth * 2);

            paddedTitleHeight = textHeight + (textBoxVerticalPadding * 2) + (borderWidth * 2);
        } finally {
            // dispose of temporary graphics context
            if (newLegendTitleBaseGraphic != null) {
                newLegendTitleBaseGraphic.dispose();
            }
        }

        titleLeftPadding = ((paddedTitleWidth / 2)
                - ((textWidth + (textBoxHorizontalPadding * 2) + (borderWidth * 2)) / 2))
                + textBoxHorizontalPadding;

        newLegendTitleBase = new BufferedImage(paddedTitleWidth, paddedTitleHeight,
                BufferedImage.TYPE_INT_ARGB);
        newLegendTitleBaseGraphic = newLegendTitleBase.createGraphics();
        newLegendTitleBaseGraphic.drawImage(newLegendTitleBase, 0, 0, null);

        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,
                RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_DITHERING,
                RenderingHints.VALUE_DITHER_ENABLE);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
                RenderingHints.VALUE_STROKE_PURE);
        newLegendTitleBaseGraphic.setFont(titleFont);

        // draw title text
        fm = newLegendTitleBaseGraphic.getFontMetrics();
        newLegendTitleBaseGraphic.setColor(Color.WHITE);
        newLegendTitleBaseGraphic.drawString(layer.getName(), titleLeftPadding,
                fm.getAscent() + textBoxVerticalPadding);

        newLegendTitleBaseGraphic.drawImage(newLegendTitleBase, 0, 0, null);
    } finally {
        if (newLegendTitleBaseGraphic != null) {
            newLegendTitleBaseGraphic.dispose();
        }
    }

    return newLegendTitleBase;
}

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

public void resizeImage(int width, int height, int interpolation) throws ExpressionException {
    Object ip;//from   w ww .j a  v a 2s  . com
    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);

}

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

public void rotate(float x, float y, float angle, int interpolation) throws ExpressionException {
    if (x == -1)//from w w  w. j  a va2s.  co m
        x = (float) getWidth() / 2;
    if (y == -1)
        y = (float) getHeight() / 2;

    angle = (float) Math.toRadians(angle);
    ColorModel cmSource = image().getColorModel();

    if (cmSource instanceof IndexColorModel && cmSource.hasAlpha() && !cmSource.isAlphaPremultiplied()) {
        image(PaletteToARGB(image()));
        cmSource = image().getColorModel();
    }

    BufferedImage alpha = null;
    if (cmSource.hasAlpha() && !cmSource.isAlphaPremultiplied()) {
        alpha = getAlpha(image());
        image(removeAlpha(image()));
    }

    Interpolation interp = Interpolation.getInstance(0);
    if (INTERPOLATION_BICUBIC == interpolation)
        interp = Interpolation.getInstance(1);
    else if (INTERPOLATION_BILINEAR == interpolation)
        interp = Interpolation.getInstance(2);

    if (alpha != null) {
        ParameterBlock params = new ParameterBlock();
        params.addSource(alpha);
        params.add(x);
        params.add(y);
        params.add(angle);
        params.add(interp);
        params.add(new double[] { 0.0 });
        RenderingHints hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
                (RenderingHints.VALUE_INTERPOLATION_BICUBIC));
        hints.add(new RenderingHints(JAI.KEY_BORDER_EXTENDER,
                new BorderExtenderConstant(new double[] { 255.0 })));
        hints.add(new RenderingHints(JAI.KEY_REPLACE_INDEX_COLOR_MODEL, Boolean.TRUE));
        alpha = JAI.create("rotate", params, hints).getAsBufferedImage();
    }

    ParameterBlock params = new ParameterBlock();
    params.addSource(image());
    params.add(x);
    params.add(y);
    params.add(angle);
    params.add(interp);
    params.add(new double[] { 0.0 });
    BorderExtender extender = new BorderExtenderConstant(new double[] { 0.0 });
    RenderingHints hints = new RenderingHints(JAI.KEY_BORDER_EXTENDER, extender);
    hints.add(new RenderingHints(JAI.KEY_REPLACE_INDEX_COLOR_MODEL, Boolean.TRUE));
    image(JAI.create("rotate", params, hints).getAsBufferedImage());
    if (alpha != null)
        image(addAlpha(image(), alpha, 0, 0));
}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * Creates rendering hints for Text.//from ww w.  jav a2s  . c o m
 */
public static RenderingHints createTextRenderingHints() {
    RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    Object value = RenderingHints.VALUE_TEXT_ANTIALIAS_ON;
    try {
        Field declaredField = RenderingHints.class.getDeclaredField("VALUE_TEXT_ANTIALIAS_LCD_HRGB");
        value = declaredField.get(null);

    } catch (Exception e) {
        // do nothing
    }
    renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, value);
    return renderingHints;
}