Java Code Examples for java.awt.image.PixelGrabber

The following are top voted examples for showing how to use java.awt.image.PixelGrabber. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: PhET   File: BufferedImageUtils.java   View source code 7 votes vote down vote up
/**
 * Gets the transparency of an image.
 *
 * @param image the image
 * @return OPAQUE, BITMASK or TRANSLUCENT (see java.awt.Transparency)
 */
public static int getTransparency( Image image ) {
    // If buffered image, the color model is readily available
    if ( image instanceof BufferedImage ) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().getTransparency();
    }
    // 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();

    int transparency = Transparency.OPAQUE;
    if ( cm != null ) {
        transparency = cm.getTransparency();
    }
    return transparency;
}
 
Example 2
Project: incubator-netbeans   File: UIUtils.java   View source code 6 votes vote down vote up
private static Color getGTKProfilerResultsBackground() {
    int[] pixels = new int[1];
    pixels[0] = -1;
    
    // Prepare textarea to grab the color from
    JTextArea textArea = new JTextArea();
    textArea.setSize(new Dimension(10, 10));
    textArea.doLayout();
    
    // Print the textarea to an image
    Image image = new BufferedImage(textArea.getSize().width, textArea.getSize().height, BufferedImage.TYPE_INT_RGB);
    textArea.printAll(image.getGraphics());
    
    // Grab appropriate pixels to get the color
    PixelGrabber pixelGrabber = new PixelGrabber(image, 5, 5, 1, 1, pixels, 0, 1);
    try {
        pixelGrabber.grabPixels();
        if (pixels[0] == -1) return Color.WHITE; // System background not customized
    } catch (InterruptedException e) {
        return getNonGTKProfilerResultsBackground();
    }
    
    return pixels[0] != -1 ? new Color(pixels[0]) : getNonGTKProfilerResultsBackground();
}
 
Example 3
Project: VASSAL-src   File: TransparentFilter.java   View source code 6 votes vote down vote up
/**
 * For the given input color, return the color that this color
 * will map to in an offscreen image created by the given Component
 */
public static int getOffscreenEquivalent(int color, Component obs) {
  Image im = obs.createImage(1, 1);
  Graphics2D g = (Graphics2D) im.getGraphics();
  g.setColor(new java.awt.Color(color));
  g.fillRect(0, 0, 1, 1);
  g.dispose();

  int[] bg = new int[1];
  PixelGrabber pg = new PixelGrabber(im, 0, 0, 1, 1, bg, 0, 1);
  try {
    pg.grabPixels();
  }
  catch (InterruptedException ex) {
    logger.error("", ex);
  }
  return bg[0];
}
 
Example 4
Project: Library-of-Kaeon   File: ImageProcessor.java   View source code 6 votes vote down vote up
public static boolean hasAlpha(Image image) {
	
	if (image instanceof BufferedImage)
		return ((BufferedImage)image).getColorModel().hasAlpha();
	
	PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
	
	try {
		pg.grabPixels();
	}
	
	catch (InterruptedException e)
	{
	}
	
	return pg.getColorModel().hasAlpha();
}
 
Example 5
Project: OpenNFMM   File: xtGraphics.java   View source code 6 votes vote down vote up
static private Image bressed(final Image image) {
    final int i = image.getHeight(null);
    final int i340 = image.getWidth(null);
    final int[] is = new int[i340 * i];
    final PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, i340, i, is, 0, i340);
    try {
        pixelgrabber.grabPixels();
    } catch (final InterruptedException ignored) {

    }
    final Color color = new Color(247, 255, 165);
    for (int i341 = 0; i341 < i340 * i; i341++)
        if (is[i341] != is[i340 * i - 1]) {
            is[i341] = color.getRGB();
        }
    return xt.createImage(new MemoryImageSource(i340, i, is, 0, i340));
}
 
