APIdia Launch Partner
Top Description Fields Constructors Methods
java.awt.image

public Class PixelGrabber

extends Object
implements ImageConsumer
Class Inheritance
All Implemented Interfaces
java.awt.image.ImageConsumer
Imports
java.util.Hashtable, java.awt.image.ImageProducer, .ImageConsumer, .ColorModel, java.awt.Image

The PixelGrabber class implements an ImageConsumer which can be attached to an Image or ImageProducer object to retrieve a subset of the pixels in that image. Here is an example:
public void handlesinglepixel(int x, int y, int pixel) {
     int alpha = (pixel >> 24) & 0xff;
     int red   = (pixel >> 16) & 0xff;
     int green = (pixel >>  8) & 0xff;
     int blue  = (pixel      ) & 0xff;
     // Deal with the pixel as necessary...
}

public void handlepixels(Image img, int x, int y, int w, int h) {
     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;
     }
     if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
         System.err.println("image fetch aborted or errored");
         return;
     }
     for (int j = 0; j < h; j++) {
         for (int i = 0; i < w; i++) {
             handlesinglepixel(x+i, y+j, pixels[j * w + i]);
         }
     }
}

Author
Jim Graham
See Also
ColorModel#getRGBdefault

Field Summary

Modifier and TypeField and Description
pack-priv byte[]
private static final int
pack-priv int
pack-priv int
pack-priv int
pack-priv int
pack-priv int
pack-priv int
private int
private static final int
private boolean
pack-priv ColorModel
pack-priv int[]
pack-priv ImageProducer

Constructor Summary

AccessConstructor and Description
public
PixelGrabber(Image
the image to retrieve pixels from
img
,
int
the x coordinate of the upper left corner of the rectangle of pixels to retrieve from the image, relative to the default (unscaled) size of the image
x
,
int
the y coordinate of the upper left corner of the rectangle of pixels to retrieve from the image
y
,
int
the width of the rectangle of pixels to retrieve
w
,
int
the height of the rectangle of pixels to retrieve
h
,
int[]
the array of integers which are to be used to hold the RGB pixels retrieved from the image
pix
,
int
the offset into the array of where to store the first pixel
off
,
int
the distance from one row of pixels to the next in the array
scansize
)

Create a PixelGrabber object to grab the (x, y, w, h) rectangular section of pixels from the specified image into the given array.

public
PixelGrabber(ImageProducer
the ImageProducer that produces the image from which to retrieve pixels
ip
,
int
the x coordinate of the upper left corner of the rectangle of pixels to retrieve from the image, relative to the default (unscaled) size of the image
x
,
int
the y coordinate of the upper left corner of the rectangle of pixels to retrieve from the image
y
,
int
the width of the rectangle of pixels to retrieve
w
,
int
the height of the rectangle of pixels to retrieve
h
,
int[]
the array of integers which are to be used to hold the RGB pixels retrieved from the image
pix
,
int
the offset into the array of where to store the first pixel
off
,
int
the distance from one row of pixels to the next in the array
scansize
)

Create a PixelGrabber object to grab the (x, y, w, h) rectangular section of pixels from the image produced by the specified ImageProducer into the given array.

public
PixelGrabber(Image
the image to retrieve the image data from
img
,
int
the x coordinate of the upper left corner of the rectangle of pixels to retrieve from the image, relative to the default (unscaled) size of the image
x
,
int
the y coordinate of the upper left corner of the rectangle of pixels to retrieve from the image
y
,
int
the width of the rectangle of pixels to retrieve
w
,
int
the height of the rectangle of pixels to retrieve
h
,
boolean
true if the pixels should always be converted to the default RGB ColorModel
forceRGB
)

Create a PixelGrabber object to grab the (x, y, w, h) rectangular section of pixels from the specified image.

Method Summary

Modifier and TypeMethod and Description
public synchronized void
abortGrabbing()

Request the PixelGrabber to abort the image fetch.

