Example usage for java.awt.image BufferedImage getTransparency

List of usage examples for java.awt.image BufferedImage getTransparency

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getTransparency.

Prototype

public int getTransparency() 

Source Link

Document

Returns the transparency.

Usage

From source file:org.hippoecm.frontend.plugins.gallery.imageutil.ImageUtils.java

public static BufferedImage scaleImage(BufferedImage img, int xOffset, int yOffset, int sourceWidth,
        int sourceHeight, int targetWidth, int targetHeight, Object hint, boolean highQuality) {

    if (sourceWidth <= 0 || sourceHeight <= 0 || targetWidth <= 0 || targetHeight <= 0) {
        return null;
    }//from   w w  w  .j  a  v a2 s.  com

    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;

    BufferedImage result = img;
    if (xOffset != 0 || yOffset != 0 || sourceWidth != img.getWidth() || sourceHeight != img.getHeight()) {
        result = result.getSubimage(xOffset, yOffset, sourceWidth, sourceHeight);
    }

    int width, height;

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

    do {
        if (highQuality && width > targetWidth) {
            width /= 2;
        }
        width = Math.max(width, targetWidth);

        if (highQuality && height > targetHeight) {
            height /= 2;
        }
        height = Math.max(height, targetHeight);

        BufferedImage tmp = new BufferedImage(width, height, type);

        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(result, 0, 0, width, height, null);
        g2.dispose();

        result = tmp;
    } while (width != targetWidth || height != targetHeight);

    return result;
}

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

/**
 *
 *///from  www .  j  ava 2s .  c  om
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.jimcat.services.imagemanager.ImageUtil.java

/**
 * get a BufferedImageType of the given image used to replicate
 * //from  ww  w  . ja v a 2 s  .c o m
 * @param img
 * @return the type of the buffered image
 */
private static int getImageType(BufferedImage img) {
    if (img.getTransparency() == Transparency.OPAQUE) {
        return BufferedImage.TYPE_INT_RGB;
    }
    return BufferedImage.TYPE_INT_ARGB;
}

From source file:org.medici.bia.controller.manuscriptviewer.IIPImageServerController.java

/**
 * Convenience method that returns a scaled instance of the provided
 * {@code BufferedImage}.//from   ww  w  . j a va2s. c om
 * 
 * @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}
 * 
 *         This method has been taken from :
 *         http://today.java.net/pub/a/today
 *         /2007/04/03/perils-of-image-getscaledinstance.html
 * 
 * 
 */
public BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) img;
    int width = 0;
    int height = 0;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        width = img.getWidth();
        height = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        width = targetWidth;
        height = targetHeight;
    }

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

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

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

        ret = tmp;
    } while (width != targetWidth || height != targetHeight);

    return ret;
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactory.java

private static BufferedImage getAlphaImage(BufferedImage image) {
    if (!image.getColorModel().hasAlpha()) {
        return null;
    }/*ww w .j a v a 2 s  . co  m*/
    if (image.getTransparency() == Transparency.BITMASK) {
        throw new UnsupportedOperationException(
                "BITMASK Transparency JPEG compression is not" + " useful, use LosslessImageFactory instead");
    }
    WritableRaster alphaRaster = image.getAlphaRaster();
    if (alphaRaster == null) {
        // happens sometimes (PDFBOX-2654) despite colormodel claiming to have alpha
        return null;
    }
    BufferedImage alphaImage = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_BYTE_GRAY);
    alphaImage.setData(alphaRaster);
    return alphaImage;
}

From source file:plugin.exporttokens.PortraitToken.java

/**
 * Convenience method that returns a scaled instance of the
 * provided {@code BufferedImage}.//from  w w  w.  j  a  v  a 2s  . c  o  m
 *
 * @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 down scaling 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}
 */
public BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    int type = (img.getTransparency() == 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;
    }

    // If we are scaling up, just do the one pass.
    if (w < targetWidth || h < targetWidth) {
        // 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:util.ui.UiUtilities.java

/**
 * Convenience method that returns a scaled instance of the
 * provided {@code BufferedImage}./*from   ww w.java2  s  . c  o m*/
 *
 * @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
 * @return a scaled version of the original {@code BufferedImage}
 */
public static BufferedImage scaleDown(final BufferedImage img, final int targetWidth, final int targetHeight) {
    if (targetWidth > img.getWidth() || targetHeight > img.getHeight()) {
        return scaleIconToBufferedImage(img, targetWidth, targetHeight);
    }

    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage result = img;
    int w = img.getWidth();
    int h = img.getHeight();

    do {
        w /= 2;
        if (w < targetWidth) {
            w = targetWidth;
        }
        h /= 2;
        if (h < targetHeight) {
            h = targetHeight;
        }

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

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

    return result;
}

From source file:VASSAL.tools.imageop.OrthoRotateOpBitmapImpl.java

public BufferedImage eval() throws Exception {
    final BufferedImage src = sop.getImage(null);
    if (size == null)
        fixSize();/*w w  w  . jav a2s  . c  om*/

    // remain opaque if our parent image is
    final BufferedImage dst = ImageUtils.createCompatibleImage(size.width, size.height,
            src.getTransparency() != BufferedImage.OPAQUE);

    final Graphics2D g = dst.createGraphics();
    g.rotate(Math.PI / 2.0 * angle, src.getWidth() / 2.0, src.getHeight() / 2.0);
    g.drawImage(src, 0, 0, null);
    g.dispose();

    return dst;
}

From source file:VASSAL.tools.imageop.SourceTileOpBitmapImpl.java

public BufferedImage eval() throws Exception {
    final BufferedImage src = sop.getImage(null);
    final BufferedImage dst = ImageUtils.createCompatibleImage(size.width, size.height,
            src.getTransparency() != BufferedImage.OPAQUE);

    final Graphics2D g = dst.createGraphics();
    g.drawImage(src, 0, 0, size.width, size.height, x0, y0, x1, y1, null);
    g.dispose();/*from w  w  w.j  a  va2s.  com*/

    return dst;
}