Example usage for java.awt.image ColorModel hasAlpha

List of usage examples for java.awt.image ColorModel hasAlpha

Introduction

In this page you can find the example usage for java.awt.image ColorModel hasAlpha.

Prototype

public final boolean hasAlpha() 

Source Link

Document

Returns whether or not alpha is supported in this ColorModel .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Image image = new ImageIcon("a.png").getImage();
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        System.out.println(bimage.getColorModel().hasAlpha());
    }//  w  w w  . j  a  v  a  2  s .c o m

    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    ColorModel cm = pg.getColorModel();
    System.out.println(cm.hasAlpha());
}

From source file:Main.java

/**
 * This method returns true if the specified image has transparent pixels
 *
 * @param image/*from   w  w  w  . j a  va2  s  .  c  om*/
 * @return
 */
public static boolean hasAlpha(Image image) {
    // If buffered image, the colour model is readily available
    if (image instanceof BufferedImage) {
        return ((BufferedImage) image).getColorModel().hasAlpha();
    }

    // Use a pixel grabber to retrieve the image's colour model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's colour model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}

From source file:Main.java

public static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().hasAlpha();
    }/* w  ww  . j  a  v  a2s .c  o m*/

    // Use a pixel grabber to retrieve the image's color model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}

From source file:ImageUtils.java

/**
 * This method returns true if the specified image has transparent pixels
 * This snippet was taken from: http://www.exampledepot.com/egs/java.awt.image/HasAlpha.html
 * @param image//from  w w  w. ja v a2 s  .com
 * @return True if the image has any transparency
 */
public static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().hasAlpha();
    }

    // Use a pixel grabber to retrieve the image's color model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}

From source file:de.laures.cewolf.util.ImageHelper.java

public static boolean hasAlpha(Image image) {
    if (image instanceof BufferedImage) {
        return ((BufferedImage) image).getColorModel().hasAlpha();
    }//from w ww  . j av  a  2 s  .c  o m
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    ColorModel cm = pg.getColorModel();
    if (cm == null) {
        return false;
    }
    return cm.hasAlpha();
}

From source file:GraphicsUtil.java

/**
 * Extracts an alpha raster from a RenderedImage. The method tries to avoid copying data
 * unnecessarily by checking if the RenderedImage is a BufferedImage which offers suitable
 * direct methods.//from w ww . j  a  v a 2 s .  com
 * @param image the image
 * @return the alpha raster
 */
public static Raster getAlphaRaster(RenderedImage image) {
    ColorModel cm = image.getColorModel();
    if (!cm.hasAlpha() || cm.getTransparency() != ColorModel.TRANSLUCENT) {
        throw new IllegalStateException("Image doesn't have an alpha channel");
    }
    Raster alpha;
    if (image instanceof BufferedImage) {
        //Optimization possible with BufferedImage (No copying)
        alpha = ((BufferedImage) image).getAlphaRaster();
    } else {
        WritableRaster wraster = GraphicsUtil.makeRasterWritable(image.getData());
        alpha = image.getColorModel().getAlphaRaster(wraster);
    }
    return alpha;
}

From source file:GraphicsUtil.java

/**
 * Coerces data within a bufferedImage to match newAlphaPreMult,
 * Note that this can not change the colormodel of bi so you
 *
 * @param wr The raster to change the state of.
 * @param cm The colormodel currently associated with data in wr.
 * @param newAlphaPreMult The desired state of alpha Premult for raster.
 * @return A new colormodel that matches newAlphaPreMult.
 *//*from ww w. j  a  va  2 s . c  o m*/
public static ColorModel coerceData(WritableRaster wr, ColorModel cm, boolean newAlphaPreMult) {

    // System.out.println("CoerceData: " + cm.isAlphaPremultiplied() +
    //                    " Out: " + newAlphaPreMult);
    if (cm.hasAlpha() == false)
        // Nothing to do no alpha channel
        return cm;

    if (cm.isAlphaPremultiplied() == newAlphaPreMult)
        // nothing to do alpha state matches...
        return cm;

    // System.out.println("CoerceData: " + wr.getSampleModel());

    if (newAlphaPreMult) {
        multiplyAlpha(wr);
    } else {
        divideAlpha(wr);
    }

    return coerceColorModel(cm, newAlphaPreMult);
}

From source file:GraphicsUtil.java

/**
 * Coerces data within a bufferedImage to match newAlphaPreMult,
 * Note that this can not change the colormodel of bi so you
 *
 * @param wr The raster to change the state of.
 * @param cm The colormodel currently associated with data in wr.
 * @param newAlphaPreMult The desired state of alpha Premult for raster.
 * @return A new colormodel that matches newAlphaPreMult.
 *//*  w w  w  .j  av  a2s  .  c om*/
public static ColorModel coerceData(WritableRaster wr, ColorModel cm, boolean newAlphaPreMult) {

    // System.out.println("CoerceData: " + cm.isAlphaPremultiplied() +
    //                    " Out: " + newAlphaPreMult);
    if (!cm.hasAlpha())
        // Nothing to do no alpha channel
        return cm;

    if (cm.isAlphaPremultiplied() == newAlphaPreMult)
        // nothing to do alpha state matches...
        return cm;

    // System.out.println("CoerceData: " + wr.getSampleModel());

    if (newAlphaPreMult) {
        multiplyAlpha(wr);
    } else {
        divideAlpha(wr);
    }

    return coerceColorModel(cm, newAlphaPreMult);
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadThumbnailer.java

private RenderedOp makeImageOpaque(RenderedOp image) {
    ColorModel colorModel = image.getColorModel();

    if (!colorModel.hasAlpha()) {
        // The image is already opaque.
        return image;
    }/*w  ww .  j ava  2s.  com*/

    if (image.getNumBands() == 4) {
        // The image has a separate alpha channel. Drop the alpha channel.
        return BandSelectDescriptor.create(image, COLOR_BAND_INDEXES, null);
    }

    // Don't know how to handle it. Probably a GIF with a transparent
    // background. Give up.
    return image;
}

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

private static boolean hasAlpha(Image image) {
    // If buffered image, the color model is readily available
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().hasAlpha();
    }//  w w w.  ja v a2  s  .  com

    // Use a pixel grabber to retrieve the image's color model;
    // grabbing a single pixel is usually sufficient
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}