private void
public synchronized ColorModel

Returns:

the ColorModel object used for storing the pixels
getColorModel
()

Get the ColorModel for the pixels stored in the array.

public synchronized int

Returns:

the final height used for the pixel buffer or -1 if the height is not yet known
getHeight
()

Get the height of the pixel buffer (after adjusting for image height).

public synchronized Object

Returns:

either a byte array or an int array
getPixels
()

Get the pixel buffer.

public synchronized int

Returns:

the bitwise OR of all relevant ImageObserver flags
getStatus
()

Return the status of the pixels.

public synchronized int

Returns:

the final width used for the pixel buffer or -1 if the width is not yet known
getWidth
()

Get the width of the pixel buffer (after adjusting for image width).

public boolean

Returns:

true if the pixels were successfully grabbed, false on abort, error or timeout
grabPixels
()

Request the Image or ImageProducer to start delivering pixels and wait for all of the pixels in the rectangle of interest to be delivered.

public synchronized boolean

Returns:

true if the pixels were successfully grabbed, false on abort, error or timeout
grabPixels
(long
the number of milliseconds to wait for the image pixels to arrive before timing out
ms
)

Request the Image or ImageProducer to start delivering pixels and wait for all of the pixels in the rectangle of interest to be delivered or until the specified timeout has elapsed.

public synchronized void
imageComplete(int
the status of image loading
status
)

Implements java.awt.image.ImageConsumer.imageComplete.

The imageComplete method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

public void
setColorModel(ColorModel
the specified ColorModel
model
)

Implements java.awt.image.ImageConsumer.setColorModel.

The setColorModel method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

public void
setDimensions(int
the width of the dimension
width
,
int
the height of the dimension
height
)

Implements java.awt.image.ImageConsumer.setDimensions.

The setDimensions method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

public void
setHints(int
a set of hints used to process the pixels
hints
)

Implements java.awt.image.ImageConsumer.setHints.

The setHints method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

public void
setPixels(int
the X coordinate of the upper-left corner of the area of pixels to be set
srcX
,
int
the Y coordinate of the upper-left corner of the area of pixels to be set
srcY
,
int
the width of the area of pixels
srcW
,
int
the height of the area of pixels
srcH
,
ColorModel
the specified ColorModel
model
,
byte[]
the array of pixels
pixels
,
int
the offset into the pixels array
srcOff
,
int
the distance from one row of pixels to the next in the pixels array
srcScan
)

Implements java.awt.image.ImageConsumer.setPixels.

The setPixels method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

public void
setPixels(int
the X coordinate of the upper-left corner of the area of pixels to be set
srcX
,
int
the Y coordinate of the upper-left corner of the area of pixels to be set
srcY
,
int
the width of the area of pixels
srcW
,
int
the height of the area of pixels
srcH
,
ColorModel
the specified ColorModel
model
,
int[]
the array of pixels
pixels
,
int
the offset into the pixels array
srcOff
,
int
the distance from one row of pixels to the next in the pixels array
srcScan
)

Implements java.awt.image.ImageConsumer.setPixels.

The setPixels method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

public void
setProperties(Hashtable<?, ?>
the list of properties
props
)

Implements java.awt.image.ImageConsumer.setProperties.

The setProperties method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

public synchronized void
startGrabbing()

Request the PixelGrabber to start fetching the pixels.

public synchronized int

Returns:

the bitwise OR of all relevant ImageObserver flags
status
()

Returns the status of the pixels.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bytePixelsback to summary
pack-priv byte[] bytePixels
DONEBITSback to summary
private static final int DONEBITS
dstHback to summary
pack-priv int dstH
dstOffback to summary
pack-priv int dstOff
dstScanback to summary
pack-priv int dstScan
dstWback to summary
pack-priv int dstW
dstXback to summary
pack-priv int dstX
dstYback to summary
pack-priv int dstY
flagsback to summary
private int flags
GRABBEDBITSback to summary
private static final int GRABBEDBITS
grabbingback to summary
private boolean grabbing
imageModelback to summary
pack-priv ColorModel imageModel
intPixelsback to summary
pack-priv int[] intPixels
producerback to summary
pack-priv ImageProducer producer

