Java Code Examples for java.awt.image.PixelGrabber#getColorModel()

The following examples show how to use java.awt.image.PixelGrabber#getColorModel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageProcessor.java    From selenium-shutterbug with MIT License 6 votes vote down vote up
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 ignored) {
    }
    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}
 
Example 2
Source File: Effect.java    From seaglass with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns true if the specified image has transparent pixels
 *
 * @param  image an image.
 *
 * @return {@code true} if the image has transparent pixels, {@code false}
 *         otherwise.
 */
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();
    }

    // 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();
}
 
Example 3
Source File: Picture.java    From Face-Recognition with GNU General Public License v3.0 6 votes vote down vote up
public double[] getImagePixels() {
	
	int w = img.getWidth(this);
	int h = img.getHeight(this);
	int[] pixels = new int[w * h];
	PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		System.err.println("interrupted waiting for pixels!");
		return new double[0];
	}
	if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
		System.err.println("image fetch aborted or errored");
		return new double[0];
	}
	double[] ret =new double[w*h];
	ColorModel cm = pg.getColorModel();
	for (int i=0; i<ret.length; i++)
	{
		ret[i] = cm.getBlue(pixels[i]) + cm.getGreen(pixels[i]) + cm.getRed(pixels[i]);
		ret[i] /= 3.0;
	}
	return ret;
}
 
Example 4
Source File: Picture.java    From Face-Recognition with GNU General Public License v3.0 6 votes vote down vote up
public double[] getImageColourPixels() {
    
    int w = img.getWidth(this);
    int h = img.getHeight(this);
    int[] pixels = new int[w * h];
    PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        System.err.println("interrupted waiting for pixels!");
        return new double[0];
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        System.err.println("image fetch aborted or errored");
        return new double[0];
    }
    double[] ret =new double[w*h];
    ColorModel cm = pg.getColorModel();
    for (int i=0; i<ret.length; i++)
    {
        Color c=new Color(cm.getRed(pixels[i]),cm.getGreen(pixels[i]),cm.getBlue(pixels[i]));
        ret[i]=c.getRGB();
     
    }
    return ret;
}
 
Example 5
Source File: PNGImageResizer.java    From entando-components with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected boolean hasAlpha(Image image) throws ApsSystemException {
       // 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) {
       	throw new ApsSystemException("Error grabbing a single pixel", e);
       }
       // Get the image's color model
       ColorModel cm = pg.getColorModel();
       return cm.hasAlpha();
}
 
Example 6
Source File: ImageUtil.java    From nextreports-designer with Apache License 2.0 6 votes vote down vote up
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();
    }

    // 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) {
        e.printStackTrace();
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}
 
Example 7
Source File: GraphicsUtils.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
/**
 * 获得一个Image对像的ColorModel
 * 
 * @param image
 * @return
 */
public static ColorModel getColorModel(Image image) {
	try {
		PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
		pg.grabPixels();
		return pg.getColorModel();
	} catch (InterruptedException e) {
		throw new RuntimeException(e);
	}
}
 
Example 8
Source File: ExportBestFlatImage.java    From TrakEM2 with GNU General Public License v3.0 4 votes vote down vote up
/**
 *
    * @return null when the dimensions make the array larger than 2GB, or the image otherwise.
   */
public ByteProcessor makeFlatGrayImage()
{
	if ( canUseAWTImage() ) {
		final Image img = createAWTImage( ImagePlus.GRAY8 );
		try {
		// Try fastest way: direct way of grabbing the underlying pixel array
			if (img instanceof BufferedImage && BufferedImage.TYPE_BYTE_GRAY == ((BufferedImage)img).getType()) {
				return new ByteProcessor( (BufferedImage)img );
			}
			final PixelGrabber pg = new PixelGrabber(img, 0, 0, img.getWidth(null), img.getHeight(null), false);
			try {
				pg.grabPixels();
			} catch (final InterruptedException ie) {
				ie.printStackTrace();
			}
			if (pg.getColorModel() instanceof IndexColorModel) {
				return new ByteProcessor(img.getWidth(null), img.getHeight(null), (byte[])pg.getPixels(), null);
			} else {
				// Let's be creative
				return new ColorProcessor(img).convertToByteProcessor();
			}
		} finally {
			img.flush();
		}
	}

	if ( !isSmallerThan2GB() ) {
		Utils.log("Cannot create an image larger than 2 GB.");
		return null;
	}

	if ( loader.isMipMapsRegenerationEnabled() )
	{
		// Use mipmaps directly: they are already Gaussian-downsampled
		// (TODO waste: generates an alpha mask that is then not used)
		return ExportUnsignedByte.makeFlatImageFromMipMaps( patches, finalBox, 0, scale ).a;
	}

	// Else: no mipmaps
	return ExportUnsignedByte.makeFlatImageFromOriginals( patches, finalBox, 0, scale ).a;
}