Example usage for java.awt Transparency BITMASK

List of usage examples for java.awt Transparency BITMASK

Introduction

In this page you can find the example usage for java.awt Transparency BITMASK.

Prototype

int BITMASK

To view the source code for java.awt Transparency BITMASK.

Click Source Link

Document

Represents image data that is guaranteed to be either completely opaque, with an alpha value of 1.0, or completely transparent, with an alpha value of 0.0.

Usage

From source file:net.pms.medialibrary.commons.helpers.FileImportHelper.java

/**
 * Creates a buffered image from an image
 * @param image the image/*  w w  w  .j  a  v a  2s  .co m*/
 * @return the buffered image
 */
public static BufferedImage getBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:net.rptools.maptool.client.functions.InputFunction.java

/**
 * Gets icon from the asset manager. Code copied and modified from
 * EditTokenDialog.java/*from   ww w . j av a  2 s .  co  m*/
 */
private ImageIcon getIcon(String id, int size, ImageObserver io) {
    // Extract the MD5Key from the URL
    if (id == null)
        return null;
    MD5Key assetID = new MD5Key(id);

    // Get the base image && find the new size for the icon
    BufferedImage assetImage = ImageManager.getImage(assetID, io);

    // Resize
    if (assetImage.getWidth() > size || assetImage.getHeight() > size) {
        Dimension dim = new Dimension(assetImage.getWidth(), assetImage.getWidth());
        if (dim.height < dim.width) {
            dim.height = (int) ((dim.height / (double) dim.width) * size);
            dim.width = size;
        } else {
            dim.width = (int) ((dim.width / (double) dim.height) * size);
            dim.height = size;
        }
        BufferedImage image = new BufferedImage(dim.width, dim.height, Transparency.BITMASK);
        Graphics2D g = image.createGraphics();
        g.drawImage(assetImage, 0, 0, dim.width, dim.height, null);
        assetImage = image;
    }
    return new ImageIcon(assetImage);
}

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

public static BufferedImage toBufferedImage(java.awt.Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from www .  j  av a2 s  .c om*/

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testPng8ForceBitmask() throws Exception {
    MockHttpServletResponse response = getAsServletResponse("wms?bbox=" + bbox + "&styles=&layers=" + layers
            + "&Format=image/png8" + "&request=GetMap" + "&width=550" + "&height=250"
            + "&srs=EPSG:4326&transparent=true&format_options=quantizer:octree");
    assertEquals("image/png; mode=8bit", response.getContentType());
    assertEquals("inline; filename=sf-states.png", response.getHeader("Content-Disposition"));

    InputStream is = getBinaryInputStream(response);
    BufferedImage bi = ImageIO.read(is);
    IndexColorModel cm = (IndexColorModel) bi.getColorModel();
    assertEquals(Transparency.BITMASK, cm.getTransparency());
    assertTrue(cm.getTransparentPixel() >= 0);
}

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

private static BufferedImage getAlphaImage(BufferedImage image) {
    if (!image.getColorModel().hasAlpha()) {
        return null;
    }/* w w w  .  j av  a2 s. c  om*/
    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:org.yccheok.jstock.gui.Utils.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from   www .  j av a2 s. c o  m*/

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see e661 Determining If an Image Has Transparent Pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}