Example 6
Project: OpenNFMM   File: xtGraphics.java   View source code 6 votes vote down vote up
static private Image pressed(final Image image) {
    final int i = image.getHeight(null);
    final int i337 = image.getWidth(null);
    final int[] is = new int[i337 * i];
    final PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, i337, i, is, 0, i337);
    try {
        pixelgrabber.grabPixels();
    } catch (final InterruptedException ignored) {

    }
    for (int i338 = 0; i338 < i337 * i; i338++)
        if (is[i338] != is[i337 * i - 1]) {
            is[i338] = -16777216;
        }
    return xt.createImage(new MemoryImageSource(i337, i, is, 0, i337));
}
 
Example 7
Project: PhET   File: BufferedImageUtils.java   View source code 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 e ) {
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}
 
Example 8
Project: BrickifyFX   File: OutputPaneController.java   View source code 6 votes vote down vote up
/**
 * Get the image to write. This is the mosaic image with alpha channel stripped, as this
 * doesn't work with the JPEG export.
 */
private BufferedImage getImageToWrite() throws InterruptedException {
	BufferedImage image = SwingFXUtils.fromFXImage(mainController.getMosaicImage(), null);
	final int[] RGB_MASKS = {0xFF0000, 0xFF00, 0xFF};
	final ColorModel rgbOpaque = new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]);

	PixelGrabber pg = new PixelGrabber(image, 0, 0, -1, -1, true);
	pg.grabPixels();
	int width = pg.getWidth(), height = pg.getHeight();

	DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight());
	WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null);
	BufferedImage bi = new BufferedImage(rgbOpaque, raster, false, null);
	
	return bi;
}
 
Example 9
Project: WarpPI   File: BMPFile.java   View source code 6 votes vote down vote up
private boolean convertImage(Image parImage, int parWidth, int parHeight) {
	int pad;
	bitmap = new int[parWidth * parHeight];
	final PixelGrabber pg = new PixelGrabber(parImage, 0, 0, parWidth, parHeight, bitmap, 0, parWidth);
	try {
		pg.grabPixels();
	} catch (final InterruptedException e) {
		e.printStackTrace();
		return (false);
	}
	pad = (4 - ((parWidth * 3) % 4)) * parHeight;
	biSizeImage = ((parWidth * parHeight) * 3) + pad;
	bfSize = biSizeImage + BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE;
	biWidth = parWidth;
	biHeight = parHeight;
	return (true);
}
 
Example 10
Project: communote-server   File: ImageScaler.java   View source code 6 votes vote down vote up
/**
 * Returns the transparency. Returns either OPAQUE, BITMASK, or TRANSLUCENT.
 *
 * @param image
 *            The image.
 * @return the transparency of this ColorModel.
 */
private int getTransparency(Image image) {
    if (image instanceof BufferedImage) {
        BufferedImage bimage = (BufferedImage) image;
        return bimage.getColorModel().getTransparency();
    }
    PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    try {
        if (pg.grabPixels() && pg.getColorModel() != null) {
            return pg.getColorModel().getTransparency();
        }
    } catch (InterruptedException e) {
        LOGGER.warn(e.getMessage());
    }
    // fallback to generic type
    return Transparency.TRANSLUCENT;
}
 
Example 11
Project: axisviewer   File: Camera.java   View source code 6 votes vote down vote up
/**
 * Use a PixelGrabber to grab the pixel data from the camera stream.
 *
 */
public void grabPixels ( ) {
	
	// Get the width and height of the camera image.
	int width = this.cameraStream.getWidth( this );
	int height = this.cameraStream.getHeight( this );
	
	// Create a new pixels array with a given width and height.
	this.pixels = new int[ width * height ];
	
	// Create a new PixelGrabber using the camera stream and place the grabbed
	// pixel data into this.pixels.
	this.grabber = new PixelGrabber( this.cameraStream, 0, 0, width, height, this.pixels, 0, width );     

	try {
		
		// Grab the pixels.
		this.grabber.grabPixels( );
		
	}
	catch ( Exception e ) {
		log.error("Failed to grab pixels.", e);
	} // end catch

}
 
