java.awt.image.PixelGrabber Java Examples

The following examples show how to use java.awt.image.PixelGrabber. 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: PostscriptWriter.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
private ArrayImageSource getImageSource( Image image ) throws IOException
{
	ImageIcon imageIcon = new ImageIcon( image );
	int w = imageIcon.getIconWidth( );
	int h = imageIcon.getIconHeight( );
	int[] pixels = new int[w * h];

	try
	{
		PixelGrabber pg = new PixelGrabber( image, 0, 0, w, h, pixels, 0, w );
		pg.grabPixels( );
		if ( ( pg.getStatus( ) & ImageObserver.ABORT ) != 0 )
		{
			throw new IOException( "failed to load image contents" );
		}
	}
	catch ( InterruptedException e )
	{
		throw new IOException( "image load interrupted" );
	}

	ArrayImageSource imageSource = new ArrayImageSource( w, h, pixels );
	return imageSource;
}
 
Example #2
Source File: GraphicsUtils.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * 生成Image的hash序列
 * 
 * @param img
 * @return
 */
public static int hashImage(Image img) {
	int hash_result = 0;
	int width = img.getWidth(null);
	int height = img.getHeight(null);
	hash_result = (hash_result << 7) ^ height;
	hash_result = (hash_result << 7) ^ width;
	PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, true);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	int pixels[] = (int[]) pg.getPixels();
	for (int pixel = 0; pixel < 20; ++pixel) {
		int x = (pixel * 50) % width;
		int y = (pixel * 100) % height;
		hash_result = (hash_result << 7) ^ pixels[x + width * y];
	}
	return hash_result;
}
 
Example #3
Source File: GraphicsUtils.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
final static public Image transparencyBlackColor(final Image img, final Color c) {
	int width = img.getWidth(null);
	int height = img.getHeight(null);
	PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, true);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	int pixels[] = (int[]) pg.getPixels();
	int length = pixels.length;
	for (int i = 0; i < length; i++) {
		int pixel = pixels[i];
		int[] rgbs = LColor.getRGBs(pixel);
		if (rgbs[0] >= 252 && rgbs[1] >= 252 && rgbs[1] >= 252) {
			pixels[i] = 0xffffff;
		}
	}
	return toolKit.createImage(new MemoryImageSource(width, height, pixels, 0, width));
}
 
Example #4
Source File: GraphicsUtils.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * 将黑色颜色部分透明化
 * 
 * @param img
 * @return
 */
final static public Image transparencyBlackColor(final Image img) {
	int width = img.getWidth(null);
	int height = img.getHeight(null);
	PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, true);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	int pixels[] = (int[]) pg.getPixels();
	int length = pixels.length;
	for (int i = 0; i < length; i++) {
		if (pixels[i] == 0) {
			pixels[i] = 0xffffff;
		}
	}
	return toolKit.createImage(new MemoryImageSource(width, height, pixels, 0, width));
}
 
Example #5
Source File: BMPFile.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
private boolean convertImage(Image parImage, int parWidth, int parHeight) {
    int pad;
    bitmap = new int[parWidth * parHeight];
    PixelGrabber pg = new PixelGrabber(parImage, 0, 0, parWidth, parHeight,
            bitmap, 0, parWidth);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        return (false);
    }

    pad = ((4 - ((parWidth * 3) % 4)) % 4) * parHeight;
    biSizeImage = ((parWidth * parHeight) * 3) + pad;
    bfSize = biSizeImage + BITMAPFILEHEADER_SIZE
            + BITMAPINFOHEADER_SIZE;
    biWidth = parWidth;
    biHeight = parHeight;
    return (true);
}
 
Example #6
Source File: LogoNode.java    From PolyGlot with MIT License 6 votes vote down vote up
/**
 * Tests pixel for pixel equality of images
 * @param image1
 * @param image2
 * @return 
 */
private boolean imagesEqual(Image image1, Image image2) {
    boolean ret;
    try {
        PixelGrabber grabImage1Pixels = new PixelGrabber(image1, 0, 0, -1, -1, false);
        PixelGrabber grabImage2Pixels = new PixelGrabber(image2, 0, 0, -1, -1, false);

        int[] image1Data = null;

        if (grabImage1Pixels.grabPixels()) {
            image1Data = (int[]) grabImage1Pixels.getPixels();
        }

        int[] image2Data = null;

        if (grabImage2Pixels.grabPixels()) {
            image2Data = (int[]) grabImage2Pixels.getPixels();
        }

        ret = Arrays.equals(image1Data, image2Data);
    } catch (InterruptedException e) {
        ret = false;
    }
    
    return ret;
}
 