Constructor Detail

PixelGrabberback to summary
public PixelGrabber(Image img, int x, int y, int w, int h, int[] pix, int off, int scansize)

Create a PixelGrabber object to grab the (x, y, w, h) rectangular section of pixels from the specified image into the given array. The pixels are stored into the array in the default RGB ColorModel. The RGB data for pixel (i, j) where (i, j) is inside the rectangle (x, y, w, h) is stored in the array at pix[(j - y) * scansize + (i - x) + off].

Parameters
img:Image

the image to retrieve pixels from

x:int

the x coordinate of the upper left corner of the rectangle of pixels to retrieve from the image, relative to the default (unscaled) size of the image

y:int

the y coordinate of the upper left corner of the rectangle of pixels to retrieve from the image

w:int

the width of the rectangle of pixels to retrieve

h:int

the height of the rectangle of pixels to retrieve

pix:int[]

the array of integers which are to be used to hold the RGB pixels retrieved from the image

off:int

the offset into the array of where to store the first pixel

scansize:int

the distance from one row of pixels to the next in the array

See Also
ColorModel#getRGBdefault
PixelGrabberback to summary
public PixelGrabber(ImageProducer ip, int x, int y, int w, int h, int[] pix, int off, int scansize)

Create a PixelGrabber object to grab the (x, y, w, h) rectangular section of pixels from the image produced by the specified ImageProducer into the given array. The pixels are stored into the array in the default RGB ColorModel. The RGB data for pixel (i, j) where (i, j) is inside the rectangle (x, y, w, h) is stored in the array at pix[(j - y) * scansize + (i - x) + off].

Parameters
ip:ImageProducer

the ImageProducer that produces the image from which to retrieve pixels

x:int

the x coordinate of the upper left corner of the rectangle of pixels to retrieve from the image, relative to the default (unscaled) size of the image

y:int

the y coordinate of the upper left corner of the rectangle of pixels to retrieve from the image

w:int

the width of the rectangle of pixels to retrieve

h:int

the height of the rectangle of pixels to retrieve

pix:int[]

the array of integers which are to be used to hold the RGB pixels retrieved from the image

off:int

the offset into the array of where to store the first pixel

scansize:int

the distance from one row of pixels to the next in the array

See Also
ColorModel#getRGBdefault
PixelGrabberback to summary
public PixelGrabber(Image img, int x, int y, int w, int h, boolean forceRGB)

Create a PixelGrabber object to grab the (x, y, w, h) rectangular section of pixels from the specified image. The pixels are accumulated in the original ColorModel if the same ColorModel is used for every call to setPixels, otherwise the pixels are accumulated in the default RGB ColorModel. If the forceRGB parameter is true, then the pixels will be accumulated in the default RGB ColorModel anyway. A buffer is allocated by the PixelGrabber to hold the pixels in either case. If (w < 0) or (h < 0), then they will default to the remaining width and height of the source data when that information is delivered.

Parameters
img:Image

the image to retrieve the image data from

x:int

the x coordinate of the upper left corner of the rectangle of pixels to retrieve from the image, relative to the default (unscaled) size of the image

y:int

the y coordinate of the upper left corner of the rectangle of pixels to retrieve from the image

w:int

the width of the rectangle of pixels to retrieve

h:int

the height of the rectangle of pixels to retrieve

forceRGB:boolean

true if the pixels should always be converted to the default RGB ColorModel

Method Detail

abortGrabbingback to summary
public synchronized void abortGrabbing()

Request the PixelGrabber to abort the image fetch.

convertToRGBback to summary
private void convertToRGB()
getColorModelback to summary
public synchronized ColorModel getColorModel()

