Example usage for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR

List of usage examples for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR

Introduction

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

Prototype

Object VALUE_INTERPOLATION_BILINEAR

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

Click Source Link

Document

Interpolation hint value -- the color samples of the 4 nearest neighboring integer coordinate samples in the image are interpolated linearly to produce a color sample.

Usage

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  w  w  w. j a  v a2s. com*/
    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:ImageOpByRomain.java

/**
 * <p>// w ww. j av a 2  s.c o 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:ImageOpByRomain.java

/**
 * <p>// w w  w.j a  v a 2  s  . c  o  m
 * 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:net.geoprism.dashboard.DashboardMap.java

private BufferedImage getLegendTitleImage(DashboardLayer layer) {

    FontMetrics fm;/*from   ww w .  j  a  v  a2 s.  co 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 resizeImage2(int width, int height) throws ExpressionException {
    image(getScaledInstance(image(), width, height, RenderingHints.VALUE_INTERPOLATION_BILINEAR, false));
}

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  2  s.  co  m
    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:FirstStatMain.java

private Image scaledImage(byte[] img, int w, int h) {
    BufferedImage sizechange = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    try {/*  w  ww . j  a v  a2 s.c  om*/
        Graphics2D g2d = sizechange.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        ByteArrayInputStream input = new ByteArrayInputStream(img);
        BufferedImage bfg = ImageIO.read(input);
        g2d.drawImage(bfg, 0, 0, w, h, null);
        g2d.dispose();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(rootPane, e);

    }
    return sizechange;
}

From source file:net.sourceforge.subsonic.controller.CoverArtControllerEx.java

public static BufferedImage scale(BufferedImage image, int width, int height) {
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage thumb = image;//www .j  av a  2 s.  c o  m

    // For optimal results, use step by step bilinear resampling - halfing the size at each step.
    do {
        w /= 2;
        h /= 2;
        if (w < width) {
            w = width;
        }
        if (h < height) {
            h = height;
        }

        double thumbRatio = (double) width / (double) height;
        double aspectRatio = (double) w / (double) h;

        //            LOG.debug("## thumbsRatio: " + thumbRatio);
        //            LOG.debug("## aspectRatio: " + aspectRatio);

        if (thumbRatio < aspectRatio) {
            h = (int) (w / aspectRatio);
        } else {
            w = (int) (h * aspectRatio);
        }

        BufferedImage temp = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = temp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(thumb, 0, 0, w, h, null);
        g2.dispose();

        thumb = temp;
    } while (w != width);

    //FIXME: check
    if (thumb.getHeight() > thumb.getWidth()) {
        thumb = thumb.getSubimage(0, 0, thumb.getWidth(), thumb.getWidth());
    }
    return thumb;
}

From source file:nz.govt.natlib.ndha.manualdeposit.dialogs.About.java

private void paintImage() {
    if (theLogo != null) {
        int width = this.getWidth();
        int height = this.getHeight();
        BufferedImage thumbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(theLogo, 0, 0, width, height, null);
        imgIndigo.setImage(thumbImage);/*ww  w . j  av a  2s .  c  o  m*/
        imgIndigo.setBounds(0, 0, width, height);
        repaint();
    }
}

From source file:org.apache.batchee.tools.maven.DiagramMojo.java

private void saveView(final Dimension currentSize, final Dimension desiredSize, final String name,
        final VisualizationViewer<Node, Edge> viewer) throws MojoExecutionException {
    BufferedImage bi = new BufferedImage(currentSize.width, currentSize.height, BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g = bi.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    final boolean db = viewer.isDoubleBuffered();
    viewer.setDoubleBuffered(false);//  www  .  ja v a  2 s. c o  m
    viewer.paint(g);
    viewer.setDoubleBuffered(db);
    if (!currentSize.equals(desiredSize)) {
        final double xFactor = desiredSize.width * 1. / currentSize.width;
        final double yFactor = desiredSize.height * 1. / currentSize.height;
        final double factor = Math.min(xFactor, yFactor);
        getLog().info("optimal size is (" + currentSize.width + ", " + currentSize.height + ")");
        getLog().info("scaling with a factor of " + factor);

        final AffineTransform tx = new AffineTransform();
        tx.scale(factor, factor);
        final AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        BufferedImage biNew = new BufferedImage((int) (bi.getWidth() * factor), (int) (bi.getHeight() * factor),
                bi.getType());
        bi = op.filter(bi, biNew);
    }
    g.dispose();

    OutputStream os = null;
    try {
        final File file = new File(output, (outputFileName != null ? outputFileName : name) + "." + format);
        os = new FileOutputStream(file);
        if (!ImageIO.write(bi, format, os)) {
            throw new MojoExecutionException("can't save picture " + name + "." + format);
        }
        getLog().info("Saved " + file.getAbsolutePath());
    } catch (final IOException e) {
        throw new MojoExecutionException("can't save the diagram", e);
    } finally {
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (final IOException e) {
                // no-op
            }
        }
    }
}