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

private Image scaledImage(byte[] img, int w, int h) {
    BufferedImage sizechange = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    try {// w  w  w . j a  v  a 2  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;//from   w w w . j a v  a 2  s  . co  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:nl.b3p.imagetool.ImageTool.java

/**
 * Combines GIF, TIFF or PNG images. Combining these images is different
 * from the JPG image types since these has to use an other imageType:
 * BufferedImage.TYPE_INT_ARGB_PRE./*from w w  w  .  ja  v a 2s .  com*/
 *
 * @param images the referenced Images
 * @param width the width of the result image (or null if the width of the
 * first image must be used)
 * @param height the height of the result image (or null if the width of the
 * first image must be used)
 *
 * @return BufferedImage
 */
private static BufferedImage combineOtherImages(List<ReferencedImage> images, Integer width, Integer height)
        throws Exception {
    if (images != null && images.get(0) != null) {
        BufferedImage bi = images.get(0).getImage();
        //if no height / width use the height/widht of the first image.
        if (height == null) {
            height = bi.getHeight();
        }
        if (width == null) {
            width = bi.getWidth();
        }
    }

    BufferedImage newBufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D gbi = newBufIm.createGraphics();
    gbi.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    if (images != null) {
        for (ReferencedImage image : images) {
            drawImage(gbi, image);
            image.dispose();
        }
    }

    return newBufIm;
}

From source file:nl.b3p.viewer.image.ImageTool.java

/** Combines GIF, TIFF or PNG images. Combining these images is different from the JPG image types since these
 * has to use an other imageType: BufferedImage.TYPE_INT_ARGB_PRE.
 *
 * @param images the referenced Images/*from   w  ww.jav  a 2s .co m*/
 * @param width the width of the result image (or null if the width of the first image must be used)
 * @param height the height of the result image (or null if the width of the first image must be used)
 *
 * @return BufferedImage
 */
private static BufferedImage combineOtherImages(List<ReferencedImage> images, Integer width, Integer height) {
    if (images.get(0) != null) {
        BufferedImage bi = images.get(0).getImage();
        //if no height / width use the height/widht of the first image.
        if (height == null) {
            height = bi.getHeight();
        }
        if (width == null) {
            width = bi.getWidth();
        }
    }

    BufferedImage newBufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D gbi = newBufIm.createGraphics();
    gbi.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    for (int i = 0; i < images.size(); i++) {
        ReferencedImage image = images.get(i);
        drawImage(gbi, image);
    }
    return newBufIm;
}

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.  ja  va  2  s  .  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);/*from   ww w .ja v a  2s  .  com*/
    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
            }
        }
    }
}

From source file:org.apache.batchee.tools.maven.doc.DiagramGenerator.java

private void saveView(final Dimension currentSize, final Dimension desiredSize, final String name,
        final VisualizationViewer<Node, Edge> viewer) {
    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);//w  w w  .j  a  v  a 2 s.c om
    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);
        info("optimal size is (" + currentSize.width + ", " + currentSize.height + ")");
        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 IllegalStateException("can't save picture " + name + "." + format);
        }
        info("Saved " + file.getAbsolutePath());
    } catch (final IOException e) {
        throw new IllegalStateException("can't save the diagram", e);
    } finally {
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (final IOException e) {
                // no-op
            }
        }
    }
}

From source file:org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.java

/**
 * High-quality image scaling./* w ww . ja  v  a  2s .c  o m*/
 */
private BufferedImage scaleImage(BufferedImage image, int width, int height) {
    BufferedImage image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image2.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawImage(image, 0, 0, width, height, 0, 0, image.getWidth(), image.getHeight(), null);
    g.dispose();
    return image2;
}

From source file:org.apache.pdfbox.rendering.PageDrawer.java

/**
 * Sets high-quality rendering hints on the current Graphics2D.
 *//*from  w  ww .  ja  v a2s .  c o m*/
private void setRenderingHints() {
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}

From source file:org.apache.pdfbox.rendering.PageDrawer.java

@Override
public void drawImage(PDImage pdImage) throws IOException {
    Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
    AffineTransform at = ctm.createAffineTransform();

    if (!pdImage.getInterpolate()) {
        boolean isScaledUp = pdImage.getWidth() < Math.round(at.getScaleX())
                || pdImage.getHeight() < Math.round(at.getScaleY());

        // if the image is scaled down, we use smooth interpolation, eg PDFBOX-2364
        // only when scaled up do we use nearest neighbour, eg PDFBOX-2302 / mori-cvpr01.pdf
        // stencils are excluded from this rule (see survey.pdf)
        if (isScaledUp || pdImage.isStencil()) {
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        }//from w w  w  .  j  a  v a2  s .co m
    }

    if (pdImage.isStencil()) {
        // fill the image with paint
        BufferedImage image = pdImage.getStencilImage(getNonStrokingPaint());

        // draw the image
        drawBufferedImage(image, at);
    } else {
        // draw the image
        drawBufferedImage(pdImage.getImage(), at);
    }

    if (!pdImage.getInterpolate()) {
        // JDK 1.7 has a bug where rendering hints are reset by the above call to
        // the setRenderingHint method, so we re-set all hints, see PDFBOX-2302
        setRenderingHints();
    }
}