Example #7
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
public static int[] makeGradientPallet() {
  BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
  Graphics2D g2 = image.createGraphics();
  Point2D start = new Point2D.Float();
  Point2D end = new Point2D.Float(99f, 0f);
  float[] dist = {.0f, .5f, 1f};
  Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
  g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
  g2.fillRect(0, 0, 100, 1);
  g2.dispose();

  int width = image.getWidth(null);
  int[] pallet = new int[width];
  PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
  try {
    pg.grabPixels();
  } catch (InterruptedException ex) {
    ex.printStackTrace();
    Toolkit.getDefaultToolkit().beep();
    Thread.currentThread().interrupt();
  }
  return pallet;
}
 
Example #8
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static int[] makeGradientPallet() {
  BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
  Graphics2D g2 = image.createGraphics();
  Point2D start = new Point2D.Float();
  Point2D end = new Point2D.Float(99f, 0f);
  float[] dist = {0f, .5f, 1f};
  Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
  g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
  g2.fillRect(0, 0, 100, 1);
  g2.dispose();

  int width = image.getWidth(null);
  int[] pallet = new int[width];
  PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
  try {
    pg.grabPixels();
  } catch (InterruptedException ex) {
    ex.printStackTrace();
    Toolkit.getDefaultToolkit().beep();
    Thread.currentThread().interrupt();
  }
  return pallet;
}
 
Example #9
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 #10
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static int[] makeGradientPallet(int range) {
  BufferedImage image = new BufferedImage(range, 1, BufferedImage.TYPE_INT_RGB);
  Graphics2D g2 = image.createGraphics();
  Point2D start = new Point2D.Float();
  Point2D end = new Point2D.Float(range - 1f, 0f);
  float[] dist = {0f, .8f, .9f, 1f};
  Color[] colors = {Color.GREEN, Color.YELLOW, Color.ORANGE, Color.RED};
  g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
  g2.fillRect(0, 0, range, 1);
  g2.dispose();

  int width = image.getWidth(null);
  int[] pallet = new int[width];
  PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
  try {
    pg.grabPixels();
  } catch (InterruptedException ex) {
    ex.printStackTrace();
    Toolkit.getDefaultToolkit().beep();
    Thread.currentThread().interrupt();
  }
  return pallet;
}
 
Example #11
Source File: ImageUtils.java    From openbd-core with GNU General Public License v3.0 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 #12
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 #13
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 #14
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 #15
Source File: Sprite.java    From 317refactor with MIT License 6 votes vote down vote up
public Sprite(final byte[] abyte0, final Component component) {
	try {
		// Image image =
		// Toolkit.getDefaultToolkit().getImage(signlink.findcachedir()+"mopar.jpg");
		final Image image = Toolkit.getDefaultToolkit().createImage(abyte0);
		final MediaTracker mediatracker = new MediaTracker(component);
		mediatracker.addImage(image, 0);
		mediatracker.waitForAll();
           this.width = image.getWidth(component);
           this.height = image.getHeight(component);
           this.maxWidth = this.width;
           this.maxHeight = this.height;
           this.offsetX = 0;
           this.offsetY = 0;
           this.pixels = new int[this.width * this.height];
		final PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, this.width, this.height, this.pixels, 0, this.width);
		pixelgrabber.grabPixels();
	} catch (final Exception _ex) {
		System.out.println("Error converting jpg");
	}
}
 
Example #16
Source File: UIUtils.java    From netbeans with Apache License 2.0 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 #17
Source File: GifEncoder.java    From jrobin with GNU Lesser General Public License v2.1 6 votes vote down vote up
DirectGif89Frame(Image img) throws IOException {
	PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true);
	String errmsg = null;
	try {
		if (!pg.grabPixels()) {
			errmsg = "can't grab pixels from image";
		}
	}
	catch (InterruptedException e) {
		errmsg = "interrupted grabbing pixels from image";
	}
	if (errmsg != null) {
		throw new IOException(errmsg + " (" + getClass().getName() + ")");
	}
	theWidth = pg.getWidth();
	theHeight = pg.getHeight();
	argbPixels = (int[]) pg.getPixels();
	ciPixels = new byte[argbPixels.length];
}
 
