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:lucee.runtime.img.Image.java

public void rotate(float x, float y, float angle, int interpolation) throws ExpressionException {
    if (x == -1)//  w ww  .j  av  a2s.c o 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 .j  a v  a2 s  .  com
 */
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;
}

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.// w w  w.  ja va 2  s.c  o m
 *
 * @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 w w .  j  a  v a  2s  . c  o 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:org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.java

/**
 * High-quality image scaling./*from ww w  .j av a  2 s . 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.
 *///w  w  w.  j  a  v  a2s  .  co  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.bigbluebuttonproject.fileupload.document.impl.FileSystemSlideManager.java

/**
 * This method create thumbImage of the image file given and save it in outFile.
 * Compression quality is also given. thumbBounds is used for calculating the size of the thumb.
 * /*w  w  w .j a  va 2  s . c  o m*/
 * @param infile slide image to create thumb
 * @param outfile output thumb file
 * @param compressionQuality the compression quality
 * @param thumbBounds the thumb bounds
 * 
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void resizeImage(File infile, File outfile, float compressionQuality, int thumbBounds)
        throws IOException {
    // Retrieve jpg image to be resized
    Image image = ImageIO.read(infile);

    // get original image size for thumb size calculation
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);

    float thumbRatio = (float) thumbBounds / Math.max(imageWidth, imageHeight);

    int thumbWidth = (int) (imageWidth * thumbRatio);
    int thumbHeight = (int) (imageHeight * thumbRatio);

    // draw original image to thumbnail image object and
    // scale it to the new size on-the-fly
    BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

    // Find a jpeg writer
    ImageWriter writer = null;
    Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg");
    if (iter.hasNext()) {
        writer = (ImageWriter) iter.next();
    }

    ImageWriteParam iwp = writer.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(compressionQuality);

    // Prepare output file
    ImageOutputStream ios = ImageIO.createImageOutputStream(outfile);
    writer.setOutput(ios);
    // write to the thumb image 
    writer.write(thumbImage);

    // Cleanup
    ios.flush();
    writer.dispose();
    ios.close();
}

From source file:org.jamwiki.parser.image.ImageProcessor.java

/**
 *
 *//*from   w w  w .  j  a  va  2s. co  m*/
private static BufferedImage resizeImage(BufferedImage tmp, int targetWidth, int targetHeight)
        throws IOException {
    int type = (tmp.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    int width = tmp.getWidth();
    int height = tmp.getHeight();
    BufferedImage resized = tmp;
    do {
        width /= 2;
        if (width < targetWidth) {
            width = targetWidth;
        }
        height /= 2;
        if (height < targetHeight) {
            height = targetHeight;
        }
        tmp = new BufferedImage(width, height, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2.drawImage(resized, 0, 0, width, height, null);
        g2.dispose();
        resized = tmp;
    } while (width != targetWidth || height != targetHeight);
    return resized;
}

From source file:org.jcurl.core.swing.RockLocationDisplayBase.java

public void exportPng(File dst) throws IOException {
    final BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g2 = (Graphics2D) img.getGraphics();
    {//from   www.j  a v  a 2  s . c o  m
        final Map hints = new HashMap();
        hints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        hints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        hints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
        hints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        hints.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
        hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.addRenderingHints(hints);
    }
    this.paintComponent(g2);
    g2.dispose();
    if (!dst.getName().endsWith(".png"))
        dst = new File(dst.getName() + ".png");
    ImageIO.write(img, "png", dst);
}

From source file:org.jcurl.core.swing.WCComponent.java

private BufferedImage renderPng(final String watermark) {
    final BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g2 = (Graphics2D) img.getGraphics();
    {//from ww  w .  j  a va2s  . c om
        final Map<Key, Object> hints = new HashMap<Key, Object>();
        hints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        hints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        hints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
        hints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        hints.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
        hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.addRenderingHints(hints);
    }
    final Font f0 = g2.getFont();
    paint(g2);
    g2.setTransform(new AffineTransform());
    if (watermark != null) {
        if (log.isDebugEnabled())
            log.debug(f0);
        g2.setFont(f0);
        g2.setColor(new Color(0, 0, 0, 128));
        g2.drawString(watermark, 10, 20);
    }
    g2.dispose();
    return img;
}