bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * This code is free software; you can redistribute it and/or modify it
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * under the terms of the GNU General Public License version 2 only, as
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * published by the Free Software Foundation. Oracle designates this
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * particular file as subject to the "Classpath" exception as provided
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * by Oracle in the LICENSE file that accompanied this code.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * This code is distributed in the hope that it will be useful, but WITHOUT
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * version 2 for more details (a copy is included in the LICENSE file that
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * accompanied this code).
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * You should have received a copy of the GNU General Public License version
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * 2 along with this work; if not, write to the Free Software Foundation,
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * or visit www.oracle.com if you need additional information or have any
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The PixelGrabber class implements an ImageConsumer which can be attached
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * to an Image or ImageProducer object to retrieve a subset of the pixels
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * in that image. Here is an example:
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * public void handlesinglepixel(int x, int y, int pixel) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * int alpha = (pixel >> 24) & 0xff;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * int red = (pixel >> 16) & 0xff;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * int green = (pixel >> 8) & 0xff;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * int blue = (pixel ) & 0xff;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * // Deal with the pixel as necessary...
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * public void handlepixels(Image img, int x, int y, int w, int h) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * int[] pixels = new int[w * h];
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * pg.grabPixels();
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * } catch (InterruptedException e) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * System.err.println("interrupted waiting for pixels!");
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * System.err.println("image fetch aborted or errored");
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * for (int j = 0; j < h; j++) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * for (int i = 0; i < w; i++) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * handlesinglepixel(x+i, y+j, pixels[j * w + i]);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see ColorModel#getRGBdefault
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @author Jim Graham
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swiftpublic class PixelGrabber implements ImageConsumer {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift private static final int GRABBEDBITS = (ImageObserver.FRAMEBITS
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift private static final int DONEBITS = (GRABBEDBITS
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * section of pixels from the specified image into the given array.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The pixels are stored into the array in the default RGB ColorModel.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The RGB data for pixel (i, j) where (i, j) is inside the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * (x, y, w, h) is stored in the array at
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * <tt>pix[(j - y) * scansize + (i - x) + off]</tt>.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see ColorModel#getRGBdefault
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param img the image to retrieve pixels from
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param x the x coordinate of the upper left corner of the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * of pixels to retrieve from the image, relative to the default
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * (unscaled) size of the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param y the y coordinate of the upper left corner of the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * of pixels to retrieve from the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param w the width of the rectangle of pixels to retrieve
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param h the height of the rectangle of pixels to retrieve
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param pix the array of integers which are to be used to hold the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * RGB pixels retrieved from the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param off the offset into the array of where to store the first pixel
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param scansize the distance from one row of pixels to the next in
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift public PixelGrabber(Image img, int x, int y, int w, int h,
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift this(img.getSource(), x, y, w, h, pix, off, scansize);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * section of pixels from the image produced by the specified
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * ImageProducer into the given array.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The pixels are stored into the array in the default RGB ColorModel.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The RGB data for pixel (i, j) where (i, j) is inside the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * (x, y, w, h) is stored in the array at
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * <tt>pix[(j - y) * scansize + (i - x) + off]</tt>.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param ip the <code>ImageProducer</code> that produces the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * image from which to retrieve pixels
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param x the x coordinate of the upper left corner of the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * of pixels to retrieve from the image, relative to the default
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * (unscaled) size of the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param y the y coordinate of the upper left corner of the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * of pixels to retrieve from the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param w the width of the rectangle of pixels to retrieve
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param h the height of the rectangle of pixels to retrieve
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param pix the array of integers which are to be used to hold the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * RGB pixels retrieved from the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param off the offset into the array of where to store the first pixel
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param scansize the distance from one row of pixels to the next in
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @see ColorModel#getRGBdefault
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift public PixelGrabber(ImageProducer ip, int x, int y, int w, int h,
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * section of pixels from the specified image. The pixels are
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * accumulated in the original ColorModel if the same ColorModel
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * is used for every call to setPixels, otherwise the pixels are
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * accumulated in the default RGB ColorModel. If the forceRGB
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * parameter is true, then the pixels will be accumulated in the
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * default RGB ColorModel anyway. A buffer is allocated by the
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * PixelGrabber to hold the pixels in either case. If (w < 0) or
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * (h < 0), then they will default to the remaining width and
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * height of the source data when that information is delivered.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param img the image to retrieve the image data from
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param x the x coordinate of the upper left corner of the rectangle
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * of pixels to retrieve from the image, relative to the default
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * (unscaled) size of the image
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param y the y coordinate of the upper left corner of the rectangle
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * of pixels to retrieve from the image
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param w the width of the rectangle of pixels to retrieve
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param h the height of the rectangle of pixels to retrieve
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param forceRGB true if the pixels should always be converted to
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * the default RGB ColorModel
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift public PixelGrabber(Image img, int x, int y, int w, int h,
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Request the PixelGrabber to start fetching the pixels.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Request the PixelGrabber to abort the image fetch.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Request the Image or ImageProducer to start delivering pixels and
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * wait for all of the pixels in the rectangle of interest to be
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @return true if the pixels were successfully grabbed, false on
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * abort, error or timeout
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @exception InterruptedException
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Another thread has interrupted this thread.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift public boolean grabPixels() throws InterruptedException {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Request the Image or ImageProducer to start delivering pixels and
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * wait for all of the pixels in the rectangle of interest to be
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * delivered or until the specified timeout has elapsed. This method
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * behaves in the following ways, depending on the value of
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * <code>ms</code>:
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * <li> If <code>ms</code> == 0, waits until all pixels are delivered
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * <li> If <code>ms</code> > 0, waits until all pixels are delivered
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * as timeout expires.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * <li> If <code>ms</code> < 0, returns <code>true</code> if all pixels
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * are grabbed, <code>false</code> otherwise and does not wait.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param ms the number of milliseconds to wait for the image pixels
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * to arrive before timing out
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @return true if the pixels were successfully grabbed, false on
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * abort, error or timeout
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @exception InterruptedException
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Another thread has interrupted this thread.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift public synchronized boolean grabPixels(long ms)
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Return the status of the pixels. The ImageObserver flags
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * representing the available pixel information are returned.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @return the bitwise OR of all relevant ImageObserver flags
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @see ImageObserver
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Get the width of the pixel buffer (after adjusting for image width).
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * If no width was specified for the rectangle of pixels to grab then
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * then this information will only be available after the image has
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * delivered the dimensions.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @return the final width used for the pixel buffer or -1 if the width
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * is not yet known
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #getStatus
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Get the height of the pixel buffer (after adjusting for image height).
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * If no width was specified for the rectangle of pixels to grab then
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * then this information will only be available after the image has
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * delivered the dimensions.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @return the final height used for the pixel buffer or -1 if the height
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * is not yet known
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #getStatus
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Get the pixel buffer. If the PixelGrabber was not constructed
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * with an explicit pixel buffer to hold the pixels then this method
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * will return null until the size and format of the image data is
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Since the PixelGrabber may fall back on accumulating the data
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * in the default RGB ColorModel at any time if the source image
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * uses more than one ColorModel to deliver the data, the array
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * object returned by this method may change over time until the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * image grab is complete.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @return either a byte array or an int array
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #getStatus
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #setPixels(int, int, int, int, ColorModel, byte[], int, int)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #setPixels(int, int, int, int, ColorModel, int[], int, int)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Get the ColorModel for the pixels stored in the array. If the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * PixelGrabber was constructed with an explicit pixel buffer then
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * this method will always return the default RGB ColorModel,
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * otherwise it may return null until the ColorModel used by the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * ImageProducer is known.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Since the PixelGrabber may fall back on accumulating the data
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * in the default RGB ColorModel at any time if the source image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * uses more than one ColorModel to deliver the data, the ColorModel
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * object returned by this method may change over time until the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * image grab is complete and may not reflect any of the ColorModel
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * objects that was used by the ImageProducer to deliver the pixels.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @return the ColorModel object used for storing the pixels
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #getStatus
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see ColorModel#getRGBdefault
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #setColorModel(ColorModel)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift public synchronized ColorModel getColorModel() {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The setDimensions method is part of the ImageConsumer API which
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * this class must implement to retrieve the pixels.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Note: This method is intended to be called by the ImageProducer
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * of the Image whose pixels are being grabbed. Developers using
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * this class to retrieve pixels from an image should avoid calling
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * this method directly since that operation could result in problems
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * with retrieving the requested pixels.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param width the width of the dimension
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param height the height of the dimension
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift public void setDimensions(int width, int height) {
convertToRGB();
convertToRGB();
convertToRGB();
grabbing = false;
switch (status) {
case IMAGEERROR:
case IMAGEABORTED:
case STATICIMAGEDONE:
case SINGLEFRAMEDONE:
notifyAll();
return flags;