Get the ColorModel for the pixels stored in the array. If the PixelGrabber was constructed with an explicit pixel buffer then this method will always return the default RGB ColorModel, otherwise it may return null until the ColorModel used by the ImageProducer is known. Since the PixelGrabber may fall back on accumulating the data in the default RGB ColorModel at any time if the source image uses more than one ColorModel to deliver the data, the ColorModel object returned by this method may change over time until the image grab is complete and may not reflect any of the ColorModel objects that was used by the ImageProducer to deliver the pixels.

Returns:ColorModel

the ColorModel object used for storing the pixels

See Also
getStatus, ColorModel#getRGBdefault, setColorModel(ColorModel)
getHeightback to summary
public synchronized int getHeight()

Get the height of the pixel buffer (after adjusting for image height). If no width was specified for the rectangle of pixels to grab then then this information will only be available after the image has delivered the dimensions.

Returns:int

the final height used for the pixel buffer or -1 if the height is not yet known

See Also
getStatus
getPixelsback to summary
public synchronized Object getPixels()

Get the pixel buffer. If the PixelGrabber was not constructed with an explicit pixel buffer to hold the pixels then this method will return null until the size and format of the image data is known. Since the PixelGrabber may fall back on accumulating the data in the default RGB ColorModel at any time if the source image uses more than one ColorModel to deliver the data, the array object returned by this method may change over time until the image grab is complete.

Returns:Object

either a byte array or an int array

See Also
getStatus, setPixels(int, int, int, int, ColorModel, byte[], int, int), setPixels(int, int, int, int, ColorModel, int[], int, int)
getStatusback to summary
public synchronized int getStatus()

Return the status of the pixels. The ImageObserver flags representing the available pixel information are returned.

Returns:int

the bitwise OR of all relevant ImageObserver flags

See Also
ImageObserver
getWidthback to summary
public synchronized int getWidth()

Get the width of the pixel buffer (after adjusting for image width). If no width was specified for the rectangle of pixels to grab then then this information will only be available after the image has delivered the dimensions.

Returns:int

the final width used for the pixel buffer or -1 if the width is not yet known

See Also
getStatus
grabPixelsback to summary
public boolean grabPixels() throws InterruptedException

Request the Image or ImageProducer to start delivering pixels and wait for all of the pixels in the rectangle of interest to be delivered.

Returns:boolean

true if the pixels were successfully grabbed, false on abort, error or timeout

Exceptions
InterruptedException:
Another thread has interrupted this thread.
grabPixelsback to summary
public synchronized boolean grabPixels(long ms) throws InterruptedException

Request the Image or ImageProducer to start delivering pixels and wait for all of the pixels in the rectangle of interest to be delivered or until the specified timeout has elapsed. This method behaves in the following ways, depending on the value of ms:

  • If ms == 0, waits until all pixels are delivered
  • If ms > 0, waits until all pixels are delivered as timeout expires.
  • If ms < 0, returns true if all pixels are grabbed, false otherwise and does not wait.
Parameters
ms:long

the number of milliseconds to wait for the image pixels to arrive before timing out

Returns:boolean

true if the pixels were successfully grabbed, false on abort, error or timeout

Exceptions
InterruptedException:
Another thread has interrupted this thread.
imageCompleteback to summary
public synchronized void imageComplete(int status)

Implements java.awt.image.ImageConsumer.imageComplete.

The imageComplete method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

Note

This method is intended to be called by the ImageProducer of the Image whose pixels are being grabbed. Developers using this class to retrieve pixels from an image should avoid calling this method directly since that operation could result in problems with retrieving the requested pixels.

Parameters
status:int

the status of image loading

setColorModelback to summary
public void setColorModel(ColorModel model)

Implements java.awt.image.ImageConsumer.setColorModel.

The setColorModel method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

Note