Example 12
Project: osrs-private-server   File: Sprite.java   View source code 6 votes vote down vote up
public Sprite(byte abyte0[], Component component) {
	try {
		Image image = Toolkit.getDefaultToolkit().createImage(abyte0);
		MediaTracker mediatracker = new MediaTracker(component);
		mediatracker.addImage(image, 0);
		mediatracker.waitForAll();
		myWidth = image.getWidth(component);
		myHeight = image.getHeight(component);
		anInt1444 = myWidth;
		anInt1445 = myHeight;
		drawOffsetX = 0;
		drawOffsetY = 0;
		myPixels = new int[myWidth * myHeight];
		PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, myWidth, myHeight, myPixels, 0, myWidth);
		pixelgrabber.grabPixels();
	} catch (Exception _ex) {
		System.out.println("Error converting jpg");
	}
}
 
Example 13
Project: osrs-private-server   File: Sprite.java   View source code 6 votes vote down vote up
public Sprite(String img, int width, int height) {
	try {
		Image image = Toolkit.getDefaultToolkit().createImage(FileOperations.ReadFile(img));
		myWidth = width;
		myHeight = height;
		anInt1444 = myWidth;
		anInt1445 = myHeight;
		drawOffsetX = 0;
		drawOffsetY = 0;
		myPixels = new int[myWidth * myHeight];
		PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, myWidth, myHeight, myPixels, 0, myWidth);
		pixelgrabber.grabPixels();
		image = null;
	} catch (Exception _ex) {
		System.out.println(_ex);
	}
}
 
Example 14
Project: osrs-private-server   File: Sprite.java   View source code 6 votes vote down vote up
public Sprite(String img) {
	try {
		Image image = Toolkit.getDefaultToolkit().getImage(location + img + ".png");
		ImageIcon sprite = new ImageIcon(image);
		myWidth = sprite.getIconWidth();
		myHeight = sprite.getIconHeight();
		anInt1444 = myWidth;
		anInt1445 = myHeight;
		drawOffsetX = 0;
		drawOffsetY = 0;
		myPixels = new int[myWidth * myHeight];
		PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, myWidth, myHeight, myPixels, 0, myWidth);
		pixelgrabber.grabPixels();
		image = null;
		setTransparency(255, 0, 255);
	} catch (Exception _ex) {
		System.out.println(_ex);
	}
}
 
Example 15
Project: osrs-private-server   File: Sprite.java   View source code 6 votes vote down vote up
public Sprite(byte spriteData[]) {
	try {
		Image image = Toolkit.getDefaultToolkit().createImage(spriteData);
		ImageIcon sprite = new ImageIcon(image);
		myWidth = sprite.getIconWidth();
		myHeight = sprite.getIconHeight();
		anInt1444 = myWidth;
		anInt1445 = myHeight;
		drawOffsetX = 0;
		drawOffsetY = 0;
		myPixels = new int[myWidth * myHeight];
		PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, myWidth, myHeight, myPixels, 0, myWidth);
		pixelgrabber.grabPixels();
		image = null;
		setTransparency(255, 0, 255);
	} catch (Exception _ex) {
		System.out.println(_ex);
	}
}
 
Example 16
Project: intellij-ce-playground   File: UIUtil.java   View source code 6 votes vote down vote up
@Nullable
public static Color getColorAt(final Icon icon, final int x, final int y) {
  if (0 <= x && x < icon.getIconWidth() && 0 <= y && y < icon.getIconHeight()) {
    final BufferedImage image = createImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
    icon.paintIcon(null, image.getGraphics(), 0, 0);

    final int[] pixels = new int[1];
    final PixelGrabber pixelGrabber = new PixelGrabber(image, x, y, 1, 1, pixels, 0, 1);
    try {
      pixelGrabber.grabPixels();
      return new Color(pixels[0]);
    }
    catch (InterruptedException ignored) {
    }
  }

  return null;
}
 
