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

The following examples show how to use java.awt.image.PixelGrabber#getPixels() . 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: 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 2
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 3
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 4
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 5
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 6
Source File: GraphicsUtils.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
/**
 * 分割指定图像为image[]
 * 
 * @param image
 * @param row
 * @param col
 * @return
 */
public static Image[] getSplitImages(Image image, int row, int col, boolean isFiltrate) {
	int index = 0;
	int wlength = image.getWidth(null) / row;
	int hlength = image.getHeight(null) / col;
	int l = wlength * hlength;
	Image[] abufferedimage = new Image[l];
	for (int y = 0; y < hlength; y++) {
		for (int x = 0; x < wlength; x++) {
			abufferedimage[index] = GraphicsUtils.createImage(row, col, true);
			Graphics g = abufferedimage[index].getGraphics();
			g.drawImage(image, 0, 0, row, col, (x * row), (y * col), row + (x * row), col + (y * col), null);
			g.dispose();
			g = null;
			PixelGrabber pgr = new PixelGrabber(abufferedimage[index], 0, 0, -1, -1, true);
			try {
				pgr.grabPixels();
			} catch (InterruptedException ex) {
			}
			int pixels[] = (int[]) pgr.getPixels();
			if (isFiltrate) {
				for (int i = 0; i < pixels.length; i++) {
					int[] rgbs = LColor.getRGBs(pixels[i]);
					if ((rgbs[0] == 247 && rgbs[1] == 0 && rgbs[2] == 255)
							|| (rgbs[0] == 255 && rgbs[1] == 255 && rgbs[2] == 255)) {
						pixels[i] = 0;
					}
				}
			}
			ImageProducer ip = new MemoryImageSource(pgr.getWidth(), pgr.getHeight(), pixels, 0, pgr.getWidth());
			abufferedimage[index] = toolKit.createImage(ip);
			index++;
		}
	}
	return abufferedimage;
}
 
Example 7
Source File: GraphicsUtils.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
/**
 * 分割指定图像为image[]
 * 
 * @param image
 * @param row
 * @param col
 * @return
 */
public static Image[][] getSplit2Images(Image image, int row, int col, boolean isFiltrate) {
	int wlength = image.getWidth(null) / row;
	int hlength = image.getHeight(null) / col;
	Image[][] abufferedimage = new Image[wlength][hlength];
	for (int y = 0; y < hlength; y++) {
		for (int x = 0; x < wlength; x++) {
			abufferedimage[x][y] = GraphicsUtils.createImage(row, col, true);
			Graphics g = abufferedimage[x][y].getGraphics();
			g.drawImage(image, 0, 0, row, col, (x * row), (y * col), row + (x * row), col + (y * col), null);
			g.dispose();
			g = null;
			PixelGrabber pgr = new PixelGrabber(abufferedimage[x][y], 0, 0, -1, -1, true);
			try {
				pgr.grabPixels();
			} catch (InterruptedException ex) {
				ex.getStackTrace();
			}
			int pixels[] = (int[]) pgr.getPixels();
			if (isFiltrate) {
				for (int i = 0; i < pixels.length; i++) {
					int[] rgbs = LColor.getRGBs(pixels[i]);
					if ((rgbs[0] == 247 && rgbs[1] == 0 && rgbs[2] == 255)
							|| (rgbs[0] == 255 && rgbs[1] == 0 && rgbs[2] == 255)
							|| (rgbs[0] == 0 && rgbs[1] == 0 && rgbs[2] == 0)) {
						pixels[i] = 0;
					}
				}
			}
			ImageProducer ip = new MemoryImageSource(pgr.getWidth(), pgr.getHeight(), pixels, 0, pgr.getWidth());
			abufferedimage[x][y] = toolKit.createImage(ip);
		}
	}
	return abufferedimage;
}
 
Example 8
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 9
Source File: BmpWriter.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * The constructor.
 * 
 * @param img
 */
public BmpWriter( Image img )
{
	if ( img == null )
	{
		return;
	}

	PixelGrabber pg = new PixelGrabber( img, 0, 0, -1, -1, true );

	try
	{
		pg.grabPixels( );
	}
	catch ( InterruptedException e )
	{
		return;
	}

	if ( ( pg.status( ) & ImageObserver.ABORT ) != 0 )
	{
		return;
	}

	this.pix = (int[]) pg.getPixels( );
	this.width = pg.getWidth( );
	this.height = pg.getHeight( );
}
 
Example 10
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 );
}
 
Example 11
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 12
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 13
Source File: AnimationHelper.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
public static AnimationHelper makeObject(String fileName, int tileWidth, int tileHeight, Color col) {
	String key = fileName.trim().toLowerCase();
	AnimationHelper animation = (AnimationHelper) animations.get(key);
	if (animation == null) {
		Image image = GraphicsUtils.loadNotCacheImage(fileName);
		int c = col.getRGB();
		int wlength = image.getWidth(null) / tileWidth;
		int hlength = image.getHeight(null) / tileHeight;
		Image[][] images = new Image[wlength][hlength];
		for (int y = 0; y < hlength; y++) {
			for (int x = 0; x < wlength; x++) {
				images[x][y] = GraphicsUtils.createImage(tileWidth, tileHeight, true);
				Graphics g = images[x][y].getGraphics();
				g.drawImage(image, 0, 0, tileWidth, tileHeight, (x * tileWidth), (y * tileHeight),
						tileWidth + (x * tileWidth), tileHeight + (y * tileHeight), null);
				g.dispose();
				g = null;
				PixelGrabber pgr = new PixelGrabber(images[x][y], 0, 0, -1, -1, true);
				try {
					pgr.grabPixels();
				} catch (InterruptedException ex) {
					ex.getStackTrace();
				}
				int pixels[] = (int[]) pgr.getPixels();
				for (int i = 0; i < pixels.length; i++) {
					if (pixels[i] == c) {
						pixels[i] = 0;
					}
				}
				ImageProducer ip = new MemoryImageSource(pgr.getWidth(), pgr.getHeight(), pixels, 0,
						pgr.getWidth());
				images[x][y] = GraphicsUtils.toolKit.createImage(ip);
			}
		}
		Image[][] result = new Image[hlength][wlength];
		for (int y = 0; y < wlength; y++) {
			for (int x = 0; x < hlength; x++) {
				result[x][y] = images[y][x];
			}
		}
		images = null;
		animations.put(key, animation = makeObject(result[0], result[1], result[3], result[2]));
	}
	return animation;

}
 
Example 14
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;
}