Example #18
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 #19
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 #20
Source File: UIUtil.java    From consulo with Apache License 2.0 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 #21
Source File: UIUtils.java    From visualvm with GNU General Public License v2.0 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 #22
Source File: Patch.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
/** Expects x,y in world coordinates.  This method is intended for grabing an occasional pixel; to grab all pixels, see @getImageProcessor method. */
public int[] getPixel(final int x, final int y, final double mag) {
	if (project.getLoader().isUnloadable(this)) return new int[4];
	final MipMapImage mipMap = project.getLoader().fetchImage(this, mag);
	if (Loader.isSignalImage(mipMap.image)) return new int[4];
	final int w = mipMap.image.getWidth(null);
	final Point2D.Double pd = inverseTransformPoint(x, y);
	final int x2 = (int)(pd.x / mipMap.scaleX);
	final int y2 = (int)(pd.y / mipMap.scaleY);
	final int[] pvalue = new int[4];
	final PixelGrabber pg = new PixelGrabber( mipMap.image, x2, y2, 1, 1, pvalue, 0, w);
	try {
		pg.grabPixels();
	} catch (final InterruptedException ie) {
		return pvalue;
	}

	approximateTransferPixel(pvalue);

	return pvalue;
}
 
Example #23
Source File: Patch.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
/** Considers the alpha mask. */
@Override
public boolean contains(final double x_p, final double y_p) {
	if (!hasAlphaChannel()) return super.contains(x_p, y_p);
	// else, get pixel from image
	if (project.getLoader().isUnloadable(this)) return super.contains(x_p, y_p);
	final MipMapImage mipMap = project.getLoader().fetchImage(this, 0.12499); // TODO ideally, would ask for image within 256x256 dimensions, but that would need knowing the screen image dimensions beforehand, or computing it from the CoordinateTransform, which may be very costly.
	if (Loader.isSignalImage(mipMap.image)) return super.contains(x_p, y_p);
	final int w = mipMap.image.getWidth(null);
	final Point2D.Double pd = inverseTransformPoint(x_p, y_p);
	final int x2 = (int)(pd.x / mipMap.scaleX);
	final int y2 = (int)(pd.y / mipMap.scaleY);
	final int[] pvalue = new int[1];
	final PixelGrabber pg = new PixelGrabber(mipMap.image, x2, y2, 1, 1, pvalue, 0, w);
	try {
		pg.grabPixels();
	} catch (final InterruptedException ie) {
		return super.contains(x_p, y_p);
	}
	// Not true if alpha value is zero
	return 0 != (pvalue[0] & 0xff000000);
}
 
Example #24
Source File: Entry.java    From aifh with Apache License 2.0 5 votes vote down vote up
/**
 * Called to downsample the image and store it in the down sample component.
 */
public void downSample() {
    final int w = this.entryImage.getWidth(this);
    final int h = this.entryImage.getHeight(this);

    final PixelGrabber grabber = new PixelGrabber(this.entryImage, 0, 0, w,
            h, true);
    try {

        grabber.grabPixels();
        this.pixelMap = (int[]) grabber.getPixels();
        findBounds(w, h);

        // now downsample
        final SampleData data = this.sample.getData();

        this.ratioX = (double) (this.downSampleRight - this.downSampleLeft)
                / (double) data.getWidth();
        this.ratioY = (double) (this.downSampleBottom - this.downSampleTop)
                / (double) data.getHeight();

        for (int y = 0; y < data.getHeight(); y++) {
            for (int x = 0; x < data.getWidth(); x++) {
                if (downSampleRegion(x, y)) {
                    data.setData(x, y, true);
                } else {
                    data.setData(x, y, false);
                }
            }
        }

        this.sample.repaint();
        repaint();
    } catch (final InterruptedException e) {
        e.printStackTrace();
    }
}
 
Example #25
Source File: ExeCompiler.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
private Hashtable calculateColorCount(Image img) {
    int width = img.getWidth(null);
    int height = img.getHeight(null);
    int[] pixels = new int[width * height];
    PixelGrabber grabber = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
    try {
        grabber.grabPixels();
    } catch (InterruptedException e) {
        System.err.println("interrupted waiting for pixels!");
        //		    throw new Exception("Can't load the image provided",e);
    }


    Hashtable result = new Hashtable();
    int colorindex = 0;
    for (int i = 0; i < pixels.length; i++) {
        int pix = pixels[i];
        if (((pix >> 24) & 0xFF) > 0) {
            pix &= 0x00FFFFFF;
            Integer pixi = new Integer(pix);
            Object o = result.get(pixi);
            if (o == null) {
                result.put(pixi, new Integer(colorindex++));
            }
            //			if (colorindex > 256)
            //			    return result;
        }
    }
    return result;
}
 