Example 17
Project: project-bianca   File: Util.java   View source code 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 e) {
	}

	// Get the image's color model
	ColorModel cm = pg.getColorModel();
	return cm.hasAlpha();
}
 
Example 18
Project: cmoct-sourcecode   File: ImageOperations.java   View source code 6 votes vote down vote up
/**
 * The next two mehtds where got from http://javaalmanac
 * .com/egs/java.awt.image/HasAlpha.html T
 * 
 * @param image
 * @return
 */
// This method returns true if the specified
// image has transparent pixels
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();
}
 
Example 19
Project: plg   File: ImageUtils.java   View source code 6 votes vote down vote up
/**
 * Given an image, this method extracts a color that is calculated as the
 * average of the colors of the image
 * 
 * @param image the given image
 * @return the "average" color
 */
public static Color getAverageColor(BufferedImage image) {
	if (image == null) {
		return null;
	}
	Image i = image.getScaledInstance(1, 1, BufferedImage.SCALE_SMOOTH);
	int[] pixels = new int[1];
	PixelGrabber pg = new PixelGrabber(i, 0, 0, 1, 1, pixels, 0, 1);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) { }
	int  c = pixels[0];
	int  red = (c & 0x00ff0000) >> 16;
	int  green = (c & 0x0000ff00) >> 8;
	int  blue = c & 0x000000ff;
	return new Color(red, green, blue);
}
 
Example 20
Project: plg   File: ImageUtils.java   View source code 6 votes vote down vote up
/**
 * Given an image, this method extracts a color that is calculated as the
 * average of the colors of the image
 * 
 * @param image the given image
 * @return the "average" color
 */
public static Color getAverageColor(BufferedImage image) {
	if (image == null) {
		return null;
	}
	Image i = image.getScaledInstance(1, 1, BufferedImage.SCALE_SMOOTH);
	int[] pixels = new int[1];
	PixelGrabber pg = new PixelGrabber(i, 0, 0, 1, 1, pixels, 0, 1);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) { }
	int  c = pixels[0];
	int  red = (c & 0x00ff0000) >> 16;
	int  green = (c & 0x0000ff00) >> 8;
	int  blue = c & 0x000000ff;
	return new Color(red, green, blue);
}
 
Example 21
Project: OpenNFMM   File: xtGraphics.java   View source code 6 votes vote down vote up
static private Image bressed(final Image image) {
    final int i = image.getHeight(null);
    final int i340 = image.getWidth(null);
    final int[] is = new int[i340 * i];
    final PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, i340, i, is, 0, i340);
    try {
        pixelgrabber.grabPixels();
    } catch (final InterruptedException ignored) {

    }
    final Color color = new Color(247, 255, 165);
    for (int i341 = 0; i341 < i340 * i; i341++)
        if (is[i341] != is[i340 * i - 1]) {
            is[i341] = color.getRGB();
        }
    return xt.createImage(new MemoryImageSource(i340, i, is, 0, i340));
}
 
Example 22
Project: OpenNFMM   File: xtGraphics.java   View source code 6 votes vote down vote up
static private Image pressed(final Image image) {
    final int i = image.getHeight(null);
    final int i337 = image.getWidth(null);
    final int[] is = new int[i337 * i];
    final PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, i337, i, is, 0, i337);
    try {
        pixelgrabber.grabPixels();
    } catch (final InterruptedException ignored) {

    }
    for (int i338 = 0; i338 < i337 * i; i338++)
        if (is[i338] != is[i337 * i - 1]) {
            is[i338] = -16777216;
        }
    return xt.createImage(new MemoryImageSource(i337, i, is, 0, i337));
}
 