This method is intended to be called by the ImageProducer of the Image whose pixels are being grabbed. Developers using this class to retrieve pixels from an image should avoid calling this method directly since that operation could result in problems with retrieving the requested pixels.

Parameters
model:ColorModel

the specified ColorModel

See Also
getColorModel
setDimensionsback to summary
public void setDimensions(int width, int height)

Implements java.awt.image.ImageConsumer.setDimensions.

The setDimensions method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

Note

This method is intended to be called by the ImageProducer of the Image whose pixels are being grabbed. Developers using this class to retrieve pixels from an image should avoid calling this method directly since that operation could result in problems with retrieving the requested pixels.

Parameters
width:int

the width of the dimension

height:int

the height of the dimension

setHintsback to summary
public void setHints(int hints)

Implements java.awt.image.ImageConsumer.setHints.

The setHints method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

Note

This method is intended to be called by the ImageProducer of the Image whose pixels are being grabbed. Developers using this class to retrieve pixels from an image should avoid calling this method directly since that operation could result in problems with retrieving the requested pixels.

Parameters
hints:int

a set of hints used to process the pixels

setPixelsback to summary
public void setPixels(int srcX, int srcY, int srcW, int srcH, ColorModel model, byte[] pixels, int srcOff, int srcScan)

Implements java.awt.image.ImageConsumer.setPixels.

The setPixels method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

Note

This method is intended to be called by the ImageProducer of the Image whose pixels are being grabbed. Developers using this class to retrieve pixels from an image should avoid calling this method directly since that operation could result in problems with retrieving the requested pixels.

Parameters
srcX:int

the X coordinate of the upper-left corner of the area of pixels to be set

srcY:int

the Y coordinate of the upper-left corner of the area of pixels to be set

srcW:int

the width of the area of pixels

srcH:int

the height of the area of pixels

model:ColorModel

the specified ColorModel

pixels:byte[]

the array of pixels

srcOff:int

the offset into the pixels array

srcScan:int

the distance from one row of pixels to the next in the pixels array

See Also
getPixels
setPixelsback to summary
public void setPixels(int srcX, int srcY, int srcW, int srcH, ColorModel model, int[] pixels, int srcOff, int srcScan)

Implements java.awt.image.ImageConsumer.setPixels.

The setPixels method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

Note

This method is intended to be called by the ImageProducer of the Image whose pixels are being grabbed. Developers using this class to retrieve pixels from an image should avoid calling this method directly since that operation could result in problems with retrieving the requested pixels.

Parameters
srcX:int

the X coordinate of the upper-left corner of the area of pixels to be set

srcY:int

the Y coordinate of the upper-left corner of the area of pixels to be set

srcW:int

the width of the area of pixels

srcH:int

the height of the area of pixels

model:ColorModel

the specified ColorModel

pixels:int[]

the array of pixels

srcOff:int

the offset into the pixels array

srcScan:int

the distance from one row of pixels to the next in the pixels array

See Also
getPixels
setPropertiesback to summary
public void setProperties(Hashtable<?, ?> props)

Implements java.awt.image.ImageConsumer.setProperties.

The setProperties method is part of the ImageConsumer API which this class must implement to retrieve the pixels.

Note

This method is intended to be called by the ImageProducer of the Image whose pixels are being grabbed. Developers using this class to retrieve pixels from an image should avoid calling this method directly since that operation could result in problems with retrieving the requested pixels.

Parameters
props:Hashtable<?, ?>

the list of properties

startGrabbingback to summary
public synchronized void startGrabbing()

Request the PixelGrabber to start fetching the pixels.

statusback to summary
public synchronized int status()

Returns the status of the pixels. The ImageObserver flags representing the available pixel information are returned. This method and getStatus have the same implementation, but getStatus is the preferred method because it conforms to the convention of naming information-retrieval methods with the form "getXXX".

Returns:int

the bitwise OR of all relevant ImageObserver flags

See Also
ImageObserver, getStatus()