Example #26
Source File: ExportARGB.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
static public final int[] extractARGBIntArray( final Image img )
{
	final int[] pix = new int[img.getWidth(null) * img.getHeight(null)];
	PixelGrabber pg = new PixelGrabber( img, 0, 0, img.getWidth(null), img.getHeight(null), pix, 0, img.getWidth(null) );
	try {
		pg.grabPixels();
	} catch (InterruptedException ie) {}
	return pix;
}
 
Example #27
Source File: AVGDialog.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public final static Image getRMXPDialog(String fileName, int width, int height) {
	if (lazyImages == null) {
		lazyImages = new HashMap<String, Image>(10);
	}
	Image dialog = GraphicsUtils.loadImage(fileName);
	int w = dialog.getWidth(null);
	int h = dialog.getHeight(null);
	PixelGrabber pg = new PixelGrabber(dialog, 0, 0, w, h, true);
	try {
		pg.grabPixels();
	} catch (InterruptedException e) {
	}
	int[] pixels = (int[]) pg.getPixels();
	int index = -1;
	int count = 0;
	int pixel;
	for (int i = 0; i < 5; i++) {
		pixel = pixels[(141 + i) + w * 12];
		if (index == -1) {
			index = pixel;
		}
		if (index == pixel) {
			count++;
		}
	}
	if (count == 5) {
		return getRMXPDialog(dialog, width, height, 16, 5);
	} else if (count == 1) {
		return getRMXPDialog(dialog, width, height, 27, 5);
	} else if (count == 2) {
		return getRMXPDialog(dialog, width, height, 20, 5);
	} else {
		return getRMXPDialog(dialog, width, height, 27, 5);
	}
}
 
Example #28
Source File: SpriteImage.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
private void bind(Image img, Color color) {
	int size = width * height;
	pixels = new int[width * height];
	transform = Sprite.TRANS_NONE;
	PixelGrabber pixelGrabber = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
	if (width < 48 && height < 48) {
		makePolygonInterval = 1;
	} else {
		makePolygonInterval = 3;
	}
	boolean result = false;
	try {
		result = pixelGrabber.grabPixels();
	} catch (InterruptedException ex) {
	}
	if (result) {
		int pixel;
		for (int i = 0; i < size; i++) {
			pixels[i] = LColor.premultiply(pixel = pixels[i]);
			if (isOpaque && (pixel >>> 24) < 255) {
				isOpaque = false;
			}
		}
	}
	BufferedImage awtImage = null;
	if (isOpaque) {
		awtImage = GraphicsUtils.newAwtRGBImage(pixels, width, height, size);
	} else {
		awtImage = GraphicsUtils.newAwtARGBImage(pixels, width, height, size);
	}
	image = new LImage(serializablelImage = awtImage);
}
 
Example #29
Source File: FSLoader.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
/** Returns the array of pixels, whose type depends on the bi.getType(); for example, for a BufferedImage.TYPE_BYTE_INDEXED, returns a byte[]. */
static public final Object grabPixels(final BufferedImage bi) {
	final PixelGrabber pg = new PixelGrabber(bi, 0, 0, bi.getWidth(), bi.getHeight(), false);
	try {
		pg.grabPixels();
		return pg.getPixels();
	} catch (InterruptedException e) {
		IJError.print(e);
	}
	return null;
}
 
Example #30
Source File: ImageWrapper.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
private void writeObject ( ObjectOutputStream stream ) throws java.io.IOException {
   stream.defaultWriteObject( ); // write non-transient, non-static data
   PixelGrabber grabber = new PixelGrabber( image , 0 , 0 , -1 , -1 , true );
   
   try {
      grabber.grabPixels( );
   } catch ( InterruptedException e ) {}
   Object pix = grabber.getPixels( );
   Dimension dim = new Dimension( image.getWidth( null ) , image.getHeight( null ) );
   stream.writeObject( dim );
   stream.writeObject( pix );
}