Example 23
Project: bigcat   File: DvidMultiscale2dVolatileArrayLoader.java   View source code 6 votes vote down vote up
static private void readImage(
		final String urlString,
		final int w,
		final int h,
		final int[] data ) throws IOException, InterruptedException
{
		final URL url = new URL( urlString );
		final BufferedImage jpg = ImageIO.read( url );
		/* This gymnastic is necessary to get reproducible gray
		* values, just opening a JPG or PNG, even when saved by
		* ImageIO, and grabbing its pixels results in gray values
		* with a non-matching gamma transfer function, I cannot tell
		* why... */
		final BufferedImage image =
				new BufferedImage( w, h, BufferedImage.TYPE_INT_ARGB );
		image.createGraphics().drawImage( jpg, 0, 0, null );
		final PixelGrabber pg = new PixelGrabber( image, 0, 0, w, h, data, 0, w );
		pg.grabPixels();
}
 
Example 24
Project: onecmdb   File: IconGenerator.java   View source code 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 e) {
    }

    // Get the image's color model
    ColorModel cm = pg.getColorModel();
    return cm.hasAlpha();
}
 
Example 25
Project: jhack   File: ImageDrawingArea.java   View source code 6 votes vote down vote up
/**
 * Returns the Color of point x, y on the image. Uses
 * <code>PixelGrabber</code> to get the exact color.
 * 
 * @param x Coordinate on the image.
 * @param y Coordinate on the image.
 * @return The Color of the specified point.
 */
public Color getPointColor(int x, int y)
{
    // return new Color(((BufferedImage) img).getRGB(x, y));

    int w = 1, h = 1;
    int[] pixels = new int[w * h];
    PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
    try
    {
        pg.grabPixels();
    }
    catch (InterruptedException e)
    {
        System.err.println("interrupted waiting for pixels!");
        return null;
    }
    int alpha, blue, red, green;

    alpha = (pixels[0] >> 24) & 0xff;
    red = (pixels[0] >> 16) & 0xff;
    green = (pixels[0] >> 8) & 0xff;
    blue = (pixels[0]) & 0xff;

    return new Color(red, green, blue, alpha);
}
 
Example 26
Project: Lucee4   File: Image.java   View source code 6 votes vote down vote up
/**
 * This method returns true if the specified image has transparent pixels
 * @param image
 * @return
 */
public static boolean hasAlpha(java.awt.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 27
Project: Lucee4   File: ImageUtils.java   View source code 6 votes vote down vote up
/**
    * Cretae a BufferedImage from an ImageProducer.
    * @param producer the ImageProducer
    * @return a new TYPE_INT_ARGB BufferedImage
    */
   public static BufferedImage createImage(ImageProducer producer) {
	PixelGrabber pg = new PixelGrabber(producer, 0, 0, -1, -1, null, 0, 0);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		throw new RuntimeException("Image fetch interrupted");
	}
	if ((pg.status() & ImageObserver.ABORT) != 0)
		throw new RuntimeException("Image fetch aborted");
	if ((pg.status() & ImageObserver.ERROR) != 0)
		throw new RuntimeException("Image fetch error");
	BufferedImage p = new BufferedImage(pg.getWidth(), pg.getHeight(), BufferedImage.TYPE_INT_ARGB);
	p.setRGB(0, 0, pg.getWidth(), pg.getHeight(), (int[])pg.getPixels(), 0, pg.getWidth());
	return p;
}
 
Example 28
Project: seaglass   File: Effect.java   View source code 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 29
Project: vassal   File: TransparentFilter.java   View source code 6 votes vote down vote up
/**
 * For the given input color, return the color that this color
 * will map to in an offscreen image created by the given Component
 */
public static int getOffscreenEquivalent(int color, Component obs) {
  Image im = obs.createImage(1, 1);
  Graphics2D g = (Graphics2D) im.getGraphics();
  g.setColor(new java.awt.Color(color));
  g.fillRect(0, 0, 1, 1);
  g.dispose();

  int[] bg = new int[1];
  PixelGrabber pg = new PixelGrabber(im, 0, 0, 1, 1, bg, 0, 1);
  try {
    pg.grabPixels();
  }
  catch (InterruptedException ex) {
    logger.error("", ex);
  }
  return bg[0];
}
 
Example 30
Project: webcamstudio   File: ImageUtils.java   View source code 6 votes vote down vote up
/**
    * Cretae a BufferedImage from an ImageProducer.
    * @param producer the ImageProducer
    * @return a new TYPE_INT_ARGB BufferedImage
    */
   public static BufferedImage createImage(ImageProducer producer) {
	PixelGrabber pg = new PixelGrabber(producer, 0, 0, -1, -1, null, 0, 0);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		throw new RuntimeException("Image fetch interrupted");
	}
	if ((pg.status() & ImageObserver.ABORT) != 0) {
                   throw new RuntimeException("Image fetch aborted");
               }
	if ((pg.status() & ImageObserver.ERROR) != 0) {
                   throw new RuntimeException("Image fetch error");
               }
	BufferedImage p = new BufferedImage(pg.getWidth(), pg.getHeight(), BufferedImage.TYPE_INT_ARGB);
	p.setRGB(0, 0, pg.getWidth(), pg.getHeight(), (int[])pg.getPixels(), 0, pg.getWidth());
	return p;
}
 
Example 31
Project: WorldOfMoreWires   File: ImageLoadingDialog.java   View source code 6 votes vote down vote up
private static int[][] getPixels(Image image) throws IOException {
    int w = image.getWidth(null);
    int h = image.getHeight(null);
    int pix[] = new int[w * h];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, w, h, pix, 0, w);

    try {
        if (!grabber.grabPixels()) {
            throw new IOException("Grabber returned false: " + grabber.status());
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    int pixels[][] = new int[w][h];
    for (int x = w; x-- > 0; ) {
        for (int y = h; y-- > 0; ) {
            pixels[x][y] = pix[y * w + x];
        }
    }

    return pixels;
}
 
Example 32
Project: WorldOfMoreWires   File: TestQuantize.java   View source code 6 votes vote down vote up
/**
 * Snag the pixels from an image.
 */
public static int[][] getPixels(Image image) throws IOException {
    int w = image.getWidth(null);
    int h = image.getHeight(null);
    int pix[] = new int[w * h];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, w, h, pix, 0, w);

    try {
        if (!grabber.grabPixels()) {
            throw new IOException("Grabber returned false: " +
                    grabber.status());
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    int pixels[][] = new int[w][h];
    for (int x = w; x-- > 0; ) {
        for (int y = h; y-- > 0; ) {
            pixels[x][y] = pix[y * w + x];
        }
    }

    return pixels;
}
 
Example 33
Project: vectorgraphics2d   File: GraphicsUtils.java   View source code 6 votes vote down vote up
/**
 * This method returns {@code true} if the specified image has the
 * possibility to store transparent pixels.
 * Inspired by http://www.exampledepot.com/egs/java.awt.image/HasAlpha.html
 * @param image Image that should be checked for alpha channel.
 * @return {@code true} if the specified image can have transparent pixels,
 *         {@code false} otherwise
 */
public static boolean hasAlpha(Image image) {
	ColorModel cm;
	// If buffered image, the color model is readily available
	if (image instanceof BufferedImage) {
		BufferedImage bimage = (BufferedImage) image;
		cm = bimage.getColorModel();
	} else {
		// 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) {
			return false;
		}
		// Get the image's color model
		cm = pg.getColorModel();
	}
	return cm.hasAlpha();
}
 
Example 34
Project: Genji   File: AttachBL.java   View source code 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();
	}

	// 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 35
Project: Lucee   File: Image.java   View source code 6 votes vote down vote up
/**
 * This method returns true if the specified image has transparent pixels
 * @param image
 * @return
 */
public static boolean hasAlpha(java.awt.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 36
Project: Lucee   File: ImageUtils.java   View source code 6 votes vote down vote up
/**
    * Cretae a BufferedImage from an ImageProducer.
    * @param producer the ImageProducer
    * @return a new TYPE_INT_ARGB BufferedImage
    */
   public static BufferedImage createImage(ImageProducer producer) {
	PixelGrabber pg = new PixelGrabber(producer, 0, 0, -1, -1, null, 0, 0);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		throw new RuntimeException("Image fetch interrupted");
	}
	if ((pg.status() & ImageObserver.ABORT) != 0)
		throw new RuntimeException("Image fetch aborted");
	if ((pg.status() & ImageObserver.ERROR) != 0)
		throw new RuntimeException("Image fetch error");
	BufferedImage p = new BufferedImage(pg.getWidth(), pg.getHeight(), BufferedImage.TYPE_INT_ARGB);
	p.setRGB(0, 0, pg.getWidth(), pg.getHeight(), (int[])pg.getPixels(), 0, pg.getWidth());
	return p;
}
 
Example 37
Project: feathers-sdk   File: ImageUtil.java   View source code 6 votes vote down vote up
public static PixelGrabber getPixelGrabber(Image image, String location)
 {
     PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, -1, -1, true);

     try
     {
         pixelGrabber.grabPixels();
     }
     catch (InterruptedException interruptedException)
     {
      if (Trace.error)
      {
       interruptedException.printStackTrace();
      }
         throw new RuntimeException("Failed to grab pixels for image " + location);
     }

  if (((pixelGrabber.getStatus() & ImageObserver.WIDTH) == 0) ||
((pixelGrabber.getStatus() & ImageObserver.HEIGHT) == 0))
  {
   throw new RuntimeException("Failed to grab pixels for image " + location);
  }

     return pixelGrabber;
 }
 
Example 38
Project: feathers-sdk   File: LosslessImage.java   View source code 6 votes vote down vote up
private void init(Image image)
{
       PixelGrabber pixelGrabber = ImageUtil.getPixelGrabber(image, location);

	width = pixelGrabber.getWidth();
	height = pixelGrabber.getHeight();
	Object p = pixelGrabber.getPixels();

	if (p != null)
	{
		Class ct = p.getClass().getComponentType();
		if (ct != null)
		{
			if (ct.equals(Integer.TYPE))
				pixels = (int[])p;
			else if (ct.equals(Byte.TYPE))
				throw new IllegalStateException("int[] of pixels expected, received byte[] instead.");
		}
	}
}
 
Example 39
Project: openbd-core   File: ImageUtils.java   View source code 6 votes vote down vote up
/**
    * Cretae a BufferedImage from an ImageProducer.
    * @param producer the ImageProducer
    * @return a new TYPE_INT_ARGB BufferedImage
    */
   public static BufferedImage createImage(ImageProducer producer) {
	PixelGrabber pg = new PixelGrabber(producer, 0, 0, -1, -1, null, 0, 0);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		throw new RuntimeException("Image fetch interrupted");
	}
	if ((pg.status() & ImageObserver.ABORT) != 0)
		throw new RuntimeException("Image fetch aborted");
	if ((pg.status() & ImageObserver.ERROR) != 0)
		throw new RuntimeException("Image fetch error");
	BufferedImage p = new BufferedImage(pg.getWidth(), pg.getHeight(), BufferedImage.TYPE_INT_ARGB);
	p.setRGB(0, 0, pg.getWidth(), pg.getHeight(), (int[])pg.getPixels(), 0, pg.getWidth());
	return p;
}
 
Example 40
Project: aj8   File: DirectSprite.java   View source code 6 votes vote down vote up
public DirectSprite(byte[] data, Component component) {
	try {
		Image image = Toolkit.getDefaultToolkit().createImage(data);
		MediaTracker mediaTracker = new MediaTracker(component);
		mediaTracker.addImage(image, 0);
		mediaTracker.waitForAll();
		width = image.getWidth(component);
		height = image.getHeight(component);
		resizeWidth = width;
		resizeHeight = height;
		horizontalOffset = 0;
		verticalOffset = 0;
		raster = new int[width * height];
		PixelGrabber grabber = new PixelGrabber(image, 0, 0, width, height, raster, 0, width);
		grabber.grabPixels();
	} catch (Exception ex) {
		System.out.println("Error converting jpg");
	}
}